Left: | ||
Right: |
OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 | 2 |
3 import os, re, subprocess, sys | 3 import os, re, subprocess, sys |
4 | 4 |
5 def extract_data(type, vnstat_output): | 5 def format_bandwidth(bits): |
6 match = re.search(r"%s\s*([\d\.]*) (.bit/s)" % type, vnstat_output) | 6 if bits >= 2000000: |
Felix Dahlke
2013/07/02 09:55:32
Shouldn't it be 1000000 here and 1000 below?
Wladimir Palant
2013/07/02 10:25:16
Not sure, I slightly remember that 1610 kbit/s is
| |
7 if not match: | 7 return "%.2f Mbit/s" % (bits / 1000000) |
8 print "Unable to extract values from '%s'" % vnstat_output | 8 elif bits >= 2000: |
9 sys.exit(1) | 9 return "%.2f kbit/s" % (bits / 1000) |
10 | 10 else: |
11 value = float(match.group(1)) | 11 return "%.2f bit/s" % bits |
12 unit = match.group(2) | |
13 return (value, unit) | |
14 | |
15 def calculate_bits(value, unit): | |
16 if unit == "Mbit/s": | |
17 value *= 1000000 | |
18 elif unit == "kbit/s": | |
19 value *= 1000 | |
20 return int(value) | |
21 | 12 |
22 if __name__ == "__main__": | 13 if __name__ == "__main__": |
23 if len(sys.argv) != 3: | 14 if len(sys.argv) != 3: |
24 script_name = os.path.basename(sys.argv[0]) | 15 script_name = os.path.basename(sys.argv[0]) |
25 print "Usage: %s WARN CRIT" % script_name | 16 print "Usage: %s WARN CRIT" % script_name |
26 sys.exit(0) | 17 sys.exit(0) |
27 | 18 |
28 (warn, crit) = sys.argv[1:3] | 19 (warn, crit) = sys.argv[1:3] |
29 warn = int(sys.argv[1]) | 20 warn = int(sys.argv[1]) |
30 crit = int(sys.argv[2]) | 21 crit = int(sys.argv[2]) |
31 | 22 |
32 vnstat_output = subprocess.check_output(["vnstat", "-tr"]) | 23 vnstat_output = subprocess.check_output(["bwm-ng", "-I", "eth0", "-t", "5000", "-c", "1", "-o", "csv"]) |
Wladimir Palant
2013/06/28 10:12:22
Oops, maybe I should have changed the name of that
Felix Dahlke
2013/07/02 09:55:32
Yup, please do :P
| |
33 (rx, rx_unit) = extract_data("rx", vnstat_output) | 24 data = vnstat_output.splitlines()[0].split(";") |
34 (tx, tx_unit) = extract_data("tx", vnstat_output) | 25 tx = float(data[2]) * 8 |
35 status = "rx %s %s tx %s %s" % (rx, rx_unit, tx, tx_unit) | 26 rx = float(data[3]) * 8 |
27 status = "rx %s tx %s" % (format_bandwidth(rx), format_bandwidth(tx)) | |
36 | 28 |
37 rx = calculate_bits(rx, rx_unit) | 29 perfdata = "rx=%i;%s;%s tx=%i;%s;%s" % (rx, warn, crit, tx, warn, crit) |
Wladimir Palant
2013/06/28 10:12:22
Might be better to use %i for the other parameters
Felix Dahlke
2013/07/02 09:55:32
Yes, no idea why I didn't.
| |
38 tx = calculate_bits(tx, tx_unit) | |
39 perfdata = "rx=%s;%s;%s tx=%s;%s;%s" % (rx, warn, crit, tx, warn, crit) | |
40 | 30 |
41 output = "%s|%s" % (status, perfdata) | 31 output = "%s|%s" % (status, perfdata) |
42 | 32 |
43 if rx >= crit or tx >= crit: | 33 if rx >= crit or tx >= crit: |
44 print "CRITICAL - " + output | 34 print "CRITICAL - " + output |
45 sys.exit(2) | 35 sys.exit(2) |
46 | 36 |
47 if rx >= warn or tx >= warn: | 37 if rx >= warn or tx >= warn: |
48 print "WARNING - " + output | 38 print "WARNING - " + output |
49 sys.exit(1) | 39 sys.exit(1) |
50 | 40 |
51 print "OK - " + output | 41 print "OK - " + output |
OLD | NEW |