diff --git a/Makefile b/Makefile index e519df9..c3b3ddf 100644 --- a/Makefile +++ b/Makefile @@ -6,10 +6,10 @@ TAR_FILE := ./ta-for-unix-and-linux-$(VERSION).tgz all: release updateversion: -ifndef NEWVERSION - $(error NEWVERSION is not specified. Usage make NEWVERSION= updateversion) +ifndef NEW + $(error NEW is not specified. Usage make NEW= updateversion) endif - sed -ri "s/$(VERSION)/$(NEWVERSION)/g" app.manifest default/app.conf VERSION + sed -ri "s/$(VERSION)/$(NEW)/g" app.manifest default/app.conf VERSION release: mkdir -p $(WORK_DIR) diff --git a/VERSION b/VERSION index 543f19d..10582ea 100644 --- a/VERSION +++ b/VERSION @@ -1,2 +1,2 @@ -9.2.0.4 -9.2.0.4 +9.2.0.5 +9.2.0.5 diff --git a/app.manifest b/app.manifest index 7dbd1e4..85496fd 100644 --- a/app.manifest +++ b/app.manifest @@ -29,7 +29,7 @@ "id": { "group": null, "name": "TA-nix", - "version": "9.2.0.4" + "version": "9.2.0.5" }, "license": { "name": "Splunk Software License Agreement", diff --git a/bin/docker.sh b/bin/docker.sh new file mode 100755 index 0000000..9369605 --- /dev/null +++ b/bin/docker.sh @@ -0,0 +1,92 @@ +#!/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 +! groups | grep -q "\bdocker\b" && docker_cmd="sudo -n $docker_cmd" +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 /dev/null 2>&1 || { echo "Error: The logger command is required for this script"; exit; } + +ta_home=/srv/TA-unix +tag_prefix=nix_ta_ +syslog_server=192.168.1.1 +run_minute=2 +run_hour=6 + +# Get the current minute now to be consistent through the script run +minute=$(date +%_M | tr -d ' ') +# Get the current hour now to be consistent through the script run +hour=$(date +%_H | tr -d ' ') +# Set defaults disabling force-mode and list-mode +force=0 +list=0 + +usage() { + echo "usage: $(basename $0) [-h] [-f] [-l] [script]" + echo " -h: print this help text" + echo " -f: run all enabled scripts regardless of interval" + echo " -l: list scripts, enabled status, and interval (if enabled)" + exit +} + +# Get the command line options +while getopts ":hlf" opt; do + case $opt in + f) force=1 ;; + l) list=1 ;; + *) usage ;; + esac +done +shift $((OPTIND -1)) + +# Function to actually run the script and pipe it to logger +runit() { + [ -z "$1" ] && return 1 + if [ -x $ta_home/bin/$1.sh ]; then + { $ta_home/bin/$1.sh 2> /dev/null; echo; } | logger -n $syslog_server -t ${tag_prefix}$(echo $1|tr '[A-Z]' '[a-z]') + else + echo Could not find $1 in $ta_home/bin + return 1 + fi +} + +# Check the inputs.conf to see if any of the checks are disabled +declare -A scripts +declare -A intervals +# Load defaults first +if [ -r $ta_home/default/inputs.conf ]; then + eval $(awk -F '[=#]' ' + /^\[/{name=""} + /^\[script:\/\//{n=split($1,a,"/");name=gensub(/\.[a-z]+\]/,"",1,a[n]);printf "scripts[%s]=1\nintervals[%s]=60\n",name,name} + name!="" && $1~/(^|\s*)disabled(\s*|$)/ {disabled=gensub(/(^ | $)/,"","g",gensub(/true/,"1",1,gensub(/false/,"0",1,$2)));printf "scripts[%s]=%s\n",name,disabled} + name!="" && $1~/(^|\s*)interval(\s*|$)/ {interval=gensub(/(^ | $)/,"","g",$2);printf "intervals[%s]=%s\n",name,interval} + ' $ta_home/default/inputs.conf) +fi +# See if any defaults are overridden in the local directory +if [ -r $ta_home/local/inputs.conf ]; then + eval $(awk -F '[=#]' ' + /^\[/{name="";disabled=1;interval=60} + /^\[script:\/\//{n=split($1,a,"/");name=gensub(/\.[a-z]+\]/,"",1,a[n])} + name!="" && $1~/(^|\s*)disabled(\s*|$)/ {disabled=gensub(/(^ | $)/,"","g",gensub(/true/,"1",1,gensub(/false/,"0",1,$2)));printf "scripts[%s]=%s\n",name,disabled} + name!="" && $1~/(^|\s*)interval(\s*|$)/ {interval=gensub(/(^ | $)/,"","g",$2);printf "intervals[%s]=%s\n",name,interval} + ' $ta_home/local/inputs.conf) +fi + +# If -l, just print the scripts +if [ $list = 1 ]; then + for script in "${!scripts[@]}"; do + if [ "${scripts[$script]}" = "0" ]; then + echo "$script is enabled (${intervals[$script]} seconds)" + else + echo "$script is disabled" + fi + done + exit +fi + +# If a script is specified on the command line, run it (even if disabled) +if [ "$1" ]; then + runit $1 + exit +fi + +# Without -l or -f, loop through the enabled scripts and run them at their interval +for script in "${!scripts[@]}"; do + # Only run enabled scripts + if [ "${scripts[$script]}" = "0" ]; then + i=${intervals[$script]} + [ $i -lt 60 ] && i=60 + min=$((i/60)) + + # If -f, always run each script + if [ $force = 1 ]; then + runit $script + + # If interval is 60 seconds or less, run every minute + elif [ $min -le 1 ]; then + runit $script + + # If the current minute is divisible by the number of interval minutes, run + # example: 600 is 5 minutes, it'll run at 0, 5, 10, 15, ... minutes + elif [ $((minute % min)) = 0 ]; then + runit $script + + # If interval is an hour or more + elif [ $min -gt 60 ]; then + hr=$((i/60/60)) + + # If interval is 1 hour or less, run every hour on $run_minute + if [ $hr -le 1 ] && [ $minute = $run_minute ]; then + runit $script + + # If the current hour is divisible by the number of interval hours, run + # example: 21600 is 6 hours, it'll run at 0, 6, 12, 18 hours + elif [ $((hour % hr)) = 0 ] && [ $minute = $run_minute ]; then + runit $script + + # If the number of hours is 24 or more, run every day at $run_hour:$run_minute + elif [ $hr -ge 24 ] && [ $hour = $run_hour ] && [ $minute = $run_minute ]; then + runit $script + fi + fi + fi +done + diff --git a/extra/syslog_inputs_nix_ta/metadata/default.meta b/extra/syslog_inputs_nix_ta/metadata/default.meta new file mode 100644 index 0000000..16a0283 --- /dev/null +++ b/extra/syslog_inputs_nix_ta/metadata/default.meta @@ -0,0 +1,4 @@ +# Application-level permissions +[] +access = read : [ * ], write : [ admin , sc_admin ] +export = system