From: Jan Kratochvil Date: Sat, 19 Jun 2021 08:35:29 +0000 (+0200) Subject: +bin/upsc-log,+bin/upsc-log-diff X-Git-Url: https://git.jankratochvil.net/?p=nethome.git;a=commitdiff_plain;h=fc43ae4268f007cec7235c5eb24af311a155cfcc +bin/upsc-log,+bin/upsc-log-diff --- diff --git a/bin/upsc-log b/bin/upsc-log new file mode 100755 index 0000000..7298a39 --- /dev/null +++ b/bin/upsc-log @@ -0,0 +1,38 @@ +#! /bin/bash +renice >/dev/null +19 -p $$ +ionice -c3 -p $$ +t=/tmp/upsc-log.$$ +rm -f $t $t.* +log=/var/log/upsc.log +exec >>$log 2>&1 +while sleep 1;do + date --iso=seconds >$t.t + upsc eaton &>$t + if grep -q '^ups.status: OL$' $t && grep -q '^battery.charge: 100$' $t;then + rm -f $t.bad $t.bad.t + if [ ! -e $t.good ];then + cat $t.t $t;echo + sync -d $log + fi + mv -f $t.t $t.good.t + mv -f $t $t.good + rm -f /tmp/upsc-smsed + continue + fi + if [ ! -e /tmp/upsc-smsed ];then + touch /tmp/upsc-smsed + date --iso=seconds|mail -s "upsc-log $(hostname)" jankratochvil@vodafonemail.cz + fi + if [ -e $t.good ];then + cat $t.good.t $t.good;echo + sync -d $log + rm -f $t.good.t $t.good + fi + if [ -e $t.bad ] && cmp -s $t $t.bad;then + continue + fi + cat $t.t $t;echo + sync -d $log + mv -f $t.t $t.bad.t + mv -f $t $t.bad +done diff --git a/bin/upsc-log-diff b/bin/upsc-log-diff new file mode 100755 index 0000000..6c21015 --- /dev/null +++ b/bin/upsc-log-diff @@ -0,0 +1,45 @@ +#! /usr/bin/perl +use strict; +use warnings; +local *F; +my $ta="/tmp/upsc-log-diff.".$$."a"; +my $tb="/tmp/upsc-log-diff.".$$."b"; +unlink $ta; unlink $tb; +END { + unlink $ta; unlink $tb; +} +my $F="/var/log/upsc.log"; +open F,$F or die "$F: $!"; +my $prev; +my $this=""; +while () { + chomp; + if (!/^$/) { + $this.="$_\n"; + next; + } + if ($prev) { + local *TA; + local *TB; + open TA,">$ta" or die "$ta: $!"; + open TB,">$tb" or die "$tb: $!"; + print TA $prev or die "$ta: $!"; + print TB $this or die "$tb: $!"; + close TA or die "$ta: $!"; + close TB or die "$ta: $!"; + local *TD; + my $td="diff -u $ta $tb|"; + open TD,$td or die "$td: $!"; + =~/^\Q--- / or die $_; + =~/^\Q+++ / or die $_; + while () { + next if /^[@ ]/; + print; + } + close TD; # or die "$td: $!"; + print "\n"; + } + $prev=$this; + $this=""; +} +die $this if $this;