Exemplo n.º 1
0
def fortune():
    if request.method == 'GET':
        cust_logger.info("Received GET request")
        try:
            file_fortune = open(
                "../" + config.fortune_service.path_file_fortunes, 'r')
            selected_line = random.choice(
                file_fortune.readlines()
            )  #No close in that call since file closes automatically after call.
            response = dict(
                host_conf=host_conf,
                result=selected_line)  # we add our informations to the answer
            cust_logger.info("line selected is " + selected_line)
        except IOError as e:
            cust_logger.error(
                "cannot open document, I/O error({0}): {1}".format(
                    e.errno, e.strerror))
            response = dict(host_conf=host_conf, result="error")
            #We save the last exception raised in the database
            mib = WebServiceMIB.objects(port=host_conf['port']).first()
            if mib:
                mib.last_excpt_raised = str(e)
                mib.save()
            return json.dumps(response), notfound
        except TypeError as e:
            cust_logger.error("I/O error({0}): {1}".format(
                e.errno, e.strerror))
            response = dict(host_conf=host_conf, result="error")
            mib = WebServiceMIB.objects(port=host_conf['port']).first()
            if mib:
                mib.last_excpt_raised = str(e)
            return json.dumps(response), notfound
        except IndexError as e:
            cust_logger.error("no lines to read, I/O error({0}): {1}".format(
                e.errno, e.strerror))
            response = dict(host_conf=host_conf, result="error")
            mib = WebServiceMIB.objects(port=host_conf['port']).first()
            if mib:
                mib.last_excpt_raised = str(e)

            return json.dumps(response)
        except:
            cust_logger.error("Unexpected error:", sys.exc_info()[0])
            response = dict(host_conf=host_conf, result="error")
            mib = WebServiceMIB.objects(port=host_conf['port']).first()
            if mib:
                mib.last_excpt_raised = str(e)
            raise

        #simulate long execution
        time.sleep(2)

        return json.dumps(response)
    #not used yet
    elif request.method == 'POST':
        cust_logger.error("Unexpected post request received")
        raise TypeError
Exemplo n.º 2
0
def lastExceptionDaemon(web_ip, web_listen_port,list_monitors):
    while(WebServiceMIB.objects(port=web_listen_port).first() is None):
        pass
    old_last_exception = WebServiceMIB.objects(port=web_listen_port).first().last_excpt_raised
    while True:
        mib = WebServiceMIB.objects(port=web_listen_port).first()
        if mib.last_excpt_raised != old_last_exception:
            hbSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            for monitor in list_monitors:
                hbSocket.sendto("last_exception#%s#%d#%s"% (web_ip,int(web_listen_port), mib.last_excpt_raised), (monitor['ip'],int(monitor['port'])))
        time.sleep(PERIOD_EXCEPTION)
Exemplo n.º 3
0
def lastExceptionDaemon(web_ip, web_listen_port, list_monitors):
    while (WebServiceMIB.objects(port=web_listen_port).first() is None):
        pass
    old_last_exception = WebServiceMIB.objects(
        port=web_listen_port).first().last_excpt_raised
    while True:
        mib = WebServiceMIB.objects(port=web_listen_port).first()
        if mib.last_excpt_raised != old_last_exception:
            hbSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            for monitor in list_monitors:
                hbSocket.sendto(
                    "last_exception#%s#%d#%s" %
                    (web_ip, int(web_listen_port), mib.last_excpt_raised),
                    (monitor['ip'], int(monitor['port'])))
        time.sleep(PERIOD_EXCEPTION)
Exemplo n.º 4
0
def fortune():
    if request.method == 'GET':
        cust_logger.info("Received GET request")
        try:
            file_fortune = open("../"+config.fortune_service.path_file_fortunes, 'r')
            selected_line = random.choice(file_fortune.readlines()) #No close in that call since file closes automatically after call.
            response = dict(host_conf=host_conf, result=selected_line) # we add our informations to the answer
            cust_logger.info("line selected is " +selected_line)
        except IOError as e:
            cust_logger.error("cannot open document, I/O error({0}): {1}".format(e.errno, e.strerror))
            response = dict(host_conf=host_conf, result="error")
            #We save the last exception raised in the database
            mib = WebServiceMIB.objects(port=host_conf['port']).first()
            if mib:
                mib.last_excpt_raised = str(e)
                mib.save()
            return json.dumps(response), notfound
        except TypeError as e:
            cust_logger.error("I/O error({0}): {1}".format(e.errno, e.strerror))
            response = dict(host_conf=host_conf, result="error")
            mib = WebServiceMIB.objects(port=host_conf['port']).first()
            if mib:
                mib.last_excpt_raised = str(e)
            return json.dumps(response), notfound
        except IndexError as e:
            cust_logger.error("no lines to read, I/O error({0}): {1}".format(e.errno, e.strerror))
            response = dict(host_conf=host_conf, result="error")
            mib = WebServiceMIB.objects(port=host_conf['port']).first()
            if mib:
                mib.last_excpt_raised = str(e)

            return json.dumps(response)
        except:
            cust_logger.error( "Unexpected error:", sys.exc_info()[0] )
            response = dict(host_conf=host_conf, result="error")
            mib = WebServiceMIB.objects(port=host_conf['port']).first()
            if mib:
                mib.last_excpt_raised = str(e)
            raise
 
        #simulate long execution
        time.sleep(2)

        return json.dumps(response)
    #not used yet
    elif request.method == 'POST':
        cust_logger.error("Unexpected post request received")
        raise TypeError
Exemplo n.º 5
0
class UdpProtocol(protocol.DatagramProtocol):
    def __init__(self, lock, source_ip, web_listen_port):
        self.lock = lock
        self.source_ip = source_ip
        self.web_listen_port = web_listen_port

    def datagramReceived(self, data, (host, port)):
        if data.startswith("request_logs"):
            cust_logger.info("logs requested")
            _, response_ip, response_port = data.split('#')
            self.lock.acquire()
            with open(
                    WebServiceMIB.objects(
                        port=web_listen_port).first().log_file_path, "r") as f:
                f.seek(0, 2)
                size = f.tell()
                f.seek(max(size - 1024, 0), 0)
                lines = f.readlines()
            self.lock.release()
            socketUDP = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            socketUDP.sendto(
                "logs#%s#%d#%s" %
                (self.source_ip, self.web_listen_port, json.dumps(lines)),
                (response_ip, int(response_port)))
        if data.startswith("add_monitor"):
            cust_logger.info("Add monitor")
            _, monitor_ip, monitor_port, monitor_hb = data.split('#')
            new_monitor = dict(ip=monitor_ip,
                               port=monitor_port,
                               port_hb=monitor_hb)
            if new_monitor not in monitors:
                monitors.append(new_monitor)