Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: modules/nagios/files/check_bandwidth

Issue 10992045: Switch to bwm-ng for bandwidth monitoring (Closed)
Patch Set: Created July 2, 2013, 10:24 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | modules/nagios/manifests/client.pp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 >= 1000000:
+ return "%.2f Mbit/s" % (bits / 1000000)
+ elif bits >= 1000:
+ 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)
+ process_output = subprocess.check_output(["bwm-ng", "-I", "eth0", "-t", "5000", "-c", "1", "-o", "csv"])
+ data = process_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;%i;%i tx=%i;%i;%i" % (rx, warn, crit, tx, warn, crit)
output = "%s|%s" % (status, perfdata)
if rx >= crit or tx >= crit:
print "CRITICAL - " + output
sys.exit(2)
if rx >= warn or tx >= warn:
« no previous file with comments | « no previous file | modules/nagios/manifests/client.pp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld