Exemple #1
0
    def init(self):
        t = tp_utc_timestamp_ms() - 10 * 60 * 1000
        cnt = int((10 * 60 + self._INTERVAL - 1) / self._INTERVAL)
        for i in range(cnt):
            val = {
                't': t,
                'cpu': {
                    'u': 0,
                    's': 0
                },
                'mem': {
                    'u': 0,
                    't': 100
                },
                'disk': {
                    'r': 0,
                    'w': 0
                },
                'net': {
                    'r': 0,
                    's': 0
                }
            }
            self._sys_stats.append(val)
            t += self._INTERVAL * 1000

        psutil.cpu_times_percent()
        net = psutil.net_io_counters(pernic=False)
        self._net_recv = net.bytes_recv
        self._net_sent = net.bytes_sent
        disk = psutil.disk_io_counters(perdisk=False)
        self._disk_read = disk.read_bytes
        self._disk_write = disk.write_bytes

        err, c = stats.get_basic_stats()
        if TPE_OK == err:
            self._counter_stats = c

        # 每 5秒 采集一次系统状态统计数据
        tp_cron().add_job('sys_status',
                          self._check_sys_stats,
                          first_interval_seconds=self._INTERVAL,
                          interval_seconds=self._INTERVAL)
        # 每 1小时 重新查询一次数据库,得到用户数/主机数/账号数/连接数,避免统计数量出现偏差
        tp_cron().add_job('query_counter',
                          self._query_counter,
                          first_interval_seconds=60 * 60,
                          interval_seconds=60 * 60)
        # 每 1分钟 检查一下临时锁定用户是否可以自动解锁了
        tp_cron().add_job('check_temp_locked_user',
                          self._check_temp_locked_user,
                          interval_seconds=60)

        tp_wss().register_get_sys_status_callback(self.get_sys_stats)
        tp_wss().register_get_stat_counter_callback(self.get_counter_stats)

        return True
Exemple #2
0
    def open(self, sid):
        # 处理新的连接
        k = '{}-{}'.format('user', sid)
        _user = tp_session().get(k, None)
        if _user is None:
            ret = {'code': TPE_NEED_LOGIN, 'message': '需要登录'}
            self.write_message(json.dumps(ret))
            return

        tp_wss().register(self)
Exemple #3
0
    def _check_sys_stats(self):
        val = {'t': tp_utc_timestamp_ms()}

        cpu = psutil.cpu_times_percent()
        val['cpu'] = {'u': cpu.user, 's': cpu.system}

        mem = psutil.virtual_memory()
        val['mem'] = {'u': mem.used, 't': mem.total}

        disk = psutil.disk_io_counters(perdisk=False)
        _read = disk.read_bytes - self._disk_read
        _write = disk.write_bytes - self._disk_write
        self._disk_read = disk.read_bytes
        self._disk_write = disk.write_bytes

        if _read < 0:
            _read = 0
        if _write < 0:
            _write = 0
        val['disk'] = {
            'r': int(_read / self._INTERVAL),
            'w': int(_write / self._INTERVAL)
        }

        net = psutil.net_io_counters(pernic=False)
        _recv = net.bytes_recv - self._net_recv
        _sent = net.bytes_sent - self._net_sent
        self._net_recv = net.bytes_recv
        self._net_sent = net.bytes_sent

        # On some systems such as Linux, on a very busy or long-lived system, the numbers
        # returned by the kernel may overflow and wrap (restart from zero)
        if _recv < 0:
            _recv = 0
        if _sent < 0:
            _sent = 0
        val['net'] = {
            'r': int(_recv / self._INTERVAL),
            's': int(_sent / self._INTERVAL)
        }

        self._sys_stats.pop(0)
        self._sys_stats.append(val)

        tp_wss().send_message('sys_status', val)
Exemple #4
0
    def _check_sys_stats(self):
        val = {'t': tp_utc_timestamp_ms()}

        cpu = psutil.cpu_times_percent()
        val['cpu'] = {'u': cpu.user, 's': cpu.system}

        mem = psutil.virtual_memory()
        val['mem'] = {'u': mem.used, 't': mem.total}

        disk = psutil.disk_io_counters(perdisk=False)
        _read = disk.read_bytes - self._disk_read
        _write = disk.write_bytes - self._disk_write
        self._disk_read = disk.read_bytes
        self._disk_write = disk.write_bytes

        if _read < 0:
            _read = 0
        if _write < 0:
            _write = 0
        val['disk'] = {'r': int(_read / self._INTERVAL), 'w': int(_write / self._INTERVAL)}

        net = psutil.net_io_counters(pernic=False)
        _recv = net.bytes_recv - self._net_recv
        _sent = net.bytes_sent - self._net_sent
        self._net_recv = net.bytes_recv
        self._net_sent = net.bytes_sent

        # On some systems such as Linux, on a very busy or long-lived system, the numbers
        # returned by the kernel may overflow and wrap (restart from zero)
        if _recv < 0:
            _recv = 0
        if _sent < 0:
            _sent = 0
        val['net'] = {'r': int(_recv / self._INTERVAL), 's': int(_sent / self._INTERVAL)}

        self._sys_stats.pop(0)
        self._sys_stats.append(val)

        tp_wss().send_message('sys_status', val)
