#
# usage: python query-proc HOST VAR
#
# Connects to pmcd on HOST and queries for the VAR performance metric.
# Particularly interesting are proc.psargs and proc.memory.maps.
#
# Florian Weimer / Red Hat Product Security Team
import socket
import sys
import pcppdu
_, host, var = sys.argv
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, 44321))
pcppdu.client_handshake(sock, from_=1)
pcppdu.send_pmns_names(sock, from_=1, names=(var.encode("utf-8"),))
ids = pcppdu.parse_pmns_ids(pcppdu.read_pdu(sock))
print(ids)
pmid = ids[1].idlist[0]
pcppdu.send_desc_req(sock, from_=1, pmid=pmid)
print(pcppdu.parse_desc(pcppdu.read_pdu(sock)))
pcppdu.send_profile(sock, from_=1, ctxnum=0, instprof=(pcppdu.Profile(0, (pcppdu.InDomProfile(0, 0, ()),),)))
pcppdu.send_fetch(sock, from_=1, ctxnum=0, pmidlist=(pmid,))
blob = pcppdu.read_pdu(sock)
print(repr(blob))
for d in pcppdu.parse_result(blob)[1][0].values:
    print(d)
sock.close()
