| 1 |
#!/bin/sh |
| 2 |
# |
| 3 |
# Example script called by Mender client to collect inventory data for a |
| 4 |
# particular device. The script needs to be located in $(datadir)/mender and its |
| 5 |
# name shall start with `mender-inventory-` prefix. The script shall exit with |
| 6 |
# non-0 status on errors. In this case the agent will discard any output the |
| 7 |
# script may have produced. |
| 8 |
# |
| 9 |
# The script shall output inventory data in <key>=<value> format, one entry per |
| 10 |
# line. Entries appearing multiple times will be joined in a list under the same |
| 11 |
# key. |
| 12 |
# |
| 13 |
# Environment variable(s): |
| 14 |
# |
| 15 |
# INCLUDE_DOCKER_INTERFACES=true -- Include docker intefaces in output |
| 16 |
# |
| 17 |
# $ ./mender-inventory-network |
| 18 |
# mac_enp0s25=de:ad:be:ef:bb:05 |
| 19 |
# network_interfaces=enp0s25 |
| 20 |
# ipv4_enp0s25=123.22.0.197/16 |
| 21 |
# ipv4_enp0s25=10.20.20.105/16 |
| 22 |
# ipv6_enp0s25=fe80::2aad:beff:feef:bb05/64 |
| 23 |
# |
| 24 |
# $ INCLUDE_DOCKER_INTERFACES=true ./mender-inventory-network |
| 25 |
# mac_br-fbfdad18c33c=02:42:7e:74:96:85 |
| 26 |
# network_interfaces=br-fbfdad18c33c |
| 27 |
# ipv4_br-fbfdad18c33c=172.21.0.1/16 |
| 28 |
# mac_enp0s25=de:ad:be:ef:bb:05 |
| 29 |
# network_interfaces=enp0s25 |
| 30 |
# ipv4_enp0s25=123.22.0.197/16 |
| 31 |
# ipv4_enp0s25=10.20.20.105/16 |
| 32 |
# ipv6_enp0s25=fe80::2aad:beff:feef:bb05/64 |
| 33 |
# |
| 34 |
# |
| 35 |
# The example script collects the list of network interfaces, as well as |
| 36 |
# ethernet and IP addresses of each of the interfaces. |
| 37 |
# |
| 38 |
|
| 39 |
set -ue |
| 40 |
|
| 41 |
INCLUDE_DOCKER_INTERFACES="${INCLUDE_DOCKER_INTERFACES:-false}" |
| 42 |
|
| 43 |
SCN=/sys/class/net |
| 44 |
min=65535 |
| 45 |
ifdev= |
| 46 |
|
| 47 |
# find iface with lowest ifindex, except loopback |
| 48 |
for devpath in $SCN/*; do |
| 49 |
dev=$(basename $devpath) |
| 50 |
if [ $dev = "lo" ]; then |
| 51 |
continue |
| 52 |
fi |
| 53 |
if [ "${INCLUDE_DOCKER_INTERFACES}" = "false" ]; then |
| 54 |
if echo $dev | grep -q -E '^(br-.*|docker.*|veth.*)'; then |
| 55 |
continue |
| 56 |
fi |
| 57 |
fi |
| 58 |
if ! [ "x$(cat $devpath/address)x" = "xx" ]; then |
| 59 |
echo "mac_$dev=$(cat $devpath/address)" |
| 60 |
fi |
| 61 |
echo "network_interfaces=$dev" |
| 62 |
|
| 63 |
ip addr show dev $dev | awk -v dev=$dev ' |
| 64 |
/inet / { printf("ipv4_%s=%s\n", dev, $2) } |
| 65 |
/inet6 / {printf("ipv6_%s=%s\n", dev, $2) } |
| 66 |
' |
| 67 |
done |