|
| 1 | +import os, signal |
| 2 | +from random import randrange |
| 3 | + |
| 4 | +from flask import Flask, render_template, jsonify, request |
| 5 | + |
| 6 | +from pyecharts import options as opts |
| 7 | +from pyecharts.charts import Bar, Line, Liquid, Gauge, Grid |
| 8 | +import pyecharts.options as opts |
| 9 | +import time |
| 10 | +import psutil |
| 11 | + |
| 12 | + |
| 13 | +app = Flask(__name__, static_folder="templates") |
| 14 | + |
| 15 | + |
| 16 | +cpu_percent_dict = {} |
| 17 | +net_io_dict = {'net_io_time':[], 'net_io_sent': [], 'net_io_recv': [], 'pre_sent': 0, 'pre_recv': 0, 'len': -1} |
| 18 | +disk_dict = {'disk_time':[], 'write_bytes': [], 'read_bytes': [], 'pre_write_bytes': 0, 'pre_read_bytes': 0, 'len': -1} |
| 19 | + |
| 20 | +def cpu(): |
| 21 | + now = time.strftime('%H:%M:%S', time.localtime(time.time())) |
| 22 | + cpu_percent = psutil.cpu_percent() |
| 23 | + cpu_percent_dict[now] = cpu_percent |
| 24 | + # 保持在图表中 10 个数据 |
| 25 | + if len(cpu_percent_dict.keys()) == 11: |
| 26 | + cpu_percent_dict.pop(list(cpu_percent_dict.keys())[0]) |
| 27 | + |
| 28 | + |
| 29 | +def cpu_line() -> Line: |
| 30 | + now = time.strftime('%Y年%m月%d日的', time.localtime(time.time())) |
| 31 | + cpu() |
| 32 | + c = ( |
| 33 | + Line() |
| 34 | + .add_xaxis(list(cpu_percent_dict.keys())) |
| 35 | + .add_yaxis('', list(cpu_percent_dict.values()), areastyle_opts=opts.AreaStyleOpts(opacity=0.5)) |
| 36 | + .set_global_opts(title_opts=opts.TitleOpts(title = now + "CPU负载",pos_left = "center"), |
| 37 | + yaxis_opts=opts.AxisOpts(min_=0,max_=100,split_number=10,type_="value", name='%')) |
| 38 | + ) |
| 39 | + return c |
| 40 | + |
| 41 | +def memory(): |
| 42 | + memory = psutil.virtual_memory() |
| 43 | + swap = psutil.swap_memory() |
| 44 | + return memory.total, memory.total - (memory.free + memory.inactive), memory.free + memory.inactive, swap.total, swap.used, swap.free, memory.percent |
| 45 | + |
| 46 | + |
| 47 | +def memory_liquid() -> Gauge: |
| 48 | + mtotal, mused, mfree, stotal, sused, sfree, mpercent = memory() |
| 49 | + c = ( |
| 50 | + Gauge() |
| 51 | + .add("", [("", mpercent)]) |
| 52 | + .set_global_opts(title_opts=opts.TitleOpts(title="内存负载", pos_left = "center")) |
| 53 | + ) |
| 54 | + return mtotal, mused, mfree, stotal, sused, sfree, c |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +def net_io(): |
| 59 | + now = time.strftime('%H:%M:%S', time.localtime(time.time())) |
| 60 | + # 获取网络信息 |
| 61 | + count = psutil.net_io_counters() |
| 62 | + g_sent = count.bytes_sent |
| 63 | + g_recv = count.bytes_recv |
| 64 | + |
| 65 | + # 第一次请求 |
| 66 | + if net_io_dict['len'] == -1: |
| 67 | + net_io_dict['pre_sent'] = g_sent |
| 68 | + net_io_dict['pre_recv'] = g_recv |
| 69 | + net_io_dict['len'] = 0 |
| 70 | + return |
| 71 | + |
| 72 | + # 当前网络发送/接收的字节速率 = 现在网络发送/接收的总字节 - 前一次请求网络发送/接收的总字节 |
| 73 | + net_io_dict['net_io_sent'].append(g_sent - net_io_dict['pre_sent']) |
| 74 | + net_io_dict['net_io_recv'].append(g_recv - net_io_dict['pre_recv']) |
| 75 | + net_io_dict['net_io_time'].append(now) |
| 76 | + net_io_dict['len'] = net_io_dict['len'] + 1 |
| 77 | + |
| 78 | + net_io_dict['pre_sent'] = g_sent |
| 79 | + net_io_dict['pre_recv'] = g_recv |
| 80 | + |
| 81 | + # 保持在图表中 10 个数据 |
| 82 | + if net_io_dict['len'] == 11: |
| 83 | + net_io_dict['net_io_sent'].pop(0) |
| 84 | + net_io_dict['net_io_recv'].pop(0) |
| 85 | + net_io_dict['net_io_time'].pop(0) |
| 86 | + net_io_dict['len'] = net_io_dict['len'] - 1 |
| 87 | + |
| 88 | + |
| 89 | +def net_io_line() -> Line: |
| 90 | + net_io() |
| 91 | + |
| 92 | + c = ( |
| 93 | + Line() |
| 94 | + .add_xaxis(net_io_dict['net_io_time']) |
| 95 | + .add_yaxis("发送字节数", net_io_dict['net_io_sent'], is_smooth=True) |
| 96 | + .add_yaxis("接收字节数", net_io_dict['net_io_recv'], is_smooth=True) |
| 97 | + .set_series_opts( |
| 98 | + areastyle_opts=opts.AreaStyleOpts(opacity=0.5), |
| 99 | + label_opts=opts.LabelOpts(is_show=False), |
| 100 | + ) |
| 101 | + .set_global_opts( |
| 102 | + title_opts=opts.TitleOpts(title="网卡IO", pos_left = "center"), |
| 103 | + xaxis_opts=opts.AxisOpts( |
| 104 | + axistick_opts=opts.AxisTickOpts(is_align_with_label=True), |
| 105 | + is_scale=False, |
| 106 | + boundary_gap=False, |
| 107 | + ), |
| 108 | + yaxis_opts=opts.AxisOpts(type_="value", name='B/2S'), |
| 109 | + legend_opts=opts.LegendOpts(pos_left="left"), |
| 110 | + )) |
| 111 | + return c |
| 112 | + |
| 113 | + |
| 114 | +def process(): |
| 115 | + result = [] |
| 116 | + process_list = [] |
| 117 | + pid = psutil.pids() |
| 118 | + for k, i in enumerate(pid): |
| 119 | + try: |
| 120 | + proc = psutil.Process(i) |
| 121 | + ctime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(proc.create_time())) |
| 122 | + process_list.append((str(i), proc.name(), proc.cpu_percent(), proc.memory_percent(), ctime)) |
| 123 | + except psutil.AccessDenied: |
| 124 | + pass |
| 125 | + except psutil.NoSuchProcess: |
| 126 | + pass |
| 127 | + except SystemError: |
| 128 | + pass |
| 129 | + |
| 130 | + process_list.sort(key=process_sort, reverse=True) |
| 131 | + for i in process_list: |
| 132 | + result.append({'PID': i[0], 'name': i[1], 'cpu': i[2], 'mem': "%.2f%%"%i[3], 'ctime': i[4]}) |
| 133 | + |
| 134 | + |
| 135 | + return jsonify({'list': result}) |
| 136 | + |
| 137 | +def process_sort(elem): |
| 138 | + return elem[3] |
| 139 | + |
| 140 | + |
| 141 | +def disk(): |
| 142 | + disk_usage = psutil.disk_usage('/') |
| 143 | + disk_used = 0 |
| 144 | + # 磁盘已使用大小 = 每个分区的总和 |
| 145 | + partitions = psutil.disk_partitions() |
| 146 | + for partition in partitions: |
| 147 | + partition_disk_usage = psutil.disk_usage(partition[1]) |
| 148 | + disk_used = partition_disk_usage.used + disk_used |
| 149 | + |
| 150 | + now = time.strftime('%H:%M:%S', time.localtime(time.time())) |
| 151 | + count = psutil.disk_io_counters() |
| 152 | + read_bytes = count.read_bytes |
| 153 | + write_bytes = count.write_bytes |
| 154 | + |
| 155 | + # 第一次请求 |
| 156 | + if disk_dict['len'] == -1: |
| 157 | + disk_dict['pre_write_bytes'] = write_bytes |
| 158 | + disk_dict['pre_read_bytes'] = read_bytes |
| 159 | + disk_dict['len'] = 0 |
| 160 | + return disk_usage.total, disk_used, disk_usage.free |
| 161 | + |
| 162 | + # 当前速率=现在写入/读取的总字节-前一次请求写入/读取的总字节 |
| 163 | + disk_dict['write_bytes'].append((write_bytes - disk_dict['pre_write_bytes'])/1024) |
| 164 | + disk_dict['read_bytes'].append((read_bytes - disk_dict['pre_read_bytes'])/ 1024) |
| 165 | + disk_dict['disk_time'].append(now) |
| 166 | + disk_dict['len'] = disk_dict['len'] + 1 |
| 167 | + |
| 168 | + # 把现在写入/读取的总字节放入前一个请求的变量中 |
| 169 | + disk_dict['pre_write_bytes'] = write_bytes |
| 170 | + disk_dict['pre_read_bytes'] = read_bytes |
| 171 | + |
| 172 | + # 保持在图表中 50 个数据 |
| 173 | + if disk_dict['len'] == 51: |
| 174 | + disk_dict['write_bytes'].pop(0) |
| 175 | + disk_dict['read_bytes'].pop(0) |
| 176 | + disk_dict['disk_time'].pop(0) |
| 177 | + disk_dict['len'] = disk_dict['len'] - 1 |
| 178 | + |
| 179 | + return disk_usage.total, disk_used, disk_usage.free |
| 180 | + |
| 181 | + |
| 182 | +def disk_line() -> Line: |
| 183 | + total, used, free = disk() |
| 184 | + |
| 185 | + c = ( |
| 186 | + Line(init_opts=opts.InitOpts(width="1680px", height="800px")) |
| 187 | + .add_xaxis(xaxis_data=disk_dict['disk_time']) |
| 188 | + .add_yaxis( |
| 189 | + series_name="写入数据", |
| 190 | + y_axis=disk_dict['write_bytes'], |
| 191 | + areastyle_opts=opts.AreaStyleOpts(opacity=0.5), |
| 192 | + linestyle_opts=opts.LineStyleOpts(), |
| 193 | + label_opts=opts.LabelOpts(is_show=False), |
| 194 | + ) |
| 195 | + .add_yaxis( |
| 196 | + series_name="读取数据", |
| 197 | + y_axis=disk_dict['read_bytes'], |
| 198 | + yaxis_index=1, |
| 199 | + areastyle_opts=opts.AreaStyleOpts(opacity=0.5), |
| 200 | + linestyle_opts=opts.LineStyleOpts(), |
| 201 | + label_opts=opts.LabelOpts(is_show=False), |
| 202 | + ) |
| 203 | + .extend_axis( |
| 204 | + yaxis=opts.AxisOpts( |
| 205 | + name_location="start", |
| 206 | + type_="value", |
| 207 | + is_inverse=True, |
| 208 | + axistick_opts=opts.AxisTickOpts(is_show=True), |
| 209 | + splitline_opts=opts.SplitLineOpts(is_show=True), |
| 210 | + name='KB/2S' |
| 211 | + ) |
| 212 | + ) |
| 213 | + .set_global_opts( |
| 214 | + title_opts=opts.TitleOpts( |
| 215 | + title="磁盘IO", |
| 216 | + pos_left="center", |
| 217 | + pos_top="top", |
| 218 | + ), |
| 219 | + tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"), |
| 220 | + legend_opts=opts.LegendOpts(pos_left="left"), |
| 221 | + xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False), |
| 222 | + yaxis_opts=opts.AxisOpts(type_="value", name='KB/2S'), |
| 223 | + ) |
| 224 | + .set_series_opts( |
| 225 | + axisline_opts=opts.AxisLineOpts(), |
| 226 | + ) |
| 227 | + ) |
| 228 | + |
| 229 | + return total, used, free, c |
| 230 | + |
| 231 | +@app.route("/") |
| 232 | +def index(): |
| 233 | + return render_template("index.html") |
| 234 | + |
| 235 | + |
| 236 | +@app.route("/cpu") |
| 237 | +def get_cpu_chart(): |
| 238 | + c = cpu_line() |
| 239 | + return c.dump_options_with_quotes() |
| 240 | + |
| 241 | +@app.route("/memory") |
| 242 | +def get_memory_chart(): |
| 243 | + mtotal, mused, mfree, stotal, sused, sfree, c = memory_liquid() |
| 244 | + return jsonify({'mtotal': mtotal, 'mused': mused, 'mfree': mfree, 'stotal': stotal, 'sused': sused, 'sfree': sfree, 'liquid': c.dump_options_with_quotes()}) |
| 245 | + |
| 246 | + |
| 247 | +@app.route("/netio") |
| 248 | +def get_net_io_chart(): |
| 249 | + c = net_io_line() |
| 250 | + return c.dump_options_with_quotes() |
| 251 | + |
| 252 | +@app.route("/process") |
| 253 | +def get_process_tab(): |
| 254 | + c = process() |
| 255 | + return c |
| 256 | + |
| 257 | +@app.route("/delprocess") |
| 258 | +def del_process(): |
| 259 | + pid = request.args.get("pid") |
| 260 | + os.kill(int(pid), signal.SIGKILL) |
| 261 | + return jsonify({'status': 'OK'}) |
| 262 | + |
| 263 | +@app.route("/disk") |
| 264 | +def get_disk_chart(): |
| 265 | + total, used, free, c = disk_line() |
| 266 | + return jsonify({'total': total, 'used': used, 'free': free, 'line': c.dump_options_with_quotes()}) |
| 267 | + |
| 268 | +if __name__ == "__main__": |
| 269 | + app.run() |
| 270 | + # print(psutil.disk_partitions()) |
| 271 | + # print(psutil.disk_usage('/')) |
| 272 | + # while 1: |
| 273 | + # print(psutil.disk_io_counters()) |
| 274 | + # time.sleep(1) |
| 275 | + |
| 276 | + |
0 commit comments