mirror of
https://github.com/midoks/mdserver-web.git
synced 2026-10-18 23:45:52 +08:00
up
This commit is contained in:
+14
-10
@@ -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
|
||||
|
||||
+3
-1
@@ -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)
|
||||
|
||||
|
||||
|
||||
@@ -11,4 +11,4 @@
|
||||
from .main import *
|
||||
from .query import *
|
||||
from .stats import *
|
||||
from .monitor import *
|
||||
from .monitor import monitor
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user