Skip to content

Commit ca9b95a

Browse files
author
Anze
committed
Send results to Grafolean
1 parent 6a20a70 commit ca9b95a

File tree

2 files changed

+32
-61
lines changed

2 files changed

+32
-61
lines changed

snmpcollector.py

Lines changed: 24 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import json
55
from pytz import utc
66
from colors import color
7+
import requests
78

89
from easysnmp import Session
910
from mathjspy import MathJS
@@ -52,8 +53,8 @@ def _apply_expression_to_results(snmp_results, methods, expression, output_path)
5253
mjs.set('${}'.format(i + 1), float(v.value))
5354
value = mjs.eval(expression)
5455
result.append({
55-
'path': f'{output_path}.{oid_index}',
56-
'value': value,
56+
'p': f'{output_path}.{oid_index}',
57+
'v': value,
5758
})
5859
except NoValueForOid:
5960
log.warn(f'Missing value for oid index: {oid_index}')
@@ -65,10 +66,20 @@ def _apply_expression_to_results(snmp_results, methods, expression, output_path)
6566
mjs.set('${}'.format(i + 1), float(r.value))
6667
value = mjs.eval(expression)
6768
return [
68-
{'path': output_path, 'value': value},
69+
{'p': output_path, 'v': value},
6970
]
7071

7172

73+
def send_results_to_grafolean(backend_url, bot_token, account_id, values):
74+
url = '{}/accounts/{}/values/?b={}'.format(backend_url, account_id, bot_token)
75+
76+
log.info("Sending results to Grafolean")
77+
try:
78+
r = requests.post(url, json=values)
79+
r.raise_for_status()
80+
log.info("Results sent.")
81+
except:
82+
log.exception("Error sending data to Grafolean")
7283

7384

