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

Side by Side 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.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | modules/nagios/manifests/client.pp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 >= 1000000:
7 if not match: 7 return "%.2f Mbit/s" % (bits / 1000000)
8 print "Unable to extract values from '%s'" % vnstat_output 8 elif bits >= 1000:
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 process_output = subprocess.check_output(["bwm-ng", "-I", "eth0", "-t", "5000" , "-c", "1", "-o", "csv"])
33 (rx, rx_unit) = extract_data("rx", vnstat_output) 24 data = process_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;%i;%i tx=%i;%i;%i" % (rx, warn, crit, tx, warn, crit)
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
OLDNEW
« 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