Skip to content

Instantly share code, notes, and snippets.

@whywaita
Last active July 5, 2016 15:00
Show Gist options
  • Save whywaita/d0e5e210ef341f072162946e7df1ce9b to your computer and use it in GitHub Desktop.
Save whywaita/d0e5e210ef341f072162946e7df1ce9b to your computer and use it in GitHub Desktop.
monitoring plugin for nagios (use https://github.com/adafruit/Adafruit_Python_DHT )
#!/bin/bash -x
PROGNAME=$(basename $0)
VERSION="1.0"
OK=0
WARNING=1
CRITICAL=2
UNKNOWN=3
for OPT in "$@"
do
case "$OPT" in
# temp or humid
'-t'|'--type' )
FLG_T=1
if [[ -z "$2" ]] || [[ "$2" =~ ^-+ ]]; then
echo "$PROGNAME:「$1」オプションには引数が必要です" 1>&2
exit $UNKNOWN
fi
TYPE="$2"
shift 2
;;
# waring
'-w'|'--warning' )
FLG_W=1
if [[ -z "$2" ]] || [[ "$2" =~ ^-+ ]]; then
echo "$PROGNAME:「$1」オプションには引数が必要です" 1>&2
exit $UNKNOWN
fi
VALUE_WARNING=$2
shift 2
;;
# critical
'-c'|'--critical' )
FLG_C=1
if [[ -z "$2" ]] || [[ "$2" =~ ^-+ ]]; then
echo "$PROGNAME:「$1」オプションには引数が必要です" 1>&2
exit $UNKNOWN
fi
VALUE_CRITICAL=$2
shift 2
;;
esac
done
RESULT=$(/usr/local/bin/${TYPE}.py | perl -ne 'print int($_)')
if [ ${RESULT} -ge ${VALUE_CRITICAL} ]; then
echo "now ${TYPE} is ${RESULT}!!!!"
exit $CRITICAL
fi
if [ ${RESULT} -ge ${VALUE_WARNING} ]; then
echo "now ${TYPE} is ${RESULT}!"
exit $WARNING
fi
echo "now ${TYPE} is ${RESULT}"
exit $OK
#!/usr/bin/python
import sys
import Adafruit_DHT
# Parse command line parameters.
sensor_args = { '11': Adafruit_DHT.DHT11,
'22': Adafruit_DHT.DHT22,
'2302': Adafruit_DHT.AM2302 }
#if len(sys.argv) == 3 and sys.argv[1] in sensor_args:
# sensor = sensor_args[sys.argv[1]]
# pin = sys.argv[2]
#else:
# print 'usage: sudo ./Adafruit_DHT.py [11|22|2302] GPIOpin#'
# print 'example: sudo ./Adafruit_DHT.py 2302 4 - Read from an AM2302 connected to GPIO #4'
# sys.exit(1)
sensor = 11
pin = 4
# Try to grab a sensor reading. Use the read_retry method which will retry up
# to 15 times to get a sensor reading (waiting 2 seconds between each retry).
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
# Un-comment the line below to convert the temperature to Fahrenheit.
# temperature = temperature * 9/5.0 + 32
# Note that sometimes you won't get a reading and
# the results will be null (because Linux can't
# guarantee the timing of calls to read the sensor).
# If this happens try again!
if humidity is not None and temperature is not None:
print '{}'.format(humidity)
else:
print 'Failed to get reading. Try again!'
sys.exit(1)
#!/usr/bin/python
import sys
import Adafruit_DHT
# Parse command line parameters.
sensor_args = { '11': Adafruit_DHT.DHT11,
'22': Adafruit_DHT.DHT22,
'2302': Adafruit_DHT.AM2302 }
#if len(sys.argv) == 3 and sys.argv[1] in sensor_args:
# sensor = sensor_args[sys.argv[1]]
# pin = sys.argv[2]
#else:
# print 'usage: sudo ./Adafruit_DHT.py [11|22|2302] GPIOpin#'
# print 'example: sudo ./Adafruit_DHT.py 2302 4 - Read from an AM2302 connected to GPIO #4'
# sys.exit(1)
sensor = 11
pin = 4
# Try to grab a sensor reading. Use the read_retry method which will retry up
# to 15 times to get a sensor reading (waiting 2 seconds between each retry).
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
# Un-comment the line below to convert the temperature to Fahrenheit.
# temperature = temperature * 9/5.0 + 32
# Note that sometimes you won't get a reading and
# the results will be null (because Linux can't
# guarantee the timing of calls to read the sensor).
# If this happens try again!
if humidity is not None and temperature is not None:
print '{}'.format(temperature)
else:
print 'Failed to get reading. Try again!'
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment