[Nagiosplug-devel] check_mssql

Roy Sigurd Karlsbakk roy at karlsbakk.net
Tue May 13 02:10:05 CEST 2003


hi

just wrote this. works fine here. any chance to get this checked into cvs?

roy

#!/usr/bin/perl -w

use DBI;
use DBD::Sybase;
use Getopt::Long;
use Date::Parse;
use strict;

my (
        $server,$database,$username,$password,$query,$help,$verbose,
        $dbh,$sth,$lasterror,
        $nagline,
        $s
);
my $exitcode = 0;
my $lastread = 0;
my $data_returned = 0;

sub syntax {
        $s = shift or $s = 'Unknown';
        printf("Error: ($s)\n") unless ($help);
        printf("Syntax: %s -s <server> -d <database> -u <username> -p 
<password> -q <query> [-v]\n", $0);
        printf("* --database -d         Database name\n");
        printf("* --server -s           Server name\n");
        printf("* --username -u         Username\n");
        printf("* --password -p         Password\n");
        printf("* --query -q            SQL query to run\n");
        printf("  --verbose -v          verbose\n");
        exit(3);
}

Getopt::Long::Configure('bundling');
GetOptions
        ("p=s" => \$password, "password=s" => \$password,
         "u=s" => \$username, "username=s" => \$username,
         "s=s" => \$server, "server=s"    => \$server,
         "d=s" => \$database, "database=s" => \$database,
         "q=s" => \$query, "query=s" => \$query,
         "h" => \$help, "help" => \$help,
         "v" => \$verbose, "verbose" => \$verbose);

syntax("Help:") if ($help);
syntax("Missing username") unless (defined($username));
syntax("Missing password") unless (defined($password));
syntax("Missing server") unless (defined($server));
syntax("Missing database") unless (defined($database));
syntax("Missing query string") unless (defined($query));

unless ($dbh = DBI->connect("dbi:Sybase:server=".uc($server), "$username", 
"$password")) {
        printf ("Can't connect to mssql server $dbh->errstr");
        exit(2);
}
unless ($dbh->do("use $database")) {
        printf ("Can't 'use $database': $dbh->errstr");
        exit(2);
}
$sth = $dbh->prepare($query);
unless ($lasterror = $sth->execute()) {
        printf("Error in query: $dbh->errstr\n");
        exit(2);
}

exit($exitcode);

-- 
Roy Sigurd Karlsbakk, Datavaktmester
ProntoTV AS - http://www.pronto.tv/
Tel: +47 9801 3356

Computers are like air conditioners.
They stop working when you open Windows.





More information about the Devel mailing list