arduino and nagios - plugin The code is well ?

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.
Locked
disbe
Posts: 1
Joined: Thu Jul 07, 2016 8:08 pm

arduino and nagios - plugin The code is well ?

Post by disbe »

hello I hope are well.
I'm starting to work with a project, which is a temperature and humidity monitoring by sensors connected to arduino, these data are monitored want to nagios. q have the plugin to connect to arduino nagios but not if it works well as defining the services tells me that know the arduino. here I leave the code if they want to review it and help me. thank you very much.

import sys, re, urllib.request, argparse


# nagios plugin API return values
OK = 0
WARNING = 1
CRITICAL = 2
UNKNOWN = 3

# default thresholds
t_warn = 25.0
t_crit = 30.0
h_warn = 60.0
h_crit = 65.0

def main(tipo,sensor):
retval = OK
outstring = 'Status is '

# Establish a connection with the Arduino Ethernet and
# fetch data. A timeout of 5 seconds is set.
try:
f = urllib.request.urlopen(sensor, timeout=5)
except:
# If the URL cannot be opened we return '3' == UNKNOWN
print('Network error')
sys.exit(UNKNOWN)

# Decode the byte stream and pick out readings using a
# regular expression
result = f.read().decode('utf-8')
#print(result)
#m = re.search('T:(\d+\.\d+),H:(\d+\.\d+),L:(\d+)', result)
#m = re.search('Temperatura (Celsius): (\d+\.\d+),Humedad: (\d+\.\d+)', result)
m = re.search(r'T: (\d+\.\d+)', result)
h = re.search(r'H: (\d+\.\d+)', result)
#print(m, '\n', h)
print(m.group(), '\n', h.group())

if tipo == 'T':
# temperature
temp = float(m.group(1))
if temp <= t_warn:
outstring += 'OK - Temperature: DHT11 ' + str(temp) + ' C|temperature=' + str(temp)
elif t_warn < temp <= t_crit:
outstring += 'WARNING - Temperature: DHT11 ' + str(temp) + ' C|temperature=' + str(temp)
retval = WARNING
elif temp > t_crit:
outstring += 'CRITICAL - Temperature: DHT11 ' + str(temp) + ' C|temperature=' + str(temp)
retval = CRITICAL
elif tipo == 'H':
# humidity
hum = float(h.group(1))
if hum <= h_warn:
outstring += 'OK - Humidity: DHT11 ' + str(hum) + ' %|humidity=' + str(hum)
elif h_warn < hum <= h_crit:
outstring += 'WARNING - Humidity: DHT11 ' + str(hum) + ' %|humidity=' + str(hum)
retval = WARNING
elif hum > h_crit:
outstring += 'CRITICAL - Humidity: DHT11 ' + str(hum) + ' %|humidity=' + str(hum)
retval = CRITICAL

print(outstring)
sys.exit(retval)

if __name__ == "__main__":
# Option parsing
parser = argparse.ArgumentParser()
parser.add_argument("-t", "--tipo", help="type of data", choices="TH")
parser.add_argument("-H", "--host", help="Numero IP, default="http://xxx.xxx.x.xxx", action="store", dest="host", required=False)
parser.add_argument("-w", "--warning", type=float, help="warning threshold")
parser.add_argument("-c", "--critical", type=float, help="critical threshold")
args = parser.parse_args()
#serverpage += args.host
# Set warning/critical thresholds if provided
if args.warning != None:
t_warn = args.warning
h_warn = args.warning
if args.critical != None:
t_crit = args.critical
h_crit = args.critical

main(args.tipo, args.host)
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: arduino and nagios - plugin The code is well ?

Post by rkennedy »

disbe wrote:hello I hope are well.
I'm starting to work with a project, which is a temperature and humidity monitoring by sensors connected to arduino, these data are monitored want to nagios. q have the plugin to connect to arduino nagios but not if it works well as defining the services tells me that know the arduino. here I leave the code if they want to review it and help me. thank you very much.

import sys, re, urllib.request, argparse


# nagios plugin API return values
OK = 0
WARNING = 1
CRITICAL = 2
UNKNOWN = 3

# default thresholds
t_warn = 25.0
t_crit = 30.0
h_warn = 60.0
h_crit = 65.0

def main(tipo,sensor):
retval = OK
outstring = 'Status is '

# Establish a connection with the Arduino Ethernet and
# fetch data. A timeout of 5 seconds is set.
try:
f = urllib.request.urlopen(sensor, timeout=5)
except:
# If the URL cannot be opened we return '3' == UNKNOWN
print('Network error')
sys.exit(UNKNOWN)

# Decode the byte stream and pick out readings using a
# regular expression
result = f.read().decode('utf-8')
#print(result)
#m = re.search('T:(\d+\.\d+),H:(\d+\.\d+),L:(\d+)', result)
#m = re.search('Temperatura (Celsius): (\d+\.\d+),Humedad: (\d+\.\d+)', result)
m = re.search(r'T: (\d+\.\d+)', result)
h = re.search(r'H: (\d+\.\d+)', result)
#print(m, '\n', h)
print(m.group(), '\n', h.group())

if tipo == 'T':
# temperature
temp = float(m.group(1))
if temp <= t_warn:
outstring += 'OK - Temperature: DHT11 ' + str(temp) + ' C|temperature=' + str(temp)
elif t_warn < temp <= t_crit:
outstring += 'WARNING - Temperature: DHT11 ' + str(temp) + ' C|temperature=' + str(temp)
retval = WARNING
elif temp > t_crit:
outstring += 'CRITICAL - Temperature: DHT11 ' + str(temp) + ' C|temperature=' + str(temp)
retval = CRITICAL
elif tipo == 'H':
# humidity
hum = float(h.group(1))
if hum <= h_warn:
outstring += 'OK - Humidity: DHT11 ' + str(hum) + ' %|humidity=' + str(hum)
elif h_warn < hum <= h_crit:
outstring += 'WARNING - Humidity: DHT11 ' + str(hum) + ' %|humidity=' + str(hum)
retval = WARNING
elif hum > h_crit:
outstring += 'CRITICAL - Humidity: DHT11 ' + str(hum) + ' %|humidity=' + str(hum)
retval = CRITICAL

print(outstring)
sys.exit(retval)

if __name__ == "__main__":
# Option parsing
parser = argparse.ArgumentParser()
parser.add_argument("-t", "--tipo", help="type of data", choices="TH")
parser.add_argument("-H", "--host", help="Numero IP, default="http://xxx.xxx.x.xxx", action="store", dest="host", required=False)
parser.add_argument("-w", "--warning", type=float, help="warning threshold")
parser.add_argument("-c", "--critical", type=float, help="critical threshold")
args = parser.parse_args()
#serverpage += args.host
# Set warning/critical thresholds if provided
if args.warning != None:
t_warn = args.warning
h_warn = args.warning
if args.critical != None:
t_crit = args.critical
h_crit = args.critical

main(args.tipo, args.host)
Can you show us the exact error you're seeing?
Former Nagios Employee
Locked