LEFT | RIGHT |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 | 2 |
3 import re, subprocess, sys | 3 import os, re, subprocess, sys |
4 | 4 |
5 def extract_data(type, vnstat_output): | 5 def extract_data(type, vnstat_output): |
6 match = re.search(r"%s\s*([\d\.]*) (.bit/s)" % type, vnstat_output) | 6 match = re.search(r"%s\s*([\d\.]*) (.bit/s)" % type, vnstat_output) |
7 if not match: | 7 if not match: |
8 print "Unable to extract values from '%s'" % vnstat_output | 8 print "Unable to extract values from '%s'" % vnstat_output |
9 sys.exit(1) | 9 sys.exit(1) |
10 | 10 |
11 value = float(match.group(1)) | 11 value = float(match.group(1)) |
12 unit = match.group(2) | 12 unit = match.group(2) |
13 return (value, unit) | 13 return (value, unit) |
14 | 14 |
15 def calculate_bits(value, unit): | 15 def calculate_bits(value, unit): |
16 if unit == "Mbit/s": | 16 if unit == "Mbit/s": |
17 value *= 1000000 | 17 value *= 1000000 |
18 elif unit == "kbit/s": | 18 elif unit == "kbit/s": |
19 value *= 1000 | 19 value *= 1000 |
20 return int(value) | 20 return int(value) |
21 | 21 |
22 if __name__ == "__main__": | 22 if __name__ == "__main__": |
23 if len(sys.argv) != 3: | 23 if len(sys.argv) != 3: |
24 print "Usage: `basename $0` WARN CRIT" | 24 script_name = os.path.basename(sys.argv[0]) |
| 25 print "Usage: %s WARN CRIT" % script_name |
25 sys.exit(0) | 26 sys.exit(0) |
26 | 27 |
27 (warn, crit) = sys.argv[1:3] | 28 (warn, crit) = sys.argv[1:3] |
28 warn = int(sys.argv[1]) | 29 warn = int(sys.argv[1]) |
29 crit = int(sys.argv[2]) | 30 crit = int(sys.argv[2]) |
30 | 31 |
31 vnstat_output = subprocess.check_output(["vnstat", "-tr"]) | 32 vnstat_output = subprocess.check_output(["vnstat", "-tr"]) |
32 (rx, rx_unit) = extract_data("rx", vnstat_output) | 33 (rx, rx_unit) = extract_data("rx", vnstat_output) |
33 (tx, tx_unit) = extract_data("tx", vnstat_output) | 34 (tx, tx_unit) = extract_data("tx", vnstat_output) |
34 status = "rx %s %s tx %s %s" % (rx, rx_unit, tx, tx_unit) | 35 status = "rx %s %s tx %s %s" % (rx, rx_unit, tx, tx_unit) |
35 | 36 |
36 rx = calculate_bits(rx, rx_unit) | 37 rx = calculate_bits(rx, rx_unit) |
37 tx = calculate_bits(tx, tx_unit) | 38 tx = calculate_bits(tx, tx_unit) |
38 perfdata = "rx=%s;%s;%s tx=%s;%s;%s" % (rx, warn, crit, tx, warn, crit) | 39 perfdata = "rx=%s;%s;%s tx=%s;%s;%s" % (rx, warn, crit, tx, warn, crit) |
39 | 40 |
40 output = "%s|%s" % (status, perfdata) | 41 output = "%s|%s" % (status, perfdata) |
41 | 42 |
42 if rx >= crit or tx >= crit: | 43 if rx >= crit or tx >= crit: |
43 print "CRITICAL - " + output | 44 print "CRITICAL - " + output |
44 sys.exit(2) | 45 sys.exit(2) |
45 | 46 |
46 if rx >= warn or tx >= warn: | 47 if rx >= warn or tx >= warn: |
47 print "WARNING - " + output | 48 print "WARNING - " + output |
48 sys.exit(1) | 49 sys.exit(1) |
49 | 50 |
50 print "OK - " + output | 51 print "OK - " + output |
LEFT | RIGHT |