#!/usr/bin/bash # # Send stdout of some commands to monitoring server. # Collect the reports with 'nc -u -k -l 1234' on 'sendto'. # Use /bin/nc.openbsd, /bin/nc.traditional seems not to work. # set -eu sendto="collector.steinbeis.schule 1234" cmds=( 'uname -a' 'uptime' 'apt list --upgradeable -o Apt::Cmd::Disable-Script-Warning=true' 'systemctl --failed' 'w' 'ls -ld /home/ansible/.ansible/tmp/' 'ip addr show' ) r="$HOSTNAME: ------- $(date) ------- $(for c in "${cmds[@]}" ; do echo "$c"; $c | sed 's/^/ /' ; done | sed "s/^/$HOSTNAME: /") ## -------------------------------------------------" echo "$r" | nc -w 1 -u $sendto ## below is the corresponding collector part: # #!/usr/bin/bash # # # # collect messages from reporter and drop it into log files # # # # set -eu # # port=1234 # # logdir="/var/log/collector" # [[ -d "$logdir" ]] || mkdir "$logdir" # # nc -k -l -u -p "$port" | while read line ; do # sndr="${line%%:*}" # msg="${line#*: }" # if [[ "$sndr" =~ [a-z0-9]+ ]] ; then # if [[ "$msg" =~ ^-------\ .+\ -------$ ]] ; then # echo "$msg" > "$logdir/$sndr" # else # echo "$msg" >> "$logdir/$sndr" # fi # fi # done