-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics_get.py
executable file
·194 lines (154 loc) · 5.85 KB
/
metrics_get.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env python3
from requests import get
from requests.exceptions import RequestException
from contextlib import closing
from bs4 import BeautifulSoup
import os
import sys
def get_config():
# TODO: I'm not sure if this is a scope problem or something else,
# but if these vars aren't exported, then `soup = BeautifulSoup()`
# gets parsed before these if blocks Probably just move this into
# a configure package and have it read json or whatever
# the common python format is.
config = {}
if os.getenv('METRICS_USER'):
config['metrics_user'] = os.environ['METRICS_USER']
else:
log_error("Export the METRICS_USER environment variable.")
sys.exit(1)
if os.getenv('METRICS_PASSWORD'):
config['metrics_password'] = os.environ['METRICS_PASSWORD']
else:
log_error("Export the METRICS_PASSWORD environment variable.")
sys.exit(1)
if os.getenv('METRICS_URL'):
config['metrics_url'] = os.environ['METRICS_URL']
else:
log_error("Export the METRICS_URL environment variable.")
sys.exit(1)
return config
def simple_get(config):
try:
with closing(get(config['metrics_url'], stream=True, \
auth=(config['metrics_user'], config['metrics_password']))) as resp:
if is_good_response(resp):
return resp.content
else:
return None
except RequestException as e:
log_error("Error during requests to {0} : {1}"\
.format(config['metrics_url'],str(e)))
def is_good_response(resp):
content_type = resp.headers['Content-Type'].lower()
return (resp.status_code == 200
and content_type is not None
and content_type.find('html') > -1 )
# TODO: Does python have a logging library?
def log_error(e):
print(e)
def get_metrics(config):
metric_request = simple_get(config)
if not metric_request:
# TODO: Bubble up and error to the caller and handle it
log_error("Error collecting metrics")
sys.exit(1)
soup = BeautifulSoup(metric_request, 'html.parser')
metric_list = []
table = soup.find('table', attrs={\
'border': '1', 'cellpadding': '0', 'cellspacing':'0', 'width': '99%'})
trs = table.find_all('tr')
for tr in trs:
tds = tr.find_all('td')
tds = [ele.text.strip() for ele in tds]
metric_list.append([ele for ele in tds if tds])
return telegraf_format_metrics(metric_list)
#for s in d:
# print(s)
def telegraf_format_metrics(metric):
"""
WAN and the wireless both have full metrics but LAN is an aggregate
for rates each individual port has their own Up Time.
{ 'Collisions': '0',
'Port': 'WAN',
'Rx B/s': '19783',
'RxPkts': '29421179',
'Status': '1000M/Full',
'Tx B/s': '22886',
'TxPkts': '12114689',
'Up Time': '2 days 03:06:30'}
{ 'Collisions': '0',
'Port': 'LAN1',
'Rx B/s': '215',
'RxPkts': '223672691',
'Status': '100M/Full',
'Tx B/s': '329',
'TxPkts': '351957437',
'Up Time': '53 days 21:45:15'}
{'Port': 'LAN2', 'Status': '1000M/Full', 'TxPkts': '03:24:06'}
{'Port': 'LAN3', 'Status': '1000M/Full', 'TxPkts': '2 days 08:53:34'}
{'Port': 'LAN4', 'Status': '1000M/Full', 'TxPkts': '36 days 03:07:26'}
{ 'Collisions': '0',
'Port': '2.4G WLAN b/g/n',
'Rx B/s': '96',
'RxPkts': '47423414',
'Status': '600M',
'Tx B/s': '408',
'TxPkts': '88949072',
'Up Time': '90 days 14:01:10'}
{ 'Collisions': '0',
'Port': '5G WLAN a/n/ac',
'Rx B/s': '456',
'RxPkts': '334657697',
'Status': '1300M',
'Tx B/s': '324',
'TxPkts': '1313778633',
'Up Time': '90 days 14:01:10'}
line_metric_bundle = {
"port": lm['Port'],
}
"""
# {'Port': '5G WLAN a/n/ac', 'Status': '1300M', 'TxPkts': '1403465875', 'RxPkts': '360767335', 'Collisions': '0', 'Tx B/s': '329', 'Rx B/s': '124', 'Up Time': '100 days 11:46:40'}
# TODO: Throw away the Up Time metric for the LAN interfaces and munge
# them into one just keeping the metrics and convert the Up Time into hours
# using datetime.striptime(). The metrics-collector module should also
# handle the sending and calling of Telegraf so we can handle formating
# these into Telegraf.metric()
keys = metric.pop(0)
mv = []
for m in metric:
lm = dict(zip(keys, m))
# The HTML table displays uptime for the TxPkts if there's not an active
# link and there isn't an RkPkts or any other metric so we toss it.
if not 'RxPkts' in lm:
del lm
continue
status = parse_status_tag(lm['Status'])
line_metric_bundle = {
"port": parse_port(lm['Port']),
"tags": "status="+status,
"values": [
"tx_packets="+lm['TxPkts'],
"rx_packets="+lm['RxPkts'],
"collisions="+lm['Collisions'],
"tx_per_second="+lm['Tx B/s'],
"rx_per_second="+lm['Rx B/s']
]
}
for v in line_metric_bundle['values']:
lms = {
"measurement": "router_"+line_metric_bundle['port']+"_"+v.split('=')[0],
"tags": line_metric_bundle['tags'],
"value": v
}
mv.append(lms)
return mv
def parse_status_tag(status):
return status.lower().replace('/', '').replace('.', '')
def parse_per_second(per_second):
strip_space = per_second.lower().replace(' ', '_')
return strip_space.replace('b/s', 'bytes_sec')
def parse_port(port):
return parse_status_tag(port.lower().replace(' ', '_'))
def parse_measurement(port, suffix):
return port + suffix.split('=')[0]