Files
rust-discovery/deploy/cloud_inventory_replay_evidence.py
2026-11-13 19:11:45 +01:00

48 lines
3.0 KiB
Python

"""Read preserved inventory identity/hash only; no collection or queue edits."""
def vm_pending(payload):
import sqlite3
def wire(raw):
result={};parts=[];i=0
def number():
nonlocal i
value=shift=0
while True:
require(i<len(raw) and shift<70,'bounded preserved protobuf')
b=raw[i];i+=1;value|=(b&127)<<shift
if not b&128:return value
shift+=7
while i<len(raw):
start=i;key=number();field=key>>3;kind=key&7
if kind==0:value=number()
elif kind==2:
length=number();require(i+length<=len(raw),'bounded protobuf field');value=raw[i:i+length];i+=length
else:raise RuntimeError('unexpected preserved protobuf type')
result[field]=value;parts.append((field,raw[start:i]))
return result,parts
with sqlite3.connect('file:/var/lib/rmm-agent-remote/journal.sqlite?mode=ro',uri=True) as db:
events=[];other=0
for ident,raw in db.execute('SELECT id,payload FROM job_events'):
require(len(raw)<=1024*1024,'bounded durable message');e,parts=wire(raw)
if 13 not in e:other+=1;continue
snap,_=wire(e[13]);data=json.loads(snap[6]);require(hashlib.sha256(snap[6]).digest()==snap[7],'original data hash')
require(e[3].decode()==ident,'original message identity')
events.append(dict(message_id=ident,agent_id=e[4].decode(),snapshot_id=snap[1].decode(),
observed_at_ms=e[9],sequence=e[8],data_sha256=hashlib.sha256(snap[6]).hexdigest(),
payload_sha256=hashlib.sha256(b''.join(raw for number,raw in parts if number!=6)).hexdigest(),
collection_started_at=data.get('collection_started_at'),collection_completed_at=data.get('collection_completed_at'),
observations=[dict(scope=o.get('scope'),source_id=o.get('source_id'),quality=o.get('quality'),observed_at=o.get('observed_at'),
bytes=len(json.dumps(o,separators=(',',':')).encode())) for o in data.get('observations',[])],
recovery_scope_present=any(o.get('scope')=='linux.apt.recovery-planning' for o in data.get('observations',[]))))
return dict(events=events,other_unacked=other)
def server_stored(payload):
root=Path('/opt/rust-discovery');agent=str(uuid.UUID(payload['agent_id']));message=str(uuid.UUID(payload['message_id']))
query="SELECT row_to_json(x) FROM (SELECT message_id,snapshot_id,payload_sha256,(extract(epoch FROM observed_at)*1000)::bigint AS observed_at_ms FROM observations WHERE tenant_id='11111111-1111-4111-8111-111111111111' AND agent_id='"+agent+"' AND message_id='"+message+"') x;"
cmd=['docker','compose','--env-file',str(root/'devstate/.env'),'-f',str(root/'deploy/server.compose.yaml'),
'exec','-T','postgres','psql','-X','-qAt','-v','ON_ERROR_STOP=1','-U','postgres','-d','rmm']
raw=subprocess.run(cmd,input=query.encode(),capture_output=True,check=True,timeout=15).stdout.strip()
return json.loads(raw) if raw else {}