diff --git a/linspector/services/udpconnect.py b/linspector/services/udpconnect.py new file mode 100644 index 0000000..0f35335 --- /dev/null +++ b/linspector/services/udpconnect.py @@ -0,0 +1,67 @@ +""" +The udpconnect service. This is to check if a UDP service on a specific +port is reachable. + +This should just return 0 on success and NOT 0 on error. + +Copyright (c) 2011-2013 "Johannes Findeisen and Rafael Timmerberg" + +This file is part of Linspector (http://linspector.org). + +Linspector is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as +published by the Free Software Foundation, either version 3 of the +License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . +""" + +import socket +from linspector.services.service import Service + + +class UdpconnectService(Service): + def __init__(self, **kwargs): + super(UdpconnectService, self).__init__(**kwargs) + + args = self.get_arguments() + if "port" in args: + self.port = args["port"] + else: + raise Exception("There is no port set") + + def needs_arguments(self): + return True + + def execute(self, jobInfo): + try: + sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + except socket.error, msg: + jobInfo.set_errorcode(2) + jobInfo.set_message("[udpconnect: " + jobInfo.jobHex + "] Could not create socket to host: " + + jobInfo.get_host() + " on port: " + str(self.port) + " (" + str(msg) + ")") + + try: + sock.connect((jobInfo.get_host(), self.port)) + except socket.error, msg: + jobInfo.set_errorcode(1) + jobInfo.set_message("[udpconnect: " + jobInfo.jobHex + "] Could not establish connection to host: " + + jobInfo.get_host() + " on port: " + str(self.port) + " (" + str(msg) + ")") + + if jobInfo.get_errorcode() == -1: + jobInfo.set_execution_successful(True) + jobInfo.set_errorcode(0) + jobInfo.set_message("[udpconnect: " + jobInfo.jobHex + "] Connection successful established to host: " + + jobInfo.get_host() + " on port: " + str(self.port)) + + sock.close() + + +def create(kwargs): + return UdpconnectService(**kwargs) \ No newline at end of file