55 lines
3.6 KiB
Python
55 lines
3.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Local telemetry and bounded terminal-log retention. No character data is deleted."""
|
|
import base64,json,os,shutil,subprocess,sys,time,urllib.request
|
|
from pathlib import Path
|
|
repo=Path(__file__).resolve().parents[1]
|
|
lab=Path(os.environ.get('AZEROTHAI_PLAYABLE_ROOT',str(Path.home()/'azerothai-pc-lab')))
|
|
def config(path):
|
|
out={}
|
|
for line in path.read_text().splitlines():
|
|
if '=' in line and not line.startswith('#'):
|
|
k,v=line.split('=',1);out[k]=v.strip().strip('"').strip("'")
|
|
return out
|
|
cfg={**config(lab/'runtime.env'),**config(repo/'.dev.vars')}
|
|
base='http://127.0.0.1:8787'
|
|
def send(data):
|
|
request=urllib.request.Request(base+'/api/operations',data=json.dumps(data).encode(),headers={'Content-Type':'application/json','Authorization':'Bearer '+cfg['AUTONOMY_TOKEN']})
|
|
with urllib.request.urlopen(request,timeout=20) as r:assert r.status==200
|
|
if len(sys.argv)==3 and sys.argv[1]=='--windows':
|
|
data=json.loads(base64.b64decode(sys.argv[2]));data['host']='windows';send(data);sys.exit(0)
|
|
def bytes_in(path):
|
|
return sum(p.stat().st_size for p in path.rglob('*') if p.is_file()) if path.exists() else 0
|
|
def cpu():
|
|
v=list(map(int,Path('/proc/stat').read_text().splitlines()[0].split()[1:]));return sum(v[:8]),v[3]+v[4]
|
|
a=cpu();time.sleep(1);b=cpu();used=100*(1-(b[1]-a[1])/max(1,b[0]-a[0]))
|
|
mem={line.split(':')[0]:int(line.split()[1])/1024 for line in Path('/proc/meminfo').read_text().splitlines()}
|
|
model=False
|
|
try:
|
|
u=cfg.get('LLM_URL','').rstrip('/')+'/api/tags'
|
|
with urllib.request.urlopen(u,timeout=5) as r:model=r.status==200
|
|
except Exception:pass
|
|
maintenance=[]
|
|
# Copy/truncate preserves existing service file descriptors. Three bounded archives.
|
|
paths=[lab/'autonomy.log',lab/'ale-relay.log',Path.home()/'azerothai-direct-runtime-live.log',lab/'operations.log']
|
|
for log in paths:
|
|
if log.exists() and log.stat().st_size>10*1024*1024:
|
|
for n in [3,2]:
|
|
older=Path(str(log)+'.'+str(n-1))
|
|
if older.exists():older.replace(str(log)+'.'+str(n))
|
|
shutil.copy2(log,str(log)+'.1')
|
|
with log.open('r+') as f:f.truncate(0)
|
|
maintenance.append('Rotated '+log.name)
|
|
# Only acknowledged transport data; never purge pending messages or lifecycle history.
|
|
try:
|
|
state=json.load(urllib.request.urlopen(base+'/api/state',timeout=10));days=max(3,min(30,int(state.get('opsRetentionDays',7))))
|
|
env=os.environ.copy();env['MYSQL_PWD']=cfg['DOCKER_DB_ROOT_PASSWORD']
|
|
def sql(q):return subprocess.check_output(['docker','exec','-e','MYSQL_PWD','ac-database','mysql','-uroot','-N','-B','-e',q],env=env,text=True)
|
|
for table,column in [('azerothai_event_outbox','delivered_at'),('azerothai_result_outbox','delivered_at')]:
|
|
sql(f'DELETE FROM acore_auth.{table} WHERE {column} IS NOT NULL AND {column}<NOW()-INTERVAL 1 DAY LIMIT 2000')
|
|
sql(f'DELETE FROM acore_auth.azerothai_snapshot_archive WHERE created_at<NOW()-INTERVAL {days} DAY LIMIT 2000')
|
|
maintenance.append('Acknowledged transport and snapshot retention checked')
|
|
except Exception as e:maintenance.append('Retention check needs attention: '+type(e).__name__)
|
|
service=subprocess.run(['systemctl','--user','is-active','azerothai-runtime'],capture_output=True,text=True).stdout.strip()
|
|
send({'host':'ubuntu','at':time.time(),'cpuPercent':max(0,min(100,used)),'memoryTotalMB':mem['MemTotal'],'memoryAvailableMB':mem['MemAvailable'],'diskFreeGB':shutil.disk_usage(repo).free/1024**3,'databaseMB':bytes_in(repo/'.wrangler/state/v3/d1')/1024**2,'logsMB':sum(p.stat().st_size for p in paths if p.exists())/1024**2,'modelAvailable':model,'runtimeService':service,'maintenance':'; '.join(maintenance)[:500]})
|
|
print(time.strftime('%Y-%m-%dT%H:%M:%SZ',time.gmtime()),'telemetry and retention complete')
|