Add nagios plugin

master
Micke Nordin 4 years ago
parent e24fc552ed
commit b8ecafcf5b

@ -14,14 +14,14 @@ import sys
def usage(): def usage():
print( print(
"Usage: " + __file__ + "Usage: " + __file__ +
"[--config path_to_file.ini] [--station smhi_station_number ] --db database] [--host dbhost] [--user dbuser] [--password dbpassword]\n" "[--config path_to_file.ini] [--station smhi_station_number ] --database database] [--host dbhost] [--user dbuser] [--password dbpassword]\n"
"Default configfile is weather.ini, any parameter can be overwritten on the command line" "Default configfile is weather.ini, any parameter can be overwritten on the command line"
) )
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('--config') parser.add_argument('--config')
parser.add_argument('--db') parser.add_argument('--database')
parser.add_argument('--host') parser.add_argument('--host')
parser.add_argument('--user') parser.add_argument('--user')
parser.add_argument('--password') parser.add_argument('--password')
@ -32,7 +32,7 @@ if args.config:
config_file = args.config config_file = args.config
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read(config_file) config.read(config_file)
db = config['MySQL']['db'] db = config['MySQL']['database']
host = config['MySQL']['host'] host = config['MySQL']['host']
user = config['MySQL']['user'] user = config['MySQL']['user']
password = config['MySQL']['password'] password = config['MySQL']['password']
@ -50,8 +50,8 @@ if args.station:
station = args.station station = args.station
if not station: if not station:
station = "97100" station = "97100"
if args.db: if args.database:
db = args.db db = args.database
if args.host: if args.host:
host = args.host host = args.host
if not host: if not host:
@ -94,7 +94,7 @@ csv = "date,T_max,T_min,T_mean,RH_max,RH_min,RH_mean,Rainfall\n"
for i in dates: for i in dates:
working_date = i[0].strftime('%Y-%m-%d') working_date = i[0].strftime('%Y-%m-%d')
day_select = ('SELECT * FROM weather ' day_select = ('SELECT * FROM weather '
'WHERE date = "{}"'.format(working_date)) 'WHERE date = "{}" AND station = {}'.format(working_date,station))
cursor.execute(day_select) cursor.execute(day_select)
day = cursor.fetchall() day = cursor.fetchall()
sum_rain = 0 sum_rain = 0

@ -0,0 +1,40 @@
#!/bin/bash
while getopts “:w:c:” opt; do
case $opt in
w) warn=$OPTARG ;;
c) crit=$OPTARG ;;
esac
done
aweekago=$(date -d "7 days ago" +%Y-%m-%d)
now=$(date +%Y-%m-%d)
diff=$(mysql --defaults-extra-file=weather.ini -NBs -e "SELECT avg(Rainfall - ETo) FROM aggregated_weather WHERE Date BETWEEN \"${aweekago}\" AND \"${now}\"" ${db})
OK=0
WARNING=1
CRITICAL=2
UNKNOWN=3
state=${OK}
msg="OK:"
if [[ "x${warn}" == "x" ]]; then
warn=-0.5
fi
if [[ "x${crit}" == "x" ]]; then
crit=-2
fi
if (( $(echo "${diff} < ${warn}" |bc -l) )); then
state=${WARNING}
msg="WARNING:"
fi
if (( $(echo "${diff} < ${crit}" |bc -l) )); then
state=${CRITICAL}
msg="CRITICAL:"
fi
echo "${msg} Rainfall - evapotransipration is ${diff}|diff=${diff};${warn};${crit}"
exit ${state}

@ -11,7 +11,7 @@ import sys
def usage(): def usage():
print( print(
"Usage: " + __file__ + "Usage: " + __file__ +
"[--config path_to_file.ini] [--station smhi_station_number] [--period latest-hour|latest-day|latest-months] [--db database] [--host dbhost] [--user dbuser] [--password dbpassword]\n" "[--config path_to_file.ini] [--station smhi_station_number] [--period latest-hour|latest-day|latest-months] [--database database] [--host dbhost] [--user dbuser] [--password dbpassword]\n"
"Default configfile is weather.ini, any parameter can be overwritten on the command line" "Default configfile is weather.ini, any parameter can be overwritten on the command line"
) )
@ -20,7 +20,7 @@ parser = argparse.ArgumentParser()
parser.add_argument('--config') parser.add_argument('--config')
parser.add_argument('--station') parser.add_argument('--station')
parser.add_argument('--period') parser.add_argument('--period')
parser.add_argument('--db') parser.add_argument('--database')
parser.add_argument('--host') parser.add_argument('--host')
parser.add_argument('--user') parser.add_argument('--user')
parser.add_argument('--password') parser.add_argument('--password')
@ -30,7 +30,7 @@ if args.config:
config_file = args.config config_file = args.config
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read(config_file) config.read(config_file)
db = config['MySQL']['db'] db = config['MySQL']['database']
host = config['MySQL']['host'] host = config['MySQL']['host']
user = config['MySQL']['user'] user = config['MySQL']['user']
password = config['MySQL']['password'] password = config['MySQL']['password']
@ -49,8 +49,8 @@ if not period:
if period not in ["latest-hour", "latest-day", "latest-months"]: if period not in ["latest-hour", "latest-day", "latest-months"]:
period = "latest-day" period = "latest-day"
if args.db: if args.database:
db = args.db db = args.database
if args.host: if args.host:
host = args.host host = args.host
if not host: if not host:

@ -1,5 +1,5 @@
[MySQL] [MySQL]
db = weather database = weather
host = localhost host = localhost
user = your_user_name user = your_user_name
password = secret_password password = secret_password

Loading…
Cancel
Save