LEFT | RIGHT |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 | 2 |
3 import os, re, subprocess, sys, socket, struct, fcntl | 3 import os, re, subprocess, sys, socket, struct, fcntl |
4 | 4 |
5 INTERVAL = 5 | 5 INTERVAL = 5 |
6 | 6 |
7 def format_bandwidth(bits): | 7 def format_bandwidth(bits): |
8 if bits >= 1000000: | 8 if bits >= 1000000: |
9 return "%.2f Mbit/s" % (bits / 1000000) | 9 return "%.2f Mbit/s" % (bits / 1000000) |
10 elif bits >= 1000: | 10 elif bits >= 1000: |
11 return "%.2f kbit/s" % (bits / 1000) | 11 return "%.2f kbit/s" % (bits / 1000) |
12 else: | 12 else: |
13 return "%.2f bit/s" % bits | 13 return "%.2f bit/s" % bits |
14 | 14 |
15 def getmacaddress(): | 15 def getmacaddress(nic="eth0"): |
16 # See man netdevice for the request structure: it has to start with 16 bytes | 16 # See man netdevice for the request structure: it has to start with 16 bytes |
17 # containing the interface name, the OS will write 8 bytes after that (2 bytes | 17 # containing the interface name, the OS will write 8 bytes after that (2 bytes |
18 # family name and 6 bytes actual MAC address). | 18 # family name and 6 bytes actual MAC address). |
19 s = socket.socket() | 19 s = socket.socket() |
20 SIOCGIFHWADDR = 0x8927 # see man ioctl_list | 20 SIOCGIFHWADDR = 0x8927 # see man ioctl_list |
21 NIC = str(sys.argv[3]) | 21 return fcntl.ioctl(s.fileno(), SIOCGIFHWADDR, struct.pack("24s", nic))[18:24] |
22 return fcntl.ioctl(s.fileno(), SIOCGIFHWADDR, struct.pack("24s", NIC))[18:24] | |
23 | 22 |
24 if __name__ == "__main__": | 23 if __name__ == "__main__": |
25 if len(sys.argv) != 4: | 24 if len(sys.argv) != 4: |
26 script_name = os.path.basename(sys.argv[0]) | 25 script_name = os.path.basename(sys.argv[0]) |
27 print "Usage: %s WARN CRIT NIC" % script_name | 26 print "Usage: %s WARN CRIT NIC" % script_name |
28 sys.exit(0) | 27 sys.exit(0) |
29 | 28 |
30 (warn, crit) = sys.argv[1:3] | 29 (warn, crit) = sys.argv[1:3] |
31 warn = int(sys.argv[1]) | 30 warn = int(sys.argv[1]) |
32 crit = int(sys.argv[2]) | 31 crit = int(sys.argv[2]) |
| 32 nic = str(sys.argv[3]) |
33 | 33 |
34 process = subprocess.Popen( | 34 process = subprocess.Popen( |
35 ["sudo", "tcpdump", "-q", "-s", "64", "-G", str(INTERVAL), "-W", "1", "-w",
"-"], | 35 ["sudo", "tcpdump", "-q", "-s", "64", "-G", str(INTERVAL), "-W", "1", "-w",
"-"], |
36 stdout=subprocess.PIPE, stderr=subprocess.PIPE) | 36 stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
37 mac = getmacaddress() | 37 mac = getmacaddress() |
38 | 38 |
39 total = {"rx": 0, "tx": 0} | 39 total = {"rx": 0, "tx": 0} |
40 http = {"rx": 0, "tx": 0} | 40 http = {"rx": 0, "tx": 0} |
41 https = {"rx": 0, "tx": 0} | 41 https = {"rx": 0, "tx": 0} |
42 ssh = {"rx": 0, "tx": 0} | 42 ssh = {"rx": 0, "tx": 0} |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 | 132 |
133 if total["rx"] >= crit or total["tx"] >= crit: | 133 if total["rx"] >= crit or total["tx"] >= crit: |
134 print "CRITICAL - " + output | 134 print "CRITICAL - " + output |
135 sys.exit(2) | 135 sys.exit(2) |
136 | 136 |
137 if total["rx"] >= warn or total["tx"] >= warn: | 137 if total["rx"] >= warn or total["tx"] >= warn: |
138 print "WARNING - " + output | 138 print "WARNING - " + output |
139 sys.exit(1) | 139 sys.exit(1) |
140 | 140 |
141 print "OK - " + output | 141 print "OK - " + output |
LEFT | RIGHT |