Implement time stamp and improve reporter syntax.

The directory access time used so far is modified on every
ansible run and independent of the ansible module/playbook.
This commit is contained in:
Andreas B. Mundt 2024-01-16 11:54:45 +01:00
parent 5ed4c2a135
commit aa8bc09ce6
4 changed files with 25 additions and 13 deletions

View file

@ -85,6 +85,10 @@
- lmn_security
tasks:
- name: Timestamp successfull ansible run
ansible.builtin.shell: date --iso-8601=seconds >> /home/ansible/.ansible/stamps
changed_when: False
## Temporary fixes and quirks:
- name: Fix 8086:4909 external graphics card
replace:

View file

@ -7,19 +7,27 @@
set -eu
sendto="collector.steinbeis.schule 1234"
n=0
cmds=(
'uname -a'
'ls -d --full-time /home/ansible/.ansible/tmp/'
'ip link | sed -nE -e "s/^[2-9]: (\S+): .+/\1/p" -e "s/.+ether ([0-9a-f:]+) .+/\1/p" | paste -d " " - -'
'tail -1 /home/ansible/.ansible/stamps'
'ip route list default'
'ip link show | \
sed -nE -e "s/^[2-9]: (\S+): .+/\1/p" -e "s/.+ether ([0-9a-f:]+) .+/\1/p" | \
paste -d " " - -'
)
# 'w'
# 'uptime'
# 'ls -d --full-time /home/ansible/.ansible/tmp/'
# 'ip addr show'
# 'apt list --upgradeable -o Apt::Cmd::Disable-Script-Warning=true'
r="$HOSTNAME: ------- $(date --rfc-3339=seconds) -------
$(for c in "${cmds[@]}" ; do echo "$c"; eval "$c" | sed 's/^/ /' ; done | sed "s/^/$HOSTNAME: /")
r="$HOSTNAME ------- $(date --rfc-3339=seconds) -------
$(for c in "${cmds[@]}" ; do
n=$(( n + 1 ))
echo -n "$n"
eval "$c" | sed 's/^/\t/'
done | sed "s/^/$HOSTNAME /")
## -------------------------------------------------"
echo "$r" | nc -w 1 -u $sendto

View file

@ -11,11 +11,11 @@ logdir="/tmp/collector"
mkdir -vp "$logdir"
nc -k -l -u -p "$port" | while read line ; do
sndr="${line%%:*}"
msg="${line#*: }"
sndr="${line%% *}"
msg="${line#* }"
if [[ "$sndr" =~ [a-z0-9]+ ]] ; then
if [[ "$msg" =~ ^-------\ .+\ -------$ ]] ; then
echo "$(date --rfc-3339=seconds) → Message from '$sndr' received."
echo "$(date --rfc-3339=seconds) → Report from '$sndr' received."
echo "$msg" > "$logdir/$sndr"
else
echo "$msg" >> "$logdir/$sndr"

View file

@ -29,14 +29,14 @@ find_outdated(){
while IFS= read -r -d '' file ; do
running=$(( running + 1 ))
$debug && echo -n "Processing host '$file' with IP address "
d="$(sed -nE "s/\s+drwx.+([0-9]{4}-[0-9]{2}-[0-9]{2} \S+).*/\1/p" "$file" | head -1)"
if [[ -z "$d" ]] || [[ $(date --date="$d" +%s) -lt $(date --date="$git_date" +%s) ]] ; then
d="$(sed -nE "s/^2\s+(\S.+)$/\1/p" "$file")"
if [[ -z "$d" ]] || \
[[ $(date --date="$d" +%s) -lt $(date --date="$git_date" +%s) ]] ; then
r='([0-9]{1,3}\.){3}[0-9]{1,3}'
ipa="$(sed -nE "s|^\s+default via.+ src ($r) metric.+|\1|p" "$file" | head -1)"
ipa="$(sed -nE "s/^3\s+default via.+ src ($r) metric.+/\1/p" "$file")"
if [[ -z "$ipa" ]] ; then
# FIXME: Outdated report format, trying fallback:
ipa="$(grep -E "\s+2: en" -A3 "$file" | \
sed -nE "s|.+inet (10\.190\.[[:digit:]]{1,3}\.[[:digit:]]{1,3})/.+|\1|p")"
# FIXME: Outdated report format, try fallback:
ipa="$(sed -nE "s|^.+default via.+ src ($r) metric.+|\1|p" "$file" | head -1)"
fi
$debug && echo "'$ipa'."
if ! grep -q "$ipa" "$dir/${git_date//T*/}" ; then