converted snmpget service to generic linspector service; raising exceptions from all services now when needed args are missing.

This commit is contained in:
Johannes Findeisen 2013-06-10 05:01:40 +02:00
commit 7f45a14802
5 changed files with 47 additions and 53 deletions

View file

@ -20,6 +20,7 @@ class HttpService(Service):
self.string = kwargs["string"] self.string = kwargs["string"]
else: else:
log.w("There is no string set to match") log.w("There is no string set to match")
raise
if "method" in kwargs: if "method" in kwargs:
self.method = kwargs["method"] self.method = kwargs["method"]

View file

@ -12,6 +12,7 @@ class ShellService(Service):
self.command = kwargs["command"] self.command = kwargs["command"]
else: else:
log.w("There is no command") log.w("There is no command")
raise
def execute(self): def execute(self):
self.command.call() self.command.call()

View file

@ -2,57 +2,47 @@
The snmpget service in pure Python. 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 pysnmp.entity.rfc3413.oneliner import cmdgen
from service import Service
cmdGen = cmdgen.CommandGenerator()
errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd( class SnmpgetService(Service):
cmdgen.CommunityData('linspector'), def __init__(self, **kwargs):
cmdgen.UdpTransportTarget(('localhost', 161)),
# Names variables: if "community" in kwargs:
#cmdgen.MibVariable('SNMPv2-MIB', 'sysName', 0) self.community = kwargs["community"]
else:
#log.w("There is no community")
raise
# OID's: if "oid" in kwargs:
cmdgen.MibVariable('.1.3.6.1.4.1.2021.10.1.3.1') # load self.oid = kwargs["oid"]
#cmdgen.MibVariable('.1.3.6.1.4.1.2021.10.1.3.2') # load else:
#cmdgen.MibVariable('.1.3.6.1.4.1.2021.10.1.3.3') # load #log.w("There is no oid")
#cmdgen.MibVariable('.1.3.6.1.2.1.1.3.0') # systems uptime raise
# 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 "port" in kwargs:
if errorIndication: self.port = kwargs["port"]
print(errorIndication) else:
else: self.port = "161"
if errorStatus:
print('%s at %s' % ( def execute(self):
errorStatus.prettyPrint(), cmdGen = cmdgen.CommandGenerator()
errorIndex and varBinds[int(errorIndex) - 1] or '?'
)) errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
else: cmdgen.CommunityData(self.community),
for name, val in varBinds: cmdgen.UdpTransportTarget((self.host, self.port)),
print('%s = %s' % (name.prettyPrint(), val.prettyPrint())) 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()))

View file

@ -17,6 +17,7 @@ class SshService(Service):
self.command = kwargs["command"] self.command = kwargs["command"]
else: else:
log.w("There is no command") log.w("There is no command")
raise
def execute(self): def execute(self):
path = os.path.join(os.environ['HOME'], '.ssh', 'id_rsa') path = os.path.join(os.environ['HOME'], '.ssh', 'id_rsa')

View file

@ -17,6 +17,7 @@ class TcpconnectService(Service):
self.port = kwargs["port"] self.port = kwargs["port"]
else: else:
log.w("There is no port set") log.w("There is no port set")
raise
def execute(self, log): def execute(self, log):
try: try: