Assistance with finishing up a plugin

This forum is intended for the discussion of Nagios plugin development. Feature requests, patches, bug fixes, and all types of development-related discussions are welcome!

NOTE: The SourceForge.net nagiosplug-devel mailing list has been deprecated in favor of this forum in order to expedite support and provide additional features not available on the old mailing list.

Re: Assistance with finishing up a plugin

Postby derekb » Mon Mar 07, 2016 5:44 pm

Box293 wrote:Can you upload the plugin please so I can have a play.


Hey there!
In my original post, I pasted the original code. Is this what you are looking for?
derekb
 
Posts: 177
Joined: Wed Jun 10, 2015 1:54 pm

Re: Assistance with finishing up a plugin

Postby Box293 » Mon Mar 07, 2016 5:50 pm

Can you upload the most recent code please.
User avatar
Box293
Too Basu
 
Posts: 4711
Joined: Sun Feb 07, 2010 10:55 pm
Location: Deniliquin, Australia

Re: Assistance with finishing up a plugin

Postby derekb » Tue Mar 08, 2016 9:26 am

Box293 wrote:Can you upload the most recent code please.


Hey there,

I'm an idiot... I have it partially working now. Here's the code -- I'm still testing stuff:

Code: Select all
#!/bin/bash
# Nagios plugin to monitor a MOBOTIX camera's recording status, as well as other fields.
# The script will download the http://<camip>/control/camerainfo?json file and store
# it locally to /tmp/$FILENAME.txt so that multiple curl are not required.
#
# The script takes several parameters
# - IP of the camera
# - camera username, needs rights to access /control/camera
# - camera password
###########################################################################################

#Set Script Name variable
#SCRIPT=`basename ${BASH_SOURCE[0]}`

# Initialize variables
USER="admin"
PW="meinsm"

# parameters for the script
# check each parameter to be set, else use default values
# quit on no IP set!

function HELP {
echo "check_mxfw - check the flash wear of a MOBOTIX camera"
echo "# arguments are : "
echo "# -H ip "
echo "# -u username "
echo "# -p password "
exit 0;
}

# check number of arguments, of no are given, show help
NUMARGS=$#
if [ $NUMARGS -eq 0 ]; then
  HELP
fi

# Process arguments
while getopts ":H:u:p:?" OPTIONS
do
case $OPTIONS in
    H) #set IP
         IP=$OPTARG
    ;;
    u) # check for username, else set default
         USER=$OPTARG
    ;;
    p) # check for password, else set default
         PW=$OPTARG
    ;;
    *) # ungecognized option, show help
         HELP
    ;;
    ?) # show help
         HELP
    ;;

esac
done

shift $((OPTIND-1))


#DEBUG
#echo "IP : $IP, USER : $USER, PW : $PW, WARNING : $WARNING, CRITICAL : $CRITICAL"
DATE=$(date | awk -F ' ' '{print $4}' | sed 's/\://g';)
TEMPNAME=$(echo $IP|sed 's/\./\_/g')
FILENAME=$(echo $TEMPNAME-$DATE)

#echo $TEMPNAME
#echo $FILENAME

