40 lines
3.2 KiB
Python
40 lines
3.2 KiB
Python
import os, json, re, subprocess, time, collections, http.client, ssl
|
|
base='/www/backup/txiaw-mysql-fix-20260831'
|
|
r={'time':time.strftime('%Y-%m-%d %H:%M:%S')}
|
|
def run(args,env=None):
|
|
return subprocess.check_output(args,stderr=subprocess.STDOUT,env=env,timeout=12).decode('utf-8','replace')
|
|
ng=run(['/www/server/nginx/sbin/nginx','-T'])
|
|
r['nginx_test_ok']='test is successful' in ng
|
|
r['loaded_txiaw_home_rules']=ng.count('txiaw_home')
|
|
r['bt_status']=run(['/etc/init.d/bt','status']).strip()
|
|
r['uptime']=run(['uptime']).strip()
|
|
def net():
|
|
return {l.split(':')[0].strip():list(map(int,l.split(':')[1].split())) for l in open('/proc/net/dev') if ':' in l}
|
|
a=net();t=time.monotonic();vm=run(['vmstat','1','3']);b=net();dt=time.monotonic()-t
|
|
r['network_Mbps']={k:{'rx':round((v[0]-a[k][0])*8/dt/1e6,2),'tx':round((v[8]-a[k][8])*8/dt/1e6,2)} for k,v in b.items() if k!='lo'}
|
|
rows=[l.split() for l in vm.splitlines()[-2:]]
|
|
r['cpu_idle_pct']=sum(int(l[14]) for l in rows)/len(rows)
|
|
r['socket_summary']=run(['ss','-s'])[:700]
|
|
ua='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36'
|
|
for name,port,path,host,useragent in [('home',80,'/','www.txiaw.com',ua),('observed_abuse_signature',80,'/?r=rollback-diagnostic','www.txiaw.com',''),('panel',8686,'/gaorui','47.106.181.28:8686',ua)]:
|
|
c=None;t=time.monotonic()
|
|
try:
|
|
c=http.client.HTTPSConnection('127.0.0.1',port,timeout=6,context=ssl._create_unverified_context()) if port==8686 else http.client.HTTPConnection('127.0.0.1',port,timeout=6)
|
|
c.request('GET',path,headers={'Host':host,'User-Agent':useragent})
|
|
q=c.getresponse();body=q.read();title=re.search(br'<title>(.*?)</title>',body,re.S)
|
|
r[name]={'status':q.status,'bytes':len(body),'seconds':round(time.monotonic()-t,2),'title':title.group(1).decode('utf-8','replace') if title else None}
|
|
except Exception as e:r[name]={'error':str(e)}
|
|
finally:
|
|
if c:c.close()
|
|
cfg={m.group(1).lower():m.group(3) for m in re.finditer(r'''['"](db_[a-z_]+)['"]\s*=>\s*(['"])(.*?)\2''',open('/www/wwwroot/www.txiaw.com/Runtime/Conf/config.php').read(),re.I)}
|
|
env=dict(os.environ,MYSQL_PWD=cfg.get('db_pwd',cfg.get('db_password')))
|
|
args=['/www/server/mysql/bin/mysql','--no-defaults','-u'+cfg['db_user'],'-h'+cfg['db_host'],'--connect-timeout=5','-B']
|
|
r['mysql_status']=run(args+['-e',"SHOW GLOBAL STATUS WHERE Variable_name IN ('Threads_running','Threads_connected','Slow_queries');"],env)
|
|
r['news_index']=run(args+['-e',"SHOW INDEX FROM xiaxia.gxl_news WHERE Key_name='idx_news_status_addtime';"],env)
|
|
lines=run(['tail','-n','1000','/www/wwwlogs/www.txiaw.com.log']).splitlines()
|
|
r['recent_access_statuses']=dict(collections.Counter(l.split('"')[2].split()[0] for l in lines if l.count('"')>2 and l.split('"')[2].split()))
|
|
r['recent_access_time_range']=[l.split('[',1)[-1].split(']',1)[0] for l in [lines[0],lines[-1]]] if lines else []
|
|
r['observed_abuse_signature_count']=sum(1 for l in lines if len(l.split('"'))>5 and l.split('"')[5] in ('','-') and re.match(r'^(GET|HEAD) /(?:index[.]php)?[?](?:[^ ]*&)?r(?:=|&| )',l.split('"')[1]))
|
|
json.dump(r,open(base+'/rate-limit-rollback-verified.json','w'),indent=2)
|
|
print(json.dumps(r),flush=True)
|