Exemple #5
0
    def init(self):
        t = tp_utc_timestamp_ms() - 10 * 60 * 1000
        cnt = int((10 * 60 + self._INTERVAL - 1) / self._INTERVAL)
        for i in range(cnt):
            val = {
                't': t,
                'cpu': {'u': 0, 's': 0},
                'mem': {'u': 0, 't': 100},
                'disk': {'r': 0, 'w': 0},
                'net': {'r': 0, 's': 0}
            }
            self._sys_stats.append(val)
            t += self._INTERVAL * 1000

        psutil.cpu_times_percent()
        net = psutil.net_io_counters(pernic=False)
        self._net_recv = net.bytes_recv
        self._net_sent = net.bytes_sent
        disk = psutil.disk_io_counters(perdisk=False)
        self._disk_read = disk.read_bytes
        self._disk_write = disk.write_bytes

        err, c = stats.get_basic_stats()
        if TPE_OK == err:
            self._counter_stats = c

        # 每 5秒 采集一次系统状态统计数据
        tp_cron().add_job('sys_status', self._check_sys_stats, first_interval_seconds=self._INTERVAL, interval_seconds=self._INTERVAL)
        # 每 1小时 重新查询一次数据库,得到用户数/主机数/账号数/连接数,避免统计数量出现偏差
        tp_cron().add_job('query_counter', self._query_counter, first_interval_seconds=60 * 60, interval_seconds=60 * 60)
        # 每 1分钟 检查一下临时锁定用户是否可以自动解锁了
        tp_cron().add_job('check_temp_locked_user', self._check_temp_locked_user, interval_seconds=60)

        tp_wss().register_get_sys_status_callback(self.get_sys_stats)
        tp_wss().register_get_stat_counter_callback(self.get_counter_stats)

        return True
Exemple #6
0
 def on_message(self, message):
     if not tp_wss().have_callbacker(self):
         ret = {'code': TPE_NEED_LOGIN, 'message': '未曾成功连接'}
         self.write_message(json.dumps(ret))
         return
     tp_wss().on_message(self, message)
Exemple #7
0
 def on_close(self):
     if not tp_wss().have_callbacker(self):
         return
     tp_wss().unregister(self)  # 删除客户端连接
Exemple #8
0
 def conn_counter_change(self, alt_count):
     self._counter_stats['conn'] += alt_count
     if self._counter_stats['conn'] < 0:
         self._counter_stats['conn'] = 0
     tp_wss().send_message('stat_counter', self._counter_stats)
Exemple #9
0
 def user_counter_change(self, count):
     self._counter_stats['user'] = count
     # self._counter_stats['user'] += alt_count
     # if self._counter_stats['user'] < 0:
     #     self._counter_stats['user'] = 0
     tp_wss().send_message('stat_counter', self._counter_stats)
Exemple #10
0
 def _query_counter(self):
     # 直接从数据库中查询数据,避免长时间运行后计数不准确
     err, c = stats.get_basic_stats()
     if TPE_OK == err:
         self._counter_stats = c
         tp_wss().send_message('stat_counter', self._counter_stats)
Exemple #11
0
 def conn_counter_change(self, alt_count):
     self._counter_stats['conn'] += alt_count
     if self._counter_stats['conn'] < 0:
         self._counter_stats['conn'] = 0
     tp_wss().send_message('stat_counter', self._counter_stats)
Exemple #12
0
 def user_counter_change(self, count):
     self._counter_stats['user'] = count
     # self._counter_stats['user'] += alt_count
     # if self._counter_stats['user'] < 0:
     #     self._counter_stats['user'] = 0
     tp_wss().send_message('stat_counter', self._counter_stats)
Exemple #13
0
 def _query_counter(self):
     # 直接从数据库中查询数据,避免长时间运行后计数不准确
     err, c = stats.get_basic_stats()
     if TPE_OK == err:
         self._counter_stats = c
         tp_wss().send_message('stat_counter', self._counter_stats)