INFO=$(curl http://$USER:$PW@$IP/control/camerainfo?json -o "/tmp/$FILENAME.txt"| grep -e "Model" -e "Factory IP Address" -e "Hardware" -e "Image Sensor" -e "Software" -e "Current Uptime" -e "Temperature" | awk '!/Hardware Gains/' | awk -F '"' '{print $2, " - ", $4}')

INFOERROR=$(cat /tmp/$FILENAME.txt | grep "couldn't connect to host")

#if [[ $INFOERROR == "" ]]; then
#        echo "CRITICAL - Cannot connect to camera!"
#        exit 2
#fi


### The camera info has been scraped and stored in /tmp/$FILENAME.txt.
### We will now grep this local file for the rest of our info, instead
### of doing multiple HTTP Requests to curl the info.

#Pull static information from the camera status page
#CAMINFO=$(cat /tmp/$FILENAME.txt |  grep -e "Model" -e "Factory IP Address" -e "Hardware" -e "Image Sensor" -e "Software" -e "Current Uptime" -e "Camera Name" -e "Link Speed" | awk '!/Hardware Gains/' | awk -F '"' '{print $2, " - ", $4}' )
CAMINFO=$(cat /tmp/$FILENAME.txt |  grep -e "Model" -e "Factory IP Address" -e "Hardware" -e "Image Sensor" -e "Software" -e "Current Uptime" -e "Camera Name" -e "Link Speed" -e "Temperature" | awk '!/Hardware Gains/' | awk -F '"' '{printf "%-30s %s\n", $2, $4}' )

###################################
# Recording Settings
###################################
RECTYPE=$(cat /tmp/$FILENAME.txt | grep "Type" | awk -F '"' '{print $4}')
RECPATH=$(cat /tmp/$FILENAME.txt | grep "Path" | awk -F '"' '{print $4}')
RECERROR=$(cat /tmp/$FILENAME.txt | grep "Status" | awk -F '"' '{print $4}')

#DEBUG
#echo "RECORDING TYPE = $RECTYPE"
#echo "RECORDING PATH = $RECPATH"
#echo "RECORDING ERROR = $RECERROR"

###################################
# Storage Usage
###################################
CURRENTUSAGE=$(cat /tmp/$FILENAME.txt | grep "Current Usage" | awk -F '"' '{print $4}')
MAXSTORAGE=$(cat /tmp/$FILENAME.txt | grep "Maximum Size" | sed 's/[A-Za-z]*//g' | awk -F '"' '{print $4}')
MAXSTORAGEGB=$(($MAXSTORAGE / 1024))

#DEBUG
#echo "CURRENT STORAGE = $CURRENTUSAGE"
#echo "MAXIMUM STORAGE = $MAXSTORAGE"
#echo "MAXIMUM STORAGE - GB = $MAXSTORAGEGB"

###################################
# Display Information
###################################
#echo "-------------------- "
#echo "Camera Information"
#echo "-------------------- "
#echo "$CAMINFO"
#echo "-------------------- "
#echo "Storage Information"
#echo "-------------------- "
#echo "Recording Type         $RECTYPE"
#echo "Recording Path         $RECPATH"
#echo "Recording Status         $RECERROR"
#echo "Current Usage         $CURRENTUSAGE"
#echo "Maximum Storage Space      $MAXSTORAGEGB GB"

exit_message=""
exit_message+="CAMERA INFORMATION\n"
exit_message+="------------------\n"
exit_message+="$CAMINFO\n"
exit_message+="------------------\n"
exit_message+="Recording Type      $RECTYPE"

echo $exit_message

###################################
# Delete $FILENAME.txt
###################################

rm  -f /tmp/$FILENAME.txt

###################################
# NAGIOS Logic Section
###################################

if [[ $RECERROR == "- checking for serverpath failed!" ]]; then
        echo "CRITICAL - Cannot find server path!"
        exit 2
elif [[ $RECERROR == *"- mount failed!"* ]]; then
        echo "CRITICAL - Failed to mount Network Share!"
        exit 2
elif [[ $RECERROR == "*" ]]; then
        echo "CRITICAL - Unknown Error: $RECERROR"
        exit 2
else
        echo "OK - Recording OK!"
        exit 0
fi


Here's how it's displaying in XI:
Capture.PNG


My biggest problem right now is the output generated by this line:
Code: Select all
CAMINFO=$(cat /tmp/$FILENAME.txt |  grep -e "Model" -e "Factory IP Address" -e "Hardware" -e "Image Sensor" -e "Software" -e "Current Uptime" -e "Camera Name" -e "Link Speed" -e "Temperature" | awk '!/Hardware Gains/' | awk -F '"' '{printf "%-30s %s\n", $2, $4}' )


You can see it's displaying all as one line. I took your theory of breaking it up with separate lines using CAMINFO, and CAMINFO+ like this:
Code: Select all
$
CAMINFO=$(cat /tmp/$FILENAME.txt |  grep -e "Model\n")
CAMINFO+=$(cat /tmp/$FILENAME.txt |  grep -e "Factory IP Address\n")

This displays no information between the ----------- lines.

Also tried putting the line-break after the quotes, which also does not display any info between the --------- lines.
Code: Select all
CAMINFO=$(cat /tmp/$FILENAME.txt |  grep -e "Model"\n)
CAMINFO+=$(cat /tmp/$FILENAME.txt |  grep -e "Factory IP Address"\n)


Just one more hurdle and I can take it from there.

Appreciate the help/guidance here without just GIVING the answer/fix.
derekb
 
Posts: 177
Joined: Wed Jun 10, 2015 1:54 pm

Re: Assistance with finishing up a plugin

Postby tmcdonald » Tue Mar 08, 2016 2:48 pm

Newlines likely won't display as you intend in an HTML page - change the \n to <br /> to be an HTML break, and if that renders then all you need to do is go to Admin -> System Settings, and check Allow HTML Tags in Host/Service Status.
Want to see me get thrown into a box? https://www.youtube.com/watch?v=pGSlqxIMxQM
User avatar
tmcdonald
Support Manager
 
Posts: 7929
Joined: Mon Sep 23, 2013 8:40 am

Re: Assistance with finishing up a plugin

Postby derekb » Tue Mar 08, 2016 2:56 pm

tmcdonald wrote:Newlines likely won't display as you intend in an HTML page - change the \n to <br /> to be an HTML break, and if that renders then all you need to do is go to Admin -> System Settings, and check Allow HTML Tags in Host/Service Status.


Which part of my issue are you referring to here and where do I insert this?

I tried what I THINK you were suggestion, and now when the cron entry runs, I can see this output:

Code: Select all
/usr/lib/nagios/plugins/testmobotix.sh: command substitution: line 91: syntax error near unexpected token `)'
/usr/lib/nagios/plugins/testmobotix.sh: command substitution: line 91: `cat /tmp/$FILENAME.txt |  grep -e "Model"<br />)'
/usr/lib/nagios/plugins/testmobotix.sh: command substitution: line 92: syntax error near unexpected token `)'
/usr/lib/nagios/plugins/testmobotix.sh: command substitution: line 92: `cat /tmp/$FILENAME.txt |  grep -e "Factory IP Address"<br />)'
/usr/lib/nagios/plugins/testmobotix.sh: line 110: / 1024: syntax error: operand expected (error token is "/ 1024")


