提交 eacdbeb25817d536c08c6fe3c2e0a592e34f6fa8

作者 qianyingz
1 个父辈 13263e89

merge

... ... @@ -19,6 +19,7 @@ from app.util.component.StructuredPrint import StructurePrint
19 19 from app.util.component.PGUtil import PGUtil
20 20 import os
21 21 from app.modules.data.io.data_entry_center import data_entry_center
  22 +from app.modules.monitor.schedule import start_schedule
22 23
23 24 class JSONEncoder(_JSONEncoder):
24 25 """
... ... @@ -102,5 +103,7 @@ def create_app():
102 103
103 104 # 不检测https
104 105 os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
  106 +
  107 + # start_schedule()
105 108 return app
106 109
... ...
不能预览此文件类型
... ... @@ -7,7 +7,7 @@
7 7 from flasgger import swag_from
8 8 from flask import Blueprint
9 9 from app.util import BlueprintApi
10   -from . import monitor_info, monitoring, metrics, monitor_host_create, monitor_host_list, monitor_host_delete, monitor_host_edit
  10 +from . import monitoring, metrics, monitor_host_create, monitor_host_list, monitor_host_delete, monitor_host_edit
11 11
12 12
13 13 user_socket_list = []
... ... @@ -18,14 +18,14 @@ class Monitor(BlueprintApi):
18 18
19 19 bp = Blueprint("Monitor", __name__, url_prefix="/API/Monitor")
20 20
21   - @staticmethod
22   - @bp.route('/Info', methods=['GET'])
23   - @swag_from(monitor_info.Api.api_doc)
24   - def monitor_info():
25   - """
26   - 性能监控
27   - """
28   - return monitor_info.Api().result
  21 + # @staticmethod
  22 + # @bp.route('/Info', methods=['GET'])
  23 + # @swag_from(monitor_info.Api.api_doc)
  24 + # def monitor_info():
  25 + # """
  26 + # 性能监控
  27 + # """
  28 + # return monitor_info.Api().result
