MSSQL Generic Checker

Votes:

0

Your Vote:

Up

Down

I can't attach files to this question so you will have to compile the following code into a C# console app, but for anyone who wants a generic SQL checker, below is the C# code. You can download the free Visual Studio C# Express from Microsoft, create a new console app and in the main page, paste the below code and compile. The save the EXE to your PRTG Network Monitor\Custom Sensors\EXE folder.

Usage:

CheckSQL.exe connstring sqlquery

Example:

CheckSQL.exe "Data Source=DB1;Failover Partner=DB2;Initial Catalog=MyDB;Integrated Security=True;Pooling=false;Connect Timeout=3" "SELECT COUNT(*) FROM information_schema.tables;"

Note:

If using Integrated Security, you will have to add the probe server (eg. DB1$) account to your SQL Logins and grant that login appropriate permission to your database

Note:

To get this script to error, you will have to set limits on the appropriate channel within the PRTG Sensor. For example, if you count the number of rows (SELECT COUNT(*) FROM table) and the result should be more than 1, then set an error limit of 1 for it to error.

using System;
using System.Data;
using System.Data.SqlClient;

namespace CheckSQL
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length == 0) return;

            string connstring = args[0];
            string sqlquery = args[1];

            ExecuteScalar(connstring, sqlquery);
        }

        private static void ExecuteScalar(string connstring, string sqlquery)
        {
            SqlConnection sqlconn = new SqlConnection(connstring);
            SqlCommand sqlcmd = new SqlCommand(sqlquery, sqlconn);

            try
            {
                sqlconn.Open();
                Console.Write(string.Format("{0}:OK", sqlcmd.ExecuteScalar()));
            }
            catch (Exception ex)
            {
                Console.Write(string.Format("0:{0}", ex.Message));
            }
            finally
            {
                if (sqlconn.State == ConnectionState.Open)
                    sqlconn.Close();
            }
        }
    }
}

Please note that this script is provided as is without any support. Use of this script is entirely at your own risk. However, if you add any suggestions or comments, we will try to respond.

check database mirroring sql

Created on May 4, 2011 8:54:24 AM by  ITManx Ltd (0) 1

Last change on May 4, 2011 9:49:45 AM by  Daniel Zobel [Paessler Support] (21,383) 3 3



Replies

Nobody has replied yet

Why not be the first?

Please log in or register to enter your reply.


Disclaimer: The information in the Paessler Knowledge Base comes without warranty of any kind. Use at your own risk. Before applying any instructions please exercise proper system administrator housekeeping. You must make sure that a proper backup of all your data is available.

PRTG
Network Monitor
Intuitive to Use.
Easy to manage.

150.000 administrators have chosen PRTG to monitor their network. Find out how you can reduce cost, increase QoS and ease planning, as well.

Visit
www.paessler.com

What is this?

This knowledgebase contains questions and answers about PRTG Network Monitor and network monitoring in general. You are invited to get involved by asking and answering questions!

Learn more

Top Tags


View all Tags