check_mssql.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/bin/sh
  2. # This script is designed to be used by Nagios. It checks for the availability of both Microsoft SQL Server 7 and 2000.
  3. #
  4. # Requirements:
  5. #
  6. # Sqsh (http://www.sqsh.org/)
  7. # FreeTDS (http://www.freetds.org/)
  8. #
  9. # It was written by Tom De Blende (tom.deblende@village.uunet.be) in 2003.
  10. #
  11. # 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...
  13. #
  14. # You might want to change these values:
  15. sqshcmd="/usr/local/bin/sqsh"
  16. catcmd=`which cat`
  17. grepcmd=`which grep`
  18. rmcmd=`which rm`
  19. mktempcmd=`which mktemp`
  20. wccmd=`which wc`
  21. sedcmd=`which sed`
  22. trcmd=`which tr`
  23. ###################################################################################################################
  24. hostname=$1
  25. usr=$2
  26. pswd=$3
  27. srv=$4
  28. if [ ! "$#" == "4" ]; then
  29. 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"
  30. elif [ $sqshcmd == "" ]; then
  31. 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"
  32. fi
  33. exit="3"
  34. # 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.
  35. tmpfile=`$mktempcmd /tmp/$hostname.XXXXXX`
  36. if [ $srv == "7" ]; then
  37. spid=7
  38. elif [ $srv == "2000" ]; then
  39. spid=50
  40. else
  41. echo -e "$srv is not a supported MS SQL Server version!" && exit "3"
  42. fi
  43. echo -e "select loginame from sysprocesses where spid > $spid order by loginame asc\ngo" > $tmpfile
  44. # Running sqsh to get the results back.
  45. resultfile=`$mktempcmd /tmp/$hostname.XXXXXX`
  46. $sqshcmd -S $hostname -U $usr -P $pswd -w 100000 -i $tmpfile -o $resultfile 2>/dev/null
  47. if [ ! -s $resultfile ]; then
  48. $rmcmd -f $tmpfile $resultfile;
  49. echo CRITICAL - Could not make connection to SQL server.;
  50. exit 2;
  51. else
  52. nmbr=`$catcmd $resultfile | $grepcmd -v "\-\-\-\-\-" | $grepcmd -v "loginame" | $grepcmd -v "affected" | $sedcmd '/^$/d' | $sedcmd 's/ //g' | $wccmd -l | sed 's/ //g'`;
  53. users=`$catcmd $resultfile | $grepcmd -v "\-\-\-\-\-" | $grepcmd -v "loginame" | $grepcmd -v "affected" | $sedcmd '/^$/d' | $sedcmd 's/ //g' | $trcmd \\\n , | $sedcmd 's/,$/./g' | $sedcmd 's/,/, /g'`;
  54. $rmcmd -f $tmpfile $resultfile;
  55. echo "OK - MS SQL Server $srv has $nmbr user(s) connected: $users" | sed 's/: $/./g';
  56. exit 0;
  57. fi
  58. # Cleaning up.
  59. $rmcmd -f $tmpfile $resultfile
  60. echo $stdio
  61. exit $exit