29 29
30 30 @staticmethod
31 31 @bp.route('/baseMonitoring', methods=['GET'])
... ...
  1 +import paramiko
  2 +from .models import MonitorHost, MonitorLog
  3 +import datetime
  4 +import math
  5 +import time
  6 +import uuid
  7 +from app.util.component.PGUtil import PGUtil
  8 +import configure
  9 +from app.util.component.StructuredPrint import StructurePrint
  10 +import traceback
  11 +
  12 +def background_job():
  13 + try:
  14 + # servers = [{'sid': 'src1', 'hostname': '172.26.99.160',
  15 + # 'username': 'monitor', 'password': '123456'},
  16 + # {'sid': 'src2', 'hostname': '172.26.60.100',
  17 + # 'username': 'root', 'password': 'DMap@123'}]
  18 +
  19 + cur_time = datetime.datetime.now()
  20 + time_stamp = cur_time.strftime(
  21 + "%Y-%m-%d %H:%M:%S")
  22 + struct_time = time.strptime(time_stamp, "%Y-%m-%d %H:%M:%S")
  23 + d_minu_stamp = math.floor(struct_time.tm_min/10)
  24 + f_minu_stamp = math.floor(struct_time.tm_min/5)
  25 +
  26 + sys_session = PGUtil.get_db_session(
  27 + configure.SQLALCHEMY_DATABASE_URI)
  28 + sys_ds = PGUtil.open_pg_data_source(
  29 + 0, configure.SQLALCHEMY_DATABASE_URI)
  30 +
  31 + hosts = sys_session.query(
  32 + MonitorHost.host, MonitorHost.user, MonitorHost.password, MonitorHost.type, MonitorHost.srcid)
  33 + servers = list(map(lambda host:
  34 + {'hostname': host.host, 'username': host.user,
  35 + 'password': host.password, 'type': host.type,
  36 + 'sid': host.srcid},
  37 + hosts))
  38 + logs = []
  39 + for info in servers:
  40 + try:
  41 + StructurePrint().print("schedule,host:{},user:{}".format(
  42 + info['hostname'], info['username']))
  43 + # 业务逻辑
  44 + client = paramiko.SSHClient()
  45 + client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
  46 + client.connect(hostname=info['hostname'],
  47 + username=info['username'], password=info['password'])
  48 +
  49 + # cpu
  50 + order = "top -b -n1 | sed -n '3p' | awk '{print $2}'"
  51 + stdin, stdout, stderr = client.exec_command(order)
  52 + cpu_usage = stdout.read().decode().split("\n")[0] # cpu使用率
  53 +
  54 + # 内存
  55 + order = "free -m | sed -n '2p' | awk '{print $2}'"
  56 + stdin, stdout, stderr = client.exec_command(order)
  57 + totalMem = stdout.read().decode().split("\n")[0] # 总内存
  58 +
  59 + order = "free -m | sed -n '2p' | awk '{print $7}'"
  60 + stdin, stdout, stderr = client.exec_command(order)
  61 + availableMem = stdout.read().decode().split("\n")[0] # 可用内存
  62 +
  63 + order = "free -m | sed -n '2p' | awk '{print $3}'"
  64 + stdin, stdout, stderr = client.exec_command(order)
  65 + usedMem = stdout.read().decode().split("\n")[0] # 已用内存
  66 +
  67 + # disk
  68 + 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}'"
  69 + stdin, stdout, stderr = client.exec_command(order)
  70 + totalDisk = int(stdout.read().decode().split("\n")
  71 + [0]) # 总磁盘空间,单位Mb
  72 +
  73 + 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}'"
  74 + stdin, stdout, stderr = client.exec_command(order)
  75 + usedDisk = int(stdout.read().decode().split("\n")
  76 + [0]) # 已使用磁盘空间,单位Mb
  77 +
  78 + # network
  79 + # 接收的字节数
  80 + rx_time = []
  81 + rx_bytes = []
  82 + tx_time = []
  83 + tx_bytes = []
  84 +
  85 + # 接收的字节数
  86 + order = "ifconfig | grep RX | grep -v 'errors'| awk -v total=0 '{total+=$5}END{print total}'"
  87 + i = 0
  88 + while i < 2:
  89 + i = i+1
  90 + stdin, stdout, stderr = client.exec_command(order)
  91 + rx_time.append(time.time())
  92 + rx_bytes.append(int(stdout.read().decode().split("\n")[0]))
  93 +
  94 + # 发送的字节数
  95 + order = "ifconfig | grep TX | grep -v 'errors'| awk -v total=0 '{total+=$5}END{print total}'"
  96 + i = 0
  97 + while i < 2:
  98 + i = i+1
  99 + stdin, stdout, stderr = client.exec_command(order)
  100 + tx_time.append(time.time())
  101 + tx_bytes.append(int(stdout.read().decode().split("\n")[0]))
  102 +
  103 + log_guid = uuid.uuid1().__str__()
  104 + monitor_log = MonitorLog(guid=log_guid,
  105 + server=info["hostname"],
  106 + time_stamp=cur_time,
  107 + cpu_usage=float(
  108 + "%.2f" % float(cpu_usage)),
  109 + total_mem=totalMem,
  110 + available_mem=availableMem,
  111 + used_mem=usedMem,
  112 + disk=totalDisk,
  113 + disk_usage=usedDisk,
  114 + net_recv=float("%.2f" % float((
  115 + rx_bytes[1] - rx_bytes[0])/(rx_time[1]-rx_time[0]))),
  116 + net_send=float("%.2f" % float((
  117 + tx_bytes[1] - tx_bytes[0])/(tx_time[1]-tx_time[0]))),
  118 + date_stamp=cur_time.strftime(
  119 + "%Y-%m-%d"),
  120 + hour_stamp=struct_time.tm_hour,
  121 + minu_stamp=struct_time.tm_min,
  122 + d_minu_stamp=1 if d_minu_stamp == 0 else d_minu_stamp,
  123 + f_minu_stamp=1 if f_minu_stamp == 0 else f_minu_stamp)
  124 +
  125 + logs.append(monitor_log)
  126 +
  127 + except Exception as e:
  128 + StructurePrint().print(e.__str__()+":" + traceback.format_exc(), "error")
  129 + sys_session.rollback()
  130 + sys_session.add_all(logs)
  131 + sys_session.commit()
  132 + except Exception as e2:
  133 + StructurePrint().print(e2.__str__()+":" + traceback.format_exc(), "error")
  134 + finally:
  135 + sys_session.rollback()
  136 + client.close()
  137 + if sys_session:
  138 + sys_session.close()
  139 + if sys_ds:
  140 + sys_ds.Destroy()
  141 +
  142 +
  143 +
  144 +def format_value(value):
  145 + # 1024*1024*1024
  146 + if value > 1_073_741_824:
  147 + value = "{}GB".format(format(value/1_073_741_824, '.1f'))
  148 + elif value > 1_048_576:
  149 + # 1024*1024
  150 + value = "{}MB".format(format(value / 1_048_576, '.1f'))
  151 + elif value > 1024:
  152 + value = "{}KB".format(format(value / 1024.0, '.1f'))
  153 + else:
  154 + value = "{}B".format(format(value, '.1f'))
  155 + return value
  156 +
  157 +
  158 +def Mb_format_value(value):
  159 + if value > 1024:
  160 + value = "{}GB".format(format(value/1024, '.1f'))
  161 + else:
  162 + value = "{}MB".format(format(value, '.1f'))
  163 + return value
