正在显示
2 个修改的文件
包含
133 行增加
和
3 行删除
| 1 | 1 | # coding=utf-8 |
| 2 | -#author: 4N | |
| 2 | +# author: 4N | |
| 3 | 3 | #createtime: 2021/5/18 |
| 4 | 4 | #email: nheweijun@sina.com |
| 5 | 5 | |
| ... | ... | @@ -8,7 +8,9 @@ from flasgger import swag_from |
| 8 | 8 | from flask import Blueprint |
| 9 | 9 | from app.util import BlueprintApi |
| 10 | 10 | |
| 11 | -from . import monitor_info | |
| 11 | +from . import monitor_info, monitoring | |
| 12 | + | |
| 13 | + | |
| 12 | 14 | class Monitor(BlueprintApi): |
| 13 | 15 | |
| 14 | 16 | bp = Blueprint("Monitor", __name__, url_prefix="/API/Monitor") |
| ... | ... | @@ -20,4 +22,13 @@ class Monitor(BlueprintApi): |
| 20 | 22 | """ |
| 21 | 23 | 性能监控 |
| 22 | 24 | """ |
| 23 | - return monitor_info.Api().result | |
| \ No newline at end of file | ||
| 25 | + return monitor_info.Api().result | |
| 26 | + | |
| 27 | + @staticmethod | |
| 28 | + @bp.route('/baseMonitoring', methods=['GET']) | |
| 29 | + @swag_from(monitoring.Api.api_doc) | |
| 30 | + def monitoring(): | |
| 31 | + """ | |
| 32 | + 基础监控 | |
| 33 | + """ | |
| 34 | + return monitoring.Api().result | ... | ... |
app/modules/monitor/monitoring.py
0 → 100644
| 1 | +from os import cpu_count, read | |
| 2 | +from app.models import * | |
| 3 | +from app.util.component.ApiTemplate import ApiTemplate | |
| 4 | +import paramiko | |
| 5 | +import re | |
| 6 | +import time | |
| 7 | + | |
| 8 | + | |
| 9 | +class Api(ApiTemplate): | |
| 10 | + api_name = "远程监控" | |
| 11 | + | |
| 12 | + def process(self): | |
| 13 | + | |
| 14 | + # 返回结果 | |
| 15 | + res = {} | |
| 16 | + res["data"] = {} | |
| 17 | + try: | |
| 18 | + # 业务逻辑 | |
| 19 | + client = paramiko.SSHClient() | |
| 20 | + client.set_missing_host_key_policy(paramiko.AutoAddPolicy) | |
| 21 | + client.connect(hostname='172.26.60.100', | |
| 22 | + username='root', password='DMap@123') | |
| 23 | + # cpu | |
| 24 | + order = "top -b -n1 | sed -n '3p' | awk '{print $2}'" | |
| 25 | + stdin, stdout, stderr = client.exec_command(order) | |
| 26 | + cpu_usage = stdout.read().decode().split("\n")[0] # cpu使用率 | |
| 27 | + | |
| 28 | + # 内存 | |
| 29 | + order = "free -h | sed -n '2p' | awk '{print $2}'" | |
| 30 | + stdin, stdout, stderr = client.exec_command(order) | |
| 31 | + totalMem = stdout.read().decode().split("\n")[0] # 总内存 | |
| 32 | + | |
| 33 | + order = "free -h | sed -n '3p' | awk '{print $3}'" | |
| 34 | + stdin, stdout, stderr = client.exec_command(order) | |
| 35 | + freeMem = stdout.read().decode().split("\n")[0] # 空余内存 | |
| 36 | + | |
| 37 | + # disk | |
| 38 | + order = "df -m | grep -v 'overlay\|Filesystem' | awk '{print $1,$2,$3}' | grep /dev | awk '{print $2}' | awk -v total=0 '{total+=$1}END{print total}'" | |
| 39 | + stdin, stdout, stderr = client.exec_command(order) | |
| 40 | + totalDisk = int(stdout.read().decode().split("\n") | |
| 41 | + [0]) # 总磁盘空间,单位Mb | |
| 42 | + | |
| 43 | + order = "df -m | grep -v 'overlay\|Filesystem' | awk '{print $1,$2,$3}' | grep /dev | awk '{print $3}' | awk -v total=0 '{total+=$1}END{print total}'" | |
| 44 | + stdin, stdout, stderr = client.exec_command(order) | |
| 45 | + usedDisk = int(stdout.read().decode().split("\n") | |
| 46 | + [0]) # 已使用磁盘空间,单位Mb | |
| 47 | + | |
| 48 | + # network | |
| 49 | + # 接收的字节数 | |
| 50 | + rx_time = [] | |
| 51 | + rx_bytes = [] | |
| 52 | + tx_time = [] | |
| 53 | + tx_bytes = [] | |
| 54 | + | |
| 55 | + # 接收的字节数 | |
| 56 | + order = "ifconfig | grep RX | grep -v 'errors'| awk -v total=0 '{total+=$5}END{print total}'" | |
| 57 | + i = 0 | |
| 58 | + while i < 2: | |
| 59 | + i = i+1 | |
| 60 | + stdin, stdout, stderr = client.exec_command(order) | |
| 61 | + rx_time.append(time.time()) | |
| 62 | + rx_bytes.append(int(stdout.read().decode().split("\n")[0])) | |
| 63 | + | |
| 64 | + # 发送的字节数 | |
| 65 | + order = "ifconfig | grep TX | grep -v 'errors'| awk -v total=0 '{total+=$5}END{print total}'" | |
| 66 | + i = 0 | |
| 67 | + while i < 2: | |
| 68 | + i = i+1 | |
| 69 | + stdin, stdout, stderr = client.exec_command(order) | |
| 70 | + tx_time.append(time.time()) | |
| 71 | + tx_bytes.append(int(stdout.read().decode().split("\n")[0])) | |
| 72 | + | |
| 73 | + res["data"] = { | |
| 74 | + "cpuUsage": "{}%".format(cpu_usage), | |
| 75 | + "totalMemory": "{}".format(totalMem), | |
| 76 | + "freeMemory": "{}".format(freeMem), | |
| 77 | + "totalDisk": "{}".format(self.format_value(totalDisk*1024**2)), | |
| 78 | + "usedDisk": "{}".format(self.format_value(usedDisk*1024**2)), | |
| 79 | + "networkRecv": "{}".format(self.format_value((rx_bytes[1] - rx_bytes[0])/(rx_time[1]-rx_time[0]))), | |
| 80 | + "networkSend": "{}".format(self.format_value((tx_bytes[1] - tx_bytes[0])/(tx_time[1]-tx_time[0]))) | |
| 81 | + } | |
| 82 | + | |
| 83 | + res["result"] = True | |
| 84 | + except Exception as e: | |
| 85 | + raise e | |
| 86 | + finally: | |
| 87 | + client.close() | |
| 88 | + return res | |
| 89 | + | |
| 90 | + api_doc = { | |
| 91 | + | |
| 92 | + "tags": ["监控接口"], | |
| 93 | + "parameters": [ | |
| 94 | + ], | |
| 95 | + "responses": { | |
| 96 | + 200: { | |
| 97 | + "schema": { | |
| 98 | + "properties": { | |
| 99 | + } | |
| 100 | + } | |
| 101 | + } | |
| 102 | + } | |
| 103 | + } | |
| 104 | + | |
| 105 | + def format_value(self, value): | |
| 106 | + if value > 1024**3: | |
| 107 | + value = "{}GB".format(format(value/1024.0**3, '.1f')) | |
| 108 | + elif value > 1024**2: | |
| 109 | + value = "{}MB".format(format(value / 1024.0 ** 2, '.1f')) | |
| 110 | + elif value > 1024: | |
| 111 | + value = "{}KB".format(format(value / 1024.0, '.1f')) | |
| 112 | + else: | |
| 113 | + value = "{}B".format(format(value, '.1f')) | |
| 114 | + return value | |
| 115 | + | |
| 116 | + | |
| 117 | +if __name__ == '__main__': | |
| 118 | + api = Api() | |
| 119 | + api.process() | ... | ... |
请
注册
或
登录
后发表评论