Archivi tag: nagios

Veeam Backup Jobs Failed – VBScript Nagios Check

' Nagios Plugn for reading Veeam Backup and Replication's log
' It is intended to use with NSClient++


Dim rad,txt,dag,alert,status

Set cnn = CreateObject("ADODB.Connection")
Set Rs = CreateObject("ADODB.Recordset")

cnn.ConnectionString = "driver={SQL Server};server=localhost\VEEAMSQL2012;Trusted_Connection=Yes;database=VeeamBackup"
cnn.Open
rs.ActiveConnection = cnn
exitcode = 0
txt=""
allert()
rs.close
If txt > "" Then
  txt = "BACKUP VIRTUAL MACHINE IN ERRORE: \n" & txt & " | "
else
  txt = "OK"
  exitcode=0
End If

wscript.echo(txt)
wscript.quit exitcode

Function allert()
  Const ForReading = 1, ForWriting = 2, ForAppending = 8
  Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
  'strsql = "SELECT name, repo_name,progress,result,target_host_name   FROM [VeeamBackup].[dbo].[ReportJobsView]   where latest_result=2"
  strsql = "SELECT b.object_name as object_name, a.job_name as job_name, a.creation_time as creation_time, a.end_time as end_time, a.total_size as total_size, a.stored_size as stored_size FROM dbo.ReportSessionView a inner join dbo.ReportSessionInfoView b on a.id = session_id where a.result = '2' and a.id is not null and cast(a.creation_time as date) > dateadd(day,-1, cast(getdate() as date)) order by 2 desc"
  rs.Open strsql , cnn,3,3
  Do While Not rs.EOF 
  
                rad = rs("object_name") & " Job: " & rs("job_name") & " Data Inizio: " & rs("creation_time") & " Data Fine: " & rs("end_time") & " Dimensioni Totali: " & rs("total_size") & " Dimensioni Compresse: " & rs("stored_size") & "\n "
        
            Set fso = CreateObject("Scripting.FileSystemObject")
            txt = txt + rad 
        
        rs.MoveNext
    Loop
    exitcode = 2
    Exit Function
End Function

modified from: https://exchange.nagios.org/directory/Plugins/Backup-and-Recovery/Check-Veeam-Backup-Jobs/details

Last Succesful Backup Bareos – Nagios Check

example:

./check_bareos_last_backup.pl -c 3 -w 6 -j CopyToTape -O 192.168.59.211 -d bareos -u bareos_webui -p citestpass

source:

#!/usr/bin/perl -w

# Author: Marco Braidotti
# Quite a lot copied from check_bacula.pl which is GPL.

## examples: 

##    This program is free software: you can redistribute it and/or modify
##    it under the terms of the GNU General Public License as published by
##    the Free Software Foundation, either version 3 of the License, or
##    (at your option) any later version.
##
##    This program is distributed in the hope that it will be useful,
##    but WITHOUT ANY WARRANTY; without even the implied warranty of
##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##    GNU General Public License for more details.
##
##    You should have received a copy of the GNU General Public License
##    along with check_bareos_last_backup.pl.  If not, see <http://www.gnu.org/licenses/>.


use strict;
use POSIX;
use File::Basename;
use DBI;
use DBD::Pg;
use Switch;
use Getopt::Long;
use vars qw(
       $opt_help
           $opt_job
           $opt_critical
           $opt_warning
           $opt_level
           $opt_usage
           $opt_version
           $out
           $sql
           $date_start
           $date_stop
           $state
           $count
           );
           
sub print_help();
sub print_usage();
sub get_now();
sub get_date;

my $sql;
my $sq11;
my $sq12;
my $sql3;
my $sql4;
my $sqlresult;
my $days;
my $end;
my $client;
my $test;
my $clientresult;
my $level;
my $progname = basename($0);
my $progVers = "0.9";
my $sqlHost;
my $sqlDB;
my $sqlUsername;
my $sqlPassword;


my %ERRORS = (  'UNKNOWN'       =>      '3',
                'OK'            =>      '0',
                'WARNING'       =>      '1',
                'CRITICAL'      =>      '2');
                
Getopt::Long::Configure('bundling');
GetOptions
        (
        "c=s"   =>      \$opt_critical, "critical=s"    =>      \$opt_critical,
        "w=s"   =>      \$opt_warning,  "warning=s"     =>      \$opt_warning,
        ##"L=s"   =>      \$opt_level,    "level=s"       =>      \$opt_level,
        "j=s"   =>      \$opt_job,      "job=s"         =>      \$opt_job,
        "h"     =>      \$opt_help,     "help"          =>      \$opt_help,
                                        "usage"         =>      \$opt_usage,
    "O=s"     =>      \$sqlHost,  "hostname"       =>      \$sqlHost,
    "d=s"     =>      \$sqlDB,  "database"       =>      \$sqlDB,
    "u=s"     =>      \$sqlUsername,  "username"       =>      \$sqlUsername,
    "p=s"     =>      \$sqlPassword,  "password"       =>      \$sqlPassword,
        "V"     =>      \$opt_version,  "version"       =>      \$opt_version
        ) || die "Try '$progname --help' for more information.\n";

    
