示例#1
0
    def open(self):
        global HANDLER, OBSERVER

        self.path, _ = os.path.split(ws.config.CONFIG['ACCESS_LOG'])
        if self.path not in HANDLER:
            HANDLER[self.path] = AccessHandler(handler=getattr(self, 'send'))
            OBSERVER[self.path] = Observer()
            logger.info("Watching: " + self.path)
            OBSERVER[self.path].schedule(HANDLER[self.path], self.path, recursive=True)
            try:
                OBSERVER[self.path].start()
            except OSError:
                logger.error('Cannot start observer')
                self.close()
                return
        self.observer = True
        self.send({ "lastline": HANDLER[self.path].lastline})
示例#2
0
    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."}))