-
Notifications
You must be signed in to change notification settings - Fork 1
/
isp_network_sum.py
executable file
·154 lines (133 loc) · 4.05 KB
/
isp_network_sum.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
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
#!/usr/bin/env python
#!-*- coding:utf8 -*-
import requests
import time
import json
import sys
import os.path
import yaml
import arrow
import itertools
counters = [
"net.if.out.bits/iface=eth_all",
"net.if.in.bits/iface=eth_all"
]
endpoints = [
"tvn-nm-043-254-169-220",
"tvn-nm-043-254-169-221",
"tvn-nm-043-254-169-222",
"tvn-nm-043-254-169-223",
"tvn-nm-043-254-169-224",
"tvn-nm-043-254-169-225",
"tvn-nm-043-254-169-226",
"tvn-nm-043-254-169-227",
"tvn-nm-043-254-169-228"
]
URL = {
"login": "http://owl.fastweb.com.cn/api/v1/auth/login",
"hostgroup": 'http://owl.fastweb.com.cn/api/v1/hostgroup/hosts',
"aggregate": 'http://query.owl.fastweb.com.cn/graph/history',
}
def login(name, password):
url = URL["login"]
payload = {
'name': name,
'password': password
}
r = requests.post(url, payload)
out = json.loads(r.text)
return out["data"]["sig"]
def hostgroup2hostnames(user, sig, hostgroup):
url = URL["hostgroup"]
payload = {
'cName': user,
'cSig': sig,
'hostgroups': hostgroup
}
r = requests.post(url, payload)
out = json.loads(r.text)
res = []
for i in out["data"]["hosts"]:
res.append(i["hostname"])
return res
def aggregate(user, sig, startTs, endTs, endpoints):
url = URL["aggregate"]
endpointCounters = [{"endpoint": x, "counter": y}
for x in endpoints for y in counters]
payload = {
"start": startTs,
"end": endTs,
"cf": "AVERAGE",
"endpoint_counters": endpointCounters
}
r = requests.post(url, data=json.dumps(payload))
out = json.loads(r.text)
return out
def formatting(raw, endpoints, startTs, endTs):
product = [(ec["endpoint"], ec["counter"], point["timestamp"],
point["value"]) for ec in raw for point in ec["Values"]]
result = []
for c in counters:
data = filter(lambda x: x[1] == c, product) # select counter
data = filter(lambda x: x[3] is not None, data) # remove None
# use timestamp as groupby argument
data = sorted(data, key=lambda x: x[2])
# list(grp) -> [ ... (u'tvn-nm-043-254-169-228', u'net.if.out.bits/iface=eth_all', 1500501660, 8965.6)]
# key -> 1500501660
peaks = []
c_result = []
for key, grp in itertools.groupby(data, key=lambda x: x[2]):
v_data = map(lambda x: x[3], grp)
peak = sum(v_data)
peaks.append(peak)
r = {
"timestamp": key,
"sum": peak
}
c_result.append(r)
c_r = {
"counter": c,
"values": c_result,
"max_value": max(peaks)
}
result.append(c_r)
final = {
"timespan": (startTs, endTs),
"result": result
}
return json.dumps(final)
def readConf():
if os.path.isfile("secret.yaml"):
pass
else:
return {}
with open("secret.yaml", 'r') as stream:
try:
return yaml.load(stream)
except yaml.YAMLError as exc:
print(exc)
if __name__ == "__main__":
if len(sys.argv) != 3:
print "usage: ./isp_network_sum.py [time Start in ISO8601] [time End in ISO8601]"
print "example: ./isp_network_sum.py 2017-02-28T06:01:00+0800 2017-03-28T06:01:00+0800"
sys.exit(1)
# ts = int(time.time()) # input
# platform = "c06.i06" # input
sTime = arrow.get(sys.argv[1])
eTime = arrow.get(sys.argv[2])
startTs = sTime.timestamp
endTs = eTime.timestamp
user = "" # data
password = "" # data
conf = readConf()
if len(conf) == 2:
user = conf["user"]
password = conf["pass"]
sig = login(user, password)
# endpoints use the global
# endpoints = hostgroup2hostnames(user, sig, platform)
if len(endpoints) == 0:
print json.dumps(False)
sys.exit(0)
raw = aggregate(user, sig, startTs, endTs, endpoints)
print formatting(raw, endpoints, sys.argv[1], sys.argv[2])