-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheck_interface_bytes
More file actions
executable file
·76 lines (58 loc) · 2.34 KB
/
check_interface_bytes
File metadata and controls
executable file
·76 lines (58 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/python
#
# The following script checks network traffic through an interface
#
from __future__ import division
import argparse
import sys
import subprocess
def get_bytes(byte, interface):
command = "cat /sys/class/net/%s/statistics/%s" % (interface, byte)
res_bytes = int(run_command(command))
return res_bytes
def run_command(command):
r = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
output, error = r.communicate()
if output:
return output
print error
sys.exit(4)
def result(p):
if int(p['gbps']) > 0:
return "%.2f Gbps" % p['gbps']
elif int(p['mbps']) > 0:
return "%.2f Mbps" % p['mbps']
elif int(p['kbps']) > 0:
return "%.2f Kbps" % p['kbps']
else:
return "%s bps" % p['bps']
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-c", required=True, type=float, help="Critical value")
parser.add_argument("-w", required=True, type=float, help="Warning value")
parser.add_argument("-i", required=True, help="Interface Name")
args = parser.parse_args()
critical_limit, warning_limit, interface = args.c, args.w, args.i
interval = 1
tx, rx = {}, {}
rx['bits1'] = get_bytes('rx_bytes', interface)*8
tx['bits1'] = get_bytes('tx_bytes', interface)*8
sleep_command = "sleep %s" % interval
subprocess.check_output(sleep_command, shell=True)
rx['bits2'] = get_bytes('rx_bytes', interface)*8
tx['bits2'] = get_bytes('tx_bytes', interface)*8
tx['bps'] = tx.get('bits2') - tx.get('bits1')
rx['bps'] = rx.get('bits2') - rx.get('bits1')
tx['kbps'], rx['kbps'] = tx['bps']/10**3, rx['bps']/10**3
tx['mbps'], rx['mbps'] = tx['bps']/10**6, rx['bps']/10**6
tx['gbps'], rx['gbps'] = tx['bps']/10**9, rx['bps']/10**9
output = "TX- %s, RX- %s\n" % (result(tx), result(rx))
output += "Crit- %sGbps, Warn- %sGbps" % (args.c, args.w)
perfdata = "tx_speed=%s;;;; rx_speed=%s;;;; tx_bytes=%s;;;; rx_bytes=%s;;;;" \
% (tx['bps'], rx['bps'], tx['bits2'], rx['bits2'])
print output + " | " + perfdata
if tx['bps'] > critical_limit*(10**9) or rx['bps'] > critical_limit*(10**9):
sys.exit(2)
elif tx['bps'] > warning_limit*(10**9) or rx['bps'] > warning_limit*(10**9):
sys.exit(1)