From 18e59ef377f064f62b5b890743797975c78a5954 Mon Sep 17 00:00:00 2001 From: Mr Chen Date: Sat, 2 Nov 2024 16:16:31 +0800 Subject: [PATCH] up --- web/core/db.py | 24 +++++++----- web/core/mw.py | 4 +- web/utils/system/__init__.py | 2 +- web/utils/system/main.py | 17 +++++++++ web/utils/system/monitor.py | 72 +++++++++++++++++++++++++++++++++++- 5 files changed, 105 insertions(+), 14 deletions(-) diff --git a/web/core/db.py b/web/core/db.py index 10bddcc09..e5c1107a8 100755 --- a/web/core/db.py +++ b/web/core/db.py @@ -12,11 +12,18 @@ # sqlite3操作 # --------------------------------------------------------------------------------- - -import sqlite3 import os import sys +import sqlite3 +import core.mw as mw + +def getPanelDir(): + return os.path.dirname(os.getcwd()) + +def getTracebackInfo(): + import traceback + return traceback.format_exc() class Sql(): #------------------------------ @@ -33,7 +40,7 @@ class Sql(): __OPT_PARAM = () # where值 def __init__(self): - self.__DB_FILE = '../data/default.db' + self.__DB_FILE = getPanelDir()+'/data/panel.db' def __getConn(self): # 取数据库对象 @@ -42,7 +49,7 @@ class Sql(): self.__DB_CONN = sqlite3.connect(self.__DB_FILE) self.__DB_CONN.text_factory = str except Exception as ex: - # print(mw.getTracebackInfo()) + print(getTracebackInfo()) return "error: " + str(ex) def changeTextFactoryToBytes(self): @@ -51,14 +58,12 @@ class Sql(): def autoTextFactory(self): if sys.version_info[0] == 3: - self.__DB_CONN.text_factory = lambda x: str( - x, encoding="utf-8", errors='ignore') + self.__DB_CONN.text_factory = lambda x: str(x, encoding="utf-8", errors='ignore') else: - self.__DB_CONN.text_factory = lambda x: unicode( - x, "utf-8", "ignore") + self.__DB_CONN.text_factory = lambda x: unicode(x, "utf-8", "ignore") def dbfile(self, name): - self.__DB_FILE = 'data/' + name + '.db' + self.__DB_FILE = getPanelDir()+'/data/' + name + '.db' return self def dbPos(self, path, name, suffix_name = 'db'): @@ -192,7 +197,6 @@ class Sql(): def getField(self, keyName): # 取回指定字段 result = self.field(keyName).select() - # print(result) if len(result) == 1: return result[0][keyName] return result diff --git a/web/core/mw.py b/web/core/mw.py index 94456abfb..2350a22e7 100644 --- a/web/core/mw.py +++ b/web/core/mw.py @@ -584,9 +584,11 @@ def writeDbLog(stype, msg, args=(), uid=1): return False -def M(table): +def M(table=''): import core.db as db sql = db.Sql() + if table == '': + return sql return sql.table(table) diff --git a/web/utils/system/__init__.py b/web/utils/system/__init__.py index 7f612c99c..d631fb713 100644 --- a/web/utils/system/__init__.py +++ b/web/utils/system/__init__.py @@ -11,4 +11,4 @@ from .main import * from .query import * from .stats import * -from .monitor import * \ No newline at end of file +from .monitor import monitor \ No newline at end of file diff --git a/web/utils/system/main.py b/web/utils/system/main.py index 4e7b8a7b4..f2a29203b 100644 --- a/web/utils/system/main.py +++ b/web/utils/system/main.py @@ -150,4 +150,21 @@ def getMemInfo(): memInfo['memRealUsed'] = memInfo['memTotal'] - memInfo['memFree'] - memInfo['memBuffers'] - memInfo['memCached'] return memInfo +def getMemUsed(): + # 取内存使用率 + try: + import psutil + mem = psutil.virtual_memory() + + if mw.getOs() == 'darwin': + return mem.percent + + memInfo = {'memTotal': mem.total / 1024 / 1024, 'memFree': mem.free / 1024 / 1024, + 'memBuffers': mem.buffers / 1024 / 1024, 'memCached': mem.cached / 1024 / 1024} + tmp = memInfo['memTotal'] - memInfo['memFree'] - \ + memInfo['memBuffers'] - memInfo['memCached'] + tmp1 = memInfo['memTotal'] / 100 + return (tmp / tmp1) + except Exception as ex: + return 1 diff --git a/web/utils/system/monitor.py b/web/utils/system/monitor.py index 1abb5db39..45dd57912 100644 --- a/web/utils/system/monitor.py +++ b/web/utils/system/monitor.py @@ -14,14 +14,82 @@ import re import time import math import psutil +import threading + + +from .main import getMemUsed import core.mw as mw +import core.db as db + +# 监控系统数据入库 class monitor: - def initDBFile(): - pass + _dbfile = mw.getPanelDataDir() + '/system.db' + _diskinfo = None + + def __init__(self): + pass + + # lock + _instance_lock = threading.Lock() + + @classmethod + def instance(cls, *args, **kwargs): + if not hasattr(monitor, "_instance"): + with monitor._instance_lock: + if not hasattr(monitor, "_instance"): + monitor._instance = monitor(*args, **kwargs) + return monitor._instance + + def initDBFile(self): + if os.path.exists(self._dbfile): + return True + sql_file = mw.getPanelDataDir() + '/sql/system.sql' + sql = db.Sql().dbPos(mw.getPanelDataDir(),'system') + csql = mw.readFile(sql_file) + csql_list = csql.split(';') + for index in range(len(csql_list)): + sql.execute(csql_list[index], ()) + return True + + # def safeRun(self): + # self.initDBFile() + + # monitor_day =mw.M('option').field('name').where('name=?',('monitor_day',)).getField('value') + # print(monitor_day) + # print("monitor data", mw.formatDate()) + + def getDiskInfo(self): + if self._diskinfo is None: + self._diskinfo = psutil.disk_io_counters() + + def run(self): + self.initDBFile() + monitor_day = mw.M('option').field('name').where('name=?',('monitor_day',)).getField('value') + print(monitor_day) + print("monitor data", mw.formatDate()) + + now_info = {} + # 取当前CPU Io + now_info['used'] = psutil.cpu_percent(interval=1) + now_info['mem'] = getMemUsed() + + network_io = psutil.net_io_counters()[:4] + diskio = psutil.disk_io_counters() + + print(network_io) + print(now_info) + print(diskio) + # if tmp['used'] > 80: + # panel_title = mw.getConfig('title') + # ip = mw.getHostAddr() + # now_time = mw.getDateFromNow() + # msg = now_time + '|节点[' + panel_title + ':' + ip + \ + # ']处于高负载[' + str(tmp['used']) + '],请排查原因!' + # mw.notifyMessage(msg, '面板监控', 600)