Server IP : 149.255.58.128 / Your IP : 216.73.216.153
System : Linux cloud516.thundercloud.uk 5.14.0-427.26.1.el9_4.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Jul 17 15:51:13 EDT 2024 x86_64
PHP Version : 8.2.28
Disable Function : allow_url_include, apache_child_terminate, apache_setenv, exec, passthru, pcntl_exec, posix_kill, posix_mkfifo, posix_getpwuid, posix_setpgid, posix_setsid, posix_setuid, posix_setgid, posix_seteuid, posix_setegid, posix_uname, proc_close, proc_get_status, proc_open, proc_terminate, shell_exec, show_source, system
cURL : ON | WGET : ON | Sudo : OFF | Pkexec : OFF
Upload Files :
Command :
Current File : /lib64/nagios/plugins//check_jetbackup
#!/usr/bin/perl -w
use POSIX qw/strftime/;
#use Data::Dumper;
use strict;
########################################################################
# check_nagios.pl
# (c) 2022 Unlimited Web Hosting
#######################################################################
#
# Documentation.
#
# Checks logfiles from /usr/local/jetapps/var/log/jetbackup/backup/
# for logfiles with the string 'Job Completed', making note of which
# days have a completed backup recorded.
#
# Based on this, report nagios status based as
#
# OK - if there was a backup Yesterday
# WARNING - if there was no backup yesterday, but there are
# at least 5 backups in the last 7 days
# CRITICAL - if there was no successful backup yesterday, and
# there are fewer than 5 backups in the last 7 days
#
# Supports Jetbackup 4 and Jetbackup 5, these have very different
# logfile formats
#######################################################################
my %completed_backups;
my %partial_backups;
if (-d '/usr/local/jetapps/var/log/jetbackup5') {
%completed_backups = &completed_backups_jetbackup5(0);
}
elsif (-d '/usr/local/jetapps/var/log/jetbackup') {
%completed_backups = &completed_backups_jetbackup4(0);
}
my @last_week_dates = &dates_last_week();
my $NAGIOS_OK = 0;
my $NAGIOS_WARNING = 1;
my $NAGIOS_CRITICAL = 2;
my $NAGIOS_UNKNOWN = 3;
my $PANEL = panel_type();
my $EMPTY = check_empty($PANEL);
# Find out if we have a backup for yesterday, and how many completed
# backups we have withihn the last wek
my $yesterday = strftime("%d %B %Y", localtime(time() - (60 * 60 * 24)));
my $today = strftime("%d %B %Y", localtime(time()));
my $completed_yesterday = $completed_backups{$yesterday} ? 1 : 0;
my $completed_today = $completed_backups{$today} ? 1 : 0;
my $partial_today; # We only check for these if needed, avoiding uneccessary processing
my $partial_yesterday;
my $num_completed = 0;
my $num_partial = 0;
foreach my $this_date (@last_week_dates) {
if ($completed_backups{$this_date}) { $num_completed++; };
}
# if we have no completed items, check for partial completion
if ($num_completed == 0) {
if (-d '/usr/local/jetapps/var/log/jetbackup5') {
%partial_backups = &completed_backups_jetbackup5(1);
}
elsif (-d '/usr/local/jetapps/var/log/jetbackup') {
%partial_backups = &completed_backups_jetbackup4(1);
}
$partial_yesterday = $partial_backups{$yesterday} ? 1 : 0;
$partial_today = $partial_backups{$today} ? 1 : 0;
foreach my $this_date (@last_week_dates) {
if ($partial_backups{$this_date}) { $num_partial++; };
}
}
if (($completed_yesterday || $completed_today) || ($num_completed >= 5) || $EMPTY) {
print "OK - backup completed yesterday, or today, or at least 5 backups in 7 days";
exit $NAGIOS_OK;
}
elsif (($partial_yesterday || $partial_today) || ($num_partial >= 5)) {
print "WARNING - backup PARTIALLY completed yesterday, or today, or at least 5 backups in 7 days";
exit $NAGIOS_WARNING;
}
else {
print "CRITICAL - no backup yesterday or today, and ";
if ($num_completed) { print "only "; };
print $num_completed . " backups in the last week";
exit $NAGIOS_CRITICAL;
}
# Jetbackup4 Logfile checker
# Returns an array of strings, each of which is a date string
# representing a completed backup found in the directory
# /usr/local/jetapps/var/log/jetbackup/backup/
sub completed_backups_jetbackup4() {
my $partial_mode = shift;
my $grep_command = '';
if ($partial_mode) {
$grep_command = 'grep -i "Partially Completed" /usr/local/jetapps/var/log/jetbackup/backup/*';
}
else {
$grep_command = 'grep -i "Completed" /usr/local/jetapps/var/log/jetbackup/backup/*';
}
my @completed_days_raw = `$grep_command`;
my %completed_days;
if (!@completed_days_raw) {
return %completed_days;
}
else {
foreach my $completed_day (@completed_days_raw) {
chomp $completed_day;
$completed_day =~ s/^.*\[//;
$completed_day =~ s/\].*//;
$completed_day =~ s/(\d{2}\s+\w+\s+\d+).*/$1/;
$completed_days{$completed_day} = 1;
}
return %completed_days;
}
}
# Jetbackup5 Logfile checker
# uses zgrep to inspect the logfile (jetbackupd.log-YYYYMMDD) for
# each of the last 7 days, including today, and count how many
# successful account backups were recorded
# returns an associative array, whose keys are Date strings of the
# form e.g. '04 April 2022', and whose values are the number of accounts
# backed up on the day as per the log
sub completed_backups_jetbackup5() {
my $partial_mode = shift;
my %completed_days;
my @logfiles;
my @days;
my %logmap;
my $timenow = time();
#Are logs rotated?
my $logs_rotated = 0;
my @rotated = glob '/usr/local/jetapps/var/log/jetbackup5/jetbackupapi.*.gz';
if (@rotated) {
$logs_rotated = 1;
}
# If logs are rotated, check the appropriate logfiles for each day
if ($logs_rotated) {
for (my $i = 0; $i < 7; $i++) {
my $thistime = $timenow - ($i * (60 * 60 * 24));
my $logfilename = 'jetbackupd.log-' . strftime("%Y%m%d", localtime($thistime)) . '.gz';
my $datestring = strftime("%d %B %Y", localtime($thistime));
push(@logfiles,$logfilename);
push(@days, $datestring);
$logmap{$logfilename} = $datestring;
}
foreach my $logfile (@logfiles) {
if (-e '/usr/local/jetapps/var/log/jetbackup5/' . $logfile) {
my $grep_command = '';
if ($partial_mode) {
$grep_command = "zgrep -c 'Backup partially completed for the account' /usr/local/jetapps/var/log/jetbackup5/$logfile";
}
else {
$grep_command = "zgrep -c 'Backup completed for the account' /usr/local/jetapps/var/log/jetbackup5/$logfile";
}
my $logfile_check = `$grep_command`;
chomp $logfile_check;
$completed_days{$logmap{$logfile}} = $logfile_check;
}
}
}
# If not, use grep to find 'Backup completed' strings for each date based on timestaps strings
else {
for (my $i = 0; $i < 7; $i++) {
my $thistime = $timenow - ($i * (60 * 60 * 24));
my $datestring_in_logs = strftime("%d/%b/%Y", localtime($thistime));
my $datestring = strftime("%d %B %Y", localtime($thistime));
my $grep_command = '';
if ($partial_mode) {
$grep_command = "grep -a -P '^\\[" . $datestring_in_logs . ".*Backup partially completed for the account' /usr/local/jetapps/var/log/jetbackup5/jetbackupd.log";
}
else {
$grep_command = "grep -a -P '^\\[" . $datestring_in_logs . ".*Backup completed for the account' /usr/local/jetapps/var/log/jetbackup5/jetbackupd.log";
}
my @hits = `$grep_command`;
if (@hits) {
$completed_days{$datestring} = scalar(@hits);
}
}
}
return %completed_days;
}
# Returns an array of strings, each of which is a date string for a day
# in the last 7 days, (starting from the current day) with a format
# matching the format used in jetbackup logs
# Note: we keep 14 days of backups, but JB only keeps 7 days of logs
sub dates_last_week() {
my @days;
my $timenow = time();
for (my $i = 0; $i < 7; $i++) {
my $thistime = $timenow - ($i * (60 * 60 * 24));
push(@days,strftime("%d %B %Y", localtime($thistime)));
}
return @days;
}
# Returns a string ('cpanel' or 'plesk') indicating the panel type
sub panel_type {
my $panel_type = `[ -f /etc/plesk-release ] && echo "plesk" || echo "cpanel"`;
chomp $panel_type;
return $panel_type;
}
# Checks if this panel is actually empty, to avoid spamming NAGIOS with alerts
# indicating that there have been no backups created for servers where there is
# nothing to back up
sub check_empty {
my $panel_type = shift;
my $is_empty = 0;
my $is_empty_command = '';
if ($panel_type eq 'cpanel') {
$is_empty_command = "grep -v 'nobody\\|0default' /etc/userdomains";
}
else {
$is_empty_command = 'plesk db -Ne "select * from clients limit 1"';
}
$is_empty = `$is_empty_command`;
return $is_empty ? 0 : 1;
}