Browse Source

Add nagios plugin

master
Micke Nordin 1 year ago
parent
commit
b8ecafcf5b
4 changed files with 52 additions and 12 deletions
  1. 6
    6
      aggregateweather.py
  2. 40
    0
      check_rainfall.sh
  3. 5
    5
      getweather.py
  4. 1
    1
      weather.ini

+ 6
- 6
aggregateweather.py View File

@@ -14,14 +14,14 @@ import sys
14 14
 def usage():
15 15
     print(
16 16
         "Usage: " + __file__ +
17
-        "[--config path_to_file.ini] [--station smhi_station_number ] --db database] [--host dbhost] [--user dbuser] [--password dbpassword]\n"
17
+        "[--config path_to_file.ini] [--station smhi_station_number ] --database database] [--host dbhost] [--user dbuser] [--password dbpassword]\n"
18 18
         "Default configfile is weather.ini, any parameter can be overwritten on the command line"
19 19
     )
20 20
 
21 21
 
22 22
 parser = argparse.ArgumentParser()
23 23
 parser.add_argument('--config')
24
-parser.add_argument('--db')
24
+parser.add_argument('--database')
25 25
 parser.add_argument('--host')
26 26
 parser.add_argument('--user')
27 27
 parser.add_argument('--password')
@@ -32,7 +32,7 @@ if args.config:
32 32
     config_file = args.config
33 33
 config = configparser.ConfigParser()
34 34
 config.read(config_file)
35
-db = config['MySQL']['db']
35
+db = config['MySQL']['database']
36 36
 host = config['MySQL']['host']
37 37
 user = config['MySQL']['user']
38 38
 password = config['MySQL']['password']
@@ -50,8 +50,8 @@ if args.station:
50 50
     station = args.station
51 51
 if not station:
52 52
     station = "97100"
53
-if args.db:
54
-    db = args.db
53
+if args.database:
54
+    db = args.database
55 55
 if args.host:
56 56
     host = args.host
57 57
 if not host:
@@ -94,7 +94,7 @@ csv = "date,T_max,T_min,T_mean,RH_max,RH_min,RH_mean,Rainfall\n"
94 94
 for i in dates:
95 95
     working_date = i[0].strftime('%Y-%m-%d')
96 96
     day_select = ('SELECT * FROM weather '
97
-                  'WHERE date = "{}"'.format(working_date))
97
+                  'WHERE date = "{}" AND station = {}'.format(working_date,station))
98 98
     cursor.execute(day_select)
99 99
     day = cursor.fetchall()
100 100
     sum_rain = 0

+ 40
- 0
check_rainfall.sh View File

@@ -0,0 +1,40 @@
1
+#!/bin/bash
2
+
3
+while getopts “:w:c:” opt; do
4
+  case $opt in
5
+    w) warn=$OPTARG ;;
6
+    c) crit=$OPTARG ;;
7
+  esac
8
+done
9
+
10
+aweekago=$(date -d "7 days ago" +%Y-%m-%d)
11
+now=$(date +%Y-%m-%d)
12
+diff=$(mysql --defaults-extra-file=weather.ini -NBs  -e "SELECT avg(Rainfall - ETo) FROM aggregated_weather WHERE Date BETWEEN \"${aweekago}\" AND \"${now}\"" ${db})
13
+
14
+OK=0
15
+WARNING=1
16
+CRITICAL=2
17
+UNKNOWN=3
18
+
19
+state=${OK}
20
+msg="OK:"
21
+if [[ "x${warn}" == "x" ]]; then
22
+	warn=-0.5 
23
+fi
24
+if [[ "x${crit}" == "x" ]]; then
25
+	crit=-2
26
+fi
27
+
28
+
29
+
30
+if (( $(echo "${diff} < ${warn}" |bc -l) )); then
31
+ 	state=${WARNING}
32
+	msg="WARNING:"
33
+fi
34
+if (( $(echo "${diff} < ${crit}" |bc -l) )); then
35
+ 	state=${CRITICAL}
36
+	msg="CRITICAL:"
37
+fi
38
+
39
+echo "${msg} Rainfall - evapotransipration is ${diff}|diff=${diff};${warn};${crit}"
40
+exit ${state}

+ 5
- 5
getweather.py View File

@@ -11,7 +11,7 @@ import sys
11 11
 def usage():
12 12
     print(
13 13
         "Usage: " + __file__ +
14
-        "[--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"
14
+        "[--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"
15 15
         "Default configfile is weather.ini, any parameter can be overwritten on the command line"
16 16
     )
17 17
 
@@ -20,7 +20,7 @@ parser = argparse.ArgumentParser()
20 20
 parser.add_argument('--config')
21 21
 parser.add_argument('--station')
22 22
 parser.add_argument('--period')
23
-parser.add_argument('--db')
23
+parser.add_argument('--database')
24 24
 parser.add_argument('--host')
25 25
 parser.add_argument('--user')
26 26
 parser.add_argument('--password')
@@ -30,7 +30,7 @@ if args.config:
30 30
     config_file = args.config
31 31
 config = configparser.ConfigParser()
32 32
 config.read(config_file)
33
-db = config['MySQL']['db']
33
+db = config['MySQL']['database']
34 34
 host = config['MySQL']['host']
35 35
 user = config['MySQL']['user']
36 36
 password = config['MySQL']['password']
@@ -49,8 +49,8 @@ if not period:
49 49
 if period not in ["latest-hour", "latest-day", "latest-months"]:
50 50
     period = "latest-day"
51 51
 
52
-if args.db:
53
-    db = args.db
52
+if args.database:
53
+    db = args.database
54 54
 if args.host:
55 55
     host = args.host
56 56
 if not host:

+ 1
- 1
weather.ini View File

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

Loading…
Cancel
Save