... ...
1 1 # import schedule
2   -import os
  2 +from flask import json
3 3 import paramiko
  4 +from sqlalchemy.sql.sqltypes import JSON
4 5 from .models import MonitorHost, MonitorLog
5 6 import datetime
6 7 import math
... ... @@ -10,177 +11,44 @@ import schedule
10 11 from app.util.component.RunContinuous import run_continuously
11 12 from app.util.component.PGUtil import PGUtil
12 13 import configure
  14 +from app.util.component.StructuredPrint import StructurePrint
  15 +import traceback
13 16
14 17
15   -def background_job():
  18 +def pull_metric():
16 19 try:
17   - # print('Hello from the background thread')
18   - # base_dir = os.getcwd()
19   -
20   - # 命令文件
21   - # cmd_filepath = os.path.join(base_dir, "file", "monitor.txt")
22   - # cmd_file = open(cmd_filepath, "r")
23   - # cmd = cmd_file.read()
24   - # cmd_file.close()
25   - # servers = [{'sid': 'src1', 'hostname': '172.26.99.160',
26   - # 'username': 'monitor', 'password': '123456'},
27   - # {'sid': 'src2', 'hostname': '172.26.60.100',
28   - # 'username': 'root', 'password': 'DMap@123'}]
29   -
30   - cur_time = datetime.datetime.now()
31   - time_stamp = cur_time.strftime(
32   - "%Y-%m-%d %H:%M:%S")
33   - struct_time = time.strptime(time_stamp, "%Y-%m-%d %H:%M:%S")
34   - d_minu_stamp = math.floor(struct_time.tm_min/10)
35   - f_minu_stamp = math.floor(struct_time.tm_min/5)
36   -
  20 + # 获获取服务器信息
37 21 sys_session = PGUtil.get_db_session(
38 22 configure.SQLALCHEMY_DATABASE_URI)
39 23 sys_ds = PGUtil.open_pg_data_source(
40 24 0, configure.SQLALCHEMY_DATABASE_URI)
  25 +
  26 + #拉取服务器信息
