AWSIoTDeviceDefenderAgentSDK

AWSIoTDeviceDefenderAgentSDK.agent

class AWSIoTDeviceDefenderAgentSDK.agent.IoTClientWrapper(endpoint, root_ca_path, certificate_path, private_key_path, client_id)

Bases: object

Wrapper around the AWS Iot Python SDK.

Sets common parameters based on the AWS Iot Python SDK’s Basic PubSub sample.

publish(publish_to_topic, payload)

Publish to MQTT

subscribe(subscribe_to_topic, callback)

Subscribe to MQTT

connect()

Connect to AWS IoT

AWSIoTDeviceDefenderAgentSDK.agent.parse_args()

Setup Commandline Argument Parsing

AWSIoTDeviceDefenderAgentSDK.agent.custom_callback(self, userdata, message)

AWSIoTDeviceDefenderAgentSDK.collector

class AWSIoTDeviceDefenderAgentSDK.collector.Collector(short_metrics_names=False)

Bases: object

Reads system information and populates a metrics object.

This implementation utilizes psutil to make parsing metrics easier and more cross-platform.

Parameters:short_metrics_names (bool) – Toggle short object tags in output metrics.
listening_ports(metrics)

Iterate over all inet connections in the LISTEN state and extract port and interface.

static network_stats(metrics)
static network_connections(metrics)
collect_metrics()

Sample system metrics and populate a metrics object suitable for publishing to Device Defender.

AWSIoTDeviceDefenderAgentSDK.metrics

class AWSIoTDeviceDefenderAgentSDK.metrics.Metrics(short_names=False, last_metric=None)

Bases: object

A collection of system metric values, providing facilities for output in a Device Defender compliant format.

Features:

Serialization Format: Metrics can be exported in either cbor or JSON format.

Delta metrics: if the class is initialized with a existing metrics object, for certain metrics, the difference between the old an current metric value will calculated and stored.

Selectable metric tags: allow for verbose metrics tags, for easier debugging, or short memonic tags, reducing the amount data transmitted and stored in memory.

Initialize a new metrics object.

Parameters:
  • short_names (bool) – Toggle short object tags in output metrics.
  • last_metric (Metrics object) – Metric object used for delta metric calculation.
network_stats

Retrieve network TCP and UDP stats aggregated across all interfaces.

listening_ports(protocol)
add_listening_ports(protocol, ports)

Add a sets of listening ports for a particular protocol.

Parameters:
  • protocol (string) – TCP or UDP, all others invalid and will not be added
  • ports (list) – List of Dictionaries, each dictionary should have a “port” and optionally an “interface” key. Example Dictionary: {‘port’: 80, ‘interface’: ‘eth0’}
add_network_stats(bytes_in, packets_in, bytes_out, packets_out)

Add cumulative network stats across all network interfaces. If a previous metrics object was supplied,attempts to calculate and store delta metric.

Parameters:
  • bytes_in (int) – Number of bytes received on this interface
  • bytes_out (int) – Number of bytes sent from this interface
  • packets_in (int) – Number of packets received on this interface
  • packets_out (int) – Number of packets sent from this interface
add_network_connection(remote_addr, remote_port, interface, local_port)

Add network connection details.

Parameters:
  • remote_addr (string) – Ip address of the remote peer, can be ipv4 or ipv6
  • remote_port (int) – Port of the remote peer
  • interface (string) – Name of local network interface associated with the connection
  • local_port (int) – Local port of the connection
network_connections
to_json_string(pretty_print=False)

Convert the metrics to a json string suitable for AWS IoT Device Defender.

Parameters:pretty_print (bool) – Set to true if you would like json to be formatted in a more human-friendly format.
to_cbor()

Returns a cbor serialized metrics object.

AWSIoTDeviceDefenderAgentSDK.tags

class AWSIoTDeviceDefenderAgentSDK.tags.Tags(short_names=False)

Bases: object

Abstract field name selection for metrics reports.

HEADER = ('header', 'hed')
METRICS = ('metrics', 'met')
REPORT_ID = ('report_id', 'rid')
VERSION = ('version', 'v')
TCP_CONN = ('tcp_connections', 'tc')
ESTABLISHED_CONNECTIONS = ('established_connections', 'ec')
CONNECTIONS = ('connections', 'cs')
REMOTE_ADDR = ('remote_addr', 'rad')
REMOTE_PORT = ('remote_port', 'rp')
LOCAL_PORT = ('local_port', 'lp')
LOCAL_INTERFACE = ('local_interface', 'li')
STATUS = ('status', 's')
LISTENING_TCP_PORTS = ('listening_tcp_ports', 'tp')
LISTENING_UDP_PORTS = ('listening_udp_ports', 'up')
PORTS = ('ports', 'pts')
PORT = ('port', 'pt')
NETWORK_STATS = ('network_stats', 'ns')
BYTES_IN = ('bytes_in', 'bi')
BYTES_OUT = ('bytes_out', 'bo')
PACKETS_IN = ('packets_in', 'pi')
PACKETS_OUT = ('packets_out', 'po')
TOTAL = ('total', 't')
get(tag)
header
metrics
report_id
version
tcp_conn
connections
established_connections
remote_addr
remote_port
local_port
local_interface
listening_tcp_ports
listening_udp_ports
ports
interface_stats
interfaces
bytes_in
bytes_out
packets_in
packets_out
total