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(): | 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", "eth0"))[18:2
4] | 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) != 3: | 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" % 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] | 29 (warn, crit) = sys.argv[1:3] |
30 warn = int(sys.argv[1]) | 30 warn = int(sys.argv[1]) |
31 crit = int(sys.argv[2]) | 31 crit = int(sys.argv[2]) |
| 32 nic = str(sys.argv[3]) |
32 | 33 |
33 process = subprocess.Popen( | 34 process = subprocess.Popen( |
34 ["sudo", "tcpdump", "-q", "-s", "64", "-G", str(INTERVAL), "-W", "1", "-w",
"-"], | 35 ["sudo", "tcpdump", "-q", "-s", "64", "-G", str(INTERVAL), "-W", "1", "-w",
"-"], |
35 stdout=subprocess.PIPE, stderr=subprocess.PIPE) | 36 stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
36 mac = getmacaddress() | 37 mac = getmacaddress() |
37 | 38 |
38 total = {"rx": 0, "tx": 0} | 39 total = {"rx": 0, "tx": 0} |
39 http = {"rx": 0, "tx": 0} | 40 http = {"rx": 0, "tx": 0} |
40 https = {"rx": 0, "tx": 0} | 41 https = {"rx": 0, "tx": 0} |
41 ssh = {"rx": 0, "tx": 0} | 42 ssh = {"rx": 0, "tx": 0} |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 | 132 |
132 if total["rx"] >= crit or total["tx"] >= crit: | 133 if total["rx"] >= crit or total["tx"] >= crit: |
133 print "CRITICAL - " + output | 134 print "CRITICAL - " + output |
134 sys.exit(2) | 135 sys.exit(2) |
135 | 136 |
136 if total["rx"] >= warn or total["tx"] >= warn: | 137 if total["rx"] >= warn or total["tx"] >= warn: |
137 print "WARNING - " + output | 138 print "WARNING - " + output |
138 sys.exit(1) | 139 sys.exit(1) |
139 | 140 |
140 print "OK - " + output | 141 print "OK - " + output |
OLD | NEW |