{"id":78,"date":"2020-09-01T16:56:01","date_gmt":"2020-09-01T14:56:01","guid":{"rendered":"http:\/\/alittlecrumbs.com\/?p=77"},"modified":"2020-09-01T16:56:01","modified_gmt":"2020-09-01T14:56:01","slug":"last-succesful-backup-bareos-nagios-check-perl","status":"publish","type":"post","link":"https:\/\/notbotnotes.com\/?p=78","title":{"rendered":"Last Succesful Backup Bareos \u2013 Nagios Check Perl"},"content":{"rendered":"\n<pre class=\"wp-block-syntaxhighlighter-code\">#!\/usr\/bin\/perl -w\n\n# Author: Marco Braidotti\n# Quite a lot copied from check_bacula.pl which is GPL.\n\n## examples: \n\n##    This program is free software: you can redistribute it and\/or modify\n##    it under the terms of the GNU General Public License as published by\n##    the Free Software Foundation, either version 3 of the License, or\n##    (at your option) any later version.\n##\n##    This program is distributed in the hope that it will be useful,\n##    but WITHOUT ANY WARRANTY; without even the implied warranty of\n##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n##    GNU General Public License for more details.\n##\n##    You should have received a copy of the GNU General Public License\n##    along with check_bareos_last_backup.pl.  If not, see &lt;http:\/\/www.gnu.org\/licenses\/&gt;.\n\n\nuse strict;\nuse POSIX;\nuse File::Basename;\nuse DBI;\nuse DBD::Pg;\nuse Switch;\nuse Getopt::Long;\nuse vars qw(\n       $opt_help\n           $opt_job\n           $opt_critical\n           $opt_warning\n           $opt_level\n           $opt_usage\n           $opt_version\n           $out\n           $sql\n           $date_start\n           $date_stop\n           $state\n           $count\n           );\n           \nsub print_help();\nsub print_usage();\nsub get_now();\nsub get_date;\n\nmy $sql;\nmy $sq11;\nmy $sq12;\nmy $sql3;\nmy $sql4;\nmy $sqlresult;\nmy $days;\nmy $end;\nmy $client;\nmy $test;\nmy $clientresult;\nmy $level;\nmy $progname = basename($0);\nmy $progVers = \"0.9\";\nmy $sqlHost;\nmy $sqlDB;\nmy $sqlUsername;\nmy $sqlPassword;\n\n\nmy %ERRORS = (  'UNKNOWN'       =&gt;      '3',\n                'OK'            =&gt;      '0',\n                'WARNING'       =&gt;      '1',\n                'CRITICAL'      =&gt;      '2');\n                \nGetopt::Long::Configure('bundling');\nGetOptions\n        (\n        \"c=s\"   =&gt;      \\$opt_critical, \"critical=s\"    =&gt;      \\$opt_critical,\n        \"w=s\"   =&gt;      \\$opt_warning,  \"warning=s\"     =&gt;      \\$opt_warning,\n        ##\"L=s\"   =&gt;      \\$opt_level,    \"level=s\"       =&gt;      \\$opt_level,\n        \"j=s\"   =&gt;      \\$opt_job,      \"job=s\"         =&gt;      \\$opt_job,\n        \"h\"     =&gt;      \\$opt_help,     \"help\"          =&gt;      \\$opt_help,\n                                        \"usage\"         =&gt;      \\$opt_usage,\n    \"O=s\"     =&gt;      \\$sqlHost,  \"hostname\"       =&gt;      \\$sqlHost,\n    \"d=s\"     =&gt;      \\$sqlDB,  \"database\"       =&gt;      \\$sqlDB,\n    \"u=s\"     =&gt;      \\$sqlUsername,  \"username\"       =&gt;      \\$sqlUsername,\n    \"p=s\"     =&gt;      \\$sqlPassword,  \"password\"       =&gt;      \\$sqlPassword,\n        \"V\"     =&gt;      \\$opt_version,  \"version\"       =&gt;      \\$opt_version\n        ) || die \"Try '$progname --help' for more information.\\n\";\n\n    \nsub print_help() {\n print \"\\n\";\n print \"If Bacula holds its MySQL-data behind password, you have to manually enter the password into the script as variable \\$sqlPassword.\\n\";\n print \"And be sure to prevent everybody from reading it!\\n\";\n print \"\\n\";\n print \"Options:\\n\";\n ##print \"L       check backup level F for full, D for differential, I for incremential\\n\";\n print \"c       max days since successful job for not returning critical\\n\";\n print \"w       max days since successful job for not returning warning\\n\";\n print \"j       name of the job to NOT check (case-sensitive)\\n\";\n print \"h       show this help\\n\";\n print \"V       print script version\\n\";\n print \"O       host postgresql\\n\";\n print \"d       database postgresql\\n\";\n print \"u       database user postgresql\\n\";\n print \"p       database password postgresql\\n\";\n}\n\nsub print_usage() {\n print \"Usage: $progname -c &lt;critical&gt; -w &lt;warning&gt; -j &lt;job-name&gt; [ -h ] [ -V ]\\n\";\n}\n\nif ($opt_help) {\n print_usage();\n print_help();\n exit $ERRORS{'UNKNOWN'};\n}\n\nif ($opt_usage) {\n print_usage();\n exit $ERRORS{'UNKNOWN'};\n}\n\nif ($opt_version) {\n print \"$progname $progVers\\n\";\n exit $ERRORS{'UNKNOWN'};\n}\n\n##switch ($opt_level) {\n\n##\t\tcase \"F\"\t{ $level = \"full\";}\n##\t\tcase \"D\"\t{ $level = \"differential\";}\n##\t\tcase \"I\"\t{ $level = \"incremental\";}\n##\t\telse\t\t{ print_usage(); exit $ERRORS{'UNKNOWN'}; }\n##}\n\nif ($opt_job &amp;&amp; $opt_warning &amp;&amp; $opt_critical) {\n my $dsn = \"DBI:Pg:dbname=$sqlDB;host=$sqlHost\";\n my $dbh = DBI-&gt;connect( $dsn,$sqlUsername,$sqlPassword ) or die \"Error connecting to: '$dsn': $DBI::errstr\\n\";\n\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;\";\n\nmy $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 &lt;&gt; 'RestoreFiles' and t.name not in ('\";\nmy $sql2 = \"') order by days\";\n\n$sql = join \"\", $sql1, $opt_job, $sql2;\n\nmy $sth = $dbh-&gt;prepare($sql) or die \"Error preparing statement\",$dbh-&gt;errstr;\n$sth-&gt;execute;\n\nmy @row;\nwhile ( @row = $sth-&gt;fetchrow_array()) {\n($days,$end,$client) = @row;\n}\n$sth-&gt;finish;\n$dbh-&gt;disconnect();\n\nif (! $end) { print \"Bareos Backup UNKNOWN: Successful job for $opt_job - $days - $end not found.\\n\"; exit $ERRORS{'UNKNOWN'}; }\n\n$state = 'OK';\nif ($days &gt; $opt_warning) { $state='WARNING' }\nif ($days &gt; $opt_critical) { $state='CRITICAL' }\nif ($days &lt; 0) { $state='UNKNOWN';} ;\n\n\n my $dsnresult = \"DBI:Pg:dbname=$sqlDB;host=$sqlHost\";\n my $dbhresult = DBI-&gt;connect( $dsnresult,$sqlUsername,$sqlPassword ) or die \"Error connecting to: '$dsn': $DBI::errstr\\n\";\n\n\nmy $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) &gt; 30 and t.name &lt;&gt; 'RestoreFiles' and t.name not in ('\";\nmy $sql4 = \"') group by test\";\n\n$sqlresult = join \"\", $sql3, $opt_job, $sql4;\n\nmy $sthresult = $dbhresult-&gt;prepare($sqlresult) or die \"Error preparing statement\",$dbhresult-&gt;errstr;\n$sthresult-&gt;execute;\n\nmy @rowresult;\nwhile ( @rowresult = $sthresult-&gt;fetchrow_array()) {\n($test,$clientresult) = @rowresult;\n}\n$sthresult-&gt;finish;\n$dbhresult-&gt;disconnect();\n\n\nprint \"Backup Clients $state. Trascorsi almeno 30 giorni dall'ultimo backup effettuato con successo dei clients: $clientresult\\n\";\nexit $ERRORS{$state};\n\n}\nelse\n{\nprint_help();\nexit $ERRORS{'UNKNOWN'};\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_seo_schema_type":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[15],"tags":[19,16,20],"class_list":["post-78","post","type-post","status-publish","format-standard","hentry","category-notes","tag-bareos","tag-nagios","tag-perl"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/notbotnotes.com\/index.php?rest_route=\/wp\/v2\/posts\/78","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/notbotnotes.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/notbotnotes.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/notbotnotes.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/notbotnotes.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=78"}],"version-history":[{"count":0,"href":"https:\/\/notbotnotes.com\/index.php?rest_route=\/wp\/v2\/posts\/78\/revisions"}],"wp:attachment":[{"href":"https:\/\/notbotnotes.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=78"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/notbotnotes.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=78"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/notbotnotes.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=78"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}