added some testing code to the snmpget builtin and renamed service to services/

This commit is contained in:
Johannes Findeisen 2013-06-10 04:14:53 +02:00
commit ee687e3f68
8 changed files with 58 additions and 0 deletions

0
lib/services/__init__.py Normal file
View file

54
lib/services/http.py Normal file
View file

@ -0,0 +1,54 @@
"""
The http service.
This is for checking the availability and output of HTTP services. Basic HTTP
content could be fetched and compared.HTTPS is not validating the server certificate!
This should just return 0 on success and NOT 0 on error. Just to make internals generic
to just report this code and not use a parser.
"""
import urllib
from service import Service
class HttpService(Service):
def __init__(self, parser, log, **kwargs):
super(Service, self).__init__(parser)
if "string" in kwargs:
self.string = kwargs["string"]
else:
log.w("There is no string set to match")
if "method" in kwargs:
self.method = kwargs["method"]
else:
self.method = "get"
if "params" in kwargs:
self.params = kwargs["params"]
if "path" in kwargs:
self.path = kwargs["path"]
else:
self.path = "/"
if "port" in kwargs:
self.port = kwargs["port"]
else:
self.port = "80"
if "protocol" in kwargs:
self.protocol = kwargs["protocol"]
else:
self.protocol = "http"
def execute(self):
params = urllib.urlencode(self.params)
if self.method is "get":
f = urllib.urlopen(self.protocol + "://" + self.host + ":" + self.port + self.path + "?%s" % params)
elif self.method is "post":
f = urllib.urlopen(self.protocol + "://" + self.host + ":" + self.port + self.path, params)
#print f.read()

5
lib/services/ping.py Normal file
View file

@ -0,0 +1,5 @@
"""
The ping service in pure Python.
"""
# http://code.activestate.com/recipes/409689-icmplib-library-for-creating-and-reading-icmp-pack/

24
lib/services/service.py Normal file
View file

@ -0,0 +1,24 @@
class Service:
def __init__(self, host, parser):
self.host = host
self.parser = parser
self.errorcode = 0
self.errormessage = "No Error!"
def _execute(self):
self.pre_execute()
executionResult = self.execute()
parseResult = self.parse_result(executionResult)
self.handle_result(parseResult)
def execute(self):
pass
def pre_execute(self):
pass
def parse_result(self, executionResult):
return self.parser._parse(executionResult)
def handle_result(self, parseResult):
pass

17
lib/services/shell.py Normal file
View file

@ -0,0 +1,17 @@
"""
The shell service. This is for executing local shell commands and retrieve the output.
"""
from service import Service
class ShellService(Service):
def __init__(self, parser, log, **kwargs):
super(Service, self).__init__(parser)
if "command" in kwargs:
self.command = kwargs["command"]
else:
log.w("There is no command")
def execute(self):
self.command.call()

58
lib/services/snmpget.py Normal file
View file

@ -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()))

43
lib/services/ssh.py Normal file
View file

@ -0,0 +1,43 @@
"""
The ssh service This is for executing remote shell commands and retrieve the output.
This service is using paramiko (http://www.lag.net/paramiko/).
"""
import paramiko
import pprint
import os
from service import Service
class SshService(Service):
def __init__(self, parser, log, **kwargs):
super(Service, self).__init__(parser)
if "command" in kwargs:
self.command = kwargs["command"]
else:
log.w("There is no command")
def execute(self):
path = os.path.join(os.environ['HOME'], '.ssh', 'id_rsa')
key = paramiko.RSAKey.from_private_key_file(path)
client = paramiko.SSHClient()
client.get_host_keys().add('hanez.org', 'ssh-rsa', key)
pprint.pprint(client._host_keys)
client.connect('hanez.org', username='hanez')
#self.command.call() ist dann das:
stdin, stdout, stderr = client.exec_command('ls')
for line in stdout:
print '... ' + line.strip('\n')
client.close()
# def main():
# # service = SshService(parser, log, command='uptime')
# return
#
# if __name__ == "__main__":
# main()

View file

@ -0,0 +1,35 @@
"""
The tcpconnect service. This is to check if a service on a specific port is reachable.
This should just return 0 on success and NOT 0 on error. Just to make internals generic to just report this code and
not use a parser.
"""
import socket
from service import Service
class TcpconnectService(Service):
def __init__(self, parser, log, **kwargs):
super(Service, self).__init__(parser)
if "port" in kwargs:
self.port = kwargs["port"]
else:
log.w("There is no port set")
def execute(self, log):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, msg:
log.w("%s\n" % msg[1])
self.errorcode = 1
try:
sock.connect((self.host, self.port))
except socket.error, msg:
log.w("%s\n" % msg[1])
self.errorcode = 2
sock.close()
return