| OLD | NEW |
| 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] |
| 29 warn = int(sys.argv[1]) |
| 30 crit = int(sys.argv[2]) |
| 28 | 31 |
| 29 vnstat_output = subprocess.check_output(["vnstat", "-tr"]) | 32 vnstat_output = subprocess.check_output(["vnstat", "-tr"]) |
| 30 (rx, rx_unit) = extract_data("rx", vnstat_output) | 33 (rx, rx_unit) = extract_data("rx", vnstat_output) |
| 31 (tx, tx_unit) = extract_data("tx", vnstat_output) | 34 (tx, tx_unit) = extract_data("tx", vnstat_output) |
| 32 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) |
| 33 | 36 |
| 34 rx = calculate_bits(rx, rx_unit) | 37 rx = calculate_bits(rx, rx_unit) |
| 35 tx = calculate_bits(tx, tx_unit) | 38 tx = calculate_bits(tx, tx_unit) |
| 36 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) |
| 37 | 40 |
| 38 output = "%s|%s" % (status, perfdata) | 41 output = "%s|%s" % (status, perfdata) |
| 39 | 42 |
| 40 if warn <= rx or warn <= tx: | 43 if rx >= crit or tx >= crit: |
| 44 print "CRITICAL - " + output |
| 45 sys.exit(2) |
| 46 |
| 47 if rx >= warn or tx >= warn: |
| 41 print "WARNING - " + output | 48 print "WARNING - " + output |
| 42 sys.exit(1) | 49 sys.exit(1) |
| 43 | 50 |
| 44 if crit <= rx or crit <= tx: | |
| 45 print "CRITICAL - " + output | |
| 46 sys.exit(2) | |
| 47 | |
| 48 print "OK - " + output | 51 print "OK - " + output |
| OLD | NEW |