Python pysnmp轮询SNMP保存csv并通过flask提供json api

 snmppoller.py

import schedule, csv, time

from datetime import datetime
from pysnmp.hlapi import *


def poll(host, com, ver, mib):
    iterator = getCmd(SnmpEngine(),
                      CommunityData(com),
                      UdpTransportTarget((host, 161)),
                      ContextData(),
                      ObjectType(ObjectIdentity(mib)))

    errorIndication, errorStatus, errorIndex, varBinds = next(iterator)

    if errorIndication:  # SNMP engine errors
        print(errorIndication)
    else:
        if errorStatus:  # SNMP agent errors
            print('%s at %s' % (errorStatus.prettyPrint(), varBinds[int(errorIndex) - 1] if errorIndex else '?'))
        else:
            for varBind in varBinds:  # SNMP response contents
                print(varBind)
                oid, value = varBind
                # str(value)
                print(oid)
                print(value)
                result = value

    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

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值