Also tried the </ br> on the outside of the quotes...
Code: Select all
/usr/lib/nagios/plugins/testmobotix.sh: line 90: syntax error near unexpected token `newline'
/usr/lib/nagios/plugins/testmobotix.sh: line 90: `CAMINFO=$(cat /tmp/$FILENAME.txt |  grep -e "Model")</ br>'
derekb
 
Posts: 177
Joined: Wed Jun 10, 2015 1:54 pm

Re: Assistance with finishing up a plugin

Postby derekb » Tue Mar 08, 2016 4:00 pm

Re-coded some things, got it working. Thanks!
derekb
 
Posts: 177
Joined: Wed Jun 10, 2015 1:54 pm

Re: Assistance with finishing up a plugin

Postby Box293 » Tue Mar 08, 2016 6:21 pm

What was the final solution, it may help others in the future.
User avatar
Box293
Too Basu
 
Posts: 4711
Joined: Sun Feb 07, 2010 10:55 pm
Location: Deniliquin, Australia

Re: Assistance with finishing up a plugin

Postby derekb » Sun Sep 11, 2016 1:22 pm

Box293 wrote:What was the final solution, it may help others in the future.


I'm revisiting this, as I wanted to add things like perf data for things etc.

It's very odd... I can run the script/plugin manually from CLI on my Linux machine, but when I run the crobjob command for NRDS to execute the script, I get a ton of errors.

Here, I can run the command manually and it outputs correctly:
Code: Select all
root@raspberrypi:/usr/lib/nagios/plugins# ./check_mobotix.sh.orig -H 192.168.1.251 -u admin -p Empire99 -w 10 -c 12
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  3693    0  3693    0     0   1178      0 --:--:--  0:00:03 --:--:--  1179
--------------------
Camera Information
--------------------
Model                          M25M-Secure
Hardware                       T2r4.3b, 806 MHz
Image Sensor                   color 5MP
Software                       MX-V4.4.0.31 (2016-04-05)
Current Uptime                 1 day 21:32:14
Camera Name                    EmpireComm-BackLot
Link Speed and Duplex          100Mbps / Full Duplex
Camera Temperature             25°C (77°F)
--------------------
Storage Information
--------------------
Recording Type                  CIFS File Server
Recording Path                  \\\\192.168.1.2\\Mobotix
Recording Status
Current Usage                   1.1 GBytes (0.0%)
Maximum Storage Space           4.00 GB
CRITICAL - Camera is too hot! 25°C|temp=25


