From 4b0238c5150b0bd9155abfcb9ea601169f7af6c3 Mon Sep 17 00:00:00 2001 From: Johannes Findeisen Date: Mon, 10 Jun 2013 04:14:53 +0200 Subject: [PATCH] added some testing code to the snmpget builtin and renamed service to services/ --- lib/{service => services}/__init__.py | 0 lib/{service => services}/http.py | 0 lib/{service => services}/ping.py | 0 lib/{service => services}/service.py | 0 lib/{service => services}/shell.py | 0 lib/services/snmpget.py | 58 +++++++++++++++++++++++++ lib/{service => services}/ssh.py | 0 lib/{service => services}/tcpconnect.py | 0 8 files changed, 58 insertions(+) rename lib/{service => services}/__init__.py (100%) rename lib/{service => services}/http.py (100%) rename lib/{service => services}/ping.py (100%) rename lib/{service => services}/service.py (100%) rename lib/{service => services}/shell.py (100%) create mode 100644 lib/services/snmpget.py rename lib/{service => services}/ssh.py (100%) rename lib/{service => services}/tcpconnect.py (100%) diff --git a/lib/service/__init__.py b/lib/services/__init__.py similarity index 100% rename from lib/service/__init__.py rename to lib/services/__init__.py diff --git a/lib/service/http.py b/lib/services/http.py similarity index 100% rename from lib/service/http.py rename to lib/services/http.py diff --git a/lib/service/ping.py b/lib/services/ping.py similarity index 100% rename from lib/service/ping.py rename to lib/services/ping.py diff --git a/lib/service/service.py b/lib/services/service.py similarity index 100% rename from lib/service/service.py rename to lib/services/service.py diff --git a/lib/service/shell.py b/lib/services/shell.py similarity index 100% rename from lib/service/shell.py rename to lib/services/shell.py diff --git a/lib/services/snmpget.py b/lib/services/snmpget.py new file mode 100644 index 0000000..4eb974d --- /dev/null +++ b/lib/services/snmpget.py @@ -0,0 +1,58 @@ +""" +The snmpget service in pure Python. +""" + +# http://pysnmp.sourceforge.net/ + +""" +1. install net-snmp package on localhost +2. Use this /etc/snmp/snmpd.conf: + +com2sec local 127.0.0.1/32 linspector +com2sec local 192.168.2.0/24 linspector +# +group MyROGroup v1 local +group MyROGroup v2c local +group MyROGroup usm local +view all included .1 80 +access MyROGroup "" any noauth exact all none none +# +syslocation Sylt +syscontact Admin {Admin@example.com} +3. start the snmpd service +4. the following code should work +""" + +from pysnmp.entity.rfc3413.oneliner import cmdgen + +cmdGen = cmdgen.CommandGenerator() + +errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd( + cmdgen.CommunityData('linspector'), + cmdgen.UdpTransportTarget(('localhost', 161)), + + # Names variables: + #cmdgen.MibVariable('SNMPv2-MIB', 'sysName', 0) + + # OID's: + cmdgen.MibVariable('.1.3.6.1.4.1.2021.10.1.3.1') # load + #cmdgen.MibVariable('.1.3.6.1.4.1.2021.10.1.3.2') # load + #cmdgen.MibVariable('.1.3.6.1.4.1.2021.10.1.3.3') # load + #cmdgen.MibVariable('.1.3.6.1.2.1.1.3.0') # systems uptime + # more: + # http://www.debianadmin.com/linux-snmp-oids-for-cpumemory-and-disk-statistics.html + # http://www.mibdepot.com/index.shtml +) + +# Check for errors and print out results +if errorIndication: + print(errorIndication) +else: + if errorStatus: + print('%s at %s' % ( + errorStatus.prettyPrint(), + errorIndex and varBinds[int(errorIndex) - 1] or '?' + )) + else: + for name, val in varBinds: + print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) \ No newline at end of file diff --git a/lib/service/ssh.py b/lib/services/ssh.py similarity index 100% rename from lib/service/ssh.py rename to lib/services/ssh.py diff --git a/lib/service/tcpconnect.py b/lib/services/tcpconnect.py similarity index 100% rename from lib/service/tcpconnect.py rename to lib/services/tcpconnect.py