Assistance with finishing up a plugin

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
derekb
Posts: 177
Joined: Wed Jun 10, 2015 1:54 pm

Assistance with finishing up a plugin

Post by derekb »

Hi there,

I've been working on a bash plugin to curl the text on a status screen of an IP Camera device that doesn't support SNMP Traps. I scrape the data, dump it to a file in /tmp, and grep for a bunch of static data to display. This data is not reported/alerted on, so there are no threshold variables in this script.

The only thing I alert on is if the camera/device is recording to it's storage path or not.

Currently, I can run the script from a bash shell, and I get the desired output on the screen. However, when it's run from NAGIOS the script shows OK, but I only see one line of the output, when there are atleast 10+ lines of output produced.

I can post the code if anyone is interested in helping me, or pointing me in the right direction.

I apologize if this is the wrong forum to be posting this in.
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: Assistance with finishing up a plugin

Post by rkennedy »

I wonder if the data is getting truncated, could you post examples of what you're seeing over the CLI and over Nagios?

Additionally, what version of Nagios are you using?
Former Nagios Employee
derekb
Posts: 177
Joined: Wed Jun 10, 2015 1:54 pm

Re: Assistance with finishing up a plugin

Post by derekb »

rkennedy wrote:I wonder if the data is getting truncated, could you post examples of what you're seeing over the CLI and over Nagios?

Additionally, what version of Nagios are you using?
Thanks for the reply!
I'm running XI 5.2.5 (latest).

Here's the output of the command via CLI:

Code: Select all

root@raspberrypi:/home/pi# ./mobotix.sh -H 192.168.1.142 -u admin -p Empire99
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  3389    0  3389    0     0    843      0 --:--:--  0:00:04 --:--:--   845
./mobotix.sh: line 100: / 1024: syntax error: operand expected (error token is "/ 1024")
--------------------
Camera Information
--------------------
Model                          Q25M-Secure
Factory IP Address             10.14.48.20
Hardware                       T2r4.3b, 806 MHz
Image Sensor                   color 5MP, L12/B016,F2.0 (ceiling-mounted)
Software                       MX-V4.3.4.66 (2015-09-10)
Current Uptime                 00:15:21
Camera Name                    DemoQ25.empirevoip.com
Link Speed and Duplex          100Mbps / Full Duplex
Camera Temperature             18°C (65°F)
--------------------
Storage Information
--------------------
Recording Type                  CIFS File Server
Recording Path                  \\\\192.168.1.114\\camera
Recording Status                        - mount failed!
Current Usage
Maximum Storage Space            GB
CRITICAL - Failed to mount Network Share!
In NAGIOS, I only see "--------------------" being reported, haha. I'm not a programmer by nature, so this was my first attempt at hacking together a script.

Here's the code I've hacked together... I will not be offended if you tear it to pieces :)

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 - YO MOMMA!"
        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"

###################################
# 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
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: Assistance with finishing up a plugin

Post by rkennedy »

Ah - with this many characters I suspect that XI might be truncating it. Take a look at this document -
https://support.nagios.com/kb/article.php?id=478

You'll need to run those 4 commands, and then force a new check. The SQL field can only be a max of 255 characters, but this increases the limit for you.

Once you've done this, let us know the result.
Former Nagios Employee
derekb
Posts: 177
Joined: Wed Jun 10, 2015 1:54 pm

Re: Assistance with finishing up a plugin

Post by derekb »

rkennedy wrote:Ah - with this many characters I suspect that XI might be truncating it. Take a look at this document -
https://support.nagios.com/kb/article.php?id=478

You'll need to run those 4 commands, and then force a new check. The SQL field can only be a max of 255 characters, but this increases the limit for you.

Once you've done this, let us know the result.
I haven't tried this yet, but I have other plugins that are producing far more characters in the output than mine. Only difference is those plugins were written in perl
User avatar
Box293
Too Basu
Posts: 5126
Joined: Sun Feb 07, 2010 10:55 pm
Location: Deniliquin, Australia
Contact:

Re: Assistance with finishing up a plugin

Post by Box293 »

derekb wrote:./mobotix.sh: line 100: / 1024: syntax error: operand expected (error token is "/ 1024")
Fix this error.

Next, you want to "build" your final exit message into a variable and ending the lines with \n
exit_message=""
exit_message+="Blah Blah\n"
exit_message+=" Some more blah\n"
Then before you exit the script, echo the variable:

Code: Select all

echo $exit_message
exit $exit_state
Tell us how that goes.
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
derekb
Posts: 177
Joined: Wed Jun 10, 2015 1:54 pm

Re: Assistance with finishing up a plugin

Post by derekb »

Box293 wrote:
derekb wrote:./mobotix.sh: line 100: / 1024: syntax error: operand expected (error token is "/ 1024")
Fix this error.

Next, you want to "build" your final exit message into a variable and ending the lines with \n
exit_message=""
exit_message+="Blah Blah\n"
exit_message+=" Some more blah\n"
Then before you exit the script, echo the variable:

Code: Select all

echo $exit_message
exit $exit_state
Tell us how that goes.
Welp, I gave that a shot, and failed miserably haha. I only built a few lines of the $exit_message variable, but you'll see what I mean....

Code: Select all

root@raspberrypi:/home/pi# ./mobotix.sh -H 192.168.1.142 -u admin -p Empire99
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  3389    0  3389    0     0    853      0 --:--:--  0:00:03 --:--:--   855

--------------------\nCamera Information\n--------------------\nModel Q25M-Secure Factory IP Address 10.14.48.20 Hardware T2r4.3b, 806 MHz Image Sensor color 5MP, L12/B016,F2.0 (ceiling-mounted) Software MX-V4.3.4.66 (2015-09-10) Current Uptime 00:05:47 Camera Name DemoQ25.empirevoip.com Link Speed and Duplex 100Mbps / Full Duplex Camera Temperature 2°C (35°F)\n
CRITICAL - Failed to mount Network Share!
You can see my line endings \n in there and everything is showing on one line...

Here's what I changed, just commented out the old information and added new exit_message variable:

Code: Select all

###################################
# 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            $MAXSTORAGE"

exit_message="--------------------\n"
exit_message+="Camera Information\n"
exit_message+="--------------------\n"
exit_message+="$CAMINFO\n"

echo $exit_message
User avatar
hsmith
Agent Smith
Posts: 3539
Joined: Thu Jul 30, 2015 11:09 am
Location: 127.0.0.1
Contact:

Re: Assistance with finishing up a plugin

Post by hsmith »

When you run it like that, is it working?
Former Nagios Employee.
me.
derekb
Posts: 177
Joined: Wed Jun 10, 2015 1:54 pm

Re: Assistance with finishing up a plugin

Post by derekb »

hsmith wrote:When you run it like that, is it working?
Not sure what you mean -- it's not working in the sense that it's not displaying the data properly in XI.
User avatar
Box293
Too Basu
Posts: 5126
Joined: Sun Feb 07, 2010 10:55 pm
Location: Deniliquin, Australia
Contact:

Re: Assistance with finishing up a plugin

Post by Box293 »

Can you upload the plugin please so I can have a play.
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
Locked