""" Copyright 2011 Julius Schlosburg This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . This Nagios plugin checks whether a Digi Watchport water sensor detects water. """ import argparse import serial import sys parser = argparse.ArgumentParser(description='Nagios plugin which grabs the status\ of a Digi Watchport water sensor') parser.add_argument('-p', '--port', dest='port', default='/dev/ttyUSB0',\ help='Specify the port that the sensor is attached to. Default is /dev\ /ttyUSB0') parser.add_argument('-t', '--timeout', dest='timeout_value', default=10,\ help='Timeout in secods for the serial port. Default is 10') args = parser.parse_args() port = args.port timeout_value = args.timeout_value """ The Nagios states. These values are returned to the calling program and eventually passed back to the Nagios process. We don't need a warning since the sensor status is binary; /water detected/ or /no water detected/. """ OK = 0 CRITICAL = 2 UNKNOWN = 3 try: ser = serial.Serial(port, timeout=timeout_value) except serial.serialutil.SerialException: print('UNKNOWN: unable to open serial port ' + port + '!') sys.exit(UNKNOWN) try: """ If the serial port's become disconnected, this should throw an exception. The read has to take place or it seems to fill up the buffer on the device and disconnect it after a couple calls. """ ser.write(b'?\r') ser.read(100) except SerialTimeoutException: print('UNKNOWN: unable to reach the USB host at ' + port + '!') sys.exit(UNKNOWN) try: status = ser.getCD() except serial.serialutil.SerialException: print('UNKNOWN: unable to verify sensor at port ' + port + '!') sys.exit(UNKNOWN) if not status: print('OK: no water detected.') sys.exit(OK) else: print('CRITICAL: water detected!') sys.exit(CRITICAL)