| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| import public, os, re, shutil, math, psutil, time |
|
|
| os.chdir("/www/server/panel") |
|
|
|
|
| class apache: |
| setupPath = '/www/server' |
| apachedefaultfile = "%s/apache/conf/extra/httpd-default.conf" % (setupPath) |
| apachempmfile = "%s/apache/conf/extra/httpd-mpm.conf" % (setupPath) |
| apachedefaultfile_backup = '/tmp/apdefault_file_bk.conf' |
| apachempmfile_backup = '/tmp/apmpm_file_bk.conf' |
|
|
| def GetProcessCpuPercent(self, i, process_cpu): |
| try: |
| pp = psutil.Process(i) |
| if pp.name() not in process_cpu.keys(): |
| process_cpu[pp.name()] = float(pp.cpu_percent(interval=0.1)) |
| process_cpu[pp.name()] += float(pp.cpu_percent(interval=0.1)) |
| except: |
| pass |
|
|
| def GetApacheStatus(self): |
| process_cpu = {} |
| apacheconf = "%s/apache/conf/httpd.conf" % (self.setupPath) |
| confcontent = public.readFile(apacheconf) |
| if not confcontent: |
| return public.returnMsg(False, "获取配置文件错误") |
| 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') |
| try: |
| workermen = int( |
| public.ExecShell("ps aux|grep httpd|grep 'start'|awk '{memsum+=$6};END {print memsum}'")[0]) / 1024 |
| except: |
| return public.returnMsg(False, "获取内存错误") |
| for proc in psutil.process_iter(): |
| if proc.name() == "httpd": |
| self.GetProcessCpuPercent(proc.pid, process_cpu) |
| time.sleep(0.5) |
|
|
| data = {} |
| |
| try: |
| Uptime = re.search("ServerUptimeSeconds:\s+(.*)", result) |
| except TypeError: |
| return public.returnMsg(False, "获取启动时间错误") |
|
|
| if not Uptime: |
| return public.returnMsg(False, "获取启动时间错误") |
| Uptime = int(Uptime.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) |
| if not restarttime: |
| return public.returnMsg(False, "获取重启时间错误") |
| restarttime = restarttime.group(1) |
| rep = "\w+,\s([\w-]+)\s([\d\:]+)\s\w+" |
| date = re.search(rep, restarttime) |
| if not date: |
| return public.returnMsg(False, "获取日期错误") |
| date = date.group(1) |
| timedetail = re.search(rep, restarttime) |
| if not timedetail: |
| return public.returnMsg(False, "获取时间详情错误") |
| timedetail = timedetail.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) |
| if not reqpersec: |
| return public.returnMsg(False, "获取 reqpersec 错误") |
| reqpersec = reqpersec.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))) |
| total_acc = re.search("Total Accesses:\s+(\d+)", result) |
| if not total_acc: |
| return public.returnMsg(False, "获取 TotalAccesses 错误") |
| data["TotalAccesses"] = total_acc.group(1) |
| total_kb = re.search("Total kBytes:\s+(\d+)", result) |
| if not total_kb: |
| return public.returnMsg(False, "获取 TotalKBytes 错误") |
| data["TotalKBytes"] = total_kb.group(1) |
| data["ReqPerSec"] = round(float(reqpersec), 2) |
| busywork = re.search("BusyWorkers:\s+(\d+)", result) |
| if not busywork: |
| return public.returnMsg(False, "获取 BusyWorkers 错误") |
| data["BusyWorkers"] = busywork.group(1) |
| idlework = re.search("IdleWorkers:\s+(\d+)", result) |
| if not idlework: |
| return public.returnMsg(False, "获取 IdleWorkers 错误") |
| data["IdleWorkers"] = idlework.group(1) |
| data["workercpu"] = round(float(process_cpu["httpd"]), 2) |
| data["workermem"] = "%s%s" % (int(workermen), "MB") |
| return data |
|
|
| def GetApacheValue(self): |
| apachedefaultcontent = public.readFile(self.apachedefaultfile) |
| apachempmcontent = public.readFile(self.apachempmfile) |
| if not apachempmcontent: |
| return public.returnMsg(False, "没有找到 /www/server/apache/conf/extra/httpd-mpm.conf 配置") |
| if not "mpm_event_module" in apachempmcontent: |
| return public.returnMsg(False, |
| "没有找到 mpm_event_module 配置或 /www/server/apache/conf/extra/httpd-mpm.conf 配置为空") |
| apachempmcontent = re.search("\<IfModule mpm_event_module\>(\n|.)+?\</IfModule\>", apachempmcontent).group() |
| ps = ["秒,请求超时时间", "保持连接", "秒,连接超时时间", "单次连接最大请求数"] |
| gets = ["Timeout", "KeepAlive", "KeepAliveTimeout", "MaxKeepAliveRequests"] |
| if public.get_webserver() == 'apache': |
| shutil.copyfile(self.apachedefaultfile, self.apachedefaultfile_backup) |
| shutil.copyfile(self.apachempmfile, self.apachempmfile_backup) |
| conflist = [] |
| n = 0 |
| for i in gets: |
| rep = "(%s)\s+(\w+)" % i |
| k = re.search(rep, apachedefaultcontent) |
| if not k: |
| return public.returnMsg(False,"获取 key {} 失败,请检查配置文件中/www/server/apache/conf/extra/httpd-mpm.conf的{}配置是否正常".format(k,i)) |
| k = k.group(1) |
| v = re.search(rep, apachedefaultcontent) |
| if not v: |
| return public.returnMsg(False,"获取 value {} 失败,请检查配置文件中/www/server/apache/conf/extra/httpd-mpm.conf的{}配置是否正常".format(v,i)) |
| v = v.group(2) |
| psstr = ps[n] |
| kv = {"name": k, "value": v, "ps": psstr} |
| conflist.append(kv) |
| n += 1 |
|
|
| ps = ["启动时默认进程数", "最大空闲线程数", "可用于处理请求峰值的最小空闲线程数", "每个子进程创建的线程数", |
| "将同时处理的最大连接数", "限制单个子服务在生命周期内处理的连接数"] |
| gets = ["StartServers", "MaxSpareThreads", "MinSpareThreads", "ThreadsPerChild", "MaxRequestWorkers", |
| "MaxConnectionsPerChild"] |
| n = 0 |
| for i in gets: |
| rep = "(%s)\s+(\w+)" % i |
| k = re.search(rep, apachempmcontent) |
| if not k: |
| return public.returnMsg(False,"获取 key {} 失败,请检查配置文件中/www/server/apache/conf/extra/httpd-mpm.conf的{}配置是否正常".format(k,i)) |
| k = k.group(1) |
| v = re.search(rep, apachempmcontent) |
| if not v: |
| return public.returnMsg(False,"获取 value {} 失败,请检查配置文件中/www/server/apache/conf/extra/httpd-mpm.conf的{}配置是否正常".format(v,i)) |
| v = v.group(2) |
| psstr = ps[n] |
| kv = {"name": k, "value": v, "ps": psstr} |
| conflist.append(kv) |
| n += 1 |
| return (conflist) |
|
|
| def SetApacheValue(self, get): |
| apachedefaultcontent = public.readFile(self.apachedefaultfile) |
| apachempmcontent = public.readFile(self.apachempmfile) |
| if not "mpm_event_module" in apachempmcontent: |
| return public.returnMsg(False, |
| "没有找到 mpm_event_module 配置或 /www/server/apache/conf/extra/httpd-mpm.conf 配置为空") |
| allow_fields = ["Timeout", "KeepAlive", "KeepAliveTimeout", "MaxKeepAliveRequests", "StartServers", |
| "MaxSpareThreads", "MinSpareThreads", "ThreadsPerChild", "MaxRequestWorkers", |
| "MaxConnectionsPerChild"] |
|
|
| for _k in allow_fields: |
| val = get.get(_k) |
| if _k == "KeepAlive": |
| if not val in ['On', 'Off']: |
| return public.returnMsg(False, f'参数值错误({_k})') |
| if _k != "KeepAlive": |
| try: |
| val = int(float(val)) |
| if val < 0: raise ValueError() |
| val = str(val) |
| except Exception as e: |
| return public.returnMsg(False, f'{_k} 参数值错误,请输入正整数。') |
|
|
| _kv = (_k, val) |
| rep = f"{_k}\s+\w+" |
| if re.search(rep, apachedefaultcontent): |
| newconf = "%s %s" % _kv |
| apachedefaultcontent = re.sub(rep, newconf, apachedefaultcontent) |
| elif re.search(rep, apachempmcontent): |
| newconf = "%s\t\t\t%s" % _kv |
| apachempmcontent = re.sub(rep, newconf, apachempmcontent) |
| public.writeFile(self.apachedefaultfile, apachedefaultcontent) |
| public.writeFile(self.apachempmfile, apachempmcontent) |
| isError = public.checkWebConfig() |
| if (isError != True): |
| shutil.copyfile(self.apachedefaultfile_backup, self.apachedefaultfile) |
| shutil.copyfile(self.apachempmfile_backup, self.apachempmfile) |
| return public.returnMsg(False, 'ERROR: 配置出错<br><a style="color:red;">' + isError.replace("\n", |
| '<br>') + '</a>') |
| public.serviceReload() |
| return public.returnMsg(True, '设置成功') |
|
|