44import json
55from pytz import utc
66from colors import color
7+ import requests
78
89from easysnmp import Session
910from 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
7485class 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 ):
0 commit comments