Compare commits

...

5 Commits

Author SHA1 Message Date
Asif Bacchus
97c7105cb9 Corrected [INFO] tagging to have '--' prepended for consistency 2018-09-27 17:02:49 -06:00
Asif Bacchus
7f4de4f0f3 Changed REGEXs to look for updated tags in log file 2018-09-27 17:00:48 -06:00
Asif Bacchus
f0bc037416 Updated comments 2018-09-27 16:57:41 -06:00
Asif Bacchus
3b92b2c509 Added all other detail levels 2018-09-27 16:53:23 -06:00
Asif Bacchus
462d081b14 Rewrite logwatch script: changed to PERL instead of BASH.
Initial code, summary only.
2018-09-27 16:40:53 -06:00
2 changed files with 116 additions and 41 deletions

View File

@ -253,21 +253,21 @@ if [ -z "$ipAddress" ]; then
quit 201 quit 201
fi fi
fi fi
echo -e "${info}${stamp} [INFO] Using IP address:" \ echo -e "${info}${stamp} -- [INFO] Using IP address:" \
"$ipAddress" >> "$logFile" "$ipAddress" >> "$logFile"
## Check if desired record(s) exist at CloudFlare ## Check if desired record(s) exist at CloudFlare
# perform checks on A or AAAA records based on invocation options # perform checks on A or AAAA records based on invocation options
if [ $ip4 -eq 1 ]; then if [ $ip4 -eq 1 ]; then
echo -e "${normal}${stamp} [INFO] Updating A records: ${dnsRecords[*]}" \ echo -e "${normal}${stamp} -- [INFO] Updating A records: ${dnsRecords[*]}" \
>> "$logFile" >> "$logFile"
for cfLookup in "${dnsRecords[@]}"; do for cfLookup in "${dnsRecords[@]}"; do
cfRecords+=("$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/${cfDetails[2]}/dns_records?name=$cfLookup&type=A" -H "X-Auth-Email: ${cfDetails[0]}" -H "X-Auth-Key: ${cfDetails[1]}" -H "Content-Type: application/json")") cfRecords+=("$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/${cfDetails[2]}/dns_records?name=$cfLookup&type=A" -H "X-Auth-Email: ${cfDetails[0]}" -H "X-Auth-Key: ${cfDetails[1]}" -H "Content-Type: application/json")")
done done
elif [ $ip6 -eq 1 ]; then elif [ $ip6 -eq 1 ]; then
echo -e "${normal}${stamp} [INFO] Updating AAAA records: ${dnsRecords[*]}" \ echo -e "${normal}${stamp} -- [INFO] Updating AAAA records:"\
>> "$logFile" "${dnsRecords[*]}" >> "$logFile"
for cfLookup in "${dnsRecords[@]}"; do for cfLookup in "${dnsRecords[@]}"; do
cfRecords+=("$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/${cfDetails[2]}/dns_records?name=$cfLookup&type=AAAA" -H "X-Auth-Email: ${cfDetails[0]}" -H "X-Auth-Key: ${cfDetails[1]}" -H "Content-Type: application/json")") cfRecords+=("$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/${cfDetails[2]}/dns_records?name=$cfLookup&type=AAAA" -H "X-Auth-Email: ${cfDetails[0]}" -H "X-Auth-Key: ${cfDetails[1]}" -H "Content-Type: application/json")")
done done

View File

@ -1,44 +1,119 @@
#!/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 use strict;
###
### Levels: ### Get Logwatch detail level
### Levels are defined in this script as 0-4, 5 & 5+ my $detailLevel = $ENV{'LOGWATCH_DETAIL_LEVEL'} || 0;
### 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.
### Create temporary file for assembling log entries ### Declare variables
cfddnsTmpFile=$(mktemp -p "${LOGWATCH_TEMP_DIR}") my $summaryErr;
my $summaryStatusUpToDate;
my $summaryStatusNeedsUpdating;
my $summarySuccess;
### Assemble the temp file according to desired Logwatch detail level my %reportHash = ();
if [ "$LOGWATCH_DETAIL_LEVEL" -lt 5 ]; then my $key;
# 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
### 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 ### Minimal detail level: provide summary data only
rm -f "${cfddnsTmpFile}" > /dev/null 2>&1 if ($detailLevel == 0) {
exit 0 ### process logfile and summarize message types
while (defined(my $ThisLine = <STDIN>)) {
if ($ThisLine =~ /\-- \[ERROR\] /) {
$summaryErr++;
}
elsif ($ThisLine =~ /up-to-date./) {
$summaryStatusUpToDate++;
}
elsif ($ThisLine =~ /needs updating.../) {
$summaryStatusNeedsUpdating++;
}
elsif ($ThisLine =~ /\-- \[SUCCESS\] /) {
$summarySuccess++;
}
}
### fill hash table 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";
}
}
### Levels 1-4 provide the actual error, status and success messages instead of
### a summary count
elsif ($detailLevel >= 1 && $detailLevel <= 4) {
while (defined(my $ThisLine = <STDIN>)) {
if ($ThisLine =~ /\-- \[ERROR\] /) {
print $ThisLine;
}
elsif ($ThisLine =~ /\-- \[STATUS\] /) {
print $ThisLine;
}
elsif ($ThisLine =~ /\-- \[SUCCESS\] /) {
print $ThisLine;
}
}
}
### Level 5 is similiar to levels 1-4 except it also reports informational
### messages such as the current working IP address and hostnames being
### checked. This is useful when verifying the cfddns.sh script's operation.
elsif ($detailLevel == 5) {
while (defined(my $ThisLine = <STDIN>)) {
if ($ThisLine =~ /\-- \[ERROR\] /) {
print $ThisLine;
}
elsif ($ThisLine =~ /\-- \[STATUS\] /) {
print $ThisLine;
}
elsif ($ThisLine =~ /\-- \[SUCCESS\] /) {
print $ThisLine;
}
elsif ($ThisLine =~ /\-- \[INFO\] /) {
print $ThisLine;
}
}
}
### Any level above 5 will echo the entire log including the debugging notes
### within the script meant for troubleshooting. Using this level of detail
### should only be done if you cannot view the actual log file directly for
### whatever reason. The actual log file is colour-coded for easier debugging.
elsif ($detailLevel > 5) {
while (defined(my $ThisLine = <STDIN>)) {
print $ThisLine;
}
}
### Exit gracefully
exit (0);
# vi: shiftwidth=3 tabstop=3 et
# Local Variables:
# mode: perl
# perl-indent-level: 3
# indent-tabs-mode: nil
# End: