diff --git a/class/core/system_api.py b/class/core/system_api.py index 17ed30071..7d8887495 100755 --- a/class/core/system_api.py +++ b/class/core/system_api.py @@ -386,6 +386,29 @@ class system_api: '/panel/' + scriptFile) return self.GetMemInfo() + def getNetWorkIo(self, start, end): + # 取指定时间段的网络Io + data = public.M('network').dbfile('system').where("addtime>=? AND addtime<=?", (start, end)).field( + 'id,up,down,total_up,total_down,down_packets,up_packets,addtime').order('id asc').select() + return self.ToAddtime(data) + + def getDiskIo(self, start, end): + # 取指定时间段的磁盘Io + data = public.M('diskio').dbfile('system').where("addtime>=? AND addtime<=?", (start, end)).field( + 'id,read_count,write_count,read_bytes,write_bytes,read_time,write_time,addtime').order('id asc').select() + return self.ToAddtime(data) + + def getCpuIo(self, start, end): + # 取指定时间段的CpuIo + data = public.M('cpuio').dbfile('system').where("addtime>=? AND addtime<=?", + (start, end)).field('id,pro,mem,addtime').order('id asc').select() + return self.ToAddtime(data, True) + + def getLoadAverage(self, start, end): + data = public.M('load_average').dbfile('system').where("addtime>=? AND addtime<=?", ( + start, end)).field('id,pro,one,five,fifteen,addtime').order('id asc').select() + return self.ToAddtime(data) + # 重启面板 def reWeb(self, get): # if not public.IsRestart(): return diff --git a/data/control.conf b/data/control.conf new file mode 100644 index 000000000..8580e7b68 --- /dev/null +++ b/data/control.conf @@ -0,0 +1 @@ +30 \ No newline at end of file diff --git a/data/sql/system.sql b/data/sql/system.sql index 2f581b401..06ef0a595 100755 --- a/data/sql/system.sql +++ b/data/sql/system.sql @@ -25,4 +25,13 @@ CREATE TABLE IF NOT EXISTS `diskio` ( `read_time` INTEGER, `write_time` INTEGER, `addtime` INTEGER +); + +CREATE TABLE IF NOT EXISTS `load_average` ( + `id` INTEGER PRIMARY KEY AUTOINCREMENT, + `pro` REAL, + `one` REAL, + `five` REAL, + `fifteen` REAL, + `addtime` INTEGER ); \ No newline at end of file diff --git a/data/system.db b/data/system.db index d01888a6f..07f7a363e 100644 Binary files a/data/system.db and b/data/system.db differ diff --git a/task.py b/task.py index edd4e7c18..7d72dc5e2 100755 --- a/task.py +++ b/task.py @@ -108,7 +108,6 @@ def WriteLogs(logMsg): def startTask(): # 任务队列 global isTask - print os.path.exists(isTask) try: while True: try: @@ -195,27 +194,26 @@ def siteEdate(): def systemTask(): # 系统监控任务 try: - import system + import system_api import psutil import time - sm = system.system() + sm = system_api.system_api() filename = 'data/control.conf' sql = db.Sql().dbfile('system') - csql = '''CREATE TABLE IF NOT EXISTS `load_average` ( - `id` INTEGER PRIMARY KEY AUTOINCREMENT, - `pro` REAL, - `one` REAL, - `five` REAL, - `fifteen` REAL, - `addtime` INTEGER -)''' - sql.execute(csql, ()) + csql = public.readFile('data/sql/system.sql') + + csql_list = csql.split(';') + for index in range(len(csql_list)): + sql.execute(csql_list[index], ()) + cpuIo = cpu = {} cpuCount = psutil.cpu_count() used = count = 0 reloadNum = 0 network_up = network_down = diskio_1 = diskio_2 = networkInfo = cpuInfo = diskInfo = None + print 'task init' while True: + print 'task init 1' if not os.path.exists(filename): time.sleep(10) continue @@ -234,11 +232,11 @@ def systemTask(): tmp['used'] = psutil.cpu_percent(interval=1) if not cpuInfo: - tmp['mem'] = GetMemUsed() + tmp['mem'] = getMemUsed() cpuInfo = tmp if cpuInfo['used'] < tmp['used']: - tmp['mem'] = GetMemUsed() + tmp['mem'] = getMemUsed() cpuInfo = tmp # 取当前网络Io @@ -257,38 +255,39 @@ def systemTask(): network_up = networkIo[0] network_down = networkIo[1] + print "tmp:", tmp + if not networkInfo: networkInfo = tmp if (tmp['up'] + tmp['down']) > (networkInfo['up'] + networkInfo['down']): networkInfo = tmp - # 取磁盘Io - if os.path.exists('/proc/diskstats'): - diskio_2 = psutil.disk_io_counters() - if not diskio_1: - diskio_1 = diskio_2 - tmp = {} - tmp['read_count'] = diskio_2.read_count - diskio_1.read_count - tmp['write_count'] = diskio_2.write_count - diskio_1.write_count - tmp['read_bytes'] = diskio_2.read_bytes - diskio_1.read_bytes - tmp['write_bytes'] = diskio_2.write_bytes - diskio_1.write_bytes - tmp['read_time'] = diskio_2.read_time - diskio_1.read_time - tmp['write_time'] = diskio_2.write_time - diskio_1.write_time - - if not diskInfo: - diskInfo = tmp - else: - diskInfo['read_count'] += tmp['read_count'] - diskInfo['write_count'] += tmp['write_count'] - diskInfo['read_bytes'] += tmp['read_bytes'] - diskInfo['write_bytes'] += tmp['write_bytes'] - diskInfo['read_time'] += tmp['read_time'] - diskInfo['write_time'] += tmp['write_time'] - + # if os.path.exists('/proc/diskstats'): + diskio_2 = psutil.disk_io_counters() + if not diskio_1: diskio_1 = diskio_2 + tmp = {} + tmp['read_count'] = diskio_2.read_count - diskio_1.read_count + tmp['write_count'] = diskio_2.write_count - diskio_1.write_count + tmp['read_bytes'] = diskio_2.read_bytes - diskio_1.read_bytes + tmp['write_bytes'] = diskio_2.write_bytes - diskio_1.write_bytes + tmp['read_time'] = diskio_2.read_time - diskio_1.read_time + tmp['write_time'] = diskio_2.write_time - diskio_1.write_time + + if not diskInfo: + diskInfo = tmp + else: + diskInfo['read_count'] += tmp['read_count'] + diskInfo['write_count'] += tmp['write_count'] + diskInfo['read_bytes'] += tmp['read_bytes'] + diskInfo['write_bytes'] += tmp['write_bytes'] + diskInfo['read_time'] += tmp['read_time'] + diskInfo['write_time'] += tmp['write_time'] + diskio_1 = diskio_2 + + print "disk:", diskInfo, count # print diskInfo - if count >= 12: try: addtime = int(time.time()) @@ -313,7 +312,7 @@ def systemTask(): "addtime 100: @@ -328,16 +327,17 @@ def systemTask(): diskInfo = None count = 0 reloadNum += 1 + print "end---|" if reloadNum > 1440: - if os.path.exists('data/ssl.pl'): - os.system( - '/etc/init.d/bt restart > /dev/null 2>&1') reloadNum = 0 + # if os.path.exists('data/ssl.pl'): + # os.system( + # '/etc/init.d/bt restart > /dev/null 2>&1') except Exception, ex: print str(ex) del(tmp) - time.sleep(5) + time.sleep(1) count += 1 except Exception, ex: print str(ex) @@ -346,7 +346,7 @@ def systemTask(): systemTask() -def GetMemUsed(): +def getMemUsed(): # 取内存使用率 try: import psutil @@ -360,10 +360,22 @@ def GetMemUsed(): except: return 1 -# 检查502错误 + +def getLoadAverage(): + import psutil + c = os.getloadavg() + data = {} + data['one'] = float(c[0]) + data['five'] = float(c[1]) + data['fifteen'] = float(c[2]) + data['max'] = psutil.cpu_count() * 2 + data['limit'] = data['max'] + data['safe'] = data['max'] * 0.75 + return data def check502(): + # 检查502错误 try: phpversions = ['53', '54', '55', '56', '70', '71', '72'] for version in phpversions: @@ -526,10 +538,10 @@ if __name__ == "__main__": # }''' # public.writeFile(pfile, pconf) - # import threading - # t = threading.Thread(target=systemTask) - # t.setDaemon(True) - # t.start() + import threading + t = threading.Thread(target=systemTask) + t.setDaemon(True) + t.start() # p = threading.Thread(target=check502Task) # p.setDaemon(True)