-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathget_op_status.rb
executable file
·129 lines (104 loc) · 3.39 KB
/
get_op_status.rb
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/ruby
# kernel interface for network queries
# --------------------------
# returns interfaces name orderd by name
# --------------------------
def get_network_interfaces
# get non virtual interface name looking in /sys/class/net
cli_output = `ls -l /sys/class/net/ | tail -n +2 | grep '^l' | grep -v virtual | awk '{print $9}' | sort`
return cli_output.split("\n")
end
# --------------------------
# parse tc output and retrieve delay, jitter, loss and bandwidth parameters
# supports only tbf but enough for us
# --------------------------
# return a hash containing operationnal tc values
def get_itf_tc_data(itf)
# empty settings:
tc_data = { delay: 0, jitter: 0, loss: 0, bandwidth: "unlimited" }
# call tc show
tc_out = `/sbin/tc qdisc show dev #{itf}`
# and parse it
if (m = tc_out.match( /rate (\S+) / ) )
tc_data[:bandwidth] = m.captures[0]
end
if (m = tc_out.match( /delay (\d+).0ms(?: (\d+).0ms)?/) )
tc_data[:jitter] = m.captures[1].to_i * 2 if m.captures[1]
tc_data[:delay] = m.captures[0].to_i - tc_data[:jitter]/2
end
if (m = tc_out.match( /loss (\d+)%/ ) )
tc_data[:loss] = m.captures[0]
end
return tc_data
end
# ---------------------------
# return down/up status and #bytes
# ---------------------------
def get_itf_ip_data(itf)
ip_data = { status: '', inpkts: 0, outpkts: 0 }
# call ip link show
ip_out = `ip link show #{itf}`
if (m = ip_out.match( /state (UP|DOWN)/) )
ip_data[:status] = m.captures[0].downcase
end
# call cat /sys/class/net/<itf>/statistics
ip_data[:inpkts] = `cat /sys/class/net/#{itf}/statistics/rx_packets`
ip_data[:outpkts] = `cat /sys/class/net/#{itf}/statistics/tx_packets`
return ip_data
end
# ---------------------------
# return lldp neighbor
# ---------------------------
def get_itf_lldp_data(itf)
lldp_data = { neighbor: '' }
lldp_out = `/usr/sbin/lldpctl -f plain #{itf} | grep SysName | awk '{print $2}' | uniq`
lldp_data[:neighbor] = lldp_out
return lldp_data
end
# ---------------------------
# return bridge interface (if any)
# ---------------------------
def get_itf_bridge(itf)
bridge_data = { bridge: '' }
cli_out = `readlink /sys/class/net/#{itf}/brport/bridge | awk -F'/' '{print $NF}'`
bridge_data[:bridge] = cli_out
return bridge_data
end
# ---------------------------
# return interface alias (if any)
# ---------------------------
def get_itf_alias(itf)
alias_data = { alias: '' }
cli_out = `test -r /sys/class/net/#{itf}/ifalias && cat /sys/class/net/#{itf}/ifalias`
alias_data[:alias] = cli_out
return alias_data
end
# -------------
# -------------
# get operationnal op_data for all interfaces
# ---------------------------
def get_op_data nics
op_datas = {}
nics.each do |nic|
op_datas[nic] = get_itf_tc_data nic
op_datas[nic].merge!( get_itf_ip_data nic )
op_datas[nic].merge!( get_itf_lldp_data nic )
op_datas[nic].merge!( get_itf_bridge nic )
op_datas[nic].merge!( get_itf_alias nic )
end
return op_datas
end
# ---------------------------
# -- self tests
# ---------------------------
if __FILE__ == $0
NICs = get_network_interfaces
print NICs
op_datas = get_op_data( NICs )
op_datas.each do |nic, data|
puts nic
data.each do |key, val|
puts " " + key.to_s + ":" + val.to_s
end
end
end