41 27
42 28 hosts = sys_session.query(
43   - MonitorHost.host, MonitorHost.user, MonitorHost.password, MonitorHost.type, MonitorHost.srcid)
44   - servers = list(map(lambda host:
45   - {'hostname': host.host, 'username': host.user,
46   - 'password': host.password, 'type': host.type,
47   - 'sid': host.srcid},
48   - hosts))
49   - for info in servers:
50   - try:
51   - # 业务逻辑
52   - client = paramiko.SSHClient()
53   - client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
54   - client.connect(hostname=info['hostname'],
55   - username=info['username'], password=info['password'])
56   -
57   - # cpu
58   - order = "top -b -n1 | sed -n '3p' | awk '{print $2}'"
59   - stdin, stdout, stderr = client.exec_command(order)
60   - cpu_usage = stdout.read().decode().split("\n")[0] # cpu使用率
61   -
62   - # 内存
63   - order = "free -m | sed -n '2p' | awk '{print $2}'"
64   - stdin, stdout, stderr = client.exec_command(order)
65   - totalMem = stdout.read().decode().split("\n")[0] # 总内存
66   -
67   - order = "free -m | sed -n '2p' | awk '{print $7}'"
68   - stdin, stdout, stderr = client.exec_command(order)
69   - availableMem = stdout.read().decode().split("\n")[0] # 可用内存
70   -
71   - order = "free -m | sed -n '2p' | awk '{print $3}'"
72   - stdin, stdout, stderr = client.exec_command(order)
73   - usedMem = stdout.read().decode().split("\n")[0] # 已用内存
74   -
75   - # disk
76   - 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}'"
77   - stdin, stdout, stderr = client.exec_command(order)
78   - totalDisk = int(stdout.read().decode().split("\n")
79   - [0]) # 总磁盘空间,单位Mb
80   -
81   - 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}'"
82   - stdin, stdout, stderr = client.exec_command(order)
83   - usedDisk = int(stdout.read().decode().split("\n")
84   - [0]) # 已使用磁盘空间,单位Mb
85   -
86   - # network
87   - # 接收的字节数
88   - rx_time = []
89   - rx_bytes = []
90   - tx_time = []
91   - tx_bytes = []
92   -
93   - # 接收的字节数
94   - order = "ifconfig | grep RX | grep -v 'errors'| awk -v total=0 '{total+=$5}END{print total}'"
95   - i = 0
96   - while i < 2:
97   - i = i+1
98   - stdin, stdout, stderr = client.exec_command(order)
99   - rx_time.append(time.time())
100   - rx_bytes.append(int(stdout.read().decode().split("\n")[0]))
101   -
102   - # 发送的字节数
103   - order = "ifconfig | grep TX | grep -v 'errors'| awk -v total=0 '{total+=$5}END{print total}'"
104   - i = 0
105   - while i < 2:
106   - i = i+1
107   - stdin, stdout, stderr = client.exec_command(order)
108   - tx_time.append(time.time())
109   - tx_bytes.append(int(stdout.read().decode().split("\n")[0]))
110   -
111   - log_guid = uuid.uuid1().__str__()
112   - monitor_log = MonitorLog(guid=log_guid,
113   - server=info["hostname"],
114   - time_stamp=cur_time,
115   - cpu_usage=float(
116   - "%.2f" % float(cpu_usage)),
117   - total_mem=totalMem,
118   - available_mem=availableMem,
119   - used_mem=usedMem,
120   - disk=totalDisk,
121   - disk_usage=usedDisk,
122   - net_recv=float("%.2f" % float((
123   - rx_bytes[1] - rx_bytes[0])/(rx_time[1]-rx_time[0]))),
124   - net_send=float("%.2f" % float((
125   - tx_bytes[1] - tx_bytes[0])/(tx_time[1]-tx_time[0]))),
126   - date_stamp=cur_time.strftime(
127   - "%Y-%m-%d"),
128   - hour_stamp=struct_time.tm_hour,
129   - minu_stamp=struct_time.tm_min,
130   - d_minu_stamp=1 if d_minu_stamp == 0 else d_minu_stamp,
131   - f_minu_stamp=1 if f_minu_stamp == 0 else f_minu_stamp)
132   -
133   - sys_session.add(monitor_log)
134   - sys_session.commit()
135   -
136   - # for line in stdout:
137   - # data = json.loads(line)
138   - # # print(type(data))
139   - # print(data)
140   - except Exception as e:
141   - sys_session.rollback()
142   - except e:
143   - print('发生了异常:', e)
144   - finally:
145   - client.close()
146   - if sys_session:
147   - sys_session.close()
148   - if sys_ds:
149   - sys_ds.Destroy()
150   -
151   -
152   -# 记录30条记录
153   -logs = []
154   -
  29 + MonitorHost.host)
  30 + for host in hosts:
  31 + request_uri="http://{}".format(host.host)
  32 +
  33 +
  34 +
  35 + # 获取数据并汇聚为1min的数据入库
  36 + # 结束
  37 + pass
  38 + except Exception as e:
  39 + StructurePrint().print(e.__str__()+":" + traceback.format_exc(), "error")
  40 + pass