Now if i run the command for the cron for NRDS to run the check...
Code: Select all
root@raspberrypi:/usr/lib/nagios/plugins#  /usr/local/nrdp/clients/nrds/nrds.pl -H 'Derek Test'
/usr/lib/nagios/plugins/check_mobotix.sh.orig: 26: /usr/lib/nagios/plugins/check_mobotix.sh.orig: function: not found
ERROR: STDIN must be either 3 or 4 fields long, I found 1
ERROR: STDIN must be either 3 or 4 fields long, I found 1
ERROR: STDIN must be either 3 or 4 fields long, I found 1
ERROR: STDIN must be either 3 or 4 fields long, I found 1
ERROR: STDIN must be either 3 or 4 fields long, I found 1


It complains about the HELP function starting at line 26. If I comment that out, the errors move further down in the script complaining about a variable.

Why do I get different results when running this manually vs how the cron runs it?


This is the latest code:
Code: Select all

#!/bin/bash
# Nagios plugin to monitor a MOBOTIX camera's recording status, as well as other fields.
# The script will download the http://<camip>/control/camerainfo?json file and store
# it locally to /tmp/$FILENAME.txt so that multiple curl are not required.
#
# The script takes several parameters
# - IP of the camera
# - camera username, needs rights to access /control/camera
# - camera password
###########################################################################################

#Set Script Name variable
#SCRIPT=`basename ${BASH_SOURCE[0]}`

# Initialize variables
USER="admin"
PW="meinsm"
CRITICALTEMP=0
WARNINGTEMP=0

# parameters for the script
# check each parameter to be set, else use default values
# quit on no IP set!

function HELP {
echo "check_mxfw - check the flash wear of a MOBOTIX camera"
echo "# arguments are : "
echo "# -H ip "
echo "# -u username "
echo "# -p password "
echo "# -c critical temp in celsius "
echo "# -w warning temp in celsius "
exit 0;
}

# check number of arguments, of no are given, show help
NUMARGS=$#
if [ $NUMARGS -eq 0 ]; then
  HELP
fi

# Process arguments
while getopts ":H:u:p:w:c:?" OPTIONS
do
case $OPTIONS in
    H) #set IP
         IP=$OPTARG
    ;;
    u) # check for username, else set default
         USER=$OPTARG
    ;;
    p) # check for password, else set default
         PW=$OPTARG
    ;;
    c) # check for critical temp
   CRITICALTEMP=$OPTARG
    ;;
    w) # check for warning temp
   WARNINGTEMP=$OPTARG
    ;;
    *) # ungecognized option, show help
         HELP
    ;;
    ?) # show help
         HELP
    ;;

esac
done

shift $((OPTIND-1))


#DEBUG
#echo "IP : $IP, USER : $USER, PW : $PW, WARNING : $WARNING, CRITICAL : $CRITICAL"
DATE=$(date | awk -F ' ' '{print $4}' | sed 's/\://g';)
TEMPNAME=$(echo $IP|sed 's/\./\_/g')
FILENAME=$(echo $TEMPNAME-$DATE)

#echo $TEMPNAME
#echo $FILENAME

