ricoberger / script_exporter

Prometheus exporter to execute scripts and collect metrics from the output or the exit status.
MIT License
354 stars 82 forks source link

about executing docker scripts #26

Closed xhuan-wang closed 4 years ago

xhuan-wang commented 4 years ago

Hi , i'm using your script_exporter . its great !!! However , when i executed a shell script about docker with your script_exporter, it returned an error result , but it return the correct result when i executed the script through sh script.sh , i can't see the log and I don't know why ,

#!/bin/sh
source /etc/profile
result="$(sudo docker exec -it mysql_slave1 mysql -utest -p'test'  -e "show slave status\G" |grep "Slave_IO_Running: Yes"|wc -l)"
echo "# HELP mysql_slave1_io_running"
echo "# TYPE mysql_slave1_io_running gauge"
echo "mysql_slave1_io_running{label=\"mysql_slave1_io_running\"} $result"
tls:
  enabled: false
  crt: server.crt
  key: server.key

basicAuth:
  enabled: false
  username: admin
  password: admin

bearerAuth:
  enabled: false
  signingKey: my_secret_key

scripts:
  - name: mysql_slave1_sql_running
    script: ./test.sh
    timeout:
      max_timeout: 60
  - name: sleep
    script: sleep 120
    timeout:
      enforced: true

Can you help me?

xhuan-wang commented 4 years ago

When i replace a command in the script, it return the correct result , only the docker command did not return the correct result .

ricoberger commented 4 years ago

Hi @xhuan-wang, thank you for the detailed issue description. I was able to reproduce the problem with the following script:

#! /bin/bash
result="$(docker exec -it test ls -la | wc -l)"
echo "$result"
echo "number_of_files $result"

The problem here is the -t flag in the docker exec command. I was able to get the correct result, when I adjust the script as follows:

#! /bin/bash
result="$(docker exec -i test ls -la | wc -l)"
echo "$result"
echo "number_of_files $result"

Can you try if this also works for you please? I think you can use the following:

#!/bin/sh
source /etc/profile
result="$(sudo docker exec -i mysql_slave1 mysql -utest -p'test'  -e "show slave status\G" |grep "Slave_IO_Running: Yes"|wc -l)"
echo "# HELP mysql_slave1_io_running"
echo "# TYPE mysql_slave1_io_running gauge"
echo "mysql_slave1_io_running{label=\"mysql_slave1_io_running\"} $result"
xhuan-wang commented 4 years ago

Yes, I removed -t and returned the correct result, thank you very much!

ricoberger commented 4 years ago

Thanks for the feedback. I added this as example and will close the issue now. If you have further questions or found problem feel free to open a new issue.