155 41
156 42 def start_schedule():
157   - # # 1分钟巡检一次
158   - schedule.every(2).minutes.do(background_job)
159   - # schedule.every(5).seconds.do(background_job)
160   - stop_run_continuously = run_continuously()
  43 + # # 2分钟巡检一次
  44 + try:
  45 + StructurePrint().print("start_schedule")
  46 + schedule.every(1).minutes.do(pull_metric)
  47 + stop_run_continuously = run_continuously()
  48 + except Exception as e:
  49 + StructurePrint().print(e.__str__()+":" + traceback.format_exc(), "error")
  50 +
161 51 # Do some other things...
162 52 # # Stop the background thread
163 53 # time.sleep(10)
164 54 # stop_run_continuously.set()
165   -
166   -
167   -def format_value(value):
168   - # 1024*1024*1024
169   - if value > 1_073_741_824:
170   - value = "{}GB".format(format(value/1_073_741_824, '.1f'))
171   - elif value > 1_048_576:
172   - # 1024*1024
173   - value = "{}MB".format(format(value / 1_048_576, '.1f'))
174   - elif value > 1024:
175   - value = "{}KB".format(format(value / 1024.0, '.1f'))
176   - else:
177   - value = "{}B".format(format(value, '.1f'))
178   - return value
179   -
180   -
181   -def Mb_format_value(value):
182   - if value > 1024:
183   - value = "{}GB".format(format(value/1024, '.1f'))
184   - else:
185   - value = "{}MB".format(format(value, '.1f'))
186   - return value
... ...
... ... @@ -4,11 +4,8 @@ import logging
4 4 deploy_ip_host = "172.26.40.105:8840"
5 5 # 系统数据库
6 6
7   -
8 7 # SQLALCHEMY_DATABASE_URI = "postgresql://postgres:chinadci@172.26.60.100:5432/dmap_manager_test"
9   -SQLALCHEMY_DATABASE_URI = "postgresql://postgres:chinadci@172.26.60.100:5432/dmap_manager"
10   -# SQLALCHEMY_DATABASE_URI = "postgresql://postgres:postgres@localhost:5433/dmap_dms_test"
11   -
  8 +SQLALCHEMY_DATABASE_URI = "postgresql://postgres:postgres@localhost:5433/dmap_dms_test"
12 9
13 10 # 指定精华表所在位置(必须为空间库),设置为None则存放在各自的实体库中
14 11 #VACUATE_DB_URI = None
... ...
... ... @@ -2,11 +2,9 @@
2 2 from flask import Flask
3 3 from app import create_app
4 4 import os
5   -from app.modules.monitor.schedule import start_schedule
6 5
7 6 os.environ['AUTHLIB_INSECURE_TRANSPORT'] = '1'
8 7 app: Flask = create_app()
9 8 if __name__ == '__main__':
10   - # start_schedule()
11 9 app.run(host="0.0.0.0", port="8840", threaded=True, debug=True)
12 10 # app.run(host="0.0.0.0", port="8840", threaded=True)
... ...
注册登录 后发表评论