sub print_help() {
 print "\n";
 print "If Bacula holds its MySQL-data behind password, you have to manually enter the password into the script as variable \$sqlPassword.\n";
 print "And be sure to prevent everybody from reading it!\n";
 print "\n";
 print "Options:\n";
 ##print "L       check backup level F for full, D for differential, I for incremential\n";
 print "c       max days since successful job for not returning critical\n";
 print "w       max days since successful job for not returning warning\n";
 print "j       name of the job to NOT check (case-sensitive)\n";
 print "h       show this help\n";
 print "V       print script version\n";
 print "O       host postgresql\n";
 print "d       database postgresql\n";
 print "u       database user postgresql\n";
 print "p       database password postgresql\n";
}

sub print_usage() {
 print "Usage: $progname -c <critical> -w <warning> -j <job-name> [ -h ] [ -V ]\n";
}

if ($opt_help) {
 print_usage();
 print_help();
 exit $ERRORS{'UNKNOWN'};
}

if ($opt_usage) {
 print_usage();
 exit $ERRORS{'UNKNOWN'};
}

if ($opt_version) {
 print "$progname $progVers\n";
 exit $ERRORS{'UNKNOWN'};
}

##switch ($opt_level) {

##		case "F"	{ $level = "full";}
##		case "D"	{ $level = "differential";}
##		case "I"	{ $level = "incremental";}
##		else		{ print_usage(); exit $ERRORS{'UNKNOWN'}; }
##}

if ($opt_job && $opt_warning && $opt_critical) {
 my $dsn = "DBI:Pg:dbname=$sqlDB;host=$sqlHost";
 my $dbh = DBI->connect( $dsn,$sqlUsername,$sqlPassword ) or die "Error connecting to: '$dsn': $DBI::errstr\n";

##$sql = "SELECT  Datediff(NOW(), EndTime) as \'days\', EndTime AS \'end\' FROM `Job` WHERE Name = \"$opt_job\" and JobStatus='T' AND Level = \"$opt_level\" ORDER BY Datediff(NOW(), EndTime) ASC LIMIT 1;";

my $sql1 = "select DATE_PART('day', now() - EndTime) as days, t.endtime as end, t.name as client, JobStatus from job t right join ( select name, max(endtime) as maxdate from job where jobstatus = 'T' group by name ) tm on t.name = tm.name and t.endtime = tm.maxdate where t.name <> 'RestoreFiles' and t.name not in ('";
my $sql2 = "') order by days";

$sql = join "", $sql1, $opt_job, $sql2;

my $sth = $dbh->prepare($sql) or die "Error preparing statement",$dbh->errstr;
$sth->execute;

my @row;
while ( @row = $sth->fetchrow_array()) {
($days,$end,$client) = @row;
}
$sth->finish;
$dbh->disconnect();

if (! $end) { print "Bareos Backup UNKNOWN: Successful job for $opt_job - $days - $end not found.\n"; exit $ERRORS{'UNKNOWN'}; }

$state = 'OK';
if ($days > $opt_warning) { $state='WARNING' }
if ($days > $opt_critical) { $state='CRITICAL' }
if ($days < 0) { $state='UNKNOWN';} ;


 my $dsnresult = "DBI:Pg:dbname=$sqlDB;host=$sqlHost";
 my $dbhresult = DBI->connect( $dsnresult,$sqlUsername,$sqlPassword ) or die "Error connecting to: '$dsn': $DBI::errstr\n";


my $sql3 = "select '1' as test, array_to_string(array_agg(t.name),', ') as clientresult from job t right join ( select name, max(endtime) as maxdate from job where jobstatus = 'T' group by name ) tm on t.name = tm.name and t.endtime = tm.maxdate where DATE_PART('day', now() - EndTime) > 30 and t.name <> 'RestoreFiles' and t.name not in ('";
my $sql4 = "') group by test";

$sqlresult = join "", $sql3, $opt_job, $sql4;

my $sthresult = $dbhresult->prepare($sqlresult) or die "Error preparing statement",$dbhresult->errstr;
$sthresult->execute;

my @rowresult;
while ( @rowresult = $sthresult->fetchrow_array()) {
($test,$clientresult) = @rowresult;
}
$sthresult->finish;
$dbhresult->disconnect();


print "Backup Clients $state. Trascorsi almeno 30 giorni dall'ultimo backup effettuato con successo dei clients: $clientresult\n";
exit $ERRORS{$state};

}
else
{
print_help();
exit $ERRORS{'UNKNOWN'};
}