| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 | 2 |
| 3 import re, subprocess, sys | 3 import 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 print "Usage: `basename $0` WARN CRIT" |
| 25 sys.exit(0) | 25 sys.exit(0) |
| 26 | 26 |
| 27 (warn, crit) = sys.argv[1:3] | 27 (warn, crit) = sys.argv[1:3] |
| 28 warn = int(sys.argv[1]) |
| 29 crit = int(sys.argv[2]) |
| 28 | 30 |
| 29 vnstat_output = subprocess.check_output(["vnstat", "-tr"]) | 31 vnstat_output = subprocess.check_output(["vnstat", "-tr"]) |
| 30 (rx, rx_unit) = extract_data("rx", vnstat_output) | 32 (rx, rx_unit) = extract_data("rx", vnstat_output) |
| 31 (tx, tx_unit) = extract_data("tx", vnstat_output) | 33 (tx, tx_unit) = extract_data("tx", vnstat_output) |
| 32 status = "rx %s %s tx %s %s" % (rx, rx_unit, tx, tx_unit) | 34 status = "rx %s %s tx %s %s" % (rx, rx_unit, tx, tx_unit) |
| 33 | 35 |
| 34 rx = calculate_bits(rx, rx_unit) | 36 rx = calculate_bits(rx, rx_unit) |
| 35 tx = calculate_bits(tx, tx_unit) | 37 tx = calculate_bits(tx, tx_unit) |
| 36 perfdata = "rx=%s;%s;%s tx=%s;%s;%s" % (rx, warn, crit, tx, warn, crit) | 38 perfdata = "rx=%s;%s;%s tx=%s;%s;%s" % (rx, warn, crit, tx, warn, crit) |
| 37 | 39 |
| 38 output = "%s|%s" % (status, perfdata) | 40 output = "%s|%s" % (status, perfdata) |
| 39 | 41 |
| 40 if warn <= rx or warn <= tx: | 42 if rx >= crit or tx >= crit: |
| 43 print "CRITICAL - " + output |
| 44 sys.exit(2) |
| 45 |
| 46 if rx >= warn or tx >= warn: |
| 41 print "WARNING - " + output | 47 print "WARNING - " + output |
| 42 sys.exit(1) | 48 sys.exit(1) |
| 43 | 49 |
| 44 if crit <= rx or crit <= tx: | |
| 45 print "CRITICAL - " + output | |
| 46 sys.exit(2) | |
| 47 | |
| 48 print "OK - " + output | 50 print "OK - " + output |
| OLD | NEW |