7485
class SNMPCollector(Collector):
@@ -118,6 +129,11 @@ def do_snmp(*args, **job_info):
118129
"account_id": 1
119130
}
120131
"""
132+
log.info("Running job for account [{account_id}], IP [{ipv4}]".format(
133+
account_id=job_info["account_id"],
134+
ipv4=job_info["details"]["ipv4"],
135+
))
136+
121137
# filter out only those sensors that are supposed to run at this interval:
122138
affecting_intervals, = args
123139

@@ -146,7 +162,6 @@ def do_snmp(*args, **job_info):
146162

147163
session = Session(**session_kwargs)
148164

149-
150165
activated_sensors = [s for s in job_info["sensors"] if s["interval"] in affecting_intervals]
151166
for sensor in activated_sensors:
152167
results = []
@@ -166,57 +181,13 @@ def do_snmp(*args, **job_info):
166181
oids_results = list(zip(oids, methods, results))
167182
log.info("Results: {}".format(oids_results))
168183

169-
expression = sensor["sensor_details"]["expression"]
170-
output_path = sensor["sensor_details"]["output_path"]
171-
# values = _apply_expression_to_results(results, walk_indexes, methods, oids, expression, output_path)
172184
# We have SNMP results and expression - let's calculate value(s). The trick here is that
173185
# if some of the data is fetched via SNMP WALK, we will have many results; if only SNMP
174-
# GET was used, we get only one.
175-
values = []
176-
if 'walk' in methods:
177-
for oid_index in walk_indexes:
178-
pass
179-
180-
181-
182-
# SNMPCollector.send_results_to_grafolean(
183-
# job_info["base_url"],
184-
# job_info["bot_token"],
185-
# job_info["account_id"],
186-
# job_info["entity_id"],
187-
# oids_results,
188-
# sensor["sensor_details"]["expression"],
189-
# sensor["sensor_details"]["output_path"],
190-
# )
191-
192-
193-
# log.info("Running job for account [{account_id}], IP [{ipv4}], nsensors: {n_sensors}, oids: {oids}".format(
194-
# account_id=job_info["account_id"],
195-
# ipv4=job_info["details"]["ipv4"],
196-
# n_sensors=len(sensors),
197-
# oids=["SNMP{} {}".format(o["fetch_method"].upper(), o["oid"]) for o in oids],
198-
# ))
199-
200-
# @staticmethod
201-
# def send_results_to_grafolean(base_url, bot_token, account_id, entity_id, results, expression, output_path):
202-
# url = '{}/api/accounts/{}/values/?b={}'.format(base_url, account_id, bot_token)
203-
# values = []
204-
# for ip in results:
205-
# for ping_index, ping_time in enumerate(results[ip]):
206-
# values.append({
207-
# 'p': 'ping.{}.{}.success'.format(ip.replace('.', '_'), ping_index),
208-
# 'v': 0 if ping_time is None else 1,
209-
# })
210-
# if ping_time is not None:
211-
# values.append({
212-
# 'p': 'ping.{}.{}.rtt'.format(ip.replace('.', '_'), ping_index),
213-
# 'v': ping_time,
214-
# })
215-
# print("Sending results to Grafolean")
216-
# r = requests.post(url, json=values)
217-
# print(r.text)
218-
# r.raise_for_status()
219-
186+
# GET was used, we get one.
187+
expression = sensor["sensor_details"]["expression"]
188+
output_path = sensor["sensor_details"]["output_path"]
189+
values = _apply_expression_to_results(results, methods, expression, f'snmp.{output_path}')
190+
send_results_to_grafolean(job_info['backend_url'], job_info['bot_token'], job_info['account_id'], values)
220191

221192

222193
def jobs(self):

test_snmpcollector.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def test_snmpget():
1111
expression = '$1'
1212
output_path = 'snmp.test123.asdf'
1313
expected_result = [
14-
{ 'path': 'snmp.test123.asdf', 'value': 68000.0 },
14+
{ 'p': 'snmp.test123.asdf', 'v': 68000.0 },
1515
]
1616
assert _apply_expression_to_results(results, methods, expression, output_path) == expected_result
1717

@@ -24,7 +24,7 @@ def test_snmpget_add():
2424
expression = '$1 + $2'
2525
output_path = 'snmp.test123.asdf'
2626
expected_result = [
27-
{ 'path': 'snmp.test123.asdf', 'value': 68200.0 },
27+
{ 'p': 'snmp.test123.asdf', 'v': 68200.0 },
2828
]
2929
assert _apply_expression_to_results(results, methods, expression, output_path) == expected_result
3030

@@ -40,9 +40,9 @@ def test_snmpwalk():
4040
expression = '$1'
4141
output_path = 'snmp.test123.asdf'
4242
expected_result = [
43-
{ 'path': 'snmp.test123.asdf.1', 'value': 60000.0 },
44-
{ 'path': 'snmp.test123.asdf.2', 'value': 61000.0 },
45-
{ 'path': 'snmp.test123.asdf.3', 'value': 62000.0 },
43+
{ 'p': 'snmp.test123.asdf.1', 'v': 60000.0 },
44+
{ 'p': 'snmp.test123.asdf.2', 'v': 61000.0 },
45+
{ 'p': 'snmp.test123.asdf.3', 'v': 62000.0 },
4646
]
4747
assert _apply_expression_to_results(results, methods, expression, output_path) == expected_result
4848

@@ -59,8 +59,8 @@ def test_expression_add():
5959
expression = '$1 + $2'
6060
output_path = 'snmp.test123.asdf'
6161
expected_result = [
62-
{ 'path': 'snmp.test123.asdf.1', 'value': 60500.0 },
63-
{ 'path': 'snmp.test123.asdf.2', 'value': 61500.0 },
64-
{ 'path': 'snmp.test123.asdf.3', 'value': 62500.0 },
62+
{ 'p': 'snmp.test123.asdf.1', 'v': 60500.0 },
63+
{ 'p': 'snmp.test123.asdf.2', 'v': 61500.0 },
64+
{ 'p': 'snmp.test123.asdf.3', 'v': 62500.0 },
6565
]
6666
assert _apply_expression_to_results(results, methods, expression, output_path) == expected_result

0 commit comments

Comments
 (0)