snmppoller.py
import schedule, csv, time
from datetime import datetime
from puresnmp import get
def poll(host, com, ver, mib):
print(host, com, mib)
result = get(host, com, mib).decode()
print(result)
with open('results.csv', 'a',newline='') as results:
resultscsv = csv.writer(results)
resultscsv.writerow([datetime.now(), host, mib, result])
with open('inventory.csv') as inventory:
invcsv = csv.reader(inventory)
for row in invcsv:
host = row[0]
freq = int(row[1])
com = row[2]
ver = int(row[3])
for mib in row[4:]:
schedule.every(freq).seconds.do(poll, host, com, ver, mib)
while True:
schedule.run_pending()
time.sleep(1)
pollerapi.py
from flask import Flask, jsonify
import csv
app = Flask(__name__)
@app.route('/')
def index():
return "working webserver"
# 2020-05-05 21:09:13.663307,192.168.1.151,sysName.0,raspberrypi
@app.route('/api/host/<string:host>', methods=['GET'])
def get_host(host):
output = []
with open('results.csv') as results:
resultscsv = csv.reader(results)
for row in resultscsv:
if row[1] == host:
output.append({'hostname': row[1],
'timestamp': row[0],
'mib': row[2],
'output': row[3]},)
return jsonify(output)
app.run(debug=True)
inventory.csv
127.0.0.1,5,public,2,.1.3.6.1.2.1.1.1.0,.1.3.6.1.2.1.1.5.0
results.csv
2021-06-20 21:58:07.880644,127.0.0.1,.1.3.6.1.2.1.1.1.0,Hardware: Intel64 Family 6 Model 42 Stepping 7 AT/AT COMPATIBLE - Software: Windows Version 6.3 (Build 19042 Multiprocessor Free)
2021-06-20 21:58:07.881644,127.0.0.1,.1.3.6.1.2.1.1.5.0,x220win10
2021-06-20 21:58:41.049945,127.0.0.1,.1.3.6.1.2.1.1.1.0,Hardware: Intel64 Family 6 Model 42 Stepping 7 AT/AT COMPATIBLE - Software: Windows Version 6.3 (Build 19042 Multiprocessor Free)
2021-06-20 21:58:41.050944,127.0.0.1,.1.3.6.1.2.1.1.5.0,x220win10
2021-06-20 21:59:53.903975,127.0.0.1,.1.3.6.1.2.1.1.1.0,Hardware: Intel64 Family 6 Model 42 Stepping 7 AT/AT COMPATIBLE - Software: Windows Version 6.3 (Build 19042 Multiprocessor Free)
2021-06-20 21:59:53.904974,127.0.0.1,.1.3.6.1.2.1.1.5.0,x220win10
2021-06-20 22:04:34.037170,127.0.0.1,.1.3.6.1.2.1.1.1.0,Hardware: Intel64 Family 6 Model 42 Stepping 7 AT/AT COMPATIBLE - Software: Windows Version 6.3 (Build 19042 Multiprocessor Free)
2021-06-20 22:04:34.041174,127.0.0.1,.1.3.6.1.2.1.1.5.0,x220win10
2021-06-20 22:08:56.493618,127.0.0.1,.1.3.6.1.2.1.1.1.0,Hardware: Intel64 Family 6 Model 42 Stepping 7 AT/AT COMPATIBLE - Software: Windows Version 6.3 (Build 19042 Multiprocessor Free)
2021-06-20 22:08:56.493618,127.0.0.1,.1.3.6.1.2.1.1.5.0,x220win10
2021-06-20 22:09:01.571505,127.0.0.1,.1.3.6.1.2.1.1.1.0,Hardware: Intel64 Family 6 Model 42 Stepping 7 AT/AT COMPATIBLE - Software: Windows Version 6.3 (Build 19042 Multiprocessor Free)
2021-06-20 22:09:01.571505,127.0.0.1,.1.3.6.1.2.1.1.5.0,x220win10
2021-06-20 22:09:06.649390,127.0.0.1,.1.3.6.1.2.1.1.1.0,Hardware: Intel64 Family 6 Model 42 Stepping 7 AT/AT COMPATIBLE - Software: Windows Version 6.3 (Build 19042 Multiprocessor Free)
2021-06-20 22:09:06.649390,127.0.0.1,.1.3.6.1.2.1.1.5.0,x220win10
2021-06-20 22:09:11.709117,127.0.0.1,.1.3.6.1.2.1.1.1.0,Hardware: Intel64 Family 6 Model 42 Stepping 7 AT/AT COMPATIBLE - Software: Windows Version 6.3 (Build 19042 Multiprocessor Free)
2021-06-20 22:09:11.709117,127.0.0.1,.1.3.6.1.2.1.1.5.0,x220win10