OLD | NEW |
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(nic="eth0"): | 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 return fcntl.ioctl(s.fileno(), SIOCGIFHWADDR, struct.pack("24s", nic))[18:24] | 21 return fcntl.ioctl(s.fileno(), SIOCGIFHWADDR, struct.pack("24s", nic))[18:24] |
22 | 22 |
23 if __name__ == "__main__": | 23 if __name__ == "__main__": |
24 if len(sys.argv) != 4: | 24 if len(sys.argv) != 4: |
25 script_name = os.path.basename(sys.argv[0]) | 25 script_name = os.path.basename(sys.argv[0]) |
26 print "Usage: %s WARN CRIT NIC" % script_name | 26 print "Usage: %s WARN CRIT NIC" % script_name |
27 sys.exit(0) | 27 sys.exit(0) |
28 | 28 |
29 (warn, crit) = sys.argv[1:3] | |
30 warn = int(sys.argv[1]) | 29 warn = int(sys.argv[1]) |
31 crit = int(sys.argv[2]) | 30 crit = int(sys.argv[2]) |
32 nic = str(sys.argv[3]) | 31 nic = str(sys.argv[3]) |
33 | 32 |
34 process = subprocess.Popen( | 33 process = subprocess.Popen( |
35 ["sudo", "tcpdump", "-q", "-s", "64", "-G", str(INTERVAL), "-W", "1", "-w",
"-"], | 34 ["sudo", "tcpdump", "-q", "-s", "64", "-G", str(INTERVAL), "-W", "1", "-w",
"-"], |
36 stdout=subprocess.PIPE, stderr=subprocess.PIPE) | 35 stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
37 mac = getmacaddress(nic) | 36 mac = getmacaddress(nic) |
38 | 37 |
39 total = {"rx": 0, "tx": 0} | 38 total = {"rx": 0, "tx": 0} |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 | 131 |
133 if total["rx"] >= crit or total["tx"] >= crit: | 132 if total["rx"] >= crit or total["tx"] >= crit: |
134 print "CRITICAL - " + output | 133 print "CRITICAL - " + output |
135 sys.exit(2) | 134 sys.exit(2) |
136 | 135 |
137 if total["rx"] >= warn or total["tx"] >= warn: | 136 if total["rx"] >= warn or total["tx"] >= warn: |
138 print "WARNING - " + output | 137 print "WARNING - " + output |
139 sys.exit(1) | 138 sys.exit(1) |
140 | 139 |
141 print "OK - " + output | 140 print "OK - " + output |
OLD | NEW |