Cannot accept jstat metrics using bash as Sensu plugin

I created a bash scirpt that accepts jstat metrics of my jvm instances!

Here is an example output:

demo.server1.sms.jstat.eden 24.34   0
demo.server1.lcms.jstat.eden    54.92   0
demo.server1.lms.jstat.eden 89.49   0
demo.server1.tms.jstat.eden 86.05   0

But when the Sensu client runs this script, it returns

Could not attach to 8584
Could not attach to 8588
Could not attach to 17141
Could not attach to 8628
demo.server1.sms.jstat.eden     0
demo.server1.lcms.jstat.eden    0
demo.server1.lms.jstat.eden     0
demo.server1.tms.jstat.eden     0

Here is an example check_cron.json

{
  "checks": {
    "jstat_metrics": {
      "type": "metric",
      "handlers": ["graphite"],
      "command": "/etc/sensu/plugins/jstat-metrics.sh",
      "interval": 5,
          "subscribers": [ "webservers" ]
    }
  }
}

And a piece of my bash script

jvm_list=("sms:$sms" "lcms:$lcms" "lms:$lms" "tms:$tms" "ums:$ums")
for jvm_instance in ${jvm_list[@]}; do
    project=${jvm_instance%%:*}
    pid=${jvm_instance#*:}
        if [ "$pid" ]; then
          metric=`jstat -gc $pid|tail -n 1`
          output=$output$'\n'"demo.server1.$project.jstat.eden"$'\t'`echo $metric |awk '{ print $3}'`$'\t0'
        fi
done
echo "$output"

I found out that the problem is with jstat, and I tried to write the full jstat path, for example /usr/lib/jvm/java-1.7.0-openjdk.x86_64/bin/jstat -gc $pid|tail -n 1, but that didn't help!

By the way, if I comment on this line, an output like " Unable to attach to 8584 " will disappear!

+5
source share
2 answers

Yes, you're right, sensu runs the entire script as the sensu user. To use jstat, you must add sensu to sudoers.

/etc/sudoers.d/sensu

:

: sensu! requiretty

: sensu secure_path = /USR/ /SBIN:/USR//:/SBIN:/:/USR/SBIN:/USR/

sensu ALL = NOPASSWD:/etc/sensu/plugins/jsat-metrics.rb

+3

Java Sensu, , .

, sensu-client script , , , "" ( , ) jvm.

, "whoami" script, sensu-client, , script , , script .

+8

All Articles