diff options
Diffstat (limited to 'contrib')
-rwxr-xr-x | contrib/check_mssql.sh | 59 |
1 files changed, 40 insertions, 19 deletions
diff --git a/contrib/check_mssql.sh b/contrib/check_mssql.sh index 7faa6be..77c4dcd 100755 --- a/contrib/check_mssql.sh +++ b/contrib/check_mssql.sh | |||
@@ -3,18 +3,21 @@ | |||
3 | # | 3 | # |
4 | # Requirements: | 4 | # Requirements: |
5 | # | 5 | # |
6 | # Sqsh (http://www.sqsh.org/) | 6 | # FreeTDS 6.0+ (http://www.freetds.org/) |
7 | # FreeTDS (http://www.freetds.org/) | ||
8 | # | 7 | # |
9 | # It was written by Tom De Blende (tom.deblende@village.uunet.be) in 2003. | 8 | # It was written by Tom De Blende (tom.deblende@village.uunet.be) in 2003. |
10 | # | 9 | # |
11 | # Version 1.0. | 10 | # Version 1.0. |
12 | # Version 1.1: rewritten the initial script so that it not only works from the CLI but also from within Nagios. Always helpful... | 11 | # Version 1.1: Rewritten the initial script so that it not only works from the CLI but also from within Nagios. Always helpful... |
13 | # Version 1.2: grouped output so things look a bit better. | 12 | # Version 1.2: Grouped output so things look a bit better. |
13 | # Version 2.0: Rewritten the plugin to support version 6.0+ of FreeTDS. | ||
14 | # Removed sqsh requirement as version 6.0+ of FreeTDS now offers its own CLI client: tsql. | ||
15 | # Older versions of FreeTDS are no longer supported. | ||
16 | # | ||
14 | # | 17 | # |
15 | # You might want to change these values: | 18 | # You might want to change these values: |
16 | 19 | ||
17 | sqshcmd="/usr/local/bin/sqsh" | 20 | tsqlcmd=`which tsql` |
18 | catcmd=`which cat` | 21 | catcmd=`which cat` |
19 | grepcmd=`which grep` | 22 | grepcmd=`which grep` |
20 | rmcmd=`which rm` | 23 | rmcmd=`which rm` |
@@ -26,7 +29,6 @@ uniqcmd=`which uniq` | |||
26 | 29 | ||
27 | ################################################################################################################### | 30 | ################################################################################################################### |
28 | 31 | ||
29 | |||
30 | hostname=$1 | 32 | hostname=$1 |
31 | usr=$2 | 33 | usr=$2 |
32 | pswd=$3 | 34 | pswd=$3 |
@@ -34,17 +36,17 @@ srv=$4 | |||
34 | 36 | ||
35 | 37 | ||
36 | if [ ! "$#" == "4" ]; then | 38 | if [ ! "$#" == "4" ]; then |
37 | echo -e "\nYou did not supply enough arguments. \nUsage: $0 <host> <username> <password> <version> \n \n$0 checks Microsoft SQL Server connectivity. It works with versions 7 and 2000.\n\nYou need a working version of Sqhs (http://www.sqsh.org/) and FreeTDS (http://www.freetds.org/) to connect to the SQL server. \nIt was written by Tom De Blende (tom.deblende@village.uunet.be) in 2003. \n\nExample:\n $0 dbserver sa f00bar 2000\n" && exit "3" | 39 | echo -e "\nYou did not supply enough arguments. \nUsage: $0 <host> <username> <password> <version> \n \n$0 checks Microsoft SQL Server connectivity. It works with versions 7 and 2000.\n\nYou need a working version of FreeTDS (http://www.freetds.org/) and tsql (included in FreeTDS 6.0+) to connect to the SQL server. \nIt was written by Tom De Blende (tom.deblende@village.uunet.be) in 2003. \n\nExample:\n $0 dbserver sa f00bar 2000\n" && exit "3" |
38 | 40 | ||
39 | elif [ $sqshcmd == "" ]; then | 41 | elif [ $tsqlcmd == "" ]; then |
40 | echo -e "Sqsh not found! Please verify you have a working version of Sqsh (http://www.sqsh.org/) and enter the full path in the script." && exit "3" | 42 | echo -e "tsql not found! Please verify you have a working version of tsql (included in the FreeTDS version 6.0+) and enter the full path in the script." && exit "3" |
41 | 43 | ||
42 | fi | 44 | fi |
43 | 45 | ||
44 | exit="3" | 46 | exit="3" |
45 | 47 | ||
46 | 48 | ||
47 | # Creating the command file that contains the sql statement that has to be run on the SQL server. Normally one would use the -C parameter of sqsh, but it seems that there is a bug that doesn't allow statements with more than one blanc. | 49 | # Creating the command file that contains the sql statement that has to be run on the SQL server. |
48 | 50 | ||
49 | tmpfile=`$mktempcmd /tmp/$hostname.XXXXXX` | 51 | tmpfile=`$mktempcmd /tmp/$hostname.XXXXXX` |
50 | 52 | ||
@@ -59,18 +61,37 @@ fi | |||
59 | echo -e "select loginame from sysprocesses where spid > $spid order by loginame asc\ngo" > $tmpfile | 61 | echo -e "select loginame from sysprocesses where spid > $spid order by loginame asc\ngo" > $tmpfile |
60 | 62 | ||
61 | 63 | ||
62 | # Running sqsh to get the results back. | 64 | # Running tsql to get the results back. |
63 | 65 | ||
64 | resultfile=`$mktempcmd /tmp/$hostname.XXXXXX` | 66 | resultfile=`$mktempcmd /tmp/$hostname.XXXXXX` |
65 | $sqshcmd -S $hostname -U $usr -P $pswd -w 100000 -i $tmpfile -o $resultfile 2>/dev/null | 67 | errorfile=`$mktempcmd /tmp/$hostname.XXXXXX` |
68 | $tsqlcmd -S $hostname -U $usr -P $pswd < $tmpfile 2>$errorfile > $resultfile | ||
69 | |||
70 | $grepcmd -q "Login failed for user" $errorfile | ||
71 | |||
72 | if [ "$?" == "0" ]; then | ||
73 | $rmcmd -f $tmpfile $resultfile $errorfile; | ||
74 | echo CRITICAL - Could not make connection to SQL server. Login failed.; | ||
75 | exit 2; | ||
76 | fi | ||
77 | |||
78 | $grepcmd -q "There was a problem connecting to the server" $errorfile | ||
79 | |||
80 | if [ "$?" == "0" ]; then | ||
81 | $rmcmd -f $tmpfile $resultfile $errorfile; | ||
82 | echo CRITICAL - Could not make connection to SQL server. Incorrect server name or SQL service not running.; | ||
83 | exit 2; | ||
84 | fi | ||
85 | |||
86 | resultfileln=`$catcmd $resultfile | $wccmd -l | $sedcmd 's/ //g'` | ||
66 | 87 | ||
67 | if [ ! -s $resultfile ]; then | 88 | if [ "$resultfileln" == "2" ]; then |
68 | $rmcmd -f $tmpfile $resultfile; | 89 | $rmcmd -f $tmpfile $resultfile $errorfile; |
69 | echo CRITICAL - Could not make connection to SQL server.; | 90 | echo CRITICAL - Could not make connection to SQL server. No data received from host.; |
70 | exit 2; | 91 | exit 2; |
71 | else | 92 | else |
72 | nmbr=`$catcmd $resultfile | $grepcmd -v "\-\-\-\-\-" | $grepcmd -v "loginame" | $grepcmd -v "affected" | $sedcmd '/^$/d' | $sedcmd 's/ //g' | $wccmd -l | sed 's/ //g'`; | 93 | nmbr=`$catcmd $resultfile | $grepcmd -v locale | $grepcmd -v charset| $grepcmd -v 1\> | $sedcmd '/^$/d' | $sedcmd 's/ //g' | $wccmd -l | sed 's/ //g'` |
73 | users=`$catcmd $resultfile | $grepcmd -v "\-\-\-\-\-" | $grepcmd -v "loginame" | $grepcmd -v "affected" | $sedcmd '/^$/d' | $sedcmd 's/ //g' | $uniqcmd -c | $trcmd \\\n , | $sedcmd 's/,$/./g' | $sedcmd 's/,/, /g' | $sedcmd 's/ //g' | $trcmd \\\t " "`; | 94 | users=`$catcmd $resultfile | $grepcmd -v locale | $grepcmd -v charset| $grepcmd -v 1\> | $sedcmd '/^$/d' | $sedcmd 's/ //g' | $uniqcmd -c | $trcmd \\\n , | $sedcmd 's/,$/./g' | $sedcmd 's/,/, /g' | $sedcmd 's/ //g' | $trcmd \\\t " " | $sedcmd 's/ \./\./g' | $sedcmd 's/ ,/,/g'` |
74 | $rmcmd -f $tmpfile $resultfile; | 95 | $rmcmd -f $tmpfile $resultfile; |
75 | echo "OK - MS SQL Server $srv has $nmbr user(s) connected: $users" | sed 's/: $/./g'; | 96 | echo "OK - MS SQL Server $srv has $nmbr user(s) connected: $users" | sed 's/: $/./g'; |
76 | exit 0; | 97 | exit 0; |
@@ -78,6 +99,6 @@ fi | |||
78 | 99 | ||
79 | # Cleaning up. | 100 | # Cleaning up. |
80 | 101 | ||
81 | $rmcmd -f $tmpfile $resultfile | 102 | $rmcmd -f $tmpfile $resultfile $errorfile |
82 | echo $stdio | 103 | echo $stdio |
83 | exit $exit | 104 | exit $exit |