[root@localhost libexec]# vim check_dbjob
use DBI;
# Nagios specific
use lib "/opt/nagios/nagiosweb/libexec/";
#use lib "/usr/lib/nagios/plugins";
use utils qw(%ERRORS $TIMEOUT);
#my $TIMEOUT = 15;
#my %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);
my $o_host;
my $o_port;
my $o_user="sa";
my $o_pw="";
my $name="";
sub print_usage {
print "\n";
print "Usage: check_dbmirroring.pl -H <host> [-u <username>] [-p <password>] [-o <port>]\n";
print "\n";
print "\tDefault Username is 'sa' without a password\n\n";
print "\tScript should be run on the PRINCIPAL with a read-only user\n";
print "\tIf you want to run it on the MIRROR, the user MUST have SYSADMIN rights on the SQL-Server\n";
print "\totherwise you get NULL\n";
print "\n";
}
sub check_options {
Getopt::Long::Configure ("bundling");
GetOptions(
'H:s' => \$o_host,
'u:s' => \$o_user,
'p:s' => \$o_pw,
'o:s' => \$o_port
);
if (!defined ($o_host)) { print_usage(); exit $ERRORS{"UNKNOWN"}};
}
########## MAIN #######
check_options();
my $exit_val;
# Connect to database
my $dbh =DBI->connect("dbi:Sybase:server=$o_host:$o_port","$o_user","$o_pw") or exit $ERRORS{"UNKNOWN"};
my $sth=$dbh->prepare("select count(*) num from msdb.dbo.sysjobservers,msdb.dbo.sysjobs_view where msdb.dbo.sysjobs_view.job_id=msdb.dbo.sysjobservers.job_id and msdb.dbo.sysjobs_view.enabled =1 and msdb.dbo.sysjobservers.last_run_date>0 and msdb.dbo.sysjobservers.last_run_outcome
<>1");
$sth->execute;
while (my @row = $sth->fetchrow_array) {
$name=$row["0"];
}
$exit_val=$ERRORS{"CRITICAL"};
$exit_val=$ERRORS{"OK"} if ( $name eq "0" );
print "OK -JOB IS OK\n" if ($exit_val eq $ERRORS{"OK"});
print "CRITICAL JOB has a error\n" if ($exit_val eq $ERRORS{"CRITICAL"});
exit $exit_val;
-----------------------------------------------------------------------------------------------------------------------------
[root@localhost libexec]# /opt/nagios/nagiosweb/libexec/check_dbjob -H xxxxx -u xxxx -p xxxx -o 38141
OK -JOB IS OK
[root@localhost libexec]#

本文介绍了一个使用Perl脚本检查Nagios中数据库作业状态的方法。通过连接到指定主机的数据库并执行查询来判断是否有错误的作业。如果作业正常,则返回OK状态;若有错误,则返回CRITICAL状态。
5169

被折叠的 条评论
为什么被折叠?



