--- /opt/plugins/check_sql	2008-10-17 14:45:13.000000000 +0200
+++ /tmp/check_sql	2008-12-18 22:23:47.000000000 +0100
@@ -194,6 +194,22 @@
   required => 0,
 );
 
+$np->add_arg(
+  spec => 'option|o=s',
+  help => "-o, --option=OPTION\n"
+    . "   Predefince querys to be executed\n"
+	. "      MSSQLJOB - followed by -n as backup job to check",
+  required => 0,
+);
+
+$np->add_arg(
+  spec => 'name|n=s',
+  help => "-n, --name=NAME\n"
+    . "   Name of object to be checked\n"
+    . "   normaly used with -o",
+  required => 0,
+);
+
 $np->getopts;
 
 # Assign, then check args
@@ -214,6 +230,8 @@
 my $show = $np->opts->show;
 my ($label, $uom) = split(/,/, $np->opts->label);
 my $verbose = $np->opts->verbose;
+my $option = $np->opts->option;
+my $name = $np->opts->name;
 
 # TODO: Should check if the DBI driver exists
 
@@ -274,6 +292,15 @@
 
 warn("Connected. Querying server with '$query'\n") if ($verbose > 1);
 
+if ($option && $option eq "MSSQLJOB" && $name) {
+	$show = 1;
+	$expect = 1;
+	$regexp = ".*The job succeeded\..*";
+	$label = "MSSQLJOB: $name ";
+	
+	$query = "SELECT TOP 1 sysjobhistory.message FROM sysjobhistory join sysjobs on (sysjobhistory.job_id=sysjobs.job_id) WHERE sysjobs.name = '$name' AND sysjobhistory.step_name = '(Job outcome)' AND DATEDIFF(dd, CONVERT(CHAR(8), sysjobhistory.run_date), GETDATE()) <= 1 ORDER BY sysjobhistory.run_date desc,sysjobhistory.run_time";
+}
+
 # selectrow_array behavior in scalar context is undefined (driver-dependent)
 # if multiple collumns are returned. Just get the first or only collumn:
 my ($result) = $dbh->selectrow_array($query)
