| Index: modules/nagios/files/check_bandwidth |
| =================================================================== |
| --- a/modules/nagios/files/check_bandwidth |
| +++ b/modules/nagios/files/check_bandwidth |
| @@ -1,47 +1,37 @@ |
| #!/usr/bin/env python |
| import os, re, subprocess, sys |
| -def extract_data(type, vnstat_output): |
| - match = re.search(r"%s\s*([\d\.]*) (.bit/s)" % type, vnstat_output) |
| - if not match: |
| - print "Unable to extract values from '%s'" % vnstat_output |
| - sys.exit(1) |
| - |
| - value = float(match.group(1)) |
| - unit = match.group(2) |
| - return (value, unit) |
| - |
| -def calculate_bits(value, unit): |
| - if unit == "Mbit/s": |
| - value *= 1000000 |
| - elif unit == "kbit/s": |
| - value *= 1000 |
| - return int(value) |
| +def format_bandwidth(bits): |
| + 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
|
| + return "%.2f Mbit/s" % (bits / 1000000) |
| + elif bits >= 2000: |
| + return "%.2f kbit/s" % (bits / 1000) |
| + else: |
| + return "%.2f bit/s" % bits |
| if __name__ == "__main__": |
| if len(sys.argv) != 3: |
| script_name = os.path.basename(sys.argv[0]) |
| print "Usage: %s WARN CRIT" % script_name |
| sys.exit(0) |
| (warn, crit) = sys.argv[1:3] |
| warn = int(sys.argv[1]) |
| crit = int(sys.argv[2]) |
| - vnstat_output = subprocess.check_output(["vnstat", "-tr"]) |
| - (rx, rx_unit) = extract_data("rx", vnstat_output) |
| - (tx, tx_unit) = extract_data("tx", vnstat_output) |
| - status = "rx %s %s tx %s %s" % (rx, rx_unit, tx, tx_unit) |
| + 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
|
| + data = vnstat_output.splitlines()[0].split(";") |
| + tx = float(data[2]) * 8 |
| + rx = float(data[3]) * 8 |
| + status = "rx %s tx %s" % (format_bandwidth(rx), format_bandwidth(tx)) |
| - rx = calculate_bits(rx, rx_unit) |
| - tx = calculate_bits(tx, tx_unit) |
| - perfdata = "rx=%s;%s;%s tx=%s;%s;%s" % (rx, warn, crit, tx, warn, crit) |
| + 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.
|
| output = "%s|%s" % (status, perfdata) |
| if rx >= crit or tx >= crit: |
| print "CRITICAL - " + output |
| sys.exit(2) |
| if rx >= warn or tx >= warn: |