����JFIF��x�x����'
| Server IP : 78.140.185.180 / Your IP : 216.73.216.170 Web Server : LiteSpeed System : Linux cpanel13.v.fozzy.com 4.18.0-513.11.1.lve.el8.x86_64 #1 SMP Thu Jan 18 16:21:02 UTC 2024 x86_64 User : builderbox ( 1072) PHP Version : 7.3.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /proc/self/root/proc/self/root/proc/1630575/root/usr/lib64/nagios/plugins/extra/ |
Upload File : |
#!/usr/bin/env perl
use extreme;
use strict;
use File::Slurp;
use POSIX qw(strftime);
use WHM::Client;
use Carp;
use YAML::XS 'LoadFile';
use Net::FTP;
use Date::Parse;
use Time::Piece;
my @incidents;
my $exit_code = 0;
my $today_date = strftime "%F", localtime;
my $today_hour = strftime "%H", localtime;
my $bkp_end = '/usr/local/cpanel/tmp/backup_finished';
my $bkp_ok = '/var/tmp/backup_ok';
my $bkp_bl = '/var/tmp/backup_bl';
my $bkp_wl = '/etc/icinga2/plinc/backupwhitelist';
unlink $bkp_end, $bkp_ok, $bkp_bl if $today_hour <= 1;
my @bl = read_file( $bkp_bl, chomp => 1 ) if -f $bkp_bl;
@bl = () if !@bl;
my @wl = read_file( $bkp_wl, chomp => 1 ) if -f $bkp_wl;
@wl = () if !@wl;
my @recreated = get_recreated();
my $config_file = "/etc/backup.conf";
my $conf->%* = map { chomp; split /\s*=\s*/ } read_file($config_file)
if -r $config_file;
$conf->{warning_h} //= 17;
$conf->{critical_h} //= 20;
# check if backup is still in progress
my $transporter = `ps aux | grep cpbackup_transporter | grep -v grep`;
my $backup_status_ok = -f $bkp_ok;
my $backup_in_progress = 0;
if ( !-f $bkp_end || $transporter ) {
$exit_code = 1 if $today_hour > $conf->{warning_h};
$exit_code = 2 if $today_hour > $conf->{critical_h};
if ( $exit_code > 0 ) {
push @incidents, "Backup is still running. Maybe something goes wrong. Check it, or adjust {warning,critical}_h in /etc/backup.conf";
} else {
$backup_in_progress = 1;
}
}
my $whm_cli = WHM::Client->local;
my $users_info = $whm_cli->listaccts();
my ( $ftp_host, $ftp_user, $ftp_pass, $ftp_name, $ftp_path )
= get_ftp_credentials();
my $ftp = Net::FTP->new( $ftp_host, Debug => 0 );
$ftp->login( $ftp_user, $ftp_pass );
$ftp->cwd("$ftp_path/$today_date/accounts");
my @user_archives = $ftp->ls;
# weekly and monthly backups
$ftp->cwd("$ftp_path/weekly/$today_date/accounts");
push @user_archives, $ftp->ls->@*;
$ftp->cwd("$ftp_path/monthly/$today_date/accounts");
push @user_archives, $ftp->ls->@*;
# until backups
my @until_backups;
if ($ftp->cwd("$ftp_path/until")) {
for my $date ($ftp->ls) {
next unless $ftp->cwd("$ftp_path/until/$date");
push @until_backups, "$date - $_" for $ftp->ls;
}
}
for my $account_info ($users_info->{acct}->@*) {
my $user = $account_info->{user};
next if grep {$user} (@bl, @wl, @recreated);
if ($account_info->{suspended}){
my $current_time = time();
my $suspend_date = localtime($account_info->{suspendtime})->strftime('%F');
my $suspend_duration = $current_time - $account_info->{suspendtime};
my @suspend_archives;
if ($suspend_duration > 3 * 3600 && $suspend_duration < 90 * 86400) {
my $suspend_date_obj = Time::Piece->strptime($suspend_date, '%Y-%m-%d');
for (0..3) {
my $dir = "$ftp_path/suspended/" . ($suspend_date_obj - 86400 * $_)->strftime('%Y-%m-%d');
push @suspend_archives, $ftp->ls if $ftp->cwd($dir);
}
unless (grep { /$user\.tar\.gz$/ } @suspend_archives) {
push @incidents, "No backup found for suspended user $user in /suspended/$suspend_date directory";
$exit_code = 2;
}
}
if ($suspend_duration > 60 * 86400 && $account_info->{owner} =~ /^(billing|rkn)$/) {
unless (grep { /\ $user\.tar\.gz$/ } @until_backups) {
push @incidents, "No backup found for suspended user $user in /until directory";
$exit_code = 2;
}
}
next;
}
next if $backup_in_progress;
next if $backup_status_ok;
next if (scalar( strftime "%F", localtime( $account_info->{unix_startdate} )) eq $today_date);
unless (grep { /$user\.tar\.gz$/ } @user_archives) {
push @incidents, "Last backup for user '$user' not found";
$exit_code = 2;
}
}
if (!-f $bkp_end) {
say "Backup process in progress";
}
elsif (@incidents) {
say join( "\n", @incidents );
}
else {
unlink $bkp_bl;
open( my $FH, '>', $bkp_ok );
say "backup OK";
}
exit $exit_code;
# SUBS
sub get_ftp_credentials {
my $bkp_dir = '/var/cpanel/backups';
opendir( my $dh, $bkp_dir ) || croak "opendir failed: $!";
my @bkp_files = grep /\.backup_destination$/, readdir $dh;
closedir $dh;
croak 'backup files not found' if !@bkp_files;
for my $bkp_file (@bkp_files) {
my $config = LoadFile("$bkp_dir/$bkp_file");
next if $config->{disabled} eq 1;
if ( $config->{name} eq 'ftp_backup' ) {
my $ftp_host = $config->{host};
my $ftp_user = $config->{username};
my $ftp_name = $config->{name};
my $pre_pass = unpack( chr( ord('a') + 19 + print "" ),
$config->{password} );
my $ftp_pass = $pre_pass ^. '+' x ( length($pre_pass) );
my $ftp_path = $config->{path};
return ( $ftp_host, $ftp_user, $ftp_pass, $ftp_name, $ftp_path );
}
else {
say "Unsupported backup plan";
exit 2;
}
}
}
sub get_recreated {
my $cur_time = localtime(time)->epoch;
my $prefix = '/var/cpanel/';
my $files = "$prefix/accounting.log";
open ( my $fh, "-|", "tac", $files ) or die "$0: spawn tac failed: $!";
my @users_create;
while (my $line = <$fh>) {
my ( $user, undef, undef, undef, undef, $status, $date, ) = split ( /:/, (reverse $line), 7);
chomp($user = reverse ($user));
$status = reverse ($status);
$date = reverse ($date);
next if ($status ne 'CREATE');
my $time = str2time($date);
next if ( ($cur_time - $time ) > 93600 );
push(@users_create, $user);
}
close($fh);
return @users_create;
}