Use grep in CheckCommand

Hi,

I want to use the “| grep” command to reduce the output of a plugin:

" “./plugin -command1 A -command2 B” | grep “Example” "

In my case it’s about the nagios-check Check_iDrac_for_Dell where I want to use the SENSOR option to get the temperature information of CPU and SYSTEM. My problem is that I want to define two Checks, where one checks the CPU with a certain warning/critical-temperature range and one checks the System with another temperature range. To get an idea what I’m talking about, here is my input and output:

‘./idrac_2.2rc4’ ‘-v’ ‘2c’ ‘-c’ ‘public’ ‘-H’ ‘XXX.XXX.XXX.’ ‘-w’ ‘SENSOR’ ‘–temp-warn’ ‘15,35’

CPU1 Temp: 52.0(!) C ENABLED/OK
CPU2 Temp: 58.0(!) C ENABLED/OK
System Board Inlet Temp: 23.0 C ENABLED/OK
System Board Exhaust Temp: 39.0(!) C ENABLED/OK

The module itself cannot specify between CPU and System-Sensor. My problem would be solved if i could use after the the command " | grep “CPU” ". But I don’t know how to define a CheckCommand like this. My idea for the CheckCommand and Service was this

object CheckCommand “check_iDrac” {
import “plugin-check-command”
command = [ “/usr/lib64/nagios/check_idrac-master/idrac_2.2rc4” ]
arguments = {
“-H” = “$host.address$”
“-w” = “$command$”
“-c” = “$community$”
“-v” = “$version$”
“–temp-warn” = “$rangewarn$”
“–temp-crit” = “$rangecrit$”
“| grep” = “$grep$” }
}

apply Service “iDrac-Sensor-CPU” {
import “generic-service-10m”
display_name = “iDrac CPU Sensor”
check_command = “check_iDrac”
vars.version = “2c”
vars.community = “public”
vars.command = “SENSOR”
vars.grep = “CPU”
vars.rangewarn = “15,80”
vars.rangecrit = “10,90”
assign where host.vars.type == “VIRT”
}

But this doesn’t worked out, because it sees the grep command as part of iDrac-plugin, which doesn’t know grep.

you can use a wrapperscript, if you don’t want, or can’t edit your check directly (f.e. its compiled code or you want updatesafety):
Example wrapper for check_snmp to show correct temprature if comma/dot is missing:

#!/usr/bin/python
#V0.1
import argparse,sys,subprocess,re,string

#Get Args
parser=argparse.ArgumentParser()
parser.add_argument('-C')
parser.add_argument('-H')
parser.add_argument('-o')
parser.add_argument('-t')
parser.add_argument('-l')
parser.add_argument('-u')
args=parser.parse_args()
arguments=""
for arg in vars(args):
 if getattr(args, arg) is not None:
  arguments += "-" + arg + " " + getattr(args, arg) + " "

#get snmp
try:
 command="/usr/lib/nagios/plugins/check_snmp " + arguments
except:
 print "Error - Can't parse Arguments!"
 sys.exit(3)

snmp_result = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
snmp_output, err = snmp_result.communicate()
snmp_returncode = snmp_result.returncode

#Converting Number
tempStringResult = re.search('\d{1,3}', snmp_output)
if tempStringResult is not None:
 tempString = tempStringResult.group(0)
 tempFloat = float(tempString) / 10
 new_snmp_output=snmp_output.replace(str(tempString),str(tempFloat))
else:
 new_snmp_output=snmp_output

#output
print(new_snmp_output)
sys.exit(snmp_returncode)
3 Likes

Hi,

grep will not only reduce the output, it will also override the exit state. A better solution would be to write a small shell wrapper which takes all the parameters, greps for content, and then sets the exit state accordingly.

Since I always need to Google that with Bash, I’ve just created such a script for your convenience.

Cheers,
Michael

3 Likes

Thanks Rafael for your answer. In principle this script works well, but I have a small problem with the code in which you might help.

import argparse,sys,subprocess,re,string

#Get Args
parser=argparse.ArgumentParser()
parser.add_argument(‘-v’)
parser.add_argument(‘-H’)
parser.add_argument(‘-w’)
parser.add_argument(‘–temp-warn=’)
parser.add_argument(‘–temp-crit=’)
parser.add_argument(‘-p’)
parser.add_argument(‘-c’)
args=parser.parse_args()
arguments=“”
for arg in vars(args):
if getattr(args, arg) is not None:
arguments += “-” + arg + " " + getattr(args, arg) + " "

#get snmp
try:
command="/usr/lib64/nagios/check_idrac-master/idrac_2.2rc4 " + arguments
except:
print “Error - Can’t parse Arguments!”
sys.exit(3)

snmp_result = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
snmp_output, err = snmp_result.communicate()
snmp_returncode = snmp_result.returncode

#Converting Number
tempStringResult = re.search(‘CPU1’, snmp_output)
if tempStringResult is not None:
tempString = tempStringResult.group(0)
new_snmp_output=snmp_output.split(“\n”,2)[2];
else:
new_snmp_output=snmp_output

#output
print(new_snmp_output)
sys.exit(snmp_returncode)

The problematic part is the --temp-warn/crit command. The foor-loop only accepts commands with one -. How can I give the for-loop the option that it takes also these commands with the right syntax, e.g. --temp-warn=25,45?

HI, actually i just started with python, so my skills are a little limited :slight_smile:

I’am building the argument line string for check_snmp with this line in the code:
arguments += "-" + arg + " " + getattr(args, arg) + " "
because arg gives the argument without the “-”. maybe change “-” to “–” would help, but than you need to determine if a “–” or a “-” is needed. I’am pretty sure there is a better solution around, maybe a python veteran has a better idea. For the debugging you could “print(arg)” whats stored in the “arg” variable to see whats the problem.