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
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)
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)
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
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)
(load_bal_ip, load_bal_port) = val.split(":") #send notification to load_balancer hbSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) hbSocket.sendto( "add_server#%s#%d#%d" % ("localhost", source_port, monitor_port), (load_bal_ip, int(load_bal_port))) ip = "localhost" db = mongoengine.connect("%s#%d" % (ip, source_port)) #db.drop_database("%s#%d"%(ip,source_port)) path_file_log = cust_logger.add_file("logWeb/%d" % source_port + "/logFweb") #we store our own condition in the database so the monitor can read it mib = WebServiceMIB(port=source_port, status=StatusWebService.STATUS_UP, log_file_path=path_file_log) mib.save() http_server = HTTPServer(WSGIContainer(app)) http_server.listen(source_port) host_conf = { 'ip': ip, 'port': int(source_port), 'monitor_port': int(monitor_port) } IOLoop.instance().start() #app.run(host="0.0.0.0", port=source_port, debug=True, use_reloader=False)
(options, args) = getopt.getopt(sys.argv[1:], "s:m:l:h", ["source=", "monitor=","loadBal=", "help"]) source_port, load_bal_ip, load_bal_port, monitor_port = None, None, None, None for opt, val in options: if (opt in ("-s", "--source")): source_port = int(val) if (opt in ("-m", "--monitor")): monitor_port = int(val) if (opt in ("-l", "--loadBal")): (load_bal_ip, load_bal_port) = val.split(":") #send notification to load_balancer hbSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) hbSocket.sendto("add_server#%s#%d#%d"%("localhost", source_port, monitor_port), (load_bal_ip, int(load_bal_port))) ip = "localhost" db = mongoengine.connect("%s#%d"%(ip,source_port)) #db.drop_database("%s#%d"%(ip,source_port)) path_file_log = cust_logger.add_file("logWeb/%d"%source_port+"/logFweb") #we store our own condition in the database so the monitor can read it mib = WebServiceMIB(port = source_port, status = StatusWebService.STATUS_UP , log_file_path = path_file_log ) mib.save() http_server = HTTPServer(WSGIContainer(app)) http_server.listen(source_port) host_conf = {'ip': ip, 'port':int(source_port), 'monitor_port':int(monitor_port)} IOLoop.instance().start() #app.run(host="0.0.0.0", port=source_port, debug=True, use_reloader=False)