41 lines
3.4 KiB
Python
41 lines
3.4 KiB
Python
import os, subprocess, json, time, collections, http.client, ssl, re, socket
|
|
base='/www/backup/txiaw-mysql-fix-20260831'
|
|
r={'time':time.strftime('%Y-%m-%d %H:%M:%S')}
|
|
def run(args):return subprocess.check_output(args,stderr=subprocess.STDOUT,timeout=10).decode('utf-8','replace')
|
|
conf=run(['/www/server/nginx/sbin/nginx','-T'])
|
|
r['nginx_test_ok']='test is successful' in conf
|
|
r['abuse_drop_rule_loaded']='if ($txiaw_home_cc_block)' in conf and 'return 444;' in conf
|
|
r['txiaw_home_frequency_limits_loaded']='zone=txiaw_home_per_ip' in conf or 'zone=txiaw_home_total' in conf
|
|
r['uptime']=run(['uptime']).strip()
|
|
r['panel_listener']=run(['ss','-ltnp','sport = :8686']).strip()
|
|
r['panel_processes']=run(['ps','-C','BT-Panel,BT-Task','-o','pid,stat,%cpu,rss,nlwp,wchan:25,comm']).strip()
|
|
tcp=run(['ss','-tan']).splitlines()[1:]
|
|
r['tcp_by_service_and_state']=dict(collections.Counter(l.split()[3].rsplit(':',1)[-1]+' '+l.split()[0] for l in tcp if len(l.split())>4 and l.split()[3].rsplit(':',1)[-1] in ('22','80','443','8686','3306')))
|
|
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();started=time.monotonic();vm=run(['vmstat','1','3']);b=net();dt=time.monotonic()-started
|
|
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'}
|
|
r['cpu_idle_pct']=sum(int(x.split()[14]) for x in vm.splitlines()[-2:])/2
|
|
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,path,host,agent in [('home','/','www.txiaw.com',ua),('article','/down/184778.html','www.txiaw.com',ua),('observed_abuse','/?r=connection-close-check','www.txiaw.com',''),('other_site','/','www.xxiaw.com',ua)]:
|
|
c=http.client.HTTPConnection('127.0.0.1',80,timeout=4);t=time.monotonic()
|
|
try:
|
|
c.request('GET',path,headers={'Host':host,'User-Agent':agent});q=c.getresponse();body=q.read()
|
|
r[name]={'status':q.status,'bytes':len(body),'seconds':round(time.monotonic()-t,2)}
|
|
except Exception as e:r[name]={'result':type(e).__name__,'detail':str(e),'seconds':round(time.monotonic()-t,2)}
|
|
finally:c.close()
|
|
s=None;phase='tcp';t=time.monotonic()
|
|
try:
|
|
s=socket.create_connection(('127.0.0.1',8686),4);s.settimeout(4);phase='tls'
|
|
s=ssl._create_unverified_context().wrap_socket(s,server_hostname='47.106.181.28');phase='http'
|
|
s.sendall(('GET /gaorui HTTP/1.1\r\nHost: 47.106.181.28:8686\r\nUser-Agent: '+ua+'\r\nConnection: close\r\n\r\n').encode())
|
|
data=s.recv(8192);r['panel']={'phase':phase,'first_line':data.split(b'\r\n')[0].decode('utf-8','replace'),'seconds':round(time.monotonic()-t,2),'bytes_received':len(data)}
|
|
except Exception as e:r['panel']={'phase':phase,'error':str(e),'seconds':round(time.monotonic()-t,2)}
|
|
finally:
|
|
if s:s.close()
|
|
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['logged_response_body_bytes']=sum(int(l.split('"')[2].split()[1]) for l in lines if len(l.split('"'))>2 and len(l.split('"')[2].split())>1 and l.split('"')[2].split()[1].isdigit())
|
|
json.dump(r,open(base+'/abuse-connection-close-verified.json','w'),indent=2)
|
|
print(json.dumps(r),flush=True)
|