トップ 差分 一覧 ソース 検索 ヘルプ RSS ログイン

PRG-py_snmp

 PY SNMP

install

pkg install tcl86 tk86 p5-Tcl p5-Tk p5-Term-ReadKey
pip3 install pysnmp pysnmp-mibs pysmi

check snmp_host

snmpwalk -v 2c -c public snmp_hosts_name_ip hrStorage

Sample

#
# -*- encoding: utf-8 -*-
import sys
from pysnmp import asn1, v1, v2c
from pysnmp import role

def snmp_set(ip, community, oid_values, port=161, retries=5, timeout=1, version='1'):
   """
   usage: snmp_set('192.168.10.1', 'comunity', [('1.3.6.1.2.1.43.10.2.1.4.1.1', 'i', 1),])
   """
   client = role.manager((ip, port))
   client.timeout = timeout
   client.retries = retries
   varargs = []
   for oid, type, value in oid_values:
       if type == 'i':
           varargs.append((oid, 'INTEGER', int(value)))
       elif type == 'u':
           varargs.append((oid, 'UNSIGNED32', int(value)))
       elif type == 't':
           varargs.append((oid, 'TIMETICKS', int(value)))
       elif type == 'a':
           varargs.append((oid, 'IPADDRESS', value))
       elif type == 'o':
           varargs.append((oid, 'OBJECTID', value))
       elif type == 's':
           varargs.append((oid, 'OCTETSTRING', value))
       elif type == 'U':
           varargs.append((oid, 'COUNTER64', long(value)))
       else:
           print 'Unknown value type \'%s\'' % (type)
           sys.exit(-1)

   try:
       req = eval('v' + version).SETREQUEST()
       rsp = eval('v' + version).GETRESPONSE()
   except (NameError, AttributeError):
       print 'Unsupported SNMP protocol version: %s\n%s' % (version, usage)
       sys.exit(-1)

   encoded_oids = []
   encoded_vals = []
   for (oid, type, val) in varargs:
       encoded_oids.append(asn1.OBJECTID().encode(oid))
       encoded_vals.append(eval('asn1.'+type+'()').encode(val))

   return client.send_and_receive(req.encode(community=community,  encoded_oids=encoded_oids, encoded_vals=encoded_vals))

disk size

https://qiita.com/mugi-tea/items/ed0a2500326533af73e5

SNMP

https://engineeringnote.hateblo.jp/entry/snmp/python/pysnmp

https://engineeringnote.hateblo.jp/entry/snmp/python/pysnmp

SNMP で応用

https://heartbeats.jp/hbblog/2013/02/predict-rrdtool.html