Monday, October 25, 2010

Monitoring MS SQL Server using Nagios

Fo this you need FreeTDS libraries for making connection to your MS SQL Server from your Linux machine.

Follow the below steps to monitor the Microsoft SQL Server through Nagios :


1. Download & install the FreeTDS.

http://www.freetds.org/software.html

tar -xvzf freetds-stable.tgz
cd freetds
./configure -with-tdsver=8.0
make
make install

2. Configure freetds, make an entry for the MS SQL Server in /usr/local/etc/freetds.conf as

[MSSQLSERVER]
host = 192.168.0.25
port = 1433
tds version = 8.0

Sql server standard port is the 1433, so check the default port on MS SQL Server.

3. Check the connectivity to ms sql server from Linux machine using tsql command as

tsql -H 192.168.0.25 -p 1433 -U sa -P SA123

If you get the 1> prompt, that means you have made successful connection with ms sql
server.

Now you can start executing the query using check_mssql plugin.

4. Download the check_mssql plugin from

http://exchange.nagios.org/directory/Plugins/Databases/SQLServer/check_mssql/details

add it to your /usr/local/nagios/libexec directory

5. Then add its command definition in commands.cfg as

define command {
command_name check_mssql
command_line $USER1$/check_mssql -H 10.31.0.37 -U sa -P SA123 -q "select count(*) from
empdata" -r "10" -d newtest -w 2 -c 5
}

And also add its host & service definition as

For hosts.cfg

define host {
host_name SQLServer
alias MS SQL Server
address 10.31.0.37
check_command check-host-alive
notification_interval 15
notification_options d,u,r
max_check_attempts 3
active_checks_enabled 1
passive_checks_enabled 0
notifications_enabled 1
check_period 24x7
notification_period 24x7
contact_groups admins
}


For services.cfg

define service {
use generic-service
host_name SQLServer
service_description querystatus
check_command check_mssql
}


Then make entries in nagios.cfg for hosts.cfg & services.cfg, and restart nagios

6. Check in nagios front end for the result.


Here in above command definition it selects the number of records from a table empdata, if it returns 10, as expected result,
then the query is OK, and you will get OK status in Nagios Services section.
And if it gives unexpected results, then ultimately the query fails, and will show CRITICAL status in Nagios Services section.

Please let me know if you have any queries on this.