def on_modified(self, event): filename = os.path.basename(event.src_path) logger.debug("(" + str(self.id) + ") Access: " + filename) if filename == os.path.basename(ws.config.CONFIG["ACCESS_LOG"]): self.read_access_log() self.handle({"accesses": self.accesses, "lastline": self.lastline})
def on_message(self, message): logger.debug(json.dumps({ "receive": str(self.avg_speeds['rcv']) + " KiB/s.", "send": str(self.avg_speeds['send']) + " KiB/s."})) self.write_message(json.dumps({ "receive": str(self.avg_speeds['rcv']) + " KiB/s.", "send": str(self.avg_speeds['send']) + " KiB/s."}))
def __init__(self, handler, *args, **kwargs): logger.debug("Creating WebSocket file access handler") super(AccessHandler, self).__init__(*args, **kwargs) self.handlers = [handler] self.accesses = 0 self.lastline = "" AccessHandler.instances += 1 logger.debug(str(AccessHandler.instances) + " active handlers") self.id = AccessHandler.instances self.read_access_log()
def update(self): # Save the old number of connections old = self.connections self.get_connections() # Check if the number of connections has changed if old != self.connections: # Send the new data. if self.connected: logger.debug(json.dumps({ "connections": self.get_connections() })) self.write_message(json.dumps({ "connections": self.get_connections() }))
def __init__(self, *args, **kwargs): logger.debug("Creating WebSocket connections handler") super(WebSocketconnectionsHandler, self).__init__(*args, **kwargs) # No WebSocket connection yet self.connected = False # We have not counted the connections yet self.connections = 0 # Update the connection count self.update() # Setup periodic callback via Tornado self.periodic_callback = PeriodicCallback(getattr(self, 'update'), 1000)
def read_access_log(self): # Count number of lines logger.debug("(" + str(self.id) + ") Reading log file.") with open(ws.config.CONFIG["ACCESS_LOG"]) as log_file: for line_number, line in enumerate(log_file, 1): pass if self.accesses <= line_number: self.accesses = line_number else: # Try compensating when logrotate empties the file. self.accesses += line_number self.lastline = line
def open(self): logger.debug(json.dumps({ "receive": str(self.avg_speeds['rcv']) + " KiB/s.", "send": str(self.avg_speeds['send']) + " KiB/s."})) self.write_message(json.dumps({ "receive": str(self.avg_speeds['rcv']) + " KiB/s.", "send": str(self.avg_speeds['send']) + " KiB/s."})) # We have a WebSocket connection self.connected = True self.periodic_callback.start()
def on_close(self): global HANDLER, OBSERVER logger.debug("Connection closed") self.connected = False if self.observer: logger.debug("Stop watching:" + self.path) HANDLER[self.path].remove_handler(getattr(self, 'send')) if len(HANDLER[self.path].handlers): OBSERVER[self.path].stop() del OBSERVER[self.path] del HANDLER[self.path] self.observer = False
def send(self, data): self.ip_addr = data['lastline'].split(' ')[0] rhost = list() try: rhost = socket.gethostbyaddr(self.ip_addr) except (socket.herror, socket.gaierror): logger.debug("DNS bugged out, sending IP: " + self.ip_addr + ".") rhost.append("") if rhost[0] != "": self.ip_addr += " (" + rhost[0] + ")" logger.debug("Sending: " + json.dumps({ "remote_host": self.ip_addr })) self.write_message(json.dumps({ "remote_host": self.ip_addr }))
def __init__(self, *args, **kwargs): logger.debug("Creating WebSocket speed handler") super(WebSocketspeedHandler, self).__init__(*args, **kwargs) # No WebSocket connection yet self.connected = False # We have not measured yet self.rcv_speed = 0 self.send_speed = 0 # Update the connection count self.update() # Setup periodic callback via Tornado self.periodic_callback = PeriodicCallback(getattr(self, 'update'), 2000) # Setup variables for calculating the speed self.last_times = {"rcv": 0, "send": 0} self.last_bytes = {"rcv": 0, "send": 0} self.avg_speeds = {"rcv": 0, "send": 0}
def update(self): logger.debug("Getting uptime") proc_time = 0 # Find the lighttpd process and get the create time, to calculate the up # time. for process in psutil.process_iter(): if process.name().find(ws.config.CONFIG['PROCESS_NAME']) != -1: logger.debug("Process found.") proc_time = (datetime.now() - datetime.fromtimestamp(process.create_time())) logger.debug("Up time: " + str(proc_time) + ".") self.uptime = str(proc_time).split('.')[0]
def __init__(self, *args, **kwargs): logger.debug("Creating WebSocket remote_host handler") super(WebSocketremote_hostHandler, self).__init__(*args, **kwargs) self.connected = False self.observer = False self.ip_addr = "0.0.0.0"
def remove_handler(self, handler): if handler in self.handlers: self.handlers.remove(handler) AccessHandler.instances -= 1 logger.debug(str(AccessHandler.instances) + " active handlers")
def open(self): logger.debug(json.dumps({ "connections": self.get_connections() })) self.write_message(json.dumps({ "connections": self.get_connections() })) # We have a WebSocket connection self.connected = True self.periodic_callback.start()
def on_message(self, message): logger.debug(json.dumps({ "connections": self.get_connections() })) self.write_message(json.dumps({ "connections": self.get_connections() }))
def handle(self, data): logger.debug("(" + str(self.id) + ") Handling: " + str(data)) for handler in self.handlers: logger.debug("(" + str(self.id) + ") Calling handler") handler(data)
def __init__(self, *args, **kwargs): logger.debug("Creating WebSocket accesses handler") super(WebSocketaccessesHandler, self).__init__(*args, **kwargs) self.connected = False self.observer = False
def send(self, data): logger.debug("Sending: " + json.dumps({ "accesses": data['accesses'] })) self.write_message(json.dumps({ "accesses": data['accesses'] }))
def update(self): if self.connected: logger.debug("Get average incoming speed for: " + CONFIG['INTERFACE']) try: interfaces = psutil.net_io_counters(True) print(interfaces) now = datetime.now() total_bytes_recv = interfaces[ws.config.CONFIG['INTERFACE']].bytes_recv total_bytes_sent = interfaces[ws.config.CONFIG['INTERFACE']].bytes_sent except KeyError: logger.error("Interface not found.") try: if not ((self.last_times['rcv'] == 0) and (self.last_times['send'] == 0)): time = ((now - self.last_times['rcv']).seconds, (now - self.last_times['send']).seconds) else: time = (now, now) logger.debug("Receive sample period: " + str(time[0]) + " seconds.") logger.debug("Send sample period: " + str(time[1]) + " seconds.") if self.last_times['rcv'] == 0: self.last_bytes['rcv'] = total_bytes_recv self.last_times['rcv'] = now logger.debug("First receive run, no average yet.") else: rcv_bytes = total_bytes_recv - self.last_bytes['rcv'] logger.debug("Bytes received: " + str(rcv_bytes) + " bytes.") speed = (rcv_bytes / time[0]) / 1024 logger.debug("Sampled receive speed: " + str(speed) + "KiB/s.") self.avg_speeds['rcv'] = (self.avg_speeds['rcv'] + speed) / 2 logger.debug("Average receive speed: " + str(self.avg_speeds['rcv']) + " KiB/s.") self.last_bytes['rcv'] = total_bytes_recv self.last_times['rcv'] = now if self.last_times['send'] == 0: self.last_bytes['send'] = total_bytes_sent self.last_times['send'] = now logger.debug("First send run, no average yet.") else: sent_bytes = total_bytes_sent - self.last_bytes['send'] logger.debug("Bytes sent: " + str(sent_bytes) + " bytes.") speed = (sent_bytes / time[1]) / 1024 logger.debug("Sampled send speed: " + str(speed) + "KiB/s.") self.avg_speeds['send'] = (self.avg_speeds['send'] + speed) / 2 logger.debug("Average send speed: " + str(self.avg_speeds['send']) + " KiB/s.") self.last_bytes['send'] = total_bytes_sent self.last_times['send'] = now except ZeroDivisionError: logger.warning("Sampling to fast, while sampling incoming speed.") logger.debug(json.dumps({ "receive": str(self.avg_speeds['rcv']) + " KiB/s.", "send": str(self.avg_speeds['send']) + " KiB/s."})) self.write_message(json.dumps({ "receive": str(self.avg_speeds['rcv']) + " KiB/s.", "send": str(self.avg_speeds['send']) + " KiB/s."}))
def send(self): self.update() logger.debug(json.dumps({ "uptime": self.uptime })) self.write_message(json.dumps({ "uptime": self.uptime }))
def on_close(self): logger.debug("Connection closed") # We no longer have a WebSocket connection. self.connected = False self.periodic_callback.stop()
def on_message(self, message): logger.debug("Sending services message: " + ws.config.CONFIG['WS_SERVICES']) self.write_message(json.dumps(ws.config.CONFIG['WS_SERVICES']))
def on_close(self): logger.debug("Connection was closed...")