5

I want to send metrics data to the Prometheus push gateway to monitor CPU and memory. What I want is to transfer $ps aux or $htop information to the push gateway. I tried this with a bash script, but I didn't succeed, which is why I'd like to try with a python script. The big problem for me is to convert $ps aux or $htop information to metrics data. I really don't know how to manage it. I tried a simple code, which works correctly:

import requests
job_name='metrics'
instance_name='10.0.0.1:9090'
team_name='cpu'
provider='Rpi'
payload_key='cpu_utilization'
payload_value='33'

response = requests.post('http://localhost:9091/metrics/job/{j}/instance/{i}/team/{t}'.format(j=job_name, i=instance_name, t=team_name), data='{k} {v}\n'.format(k=payload_key, v=payload_value))
print(response.status_code)

What I want now is to modify payload_key='cpu_utilization' and payload_value='33' to a list of process as $ps aux or $htop command.

starball
  • 20,030
  • 7
  • 43
  • 238
wanwan
  • 55
  • 1
  • 7

2 Answers2

1

The python command run perfectly:

os.system('ps –eo pid,user,%mem,cmd --sort=-%mem')

But how insert it into the previous code ?

I tested :

import requests, os
job_name='metrics'
instance_name='10.0.0.1:9090'
team_name='cpu'
provider='Rpi'
payload_key=os.system('ps –eo pid')
payload_value=os.system('ps –eo %cpu')

response = requests.post('http://localhost:9091/metrics/job/{j}/instance/{i}/team/{t}'.format(j=job_name, i=instance_name, t=team_name), data='{k} {v}\n'.format(k=payload_key, v=payload_value))
print(response.status_code)

But (not surprised), that doesn't run. How can I affect PID 1 with %cpu to the metric n# 1 ? and all cpu data to a list of metrics ?

At least convert the os.system('ps –eo pid,user,%mem,cmd --sort=-%mem') response to a list and after do a while for the request.post wich take all the list elements.

Thanks.

wanwan
  • 55
  • 1
  • 7
0

I'm going to test this command :

$ps –eo pid,user,%mem,cmd --sort=-%mem

and change :

payload_key='cpu_utilization'
payload_value='33'

by the "$ps" command :

os.system('ps –eo pid,user,%mem,cmd --sort=-%mem')

maybe this would be run... or not !!

wanwan
  • 55
  • 1
  • 7