def GetAd(self, get): try: return public.HttpGet( public.GetConfigValue('home') + '/Api/GetAD?name=' + get.name + '&soc=' + get.soc) except: return ''
def GetLocalIp(self): # 取本地外网IP try: filename = '/www/server/panel/data/iplist.txt' ipaddress = public.readFile(filename) if not ipaddress: try: import urllib2 except: import urllib as urllib2 urllib2 = urllib2.request url = 'http://pv.sohu.com/cityjson?ie=utf-8' opener = urllib2.urlopen(url) m_str = opener.read() if isinstance(m_str, bytes): ipaddress = re.search('\d+.\d+.\d+.\d+', m_str.decode('utf-8')).group(0) else: ipaddress = re.search('\d+.\d+.\d+.\d+', m_str).group(0) public.WriteFile(filename, ipaddress) c_ip = public.check_ip(ipaddress) if not c_ip: a, e = public.ExecShell("curl ifconfig.me") return a return ipaddress except: try: url = public.GetConfigValue('home') + '/Api/getIpAddress' return public.HttpGet(url) except: return public.GetHost()
def GetNginxStatus(self,get): process_cpu = {} worker = int(public.ExecShell("ps aux|grep nginx|grep 'worker process'|wc -l")[0])-1 workermen = int(public.ExecShell("ps aux|grep nginx|grep 'worker process'|awk '{memsum+=$6};END {print memsum}'")[0]) / 1024 for proc in psutil.process_iter(): if proc.name() == "nginx": self.GetProcessCpuPercent(proc.pid,process_cpu) time.sleep(0.5) #取Nginx负载状态 self.CheckStatusConf() result = public.HttpGet('http://127.0.0.1/nginx_status') tmp = result.split() data = {} if "request_time" in tmp: data['accepts'] = tmp[8] data['handled'] = tmp[9] data['requests'] = tmp[10] data['Reading'] = tmp[13] data['Writing'] = tmp[15] data['Waiting'] = tmp[17] else: data['accepts'] = tmp[9] data['handled'] = tmp[7] data['requests'] = tmp[8] data['Reading'] = tmp[11] data['Writing'] = tmp[13] data['Waiting'] = tmp[15] data['active'] = tmp[2] data['worker'] = worker data['workercpu'] = round(float(process_cpu["nginx"]),2) data['workermen'] = "%s%s" % (int(workermen), "MB") return data
def get_beta_logs(self,get): try: # data = json.loads(public.HttpGet(public.GetConfigValue('home') + '/api/panel/get_beta_logs_en')) data = json.loads(public.HttpGet('https://console.aapanel.com/api/panel/get_beta_logs_en')) return data except: return public.returnMsg(False,'AJAX_CONN_ERR')
def syncDate(self,get): time_str = public.HttpGet(public.GetConfigValue('home') + '/api/index/get_time') new_time = int(time_str) time_arr = time.localtime(new_time) date_str = time.strftime("%Y-%m-%d %H:%M:%S", time_arr) result = public.ExecShell('date -s "%s"' % date_str); public.WriteLog("TYPE_PANEL", "DATE_SUCCESS"); return public.returnMsg(True,"DATE_SUCCESS");
def GetPHPInfo(self,get): self.CheckPHPINFO(); sPath = public.GetConfigValue('setup_path') + '/phpinfo/' + get.version; public.ExecShell("rm -rf /www/server/phpinfo/*"); public.ExecShell("mkdir -p " + sPath); public.writeFile(sPath + '/phpinfo.php','<?php phpinfo(); ?>'); phpinfo = public.HttpGet('http://127.0.0.2/' + get.version + '/phpinfo.php'); os.system("rm -rf " + sPath); return phpinfo;
def GetPHPStatus(self,get): #取指定PHP版本的负载状态 self.CheckStatusConf(); version = get.version result = public.HttpGet('http://127.0.0.1/phpfpm_'+version+'_status?json') tmp = json.loads(result) fTime = time.localtime(int(tmp['start time'])) tmp['start time'] = time.strftime('%Y-%m-%d %H:%M:%S',fTime) return tmp
def get_beta_logs(self, get): try: data = json.loads( public.HttpGet( public.GetConfigValue('home') + '/api/panel/get_beta_logs')) return data except: return public.returnMsg(False, 'AJAX_CONN_ERR')
def GetApacheStatus(self): process_cpu = {} apacheconf = "%s/apache/conf/httpd.conf" % (self.setupPath) confcontent = public.readFile(apacheconf) rep = "#Include conf/extra/httpd-info.conf" if re.search(rep,confcontent): confcontent = re.sub(rep,"Include conf/extra/httpd-info.conf",confcontent) public.writeFile(apacheconf,confcontent) public.serviceReload() result = public.HttpGet('http://127.0.0.1/server-status?auto') workermen = int(public.ExecShell("ps aux|grep httpd|grep 'start'|awk '{memsum+=$6};END {print memsum}'")[0]) / 1024 for proc in psutil.process_iter(): if proc.name() == "httpd": self.GetProcessCpuPercent(proc.pid,process_cpu) time.sleep(0.5) data = {} # 计算启动时间 Uptime = int(re.search("ServerUptimeSeconds:\s+(.*)",result).group(1)) min = Uptime / 60 hours = min / 60 days = math.floor(hours / 24) hours = math.floor(hours - (days * 24)) min = math.floor(min - (days * 60 * 24) - (hours * 60)) #格式化重启时间 restarttime = re.search("RestartTime:\s+(.*)",result).group(1) rep = "\w+,\s([\w-]+)\s([\d\:]+)\sCST" date = re.search(rep,restarttime).group(1) timedetail = re.search(rep,restarttime).group(2) monthen = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"] n = 0 for m in monthen: if m in date: date = re.sub(m,str(n+1),date) n+=1 date = date.split("-") date = "%s-%s-%s" % (date[2],date[1],date[0]) reqpersec = re.search("ReqPerSec:\s+(.*)", result).group(1) if re.match("^\.", reqpersec): reqpersec = "%s%s" % (0,reqpersec) data["RestartTime"] = "%s %s" % (date,timedetail) data["UpTime"] = "%s day %s hour %s minute" % (str(int(days)),str(int(hours)),str(int(min))) data["TotalAccesses"] = re.search("Total Accesses:\s+(\d+)",result).group(1) data["TotalKBytes"] = re.search("Total kBytes:\s+(\d+)",result).group(1) data["ReqPerSec"] = round(float(reqpersec), 2) data["BusyWorkers"] = re.search("BusyWorkers:\s+(\d+)",result).group(1) data["IdleWorkers"] = re.search("IdleWorkers:\s+(\d+)",result).group(1) data["workercpu"] = round(float(process_cpu["httpd"]),2) data["workermem"] = "%s%s" % (int(workermen),"MB") return data
def GetPHPStatus(self,get): #取指定PHP版本的负载状态 try: self.CheckStatusConf() version = get.version result = public.HttpGet('http://127.0.0.1/phpfpm_'+version+'_status?json') tmp = json.loads(result) fTime = time.localtime(int(tmp['start time'])) tmp['start time'] = time.strftime('%Y-%m-%d %H:%M:%S',fTime) return tmp except Exception as ex: public.WriteLog('Get Info',"PHP load status acquisition failed: %s" % ex) return public.returnMsg(False,'PHP load status acquisition failed!')
def GetNginxStatus(self,get): #取Nginx负载状态 self.CheckStatusConf(); result = public.HttpGet('http://127.0.0.1/nginx_status') tmp = result.split() data = {} data['active'] = tmp[2] data['accepts'] = tmp[9] data['handled'] = tmp[7] data['requests'] = tmp[8] data['Reading'] = tmp[11] data['Writing'] = tmp[13] data['Waiting'] = tmp[15] return data
def GetNginxStatus(self, get): cpunum = int( public.ExecShell('cat /proc/cpuinfo |grep "processor"|wc -l')[0]) worker = int( public.ExecShell("ps aux|grep nginx|grep 'worker process'|wc -l") [0]) - 1 workercpu = round( float( public.ExecShell( "ps aux|grep nginx|grep 'worker process'|awk '{cpusum += $3};END {print cpusum}'" )[0]) / cpunum, 2) workermen = int( public.ExecShell( "ps aux|grep nginx|grep 'worker process'|awk '{memsum+=$6};END {print memsum}'" )[0]) / 1024 #取Nginx负载状态 self.CheckStatusConf() result = public.HttpGet('http://127.0.0.1/nginx_status') tmp = result.split() data = {} if "request_time" in tmp: data['accepts'] = tmp[8] data['handled'] = tmp[9] data['requests'] = tmp[10] data['Reading'] = tmp[13] data['Writing'] = tmp[15] data['Waiting'] = tmp[17] else: data['accepts'] = tmp[9] data['handled'] = tmp[7] data['requests'] = tmp[8] data['Reading'] = tmp[11] data['Writing'] = tmp[13] data['Waiting'] = tmp[15] data['active'] = tmp[2] data['worker'] = worker data['workercpu'] = workercpu data['workermen'] = "%s%s" % (workermen, "MB") return data
def php_conn_max(self, get): ret = [] php_count = self.return_php() if len(php_count) >= 1: for i in php_count: get.sName = i try: result = public.HttpGet('http://127.0.0.1/phpfpm_' + i + '_status?json') tmp = json.loads(result) php = {} php['version'] = i php['children'] = tmp['max children reached'] ret.append(php) except: php = {} php['version'] = i php['children'] = 'ERROR' ret.append(php) else: return ret else: return ret
def UpdatePanel(self,get): try: if not public.IsRestart(): return public.returnMsg(False,'EXEC_ERR_TASK'); import json if int(session['config']['status']) == 0: public.HttpGet(public.GetConfigValue('home')+'/Api/SetupCount?type=Linux'); public.M('config').where("id=?",('1',)).setField('status',1); #取回远程版本信息 if 'updateInfo' in session and hasattr(get,'check') == False: updateInfo = session['updateInfo']; else: login_temp = 'data/login.temp'; if os.path.exists(login_temp): logs = public.readFile(login_temp) os.remove(login_temp); else: logs = ''; import psutil,panelPlugin,system; mem = psutil.virtual_memory(); mplugin = panelPlugin.panelPlugin(); mplugin.ROWS = 10000; panelsys = system.system(); data = {} data['sites'] = str(public.M('sites').count()); data['ftps'] = str(public.M('ftps').count()); data['databases'] = str(public.M('databases').count()); data['system'] = panelsys.GetSystemVersion() + '|' + str(mem.total / 1024 / 1024) + 'MB|' + public.getCpuType() + '*' + str(psutil.cpu_count()) + '|' + public.get_webserver() + '|' +session['version']; data['system'] += '||'+self.GetInstalleds(mplugin.getPluginList(None)); data['logs'] = logs data['oem'] = '' data['intrusion'] = 0; msg = public.getMsg('PANEL_UPDATE_MSG'); sUrl = public.GetConfigValue('home') + '/api/panel/updateLinux'; updateInfo = json.loads(public.httpPost(sUrl,data)); if not updateInfo: return public.returnMsg(False,"CONNECT_ERR"); updateInfo['msg'] = msg; session['updateInfo'] = updateInfo; #检查是否需要升级 if updateInfo['version'] ==session['version']: try: return public.returnMsg(False,updateInfo['msg']); except: return public.returnMsg(False,'PANEL_UPDATE_ERR_NEW'); #是否执行升级程序 if(updateInfo['force'] == True or hasattr(get,'toUpdate') == True or os.path.exists('data/autoUpdate.pl') == True): setupPath = public.GetConfigValue('setup_path'); uptype = 'update'; httpUrl = public.get_url(); if httpUrl: updateInfo['downUrl'] = httpUrl + '/install/' + uptype + '/LinuxPanel-' + updateInfo['version'] + '.zip'; public.downloadFile(updateInfo['downUrl'],'panel.zip'); if os.path.getsize('panel.zip') < 1048576: return public.returnMsg(False,"PANEL_UPDATE_ERR_DOWN"); public.ExecShell('unzip -o panel.zip -d ' + setupPath + '/'); import compileall if os.path.exists('/www/server/panel/runserver.py'): public.ExecShell('rm -f /www/server/panel/*.pyc'); if os.path.exists('/www/server/panel/class/common.py'): public.ExecShell('rm -f /www/server/panel/class/*.pyc'); if os.path.exists('panel.zip'):os.remove("panel.zip") session['version'] = updateInfo['version'] if 'getCloudPlugin' in session: del(session['getCloudPlugin']); return public.returnMsg(True,'PANEL_UPDATE',(updateInfo['version'],)); #输出新版本信息 data = { 'status' : True, 'version': updateInfo['version'], 'updateMsg' : updateInfo['updateMsg'] }; public.ExecShell('rm -rf /www/server/phpinfo/*'); return data; except Exception as ex: return public.returnMsg(False,"CONNECT_ERR");
def UpdatePanel(self, get): try: if not public.IsRestart(): return public.returnMsg(False, 'EXEC_ERR_TASK') import json if int(session['config']['status']) == 0: public.HttpGet( public.GetConfigValue('home') + '/Api/SetupCount?type=Linux') public.M('config').where("id=?", ('1', )).setField('status', 1) #取回远程版本信息 if 'updateInfo' in session and hasattr(get, 'check') == False: updateInfo = session['updateInfo'] else: logs = '' import psutil, system, sys mem = psutil.virtual_memory() import panelPlugin mplugin = panelPlugin.panelPlugin() mplugin.ROWS = 10000 panelsys = system.system() data = {} data['sites'] = str(public.M('sites').count()) data['ftps'] = str(public.M('ftps').count()) data['databases'] = str(public.M('databases').count()) data['system'] = panelsys.GetSystemVersion() + '|' + str( mem.total / 1024 / 1024) + 'MB|' + str(public.getCpuType()) + '*' + str( psutil.cpu_count()) + '|' + str( public.get_webserver()) + '|' + session['version'] data['system'] += '||' + self.GetInstalleds( mplugin.getPluginList(None)) data['logs'] = logs data['client'] = request.headers.get('User-Agent') data['oem'] = '' data['intrusion'] = 0 data['uid'] = self.get_uid() #msg = public.getMsg('PANEL_UPDATE_MSG'); data['o'] = '' filename = '/www/server/panel/data/o.pl' if os.path.exists(filename): data['o'] = str(public.readFile(filename)) sUrl = public.GetConfigValue('home') + '/api/panel/updateLinux' updateInfo = json.loads(public.httpPost(sUrl, data)) if not updateInfo: return public.returnMsg(False, "CONNECT_ERR") #updateInfo['msg'] = msg; session['updateInfo'] = updateInfo #检查是否需要升级 if updateInfo['is_beta'] == 1: if updateInfo['beta']['version'] == session['version']: return public.returnMsg(False, updateInfo) else: if updateInfo['version'] == session['version']: return public.returnMsg(False, updateInfo) #是否执行升级程序 if (updateInfo['force'] == True or hasattr(get, 'toUpdate') == True or os.path.exists('data/autoUpdate.pl') == True): if updateInfo['is_beta'] == 1: updateInfo['version'] = updateInfo['beta']['version'] setupPath = public.GetConfigValue('setup_path') uptype = 'update' httpUrl = public.get_url() if httpUrl: updateInfo[ 'downUrl'] = httpUrl + '/install/' + uptype + '/LinuxPanel-' + updateInfo[ 'version'] + '.zip' public.downloadFile(updateInfo['downUrl'], 'panel.zip') if os.path.getsize('panel.zip') < 1048576: return public.returnMsg(False, "PANEL_UPDATE_ERR_DOWN") public.ExecShell('unzip -o panel.zip -d ' + setupPath + '/') import compileall if os.path.exists('/www/server/panel/runserver.py'): public.ExecShell('rm -f /www/server/panel/*.pyc') if os.path.exists('/www/server/panel/class/common.py'): public.ExecShell('rm -f /www/server/panel/class/*.pyc') if os.path.exists('panel.zip'): os.remove("panel.zip") session['version'] = updateInfo['version'] if 'getCloudPlugin' in session: del (session['getCloudPlugin']) if updateInfo['is_beta'] == 1: self.to_beta() public.ExecShell("/etc/init.d/bt start") public.writeFile('data/restart.pl', 'True') return public.returnMsg(True, 'PANEL_UPDATE', (updateInfo['version'], )) #输出新版本信息 data = { 'status': True, 'version': updateInfo['version'], 'updateMsg': updateInfo['updateMsg'] } public.ExecShell('rm -rf /www/server/phpinfo/*') return public.returnMsg(True, updateInfo) except Exception as ex: return public.returnMsg(False, "CONNECT_ERR")
def syncDate(self, get): dateStr = public.HttpGet( public.GetConfigValue('home') + '/api/index/get_date') result = public.ExecShell('date -s "%s"' % dateStr) public.WriteLog("TYPE_PANEL", "DATE_SUCCESS") return public.returnMsg(True, "DATE_SUCCESS")