-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat.py
47 lines (35 loc) · 1.09 KB
/
format.py
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
#!/usr/local/bin/python3
import csv
import socket
import sys
import re
def getHostName(ipAddress):
hostName = ipAddress
try:
hostName = socket.gethostbyaddr(ipAddress.strip())[0]
except socket.herror:
pass
return hostName
def prefixToMultiplier(prefix):
multiplier = {
'K': 1000,
'M': 1000000,
'G': 1000000000
}
return multiplier.get(prefix, 1)
def expandBitRate(bitRate):
groups = re.match(r"(\d+\.?\d*)(?:(K|M|G)?)", bitRate).groups()
multiplier = 1.0
if len(groups) > 1:
multiplier = prefixToMultiplier(groups[1])
value = float(groups[0])
return value * multiplier
host = socket.gethostname()
with sys.stdin as csvfile:
csvReader = csv.reader(csvfile)
for row in csvReader:
(senderIp, receiverIp, receiveRate, sendRate) = (row[0], row[1], expandBitRate(row[2]), expandBitRate(row[3]))
sender = getHostName(senderIp)
receiver = getHostName(receiverIp)
# print("%s -> %s %s %s" % (sender, receiver, sendRate, receiveRate))
print("nstat,hosts=" + host +",sender=" + sender + ",receiver=" + receiver + " sendRate=" + str(sendRate) + ",receiveRate=" + str(receiveRate))