OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 | 2 |
3 import os, re, subprocess, sys | 3 import os, re, subprocess, sys, socket, struct, fcntl |
| 4 |
| 5 INTERVAL = 5 |
4 | 6 |
5 def format_bandwidth(bits): | 7 def format_bandwidth(bits): |
6 if bits >= 1000000: | 8 if bits >= 1000000: |
7 return "%.2f Mbit/s" % (bits / 1000000) | 9 return "%.2f Mbit/s" % (bits / 1000000) |
8 elif bits >= 1000: | 10 elif bits >= 1000: |
9 return "%.2f kbit/s" % (bits / 1000) | 11 return "%.2f kbit/s" % (bits / 1000) |
10 else: | 12 else: |
11 return "%.2f bit/s" % bits | 13 return "%.2f bit/s" % bits |
12 | 14 |
| 15 def getmacaddress(): |
| 16 # We are calling SIOCGIFHWADDR (0x8927 according to man ioctl_list) here. See |
| 17 # man netdevice for the request structure: it has to start with 16 bytes |
| 18 # containing the interface name, the OS will write 8 bytes after that (2 bytes |
| 19 # family name and 6 bytes actual MAC address). |
| 20 s = socket.socket() |
| 21 return fcntl.ioctl(s.fileno(), 0x8927, struct.pack("24s", "eth0"))[18:24] |
| 22 |
| 23 def readtime(time): |
| 24 hour, minute, second = time.split(":") |
| 25 return int(hour) * 3600 + int(minute) * 60 + float(second) |
| 26 |
13 if __name__ == "__main__": | 27 if __name__ == "__main__": |
14 if len(sys.argv) != 3: | 28 if len(sys.argv) != 3: |
15 script_name = os.path.basename(sys.argv[0]) | 29 script_name = os.path.basename(sys.argv[0]) |
16 print "Usage: %s WARN CRIT" % script_name | 30 print "Usage: %s WARN CRIT" % script_name |
17 sys.exit(0) | 31 sys.exit(0) |
18 | 32 |
19 (warn, crit) = sys.argv[1:3] | 33 (warn, crit) = sys.argv[1:3] |
20 warn = int(sys.argv[1]) | 34 warn = int(sys.argv[1]) |
21 crit = int(sys.argv[2]) | 35 crit = int(sys.argv[2]) |
22 | 36 |
23 process_output = subprocess.check_output(["bwm-ng", "-I", "eth0", "-t", "5000"
, "-c", "1", "-o", "csv"]) | 37 process = subprocess.Popen(["tcpdump", "-q", "-n", "-s", "64", "-w", "-"], std
out=subprocess.PIPE, stderr=subprocess.PIPE) |
24 data = process_output.splitlines()[0].split(";") | 38 starttime = None |
25 tx = float(data[2]) * 8 | 39 mac = getmacaddress() |
26 rx = float(data[3]) * 8 | |
27 status = "rx %s tx %s" % (format_bandwidth(rx), format_bandwidth(tx)) | |
28 | 40 |
29 perfdata = "rx=%i;%i;%i tx=%i;%i;%i" % (rx, warn, crit, tx, warn, crit) | 41 total = {"rx": 0, "tx": 0} |
| 42 http = {"rx": 0, "tx": 0} |
| 43 https = {"rx": 0, "tx": 0} |
| 44 ssh = {"rx": 0, "tx": 0} |
| 45 dns = {"rx": 0, "tx": 0} |
| 46 other = {"rx": 0, "tx": 0} |
| 47 other_detailed = {} |
30 | 48 |
31 output = "%s|%s" % (status, perfdata) | 49 # See http://wiki.wireshark.org/Development/LibpcapFileFormat for libpcap form
at description |
| 50 magic_number, _, _, _, _, _, _ = struct.unpack("IHHiIII", process.stdout.read(
24)) |
| 51 if magic_number != 0xa1b2c3d4: |
| 52 raise Exception("Unexpected format") |
| 53 while True: |
| 54 sec, usec, incl_len, orig_len = struct.unpack("IIII", process.stdout.read(16
)) |
32 | 55 |
33 if rx >= crit or tx >= crit: | 56 # Convert bytes to bits and normalize to seconds |
| 57 length = float(orig_len * 8) / INTERVAL |
| 58 |
| 59 time = sec + float(usec) / 1000000 |
| 60 if starttime == None: |
| 61 starttime = time |
| 62 if time - starttime > INTERVAL: |
| 63 break |
| 64 |
| 65 def add_other(description): |
| 66 other[direction] += length |
| 67 other_detailed[description] = other_detailed.get(description, 0) + length |
| 68 |
| 69 payload = process.stdout.read(incl_len) |
| 70 |
| 71 # Unpack Ethernet frame, http://en.wikipedia.org/wiki/Ethernet_frame#Structu
re |
| 72 destination, source, protocol = struct.unpack("!6s6sH", payload[:14]) |
| 73 payload = payload[14:] |
| 74 direction = "rx" if destination == mac else "tx" |
| 75 total[direction] += length |
| 76 |
| 77 # Check Level 3 protocol |
| 78 if protocol == 0x0800: # IPv4, http://en.wikipedia.org/wiki/Internet_Pro
tocol_version_4#Header |
| 79 ihl = ord(payload[0]) & 0xF |
| 80 protocol = ord(payload[9]) |
| 81 payload = payload[ihl * 4:] |
| 82 elif protocol == 0x86DD: # IPv6, http://en.wikipedia.org/wiki/IPv6_packet#
Fixed_header |
| 83 protocol = ord(payload[6]) |
| 84 payload = payload[40:] |
| 85 else: |
| 86 add_other("L3 0x%04X" % protocol) |
| 87 continue |
| 88 |
| 89 # Check Level 4 protocol |
| 90 if protocol in (0x06, 0x11): # TCP, UDP |
| 91 # The lower port number should be the real port, the other one will be |
| 92 # the ephemeral port. |
| 93 source_port, destination_port = struct.unpack('!HH', payload[:4]) |
| 94 protocol = "TCP" if protocol == 0x06 else "UDP" |
| 95 port = min(source_port, destination_port) |
| 96 else: |
| 97 add_other("L4 0x%02X" % protocol) |
| 98 continue |
| 99 |
| 100 if protocol == "TCP" and port == 80: |
| 101 http[direction] += length |
| 102 elif protocol == "TCP" and port == 443: |
| 103 https[direction] += length |
| 104 elif protocol == "TCP" and port == 22: |
| 105 ssh[direction] += length |
| 106 elif port == 53: |
| 107 dns[direction] += length |
| 108 else: |
| 109 add_other("Port %i" % port) |
| 110 continue |
| 111 |
| 112 status = [] |
| 113 perfdata = [] |
| 114 def add_status(id, values): |
| 115 rx = values["rx"] |
| 116 tx = values["tx"] |
| 117 status.append("%srx %s %stx %s" % (id, format_bandwidth(rx), id, format_band
width(tx))) |
| 118 if id == "": |
| 119 perfdata.append("rx=%i;%i;%i tx=%i;%i;%i" % (rx, warn, crit, tx, warn, cri
t)) |
| 120 else: |
| 121 perfdata.append("%srx=%i %stx=%i" % (id, rx, id, tx)) |
| 122 |
| 123 add_status("", total) |
| 124 add_status("http_", http) |
| 125 add_status("https_", https) |
| 126 add_status("ssh_", ssh) |
| 127 add_status("dns_", dns) |
| 128 add_status("other_", other) |
| 129 for key in sorted(other_detailed.iterkeys(), key=lambda k: other_detailed[k],
reverse=True): |
| 130 status.append("%s %s" % (key, format_bandwidth(float(other_detailed[key]) /
INTERVAL))) |
| 131 |
| 132 output = "%s|%s" % (", ".join(status), " ".join(perfdata)) |
| 133 |
| 134 if total["rx"] >= crit or total["tx"] >= crit: |
34 print "CRITICAL - " + output | 135 print "CRITICAL - " + output |
35 sys.exit(2) | 136 sys.exit(2) |
36 | 137 |
37 if rx >= warn or tx >= warn: | 138 if total["rx"] >= warn or total["tx"] >= warn: |
38 print "WARNING - " + output | 139 print "WARNING - " + output |
39 sys.exit(1) | 140 sys.exit(1) |
40 | 141 |
41 print "OK - " + output | 142 print "OK - " + output |
OLD | NEW |