-1

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...

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

3 Answers3

2

If your problem is too complex, divide it into smaller, more manageable pieces and see what they do. Start by analysing output from the awk part.

AWK can be a bit of a handful.

Try a simpler approach:

ps aux | tr -s ' ' ',' | cut -d, -f2,11 |
while read pid process; do
    req="cpu_usage{process=$process,pid=$pid}"
    echo "Sending CURL Request with this data: $req"
    curl -X POST -H "Content-type: text/plain" --data "$req" http://localhost:9091/metrics/job/top/instance/machine
 done

You may want to review the brackets. I have no means of testing this.

tripleee
  • 175,061
  • 34
  • 275
  • 318
ewoj
  • 88
  • 7
  • Thanks, it is what I am doing, but I spend to must time and I am not sure about bash script syntax. That's why I need some help. – wanwan Sep 10 '21 at 15:44
  • Here's a simpler approach which will help you learn the syntax. ps aux | tr -s ' ' ',' | cut -d, -f2,11 | while read pid process; do req="cpu_usage{process=$process,pid=$pid}"; echo $req; done – ewoj Sep 10 '21 at 15:54
0

You seem to be confused about several details of basic Bash syntax.

command <<<"string"

simply passes the literal string as standard input to command. The syntax you seem to be looking for is the process substitution

command < <(other)

which runs other and passes its output as input to command. But this too is an overcomplication; you probably want a simpler straight pipeline.

load=$(ps aux | awk '{ print "cpu_usage{ process=\"" $11 "\",pid=\"" $2 "\"}," $3 }')
curl -X POST -H "Content-type: text/plain" --data "$load" http://localhost:9091/metrics/job/top/instance/machine

where I had to resort to some wild speculation around what you hoped the Awk script should do.

Also, the argument to read is the name of a variable, so read z, not read $z (and usually use read -r unless you specifically require the odd legacy behavior of read with backslashes in its input).

Finally, you basically never want to store a command in a variable; see https://mywiki.wooledge.org/BashFAQ/050

Going forward, probably try http://shellcheck.net/ before asking for human assistance.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Thanks all for helping me. I try the code : 'load=$(ps aux | awk '{ print "cpu_usage{ process=\"" $11 "\",pid=\"" $2 "\"}," $3 }') curl -X POST -H "Content-type: text/plain" --data "$load" http://localhost:9091/metrics/job/top/instance/machine' but it doesn't work correctly. I find a error message : "curl: (3) URL using bad/illegal format or missing URL. I don't know why... – wanwan Sep 13 '21 at 13:37
  • The error message suggests something with the URL rather than with the data in `"$load"`. Without knowledge of what exactly your `ps` output looks like or how the POST data should look, there are several free variables here; it would help if you could update your question to provide more details around these matters. As you can tell here, code in comments doesn't work very well at all. – tripleee Sep 13 '21 at 13:43
  • Does `echo "$load"` look like what you expect? – tripleee Sep 13 '21 at 13:43
  • Yes when I run echo "$load" the response is what I want. – wanwan Sep 13 '21 at 13:50
  • The error message in your screenshot looks like it's using `"$load"` as the URL. I notice that your first, working attempt uses a cuite different syntax for passing in the data. – tripleee Sep 13 '21 at 14:01
  • I try to change the <"$load"> position into the curl ligne. I think the result could be better, because I don't have curl error message BUT now I have this kind of message : "invalid metric name" or "unexpected end of label value" or " expected float as value, got", so I understand the data to transfert is not in the correct form.. – wanwan Sep 13 '21 at 14:29
  • Without knowlede about what the server on `localhost` expects or does, we can't help with that part. Perhaps (accept this answer and) ask a new question about that with enough details. – tripleee Sep 13 '21 at 15:17
  • Based on a quick read of https://github.com/prometheus/pushgateway/blob/master/README.md it looks like you should have a space instead of a comma before `$3`. But this probably also depends on other things; I've never heard of this service. – tripleee Sep 13 '21 at 15:34
  • Yes I will ask a new question. Thanks for all ! – wanwan Sep 13 '21 at 15:57
0

/!\ 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...

wanwan
  • 55
  • 1
  • 7