I am trying to use a bash script for Pushgateway and Prometheus, but can't get it working. I have Pushgateway on my raspberry and Prometheus on another raspberry. All work correctly and I test a simple bash script, this script works correctly. My simple script (test.sh):
echo "metric 99" | curl --data-binary @- http://localhost:9091/metrics/job/some_job
Now I want to write a more complex script. This script must push to prometheus the metric from CPU usage (the same as the "$ps aux" command or same as "$ top" command). But this script doesn't work and I don't know what to do to modify it.. My more complex script :
#!/bin/bash
z="ps aux"
while read -r $z
do
var=$var$(awk '{print "cpu_usage{process=\""$11"\", pid=\""$2"\"}", $3$z}');
done <<< "$z"
curl -X POST -H "Content-type: text/plain" --data "$var" http://localhost:9091/metrics/job/top/instance/machine
If anyone could help me. Thanks a lot.
I also try this code :
#!/bin/bash
z="ps aux"
while read -r "ps aux"
do
var=$var$(awk '{print "cpu_usage{process=\""$11"\", pid=\""$2"\"}", $3$z}');
done <<< "$z"
curl -X POST -H "Content-type: text/plain" --data "$var" http://localhost:9091/metrics/job/top/instance/machine
But I am not sure about the syntax. What's wrong ?
I try the code :
load=$(ps aux | awk '{ print "cpu_usage{ process=\"" $11 "\",pid=\"" $2 "\"}," $3 }')
curl -X POST -H --data "$load" http://localhost:9091/metrics/job/top/instance/machine
But it doesn't work. The first line is ok, but when i run this code, I find an error message to the curl command:
curl: (3) URL using bad/illegal format or missing URL
========== The solution to my problem is : ==========
ps aux | awk '$3>0 {print "cpu_usage"$2" "$3""}' | curl --data-binary @- http://localhost:9091/metrics/job/top/instance/machine
This command could transfer to the pushgateway all process data with % CPU > 0. In this line, $3 = %CPU, $2 = PID. Be carefull with special caracters. If the result command is an error message, maybe it is because there is special caracter...
/!\ The solution to my problem is : /!\
ps aux | awk '$3>0 {print "cpu_usage"$2" "$3""}' | curl --data-binary @- http://localhost:9091/metrics/job/top/instance/machine
This command could transfer to the pushgateway all process data with % CPU > 0. In this line, $3 = %CPU, $2 = PID. Be carefull with special caracters. If the result command is an error message, maybe it is because there is special caracter...
Thanks...