#!/bin/bash # SPDX-FileCopyrightText: 2022 Michael Erdely # SPDX-License-Identifier: MIT # shellcheck disable=SC1091 . "$(dirname "$0")"/common.sh assertHaveCommand docker assertHaveCommand bc assertHaveCommand ip assertHaveCommand awk declare -A pids declare -A time_start declare -A cpu_start declare -A rx_start declare -A tx_start declare -A br_start declare -A bw_start [[ $0 =~ .*_metric.sh ]] && mode=metric # Either add the splunk user to the docker group or add the following to /etc/sudoers: # splunk ALL=(root) NOPASSWD: /usr/bin/docker stats --no-stream --no-trunc --all # splunk ALL=(root) NOPASSWD: /usr/bin/docker ps --all --no-trunc --format * # splunk ALL=(root) NOPASSWD: /usr/bin/docker inspect -f * docker_cmd=docker if [ $(id -u) != 0 ]; then ! groups | grep -q "\bdocker\b" && docker_cmd="sudo -n $docker_cmd" fi docker_list=$($docker_cmd ps --all --no-trunc --format '{{ .ID }}') header_string="ContainerId Name CPUPct MemUsage MemTotal MemPct NetRX RXps NetTX TXps BlockRead BRps BlockWrite BWps Pids" metric_string="" header_format="%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" string_format="%s\t%s\t%s\t%.2f\t%s\t%s\t%.2f\t%s\t%.2f\t%s\t%.2f\t%s\t%.2f\t%s\t%.2f\t%s\n" json_format='{ "time": "%s", "ContainerId": "%s", "Name": "%s", "CPUPct": %.2f, "MemUsage": %s, "MemTotal": %s, "MemPct": %.2f, "NetRX": %s, "RXps": %.2f, "NetTX": %s, "TXps": %.2f, "BlockRead": %s, "BRps": %.2f, "BlockWrite": %s, "BWps": %.2f, "Pids": %s }\n' if [ "$mode" = "metric" ]; then metric_name=docker_metric if [ ! -f "/etc/os-release" ] ; then OSName=$(cat /etc/*release | head -n 1| awk -F" release " '{print $1}'| tr ' ' '_') OS_version=$(cat /etc/*release | head -n 1| awk -F" release " '{print $2}' | cut -d\. -f1) IP_address=$(ip addr show dev $(ip route show | awk 'BEGIN{m=1000}$1=="default"$0!~/ metric /{print $5;exit}$1=="default"{if($NF