check_mssql.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. # FreeTDS 6.0+ (http://www.freetds.org/)
  7. #
  8. # It was written by Tom De Blende (tom.deblende@village.uunet.be) in 2003.
  9. #
  10. # Version 1.0.
  11. # Version 1.1: Rewritten the initial script so that it not only works from the CLI but also from within Nagios. Always helpful...
  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. #
  17. #
  18. # You might want to change these values:
  19. tsqlcmd=`which tsql`
  20. catcmd=`which cat`
  21. grepcmd=`which grep`
  22. rmcmd=`which rm`
  23. mktempcmd=`which mktemp`
  24. wccmd=`which wc`
  25. sedcmd=`which sed`
  26. trcmd=`which tr`
  27. uniqcmd=`which uniq`
  28. ###################################################################################################################
  29. hostname=$1
  30. usr=$2
  31. pswd=$3
  32. srv=$4
  33. if [ ! "$#" == "4" ]; then
  34. 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"
  35. elif [ $tsqlcmd == "" ]; then
  36. 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"
  37. fi
  38. exit="3"
  39. # Creating the command file that contains the sql statement that has to be run on the SQL server.
  40. tmpfile=`$mktempcmd /tmp/$hostname.XXXXXX`
  41. if [ $srv == "7" ]; then
  42. spid=7
  43. elif [ $srv == "2000" ]; then
  44. spid=50
  45. else
  46. echo -e "$srv is not a supported MS SQL Server version!" && exit "3"
  47. fi
  48. echo -e "select loginame from sysprocesses where spid > $spid order by loginame asc\ngo" > $tmpfile
  49. # Running tsql to get the results back.
  50. resultfile=`$mktempcmd /tmp/$hostname.XXXXXX`
  51. errorfile=`$mktempcmd /tmp/$hostname.XXXXXX`
  52. $tsqlcmd -S $hostname -U $usr -P $pswd < $tmpfile 2>$errorfile > $resultfile
  53. $grepcmd -q "Login failed for user" $errorfile
  54. if [ "$?" == "0" ]; then
  55. $rmcmd -f $tmpfile $resultfile $errorfile;
  56. echo CRITICAL - Could not make connection to SQL server. Login failed.;
  57. exit 2;
  58. fi
  59. $grepcmd -q "There was a problem connecting to the server" $errorfile
  60. if [ "$?" == "0" ]; then
  61. $rmcmd -f $tmpfile $resultfile $errorfile;
  62. echo CRITICAL - Could not make connection to SQL server. Incorrect server name or SQL service not running.;
  63. exit 2;
  64. fi
  65. resultfileln=`$catcmd $resultfile | $wccmd -l | $sedcmd 's/ //g'`
  66. if [ "$resultfileln" == "2" ]; then
  67. $rmcmd -f $tmpfile $resultfile $errorfile;
  68. echo CRITICAL - Could not make connection to SQL server. No data received from host.;
  69. exit 2;
  70. else
  71. nmbr=`$catcmd $resultfile | $grepcmd -v locale | $grepcmd -v charset| $grepcmd -v 1\> | $sedcmd '/^$/d' | $sedcmd 's/ //g' | $wccmd -l | sed 's/ //g'`
  72. 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'`
  73. $rmcmd -f $tmpfile $resultfile;
  74. echo "OK - MS SQL Server $srv has $nmbr user(s) connected: $users" | sed 's/: $/./g';
  75. exit 0;
  76. fi
  77. # Cleaning up.
  78. $rmcmd -f $tmpfile $resultfile $errorfile
  79. echo $stdio
  80. exit $exit