当前机器的状态信息上报,包括cup,内存,硬盘(当前文件所在盘)等
| 132 | |
| 133 | |
| 134 | class NodeStatusMonitor(object): |
| 135 | """ |
| 136 | 当前机器的状态信息上报,包括cup,内存,硬盘(当前文件所在盘)等 |
| 137 | """ |
| 138 | def __init__(self, node_server): |
| 139 | """ |
| 140 | |
| 141 | :param node_server: 上报api实例 |
| 142 | :return: |
| 143 | """ |
| 144 | self._server = node_server |
| 145 | self._profiling_interval = 2 * 60 # sec 状态上报的频率 |
| 146 | |
| 147 | def _get_status_info(self): |
| 148 | ''' |
| 149 | 返回当前机器的状态信息,包括cup,内存,硬盘(当前文件所在盘)等 |
| 150 | ''' |
| 151 | cpu_num = psutil.cpu_count() |
| 152 | cpu_usage = int(psutil.cpu_percent(interval=1.0)) |
| 153 | # return staticstics about system memory usage as a nametuple including the following fields(unit:bytes) |
| 154 | # (total, available, percent, used, free) |
| 155 | memory_info = psutil.virtual_memory() |
| 156 | memory_space = memory_info.total |
| 157 | memory_free_space = memory_info.available |
| 158 | # return staticstics about disk usage as a nametuple including the following fields(unit:bytes) |
| 159 | # (total, used, free, percent) |
| 160 | disk_info = psutil.disk_usage(app.settings.BASE_DIR) |
| 161 | hdrive_space = disk_info.total |
| 162 | hdrive_free_space = disk_info.free |
| 163 | os = platform() |
| 164 | status_info = {"cpu_num": cpu_num, "cpu_usage": cpu_usage, |
| 165 | "mem_space":str(memory_space), "mem_free_space": str(memory_free_space), |
| 166 | "hdrive_space": str(hdrive_space), "hdrive_free_space": str(hdrive_free_space), |
| 167 | "network_latency": 0, "os": os} |
| 168 | return status_info |
| 169 | |
| 170 | def _thread_update_status(self): |
| 171 | """ |
| 172 | 机器状态上报线程 |
| 173 | """ |
| 174 | while True: |
| 175 | self._server.update_status(self._get_status_info()) |
| 176 | time.sleep(self._profiling_interval) |
| 177 | |
| 178 | def start(self): |
| 179 | status_thread = threading.Thread(target=self._thread_update_status) |
| 180 | status_thread.daemon = True |
| 181 | status_thread.start() |
| 182 | LogPrinter.info("node status profiling thread is started.") |