From 462d081b149a5b4432c002ac1d83dedcfd7d74cc Mon Sep 17 00:00:00 2001 From: Asif Bacchus Date: Thu, 27 Sep 2018 16:40:53 -0600 Subject: [PATCH] Rewrite logwatch script: changed to PERL instead of BASH. Initial code, summary only. --- etc/logwatch/scripts/services/cfddns | 107 ++++++++++++++++++--------- 1 file changed, 70 insertions(+), 37 deletions(-) diff --git a/etc/logwatch/scripts/services/cfddns b/etc/logwatch/scripts/services/cfddns index 41c8099..16e3a6d 100644 --- a/etc/logwatch/scripts/services/cfddns +++ b/etc/logwatch/scripts/services/cfddns @@ -1,44 +1,77 @@ -#!/bin/bash +#!/usr/bin/perl + +############################################################################# +# $Id$ +############################################################################# +# Log: CloudFlare updater script (cfddns) +# Revision 1.0 2018/09/26 +# Written by Asif Bacchus +############################################################################# -### Script to parse cfddns.sh log file and display important information -### -### Levels: -### Levels are defined in this script as 0-4, 5 & 5+ -### Levels 0-4 (same): Basic information reporting -### Reports only critical information such as errors, update successes and -### failures. -### Level 5: Recommended information reporting level -### Adds informational messages such as the hostnames being updated and the -### IP address being used for that update. -### Level 5+ (same): Verbose logging -### This will just reprint the entire log. This should only be used when -### checking the actual logfile is not possible or you prefer looking at it -### via Logwatch for some reason. Note: ANSI colouring is removed by this -### script to faciliate Logwatch conversion to HTML. Therefore, viewing -### that actual log file is highly recommended instead of via this level of -### reporting in Logwatch. +use strict; + +### Get Logwatch detail level +my $detailLevel = $ENV{'LOGWATCH_DETAIL_LEVEL'} || 0; -### Create temporary file for assembling log entries -cfddnsTmpFile=$(mktemp -p "${LOGWATCH_TEMP_DIR}") +### Declare variables +my $summaryErr; +my $summaryStatusUpToDate; +my $summaryStatusNeedsUpdating; +my $summarySuccess; -### Assemble the temp file according to desired Logwatch detail level -if [ "$LOGWATCH_DETAIL_LEVEL" -lt 5 ]; then - # get error, success and status messages - { grep '\-- \[ERROR\]' ; grep '\-- \[SUCCESS\]' ; \ - grep '\-- \[STATUS\]' ; } >> "$cfddnsTmpFile" -elif [ "$LOGWATCH_DETAIL_LEVEL" -eq 5 ]; then - # get error, success, status and info messages - { grep '\-- \[ERROR\]' ; grep '\-- \[SUCCESS\]' ; \ - grep '\-- \[STATUS\]' ; grep '\-- \[INFO\]' ; } >> "$cfddnsTmpFile" -elif [ "$LOGWATCH_DETAIL_LEVEL" -gt 5 ]; then - cat -fi +my %reportHash = (); +my $key; -### Strip ANSI escape codes and sort entries according to timestamps -sed -e 's,\x1B\[[0-9;]*[a-zA-Z],,g' "${cfddnsTmpFile}" | sort -n -### Clean-up and exit -rm -f "${cfddnsTmpFile}" > /dev/null 2>&1 -exit 0 +### Minimal detail level: provide summary data only +if ($detailLevel == 0) { + ### Process logfile and summarize message types + while (defined(my $ThisLine = )) { + if ($ThisLine =~ /\-- \[ERROR\] /) { + $summaryErr++; + } + elsif ($ThisLine =~ /up-to-date./) { + $summaryStatusUpToDate++; + } + elsif ($ThisLine =~ /needs updating.../) { + $summaryStatusNeedsUpdating++; + } + elsif ($ThisLine =~ /\-- \[SUCCESS\] /) { + $summarySuccess++; + } + } + + ### Fill hashtable with headings and summary counts + if ($summaryStatusNeedsUpdating > 0) { + $reportHash{"Entries needing updates"} = $summaryStatusNeedsUpdating; + } + if ($summarySuccess > 0) { + $reportHash{"Entries successfully updated"} = $summarySuccess; + } + if ($summaryStatusUpToDate > 0) { + $reportHash{"Entries already up-to-date"} = $summaryStatusUpToDate; + } + if ($summaryErr > 0) { + $reportHash{"Errors encountered"} = $summaryErr; + } + + ### Print hash table + foreach $key (sort keys %reportHash) { + print "$key: $reportHash{$key}\n"; + } +} + + + + +### Exit gracefully +exit (0); + +# vi: shiftwidth=3 tabstop=3 et +# Local Variables: +# mode: perl +# perl-indent-level: 3 +# indent-tabs-mode: nil +# End: \ No newline at end of file