#!/bin/sh
#
# Example script called by Mender client to collect inventory data for a
# particular device. The script needs to be located in $(datadir)/mender and its
# name shall start with `mender-inventory-` prefix. The script shall exit with
# non-0 status on errors. In this case the agent will discard any output the
# script may have produced.
#
# The script shall output inventory data in <key>=<value> format, one entry per
# line. Entries appearing multiple times will be joined in a list under the same
# key.
#
# Environment variable(s):
#
# INCLUDE_DOCKER_INTERFACES=true -- Include docker intefaces in output
#
# $ ./mender-inventory-network
# mac_enp0s25=de:ad:be:ef:bb:05
# network_interfaces=enp0s25
# ipv4_enp0s25=123.22.0.197/16
# ipv4_enp0s25=10.20.20.105/16
# ipv6_enp0s25=fe80::2aad:beff:feef:bb05/64
#
# $ INCLUDE_DOCKER_INTERFACES=true ./mender-inventory-network
# mac_br-fbfdad18c33c=02:42:7e:74:96:85
# network_interfaces=br-fbfdad18c33c
# ipv4_br-fbfdad18c33c=172.21.0.1/16
# mac_enp0s25=de:ad:be:ef:bb:05
# network_interfaces=enp0s25
# ipv4_enp0s25=123.22.0.197/16
# ipv4_enp0s25=10.20.20.105/16
# ipv6_enp0s25=fe80::2aad:beff:feef:bb05/64
#
#
# The example script collects the list of network interfaces, as well as
# ethernet and IP addresses of each of the interfaces.
#

set -ue

INCLUDE_DOCKER_INTERFACES="${INCLUDE_DOCKER_INTERFACES:-false}"

SCN=/sys/class/net
min=65535
ifdev=

# find iface with lowest ifindex, except loopback
for devpath in $SCN/*; do
    dev=$(basename $devpath)
    if [ $dev = "lo" ]; then
        continue
    fi
    if [ "${INCLUDE_DOCKER_INTERFACES}" = "false" ]; then
        if echo $dev | grep -q -E '^(br-.*|docker.*|veth.*)'; then
            continue
        fi
    fi
    if ! [ "x$(cat $devpath/address)x" = "xx" ]; then
        echo "mac_$dev=$(cat $devpath/address)"
    fi
    echo "network_interfaces=$dev"

    ip addr show dev $dev | awk -v dev=$dev '
       /inet / { printf("ipv4_%s=%s\n", dev, $2) }
       /inet6 / {printf("ipv6_%s=%s\n", dev, $2) }
    '
done
