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.