Return filtered values in Shell Bash for insertion in BD

Asked

Viewed 50 times

2

I’m writing a script on python who captures the TOP 5 ram and CPU memory consumption processes of the machine, so far the script is functional only for LINUX, goes below:

Script:

#!/usr/bin/env python
# Desenvolvimento Aberto
# shell.py

# Importar modulos do sistema operacional
import os
import subprocess 
import commands

# passando comando em uma variavel para pegar top 5 consumo de memoria
command_mem = '''ps aux --sort=-%mem | head -n 5 | jq -cR '[splits(" +")]' '''

# criando array do comando
value_mem = commands.getoutput(command_mem)

# exibir resultado
print("MEMORIA:\n"+value_mem)

Output:

["user+","10340","13.2","6.9","5119880","553860","?","Sl","ago12","4:46","/usr/lib/jvm/jdk1.8.0_211/bin/java","-XX:+IgnoreUnrecognizedVMOptions","-Xms64m","-Xmx1024m","-jar","/usr/share/dbeaver//plugins/org.eclipse.equinox.launcher_1.4.0.v20161219-1356.jar","-os","linux","-ws","gtk","-arch","x86_64","-showsplash","-launcher","/usr/share/dbeaver/dbeaver","-name","Dbeaver","--launcher.library","/usr/share/dbeaver//plugins/org.eclipse.equinox.launcher.gtk.linux.x86_64_1.1.551.v20171108-1834/eclipse_1630.so","-startup","/usr/share/dbeaver//plugins/org.eclipse.equinox.launcher_1.4.0.v20161219-1356.jar","--launcher.overrideVmargs","-exitdata","5ee801c","-vm","/usr/bin/java","-vmargs","-XX:+IgnoreUnrecognizedVMOptions","-Xms64m","-Xmx1024m","-jar","/usr/share/dbeaver//plugins/org.eclipse.equinox.launcher_1.4.0.v20161219-1356.jar"]
["user+","12548","1.1","4.1","1226988","332960","?","Sl","00:12","0:02","/opt/kingsoft/wps-office/office6/wps","/home/user/Documents/script/shell-bash/pega-memoria-linux.txt"]
["user+","10062","3.2","3.6","1891904","286776","?","Sl","ago12","1:14","/usr/share/code/code","--type=renderer","--no-sandbox","--service-pipe-token=4B72CA3F1395A79FC84737040282E34F","--lang=pt-BR","--app-path=/usr/share/code/resources/app","--node-integration=true","--webview-tag=true","--no-sandbox","--background-color=#1e1e1e","--num-raster-threads=2","--enable-main-frame-before-activation","--enable-compositor-image-animations","--service-request-channel-token=4B72CA3F1395A79FC84737040282E34F","--renderer-client-id=9","--shared-files=v8_context_snapshot_data:100,v8_natives_data:101"]
["user+","9494","0.1","2.1","1076224","169484","?","SLl","ago12","0:03","/opt/google/chrome/chrome"]

I’d like that in the field COMMAND which is the last field of command PS AUX it return me only the name of the service/program that is running, because this way that is coming is making it difficult to upload the data to the database because the number of columns becomes relative because of the field COMMAND

2 answers

2


Why don’t you use the library psutil ? It is very simple and can be used on Windows, Linux, etc. For what you want, create an object of Process and then get the process name and CPU usage in percentage by calling the methods name() and cpu_percent(). Example:

import psutil

maiorConsumo = ("None",0.0)
interval = 1
cpu_count = psutil.cpu_count()

for pid in psutil.pids():

    try:
        process = psutil.Process(pid)
        cpu_percent = process.cpu_percent(interval)/cpu_count
        name = process.name()
    except: continue

    if cpu_percent > maiorConsumo[1]:
        maiorConsumo = (name,cpu_percent)

print("Maior consumo: Name = {}  Uso = {}%".format(*maiorConsumo))

With the psutil you can get a lot of information related to your system, including networks and others. You can extract process information such as program directory, CPU usage, process name, process ID, connections, memory usage, and manage it.

To install the psutil in windows type pip install psutil. Clicking here to see your documentation.

  • I did not know this lib of py, thank you very much for your reply, I will study more her, hug!

1

I modified it according to my need, if I help someone in the future:

import psutil, datetime

process = []
veryConsumeCPU = ("None",0.0)
veryConsumeRAM = ("None",0.0)
cpu_count = psutil.cpu_count()

for pid in psutil.pids():

try:
    p = psutil.Process(pid)
    name = p.name()  # execute internal routine once collecting multiple info
    time = p.cpu_times()  # return cached value
    mem_percent = p.memory_percent(memtype="rss") # return cached value
    cpu_percent = p.cpu_percent(interval=1) / cpu_count # return cached value
    create_time = p.create_time()  # return cached value
    pid = p.ppid()  # return cached value
    status = p.status()  # return cached value
except: continue

process = [pid,name,cpu_percent,mem_percent,datetime.datetime.fromtimestamp(create_time).strftime("%Y-%m-%d %H:%M:%S"),time,status]
print("PID = {} Name = {}  Uso de CPU = {}% Uso de MEMORIA = {}% Criado em = {} Tempo corrente: = {} Status = {}".format(*process))

if cpu_percent > veryConsumeCPU[1]:
    veryConsumeCPU = (name,cpu_percent)
elif mem_percent > veryConsumeRAM[1]:
    veryConsumeRAM = (name,mem_percent)

print("Maior consumo CPU: Name = {}  Uso = {}%".format(*veryConsumeCPU))
print("Maior consumo RAM: Name = {}  Uso = {}%".format(*veryConsumeRAM))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.