INFO=$(curl http://$USER:$PW@$IP/control/camerainfo?json -o "/tmp/$FILENAME.txt"| grep -e "Model" -e "Factory IP Address" -e "Hardware" -e "Image Sensor" -e "Software" -e "Current Uptime" -e "Temperature" | awk '!/Hardware Gains/' | awk -F '"' '{print $2, " - ", $4}')

### The camera info has been scraped and stored in /tmp/$FILENAME.txt.
### We will now grep this local file for the rest of our info, instead
### of doing multiple HTTP Requests to curl the info.

#Pull static information from the camera status page
#CAMINFO=$(cat /tmp/$FILENAME.txt |  grep -e "Model" -e "Factory IP Address" -e "Hardware" -e "Image Sensor" -e "Software" -e "Current Uptime" -e "Camera Name" -e "Link Speed" | awk '!/Hardware Gains/' | awk -F '"' '{print $2, " - ", $4}' )
CAMINFO=$(cat /tmp/$FILENAME.txt |  grep -e "Model" -e "Factory IP Address" -e "Hardware" -e "Image Sensor" -e "Software" -e "Current Uptime" -e "Camera Name" -e "Link Speed" -e "Temperature" | awk '!/Hardware Gains/' | awk -F '"' '{printf "%-30s %s\n", $2, $4}' )

###################################
# Recording Settings
###################################
RECTYPE=$(cat /tmp/$FILENAME.txt | grep "Type" | awk -F '"' '{print $4}')
RECPATH=$(cat /tmp/$FILENAME.txt | grep "Path" | awk -F '"' '{print $4}')
RECERROR=$(cat /tmp/$FILENAME.txt | grep -m 1 "Status" | awk -F '"' '{print $4}')
TEMP=$(cat /tmp/$FILENAME.txt | grep "Temperature" | awk -F '"' '{print $4}')
TEMPARR=($TEMP)
CELSIUS=${TEMPARR[0]//[^0-9]*}
#DEBUG
#echo "RECORDING TYPE = $RECTYPE"
#echo "RECORDING PATH = $RECPATH"
#echo "RECORDING ERROR = $RECERROR"

###################################
# Storage Usage
###################################
CURRENTUSAGE=$(cat /tmp/$FILENAME.txt | grep "Current Usage" | awk -F '"' '{print $4}')
MAXSTORAGE=$(cat /tmp/$FILENAME.txt | grep "Maximum Size" | sed 's/[A-Za-z]*//g' | awk -F '"' '{print $4}')
MAXSTORAGEGB=$(awk "BEGIN {printf \"%.2f\",${MAXSTORAGE} / 1024}")

#DEBUG
#echo "CURRENT STORAGE = $CURRENTUSAGE"
#echo "MAXIMUM STORAGE = $MAXSTORAGE"
#echo "MAXIMUM STORAGE - GB = $MAXSTORAGEGB"

###################################
# Display Information
###################################
echo "-------------------- "
echo "Camera Information"
echo "-------------------- "
echo "$CAMINFO"
echo "-------------------- "
echo "Storage Information"
echo "-------------------- "
echo "Recording Type         $RECTYPE"
echo "Recording Path         $RECPATH"
echo "Recording Status         $RECERROR"
echo "Current Usage         $CURRENTUSAGE"
echo "Maximum Storage Space      $MAXSTORAGEGB GB"

###################################
# Delete $FILENAME.txt
###################################

rm  -f /tmp/$FILENAME.txt

###################################
# NAGIOS Logic Section
###################################

#if [[ $RECERROR == "- checking for serverpath failed!" ]]; then
#        echo "CRITICAL - Cannot find server path!"
#        exit 2
#elif [[ $RECERROR == *"- mount failed!"* ]]; then
#        echo "CRITICAL - Failed to mount Network Share!"
#        exit 2
#elif [[ $RECERROR == "*" ]]; then
#        echo "CRITICAL - Unknown Error: $RECERROR"
#        exit 2

#if [ -z ${RECERROR+x} ]; then
if [ $CELSIUS -gt $CRITICALTEMP ] && [ $CRITICALTEMP -gt 0 ]; then
   echo "CRITICAL - Camera is too hot! $CELSIUS°C|temp=$CELSIUS"
   exit 2
elif [ $CELSIUS -gt $WARNINGTEMP ] && [ $WARNINGTEMP -gt 0 ]; then
   echo "WARNING - Camera could overheat soon! $CELSIUS°C"
   exit 1
elif [[ -z $RECERROR ]]; then
   echo "OK - Recording OK!"
   exit 0
else
        echo "CRITICAL $RECERROR"
        exit 2
fi
derekb
 
Posts: 177
Joined: Wed Jun 10, 2015 1:54 pm

Re: Assistance with finishing up a plugin

Postby derekb » Sun Sep 11, 2016 4:18 pm

I found something interesting... I can replicate the errors that are produced when running through nrds.pl by executing the script manually via sh check_mobotix -H blah blah blah. If I run it with ./check_mobotix -H blah blah blah, it works fine.
derekb
 
Posts: 177
Joined: Wed Jun 10, 2015 1:54 pm

Re: Assistance with finishing up a plugin

Postby tmcdonald » Mon Sep 12, 2016 2:58 pm

Want to see me get thrown into a box? https://www.youtube.com/watch?v=pGSlqxIMxQM
User avatar
tmcdonald
Support Manager
 
Posts: 7929
Joined: Mon Sep 23, 2013 8:40 am

PreviousNext

Return to Nagios Plugin Development

Who is online

Users browsing this forum: No registered users and 2 guests