提交 170b08aa22d3331ff55227138804467386461b1b

作者 nheweijun
2 个父辈 237b014b 173768a3
1 # coding=utf-8 1 # coding=utf-8
2 -#author: 4N 2 +# author: 4N
3 #createtime: 2021/5/18 3 #createtime: 2021/5/18
4 #email: nheweijun@sina.com 4 #email: nheweijun@sina.com
5 5
@@ -8,7 +8,9 @@ from flasgger import swag_from @@ -8,7 +8,9 @@ from flasgger import swag_from
8 from flask import Blueprint 8 from flask import Blueprint
9 from app.util import BlueprintApi 9 from app.util import BlueprintApi
10 10
11 -from . import monitor_info 11 +from . import monitor_info, monitoring
  12 +
  13 +
12 class Monitor(BlueprintApi): 14 class Monitor(BlueprintApi):
13 15
14 bp = Blueprint("Monitor", __name__, url_prefix="/API/Monitor") 16 bp = Blueprint("Monitor", __name__, url_prefix="/API/Monitor")
@@ -20,4 +22,13 @@ class Monitor(BlueprintApi): @@ -20,4 +22,13 @@ class Monitor(BlueprintApi):
20 """ 22 """
21 性能监控 23 性能监控
22 """ 24 """
23 - return monitor_info.Api().result  
  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
  1 +from app.models import *
  2 +from app.util.component.ApiTemplate import ApiTemplate
  3 +import paramiko
  4 +import time
  5 +
  6 +
  7 +class Api(ApiTemplate):
  8 + api_name = "远程监控"
  9 +
  10 + def process(self):
  11 +
  12 + # 返回结果
  13 + res = {}
  14 + res["data"] = {}
  15 + try:
  16 + # 业务逻辑
  17 + client = paramiko.SSHClient()
  18 + client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
  19 + client.connect(hostname='172.26.60.100',
  20 + username='root', password='DMap@123')
  21 + # cpu
  22 + order = "top -b -n1 | sed -n '3p' | awk '{print $2}'"
  23 + stdin, stdout, stderr = client.exec_command(order)
  24 + cpu_usage = stdout.read().decode().split("\n")[0] # cpu使用率
  25 +
  26 + # 内存
  27 + order = "free -h | sed -n '2p' | awk '{print $2}'"
  28 + stdin, stdout, stderr = client.exec_command(order)
  29 + totalMem = stdout.read().decode().split("\n")[0] # 总内存
  30 +
  31 + order = "free -h | sed -n '3p' | awk '{print $3}'"
  32 + stdin, stdout, stderr = client.exec_command(order)
  33 + freeMem = stdout.read().decode().split("\n")[0] # 空余内存
  34 +
  35 + # disk
  36 + 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}'"
  37 + stdin, stdout, stderr = client.exec_command(order)
  38 + totalDisk = int(stdout.read().decode().split("\n")
  39 + [0]) # 总磁盘空间,单位Mb
  40 +
  41 + 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}'"
  42 + stdin, stdout, stderr = client.exec_command(order)
  43 + usedDisk = int(stdout.read().decode().split("\n")
  44 + [0]) # 已使用磁盘空间,单位Mb
  45 +
  46 + # network
  47 + # 接收的字节数
  48 + rx_time = []
  49 + rx_bytes = []
  50 + tx_time = []
  51 + tx_bytes = []
  52 +
  53 + # 接收的字节数
  54 + order = "ifconfig | grep RX | grep -v 'errors'| awk -v total=0 '{total+=$5}END{print total}'"
  55 + i = 0
  56 + while i < 2:
  57 + i = i+1
  58 + stdin, stdout, stderr = client.exec_command(order)
  59 + rx_time.append(time.time())
  60 + rx_bytes.append(int(stdout.read().decode().split("\n")[0]))
  61 +
  62 + # 发送的字节数
  63 + order = "ifconfig | grep TX | grep -v 'errors'| awk -v total=0 '{total+=$5}END{print total}'"
  64 + i = 0
  65 + while i < 2:
  66 + i = i+1
  67 + stdin, stdout, stderr = client.exec_command(order)
  68 + tx_time.append(time.time())
  69 + tx_bytes.append(int(stdout.read().decode().split("\n")[0]))
  70 +
  71 + res["data"] = {
  72 + "cpuUsage": "{}%".format(cpu_usage),
  73 + "totalMemory": "{}".format(totalMem),
  74 + "freeMemory": "{}".format(freeMem),
  75 + "totalDisk": "{}".format(self.format_value(totalDisk*1024**2)),
  76 + "usedDisk": "{}".format(self.format_value(usedDisk*1024**2)),
  77 + "networkRecv": "{}".format(self.format_value((rx_bytes[1] - rx_bytes[0])/(rx_time[1]-rx_time[0]))),
  78 + "networkSend": "{}".format(self.format_value((tx_bytes[1] - tx_bytes[0])/(tx_time[1]-tx_time[0])))
  79 + }
  80 +
  81 + res["result"] = True
  82 + except Exception as e:
  83 + raise e
  84 + finally:
  85 + client.close()
  86 + return res
  87 +
  88 + api_doc = {
  89 +
  90 + "tags": ["监控接口"],
  91 + "parameters": [
  92 + ],
  93 + "responses": {
  94 + 200: {
  95 + "schema": {
  96 + "properties": {
  97 + }
  98 + }
  99 + }
  100 + }
  101 + }
  102 +
  103 + def format_value(self, value):
  104 + if value > 1024**3:
  105 + value = "{}GB".format(format(value/1024.0**3, '.1f'))
  106 + elif value > 1024**2:
  107 + value = "{}MB".format(format(value / 1024.0 ** 2, '.1f'))
  108 + elif value > 1024:
  109 + value = "{}KB".format(format(value / 1024.0, '.1f'))
  110 + else:
  111 + value = "{}B".format(format(value, '.1f'))
  112 + return value
  113 +
  114 +
  115 +if __name__ == '__main__':
  116 + api = Api()
  117 + api.process()
@@ -4,7 +4,8 @@ import logging @@ -4,7 +4,8 @@ import logging
4 deploy_ip_host = "172.26.40.105:8840" 4 deploy_ip_host = "172.26.40.105:8840"
5 # 系统数据库 5 # 系统数据库
6 6
7 -SQLALCHEMY_DATABASE_URI = "postgresql://postgres:chinadci@172.26.60.100:5432/dmap_manager" 7 +SQLALCHEMY_DATABASE_URI = "postgresql://postgres:chinadci@172.26.99.160:5432/dmap_dms_test"
  8 +# SQLALCHEMY_DATABASE_URI = "postgresql://postgres:postgres@localhost:5433/dmap_dms_test"
8 9
9 # 指定精华表所在位置(必须为空间库),设置为None则存放在各自的实体库中 10 # 指定精华表所在位置(必须为空间库),设置为None则存放在各自的实体库中
10 #VACUATE_DB_URI = None 11 #VACUATE_DB_URI = None
@@ -13,7 +13,8 @@ Pillow==8.1.2 @@ -13,7 +13,8 @@ Pillow==8.1.2
13 #Rtree==0.9.7 13 #Rtree==0.9.7
14 opencv-python==4.5.1.48 14 opencv-python==4.5.1.48
15 psutil==5.8.0 15 psutil==5.8.0
16 -mod_wsgi==4.8.0 16 +# mod_wsgi==4.8.0
17 thrift==0.13.0 17 thrift==0.13.0
18 Authlib==0.13 18 Authlib==0.13
19 kazoo==2.8.0 19 kazoo==2.8.0
  20 +paramiko==2.8.0
注册登录 后发表评论