in AWSIoTDeviceDefenderAgentSDK/agent.py [0:0]
def main():
# Read in command-line parameters
args = parse_args()
io.init_logging(getattr(io.LogLevel, args.verbosity), 'stderr')
if not args.dry_run:
client_id = ""
thing_name = ""
if not args.client_id:
client_id = gethostname()
else:
client_id = args.client_id
if not args.thing_name:
thing_name = client_id
else:
thing_name = args.thing_name
iot_client = IoTClientWrapper(args.endpoint, args.root_ca_path,
args.certificate_path, args.private_key_path, client_id,
args.signing_region, args.proxy_host, args.proxy_port,
args.use_websocket)
iot_client.connect()
# client_id must match a registered thing name in your account
topic = "$aws/things/" + thing_name + "/defender/metrics/" + args.format
# Subscribe to the accepted/rejected topics to indicate status of published metrics reports
iot_client.subscribe(topic + "/accepted", custom_callback)
iot_client.subscribe(topic + "/rejected", custom_callback)
sample_rate = args.upload_interval
# Collector samples metrics from the system, it can track the previous metric to generate deltas
coll = collector.Collector(args.short_tags, args.custom_metrics)
metric = None
first_sample = True # don't publish first sample, so we can accurately report delta metrics
while True:
metric = coll.collect_metrics()
if args.dry_run:
print(metric.to_json_string(pretty_print=True))
if args.format == 'cbor':
with open("cbor_metrics", "w+b") as outfile:
outfile.write(bytearray(metric.to_cbor()))
else:
if first_sample:
first_sample = False
elif args.format == "cbor":
iot_client.publish(topic, bytearray(metric.to_cbor()))
else:
iot_client.publish(topic, metric.to_json_string())
sleep(float(sample_rate))