From b2a68e03c1c62b94e59e89796cd51601e198f850 Mon Sep 17 00:00:00 2001 From: Johannes Findeisen Date: Mon, 10 Jun 2013 05:01:40 +0200 Subject: [PATCH] converted snmpget service to generic linspector service; raising exceptions from all services now when needed args are missing. --- lib/services/http.py | 1 + lib/services/shell.py | 1 + lib/services/snmpget.py | 86 +++++++++++++++++--------------------- lib/services/ssh.py | 1 + lib/services/tcpconnect.py | 1 + 5 files changed, 42 insertions(+), 48 deletions(-) diff --git a/lib/services/http.py b/lib/services/http.py index f967ad4..706d419 100644 --- a/lib/services/http.py +++ b/lib/services/http.py @@ -20,6 +20,7 @@ class HttpService(Service): self.string = kwargs["string"] else: log.w("There is no string set to match") + raise if "method" in kwargs: self.method = kwargs["method"] diff --git a/lib/services/shell.py b/lib/services/shell.py index 097ac35..7b5c78c 100644 --- a/lib/services/shell.py +++ b/lib/services/shell.py @@ -12,6 +12,7 @@ class ShellService(Service): self.command = kwargs["command"] else: log.w("There is no command") + raise def execute(self): self.command.call() \ No newline at end of file diff --git a/lib/services/snmpget.py b/lib/services/snmpget.py index 4eb974d..68652e6 100644 --- a/lib/services/snmpget.py +++ b/lib/services/snmpget.py @@ -2,57 +2,47 @@ 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 +from service import Service -cmdGen = cmdgen.CommandGenerator() -errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd( - cmdgen.CommunityData('linspector'), - cmdgen.UdpTransportTarget(('localhost', 161)), +class SnmpgetService(Service): + def __init__(self, **kwargs): - # Names variables: - #cmdgen.MibVariable('SNMPv2-MIB', 'sysName', 0) + if "community" in kwargs: + self.community = kwargs["community"] + else: + #log.w("There is no community") + raise - # 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 -) + if "oid" in kwargs: + self.oid = kwargs["oid"] + else: + #log.w("There is no oid") + raise -# 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 + if "port" in kwargs: + self.port = kwargs["port"] + else: + self.port = "161" + + def execute(self): + cmdGen = cmdgen.CommandGenerator() + + errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd( + cmdgen.CommunityData(self.community), + cmdgen.UdpTransportTarget((self.host, self.port)), + cmdgen.MibVariable('.1.3.6.1.4.1.2021.10.1.3.1') # linux system load + ) + + 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/services/ssh.py b/lib/services/ssh.py index 7b94c02..3131571 100644 --- a/lib/services/ssh.py +++ b/lib/services/ssh.py @@ -17,6 +17,7 @@ class SshService(Service): self.command = kwargs["command"] else: log.w("There is no command") + raise def execute(self): path = os.path.join(os.environ['HOME'], '.ssh', 'id_rsa') diff --git a/lib/services/tcpconnect.py b/lib/services/tcpconnect.py index 2306b4b..b81bc7e 100644 --- a/lib/services/tcpconnect.py +++ b/lib/services/tcpconnect.py @@ -17,6 +17,7 @@ class TcpconnectService(Service): self.port = kwargs["port"] else: log.w("There is no port set") + raise def execute(self, log): try: