Repository navigation
Expand file tree
/
Copy pathnetworkStats.py
More file actions
163 lines (128 loc) · 5 KB
/
Copy pathnetworkStats.py
File metadata and controls
163 lines (128 loc) · 5 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import Library
import socket
import re
import time
import math
remoteServer = "www.google.com"
if Library.isConnected(remoteServer):
network = {"networkState" : "Connected"}
else:
network = {"networkState" : "Disconnected"}
availableInterfaces,_,_,_ = Library.commandExecute("ip -o addr | awk '{split($4, a, \"/\"); print $2\" : \"a[1]}'")
availableInterfaces = availableInterfaces.splitlines()
interfaceList = []
foundInterface = []
for index in availableInterfaces:
interfaceName = index.split(":")[0].strip()
if interfaceName in foundInterface:
location = foundInterface.index(interfaceName)
interfaceList[location].update({"IPv6" : ":".join(index.split(":")[1:])})
pass
else:
foundInterface.append(interfaceName)
interfaceList.append({"interfaceName" : interfaceName , "IPv4" : index.split(":")[1].strip()})
interfaceInfoTable,_,_,_ = Library.commandExecute("netstat -i")
interfaceInfoTable = interfaceInfoTable.splitlines()
for interfaceData in interfaceList:
interfaceName = interfaceData["interfaceName"]
for interfaceTable in interfaceInfoTable:
interfaceTable = re.sub(r'\s+',' ',interfaceTable)
if interfaceName in interfaceTable:
interfaceTable = interfaceTable.split(" ")
MTU = interfaceTable[1]
RXOK = interfaceTable[2]
RXERR = interfaceTable[3]
RXDRP = interfaceTable[4]
RXOVR = interfaceTable[5]
TXOK = interfaceTable[6]
TXERR = interfaceTable[7]
TXDRP = interfaceTable[8]
TXOVR = interfaceTable[9]
trafficInfo = {
"RXOK" : RXOK,
"RXERR" :RXERR ,
"RXDRP" :RXDRP ,
"RXOVR" :RXOVR ,
"TXOK" :TXOK ,
"TXERR" : TXERR ,
"TXDRP" : TXDRP,
"TXOVR" : TXOVR,
"MTU" : MTU,
}
interfaceData.update({"trafficInfo" : trafficInfo})
break
else:
pass
INTERVAL = 1 # 1 second
AVG_LOW_PASS = 0.2 # Simple Complemetary Filter
ifaces = {}
idata = Library.GetNetworkInterfaces()
for eth in idata:
ifaces[eth["interface"]] = {
"rxrate" : 0,
"txrate" : 0,
"avgrx" : 0,
"avgtx" : 0,
"toptx" : 0,
"toprx" : 0,
"sendbytes" : eth["tx"]["bytes"],
"recvbytes" : eth["rx"]["bytes"]
}
set = 0
while set<3:
idata = Library.GetNetworkInterfaces()
for eth in idata:
# Calculate the Rate
ifaces[eth["interface"]]["rxrate"] = (eth["rx"]["bytes"] - ifaces[eth["interface"]]["recvbytes"]) / INTERVAL
ifaces[eth["interface"]]["txrate"] = (eth["tx"]["bytes"] - ifaces[eth["interface"]]["sendbytes"]) / INTERVAL
# Set the rx/tx bytes
ifaces[eth["interface"]]["recvbytes"] = eth["rx"]["bytes"]
ifaces[eth["interface"]]["sendbytes"] = eth["tx"]["bytes"]
# Calculate the Average Rate
ifaces[eth["interface"]]["avgrx"] = int(ifaces[eth["interface"]]["rxrate"] * AVG_LOW_PASS + ifaces[eth["interface"]]["avgrx"] * (1.0-AVG_LOW_PASS))
ifaces[eth["interface"]]["avgtx"] = int(ifaces[eth["interface"]]["txrate"] * AVG_LOW_PASS + ifaces[eth["interface"]]["avgtx"] * (1.0-AVG_LOW_PASS))
# Set the Max Rates
ifaces[eth["interface"]]["toprx"] = ifaces[eth["interface"]]["rxrate"] if ifaces[eth["interface"]]["rxrate"] > ifaces[eth["interface"]]["toprx"] else ifaces[eth["interface"]]["toprx"]
ifaces[eth["interface"]]["toptx"] = ifaces[eth["interface"]]["txrate"] if ifaces[eth["interface"]]["txrate"] > ifaces[eth["interface"]]["toptx"] else ifaces[eth["interface"]]["toptx"]
time.sleep(INTERVAL)
final = ifaces
set = set + 1
for interfaceData in interfaceList:
interfaceName = interfaceData["interfaceName"]
interfaceData.update({"bandwidthInfo" : final[interfaceName]})
listeningConnectionsList = []
listeningConnections,_,_,_ = Library.commandExecute("netstat -tulpn | sed -n 2,1000p | grep LISTEN")
listeningConnections = listeningConnections.splitlines()
for connections in listeningConnections:
connections = re.sub(r'\s+',' ',connections)
connection = connections.split(" ")
connectionData = {
"protocol" : connection[0],
"localAddress" : connection[3],
"foreignAddress" : connection[4],
"processID" : connection[6].split("/")[0],
"service" : connection[6].split("/")[1]
}
listeningConnectionsList.append(connectionData)
gatewayInformation,_,_,_ = Library.commandExecute("netstat -rn | grep 0.0.0.0 | awk {'print $2'}")
gatewayInformation = gatewayInformation.split("\n")
for index in gatewayInformation:
if index != "0.0.0.0":
gateway = index
break
pingIPs = [ gateway,"127.0.0.1"]
pingNames = ["gateway","loopback"]
pingData = []
counter = 0
for ips in pingIPs:
pingStats,_,_,_ = Library.commandExecute("ping " + ips + " -c 1 | grep rtt")
pingTime = pingStats.split("/")[5]
pingData.append({pingNames[counter] : float(pingTime)})
counter = counter + 1
networkStats = {
"connectionState" : network,
"pingTimings" : pingData,
"servicesStats" : listeningConnectionsList,
"interfaceData" : interfaceList,
}
print networkStats