windows C# code

Jeff Newman jeffnewman75 at gmail.com
Wed May 23 02:55:20 CEST 2007


Hi,

I have a windows programmer writing some C# code to send events to
hobbit. I am having trouble with the right way to do the below code.
If anyone has any insight to share, please let me know.

The below code displays on the page right, however, when the hobbit
ncv module builds the RRD file, the ds names are things like:
greenCurrentConnections
greenTimeinGC
yellowBytesinallHeapsMB
redApplicationsRunning

So I think we are close, just need to figure out how to send the right
way for hobbit to seperate the status color and column name.
Code below.

Thanks,
Jeff


using System;
using System.IO;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;
using Microsoft.Win32;

public class Hobbit
{
    //This reg key says where to write the output file.  However,
right now it is blank, so we're going to skip this part.
    //private static RegistryKey HobbitKey =
Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Quest
Software\BigBrother\bbnt\ExternalPath");

    //This string is is the title of the column that will appear in
Big Brother.  Also used to determine name of output file.
    private static string strBBTestName = "iishealth";

    //Result string
    private static StringBuilder ResStringBuilder = new
StringBuilder("IIS Health Check:" + Environment.NewLine);

    //Formatted server status string
    private static StringBuilder SrvStat = new StringBuilder();

    //Server Status (green, yellow, red)
    private static string IISStat = "green";

    //Time stamp
    private static StringBuilder dtStamp = new StringBuilder();

    //Target server.  Normally, this should be "."
    //General note: you can read remote counters, but not write to them.
    private static string sName = ".";

    //Web Service counter values
    private static PerformanceCounter CurrConn;
    private static PerformanceCounter WSBytesInPerSec;
    private static PerformanceCounter WSBytesOutPerSec;

    //CLR Mem counter values
    private static PerformanceCounter PercentGC;
    private static PerformanceCounter ClrHeapBytes;

    //ASPNet counter values
    private static PerformanceCounter AppsRunning;
    private static PerformanceCounter ReqTime;
    private static PerformanceCounter ReqQueued;
    private static PerformanceCounter ReqWaitTime;
    private static PerformanceCounter ReqRejected;
    private static PerformanceCounter ReqCurrent;

    //ASPNet Apps counter values
    private static PerformanceCounter ReqsExec;
    private static PerformanceCounter ReqsFailed;
    private static PerformanceCounter ReqPerSec;
    private static PerformanceCounter ReqOk;
    private static PerformanceCounter ReqTO;
    private static PerformanceCounter ReqTotal;

    private static int minorCurrConnections = 300;
    private static int majorCurrConnections = 500;
    private static int minorWSBytesInPerSec = 1000000;
    private static int majorWSBytesInPerSec = 3000000;
    private static int minorWSBytesOutPerSec = 3000000;
    private static int majorWSBytesOutPerSec = 6000000;
    //CLR Mem
    private static int minorPercentGC = 25;
    private static int majorPercentGC = 50;
    private static int minorClrHeapBytes = 400;
    private static int majorClrHeapBytes = 600;
    //ASPNet
    private static int minorAppsRunning = 2;
    private static int majorAppsRunning = 2;
    private static int minorReqTime = 30000;
    private static int majorReqTime = 45000;
    private static int minorReqQueued = 0;
    private static int majorReqQueued = 5;
    private static int minorReqWaitTime = 1000;
    private static int majorReqWaitTime = 5000;
    private static int minorReqRejected = 10000;
    private static int majorReqRejected = 11000;
    private static int minorReqCurrent = 100;
    private static int majorReqCurrent = 120;
    //ASPNet Apps
    private static int minorReqsExec = 100;
    private static int majorReqsExec = 120;
    private static int minorReqsFailed = 10000;
    private static int majorReqsFailed = 11000;
    private static int minorReqPerSec = 100;
    private static int majorReqPerSec = 120;
    private static int minorReqOk = 10000000;
    private static int majorReqOk = 15000000;
    private static int minorReqTO = 10000;
    private static int majorReqTO = 11000;
    private static int minorReqTotal = 10000000;
    private static int majorReqTotal = 15000000;

    public static void Main()
    {
        GetTimeStamp(dtStamp);
        GetCounters();
        GetFinalStatus(SrvStat, IISStat, dtStamp);
        Console.WriteLine(ResStringBuilder);
        WriteFile(strBBTestName, SrvStat, ResStringBuilder);

    }



    private static void GetCounters()
    {
        // Define and call the counters.

        // If a counter reports rates (requests per sec, bytes per
sec), then the "NextValue()"
        // method must be called twice with sufficient time in between
calls.  Call these counters
        // the first time before the others, and the second time after
the others. Add sleep time
        // if necessary.

        ReqPerSec = new PerformanceCounter();
        ReqPerSec.MachineName = sName;
        ReqPerSec.CategoryName = "ASP.NET Apps v2.0.50727";
        ReqPerSec.CounterName = "Requests/Sec";
        ReqPerSec.InstanceName = "__Total__";
        ReqPerSec.NextValue();

        WSBytesInPerSec = new PerformanceCounter();
        WSBytesInPerSec.MachineName = sName;
        WSBytesInPerSec.CategoryName = "Web Service";
        WSBytesInPerSec.CounterName = "Bytes Received/sec";
        WSBytesInPerSec.InstanceName = "_Total";
        WSBytesInPerSec.NextValue();

        WSBytesOutPerSec = new PerformanceCounter();
        WSBytesOutPerSec.MachineName = sName;
        WSBytesOutPerSec.CategoryName = "Web Service";
        WSBytesOutPerSec.CounterName = "Bytes Sent/sec";
        WSBytesOutPerSec.InstanceName = "_Total";
        WSBytesOutPerSec.NextValue();

        //Web Service counter values
        CurrConn = new PerformanceCounter();
        CurrConn.MachineName = sName;
        CurrConn.CategoryName = "Web Service";
        CurrConn.CounterName = "Current Connections";
        CurrConn.InstanceName = "_Total";
        DetermineResults(CurrConn.NextValue(), CurrConn.CounterName,
minorCurrConnections, majorCurrConnections, ResStringBuilder);
        //Console.WriteLine("Counter: " + CurrConn.CounterName + ",
Value: " + CurrConn.NextValue().ToString());

        //CLR Mem counter values
        PercentGC = new PerformanceCounter();
        PercentGC.MachineName = sName;
        PercentGC.CategoryName = ".NET CLR Memory";
        PercentGC.CounterName = "% Time in GC";
        PercentGC.InstanceName = "_Global_";
        DetermineResults(PercentGC.NextValue(), PercentGC.CounterName,
minorPercentGC, majorPercentGC, ResStringBuilder);
        //Console.WriteLine("Counter: " + PercentGC.CounterName + ",
Value: " + PercentGC.NextValue().ToString());

        ClrHeapBytes = new PerformanceCounter();
        ClrHeapBytes.MachineName = sName;
        ClrHeapBytes.CategoryName = ".NET CLR Memory";
        ClrHeapBytes.CounterName = "# Bytes in all Heaps";
        ClrHeapBytes.InstanceName = "_Global_";
        DetermineResults(ClrHeapBytes.NextValue()/(1024*1000),
ClrHeapBytes.CounterName + " (MB)", minorClrHeapBytes,
majorClrHeapBytes, ResStringBuilder);
        //Console.WriteLine("Counter: " + ClrHeapBytes.CounterName +
", Value: " + ClrHeapBytes.NextValue().ToString());

        //ASPNet 2.0 counter values
        AppsRunning = new PerformanceCounter();
        AppsRunning.MachineName = sName;
        AppsRunning.CategoryName = "ASP.NET v2.0.50727";
        AppsRunning.CounterName = "Applications Running";
        DetermineResults(AppsRunning.NextValue(),
AppsRunning.CounterName, minorAppsRunning, majorAppsRunning,
ResStringBuilder);
        //Console.WriteLine("Counter: " + AppsRunning.CounterName + ",
Value: " + AppsRunning.NextValue().ToString());

        ReqTime = new PerformanceCounter();
        ReqTime.MachineName = sName;
        ReqTime.CategoryName = "ASP.NET v2.0.50727";
        ReqTime.CounterName = "Request Execution Time";
        DetermineResults(ReqTime.NextValue(), ReqTime.CounterName,
minorReqTime, majorReqTime, ResStringBuilder);
        //Console.WriteLine("Counter: " + ReqTime.CounterName + ",
Value: " + ReqTime.NextValue().ToString());

        ReqQueued = new PerformanceCounter();
        ReqQueued.MachineName = sName;
        ReqQueued.CategoryName = "ASP.NET v2.0.50727";
        ReqQueued.CounterName = "Requests Queued";
        DetermineResults(ReqQueued.NextValue(), ReqQueued.CounterName,
minorReqQueued, majorReqQueued, ResStringBuilder);
        //Console.WriteLine("Counter: " + ReqQueued.CounterName + ",
Value: " + ReqQueued.NextValue().ToString());

        ReqWaitTime = new PerformanceCounter();
        ReqWaitTime.MachineName = sName;
        ReqWaitTime.CategoryName = "ASP.NET v2.0.50727";
        ReqWaitTime.CounterName = "Request Wait Time";
        DetermineResults(ReqWaitTime.NextValue(),
ReqWaitTime.CounterName, minorReqWaitTime, majorReqWaitTime,
ResStringBuilder);
        //Console.WriteLine("Counter: " + ReqWaitTime.CounterName + ",
Value: " + ReqWaitTime.NextValue().ToString());

        ReqRejected = new PerformanceCounter();
        ReqRejected.MachineName = sName;
        ReqRejected.CategoryName = "ASP.NET v2.0.50727";
        ReqRejected.CounterName = "Requests Rejected";
        DetermineResults(ReqRejected.NextValue(),
ReqRejected.CounterName, minorReqRejected, majorReqRejected,
ResStringBuilder);
        //Console.WriteLine("Counter: " + ReqRejected.CounterName + ",
Value: " + ReqRejected.NextValue().ToString());

        ReqCurrent = new PerformanceCounter();
        ReqCurrent.MachineName = sName;
        ReqCurrent.CategoryName = "ASP.NET v2.0.50727";
        ReqCurrent.CounterName = "Requests Current";
        DetermineResults(ReqCurrent.NextValue(),
ReqCurrent.CounterName, minorReqCurrent, majorReqCurrent,
ResStringBuilder);
        //Console.WriteLine("Counter: " + ReqCurrent.CounterName + ",
Value: " + ReqCurrent.NextValue().ToString());

        //ASPNet 2.0 Apps counter values
        ReqsExec = new PerformanceCounter();
        ReqsExec.MachineName = sName;
        ReqsExec.CategoryName = "ASP.NET Apps v2.0.50727";
        ReqsExec.CounterName = "Requests Executing";
        ReqsExec.InstanceName = "__Total__";
        DetermineResults(ReqsExec.NextValue(), ReqsExec.CounterName,
minorReqsExec, majorReqsExec, ResStringBuilder);
        //Console.WriteLine("Counter: " + ReqsExec.CounterName + ",
Value: " + ReqsExec.NextValue().ToString());

        ReqsFailed = new PerformanceCounter();
        ReqsFailed.MachineName = sName;
        ReqsFailed.CategoryName = "ASP.NET Apps v2.0.50727";
        ReqsFailed.CounterName = "Requests Failed";
        ReqsFailed.InstanceName = "__Total__";
        DetermineResults(ReqsFailed.NextValue(),
ReqsFailed.CounterName, minorReqsFailed, majorReqsFailed,
ResStringBuilder);
        //Console.WriteLine("Counter: " + ReqsFailed.CounterName + ",
Value: " + ReqsFailed.NextValue().ToString());


        ReqOk = new PerformanceCounter();
        ReqOk.MachineName = sName;
        ReqOk.CategoryName = "ASP.NET Apps v2.0.50727";
        ReqOk.CounterName = "Requests Succeeded";
        ReqOk.InstanceName = "__Total__";
        DetermineResults(ReqOk.NextValue(), ReqOk.CounterName,
minorReqOk, majorReqOk, ResStringBuilder);
        //Console.WriteLine("Counter: " + ReqOk.CounterName + ",
Value: " + ReqOk.NextValue().ToString());

        ReqTO = new PerformanceCounter();
        ReqTO.MachineName = sName;
        ReqTO.CategoryName = "ASP.NET Apps v2.0.50727";
        ReqTO.CounterName = "Requests Timed Out";
        ReqTO.InstanceName = "__Total__";
        DetermineResults(ReqTO.NextValue(), ReqTO.CounterName,
minorReqTO, majorReqTO, ResStringBuilder);
        //Console.WriteLine("Counter: " + ReqTO.CounterName + ",
Value: " + ReqTO.NextValue().ToString());

        ReqTotal = new PerformanceCounter();
        ReqTotal.MachineName = sName;
        ReqTotal.CategoryName = "ASP.NET Apps v2.0.50727";
        ReqTotal.CounterName = "Requests Total";
        ReqTotal.InstanceName = "__Total__";
        DetermineResults(ReqTotal.NextValue(), ReqTotal.CounterName,
minorReqTotal, majorReqTotal, ResStringBuilder);
        //Console.WriteLine("Counter: " + ReqTotal.CounterName + ",
Value: " + ReqTotal.NextValue().ToString());


        //Take a nap, then get the rate-based counters.  You don't
need to sleep for remote counters,
        //but local counter execution is too fast and the numbers are
all over the place.
        System.Threading.Thread.Sleep(1000);

        //WSBytesInPerSec = new PerformanceCounter();
        //WSBytesInPerSec.MachineName = sName;
        //WSBytesInPerSec.CategoryName = "Web Service";
        //WSBytesInPerSec.CounterName = "Bytes Received/sec";
        //WSBytesInPerSec.InstanceName = "_Total";
        DetermineResults(WSBytesInPerSec.NextValue(),
WSBytesInPerSec.CounterName, minorWSBytesInPerSec,
majorWSBytesInPerSec, ResStringBuilder);
        //Console.WriteLine("Counter: " + WSBytesInPerSec.CounterName
+ ", Value: " + WSBytesInPerSec.NextValue().ToString());

        //WSBytesOutPerSec = new PerformanceCounter();
        //WSBytesOutPerSec.MachineName = sName;
        //WSBytesOutPerSec.CategoryName = "Web Service";
        //WSBytesOutPerSec.CounterName = "Bytes Sent/sec";
        //WSBytesOutPerSec.InstanceName = "_Total";
        DetermineResults(WSBytesOutPerSec.NextValue(),
WSBytesOutPerSec.CounterName, minorWSBytesOutPerSec,
majorWSBytesOutPerSec, ResStringBuilder);
        //Console.WriteLine("Counter: " + WSBytesOutPerSec.CounterName
+ ", Value: " + WSBytesOutPerSec.NextValue().ToString());


        //ReqPerSec = new PerformanceCounter();
        //ReqPerSec.MachineName = sName;
        //ReqPerSec.CategoryName = "ASP.NET Apps v2.0.50727";
        //ReqPerSec.CounterName = "Requests/Sec";
        //ReqPerSec.InstanceName = "__Total__";
        //ReqPerSec.NextValue();
        DetermineResults(ReqPerSec.NextValue(),ReqPerSec.CounterName,
minorReqPerSec, majorReqPerSec, ResStringBuilder);
        //GreenResult(ReqPerSec.NextValue().ToString(),
ReqPerSec.CounterName, ResStringBuilder);
        //Console.WriteLine("Counter: " + ReqPerSec.CounterName + ",
Value: " + ReqPerSec.GetType());

    }

    private static void DetermineResults(float cValue, string cDesc,
int minorThresh, int majorThresh, StringBuilder cResult)
    {
        if (cValue > majorThresh)
        {
            cResult.Append("&red\t" + Regex.Replace(cDesc,
"[^A-Z,a-z]", "") + ":\t");
            cResult.AppendFormat("{0:n2}\n",cValue);
            if (IISStat != "red")
            {
                IISStat = "red";
            }
        }
        else if (cValue > minorThresh)
        {
            cResult.Append("&yellow\t" + Regex.Replace(cDesc,
"[^A-Z,a-z]", "") + ":\t");
            cResult.AppendFormat("{0:n2}\n", cValue);
            if (IISStat == "green")
            {
                IISStat = "green";
            }
        }
        else if (cValue <= minorThresh)
        {
            cResult.Append("&green\t" + Regex.Replace(cDesc,
"[^A-Z,a-z]", "") + ":\t");
            cResult.AppendFormat("{0:n2}\n",cValue);
        }
        else
        {
            cResult.Append("&red\t" + Regex.Replace(cDesc,
"[^A-Z,a-z]", "") + ":\t" + "Failed to get valid status!\n");
            if (IISStat != "red")
            {
                IISStat = "red";
            }
        }


    }
/*    private static void GreenResult(string cValue, string cDesc,
StringBuilder cResult)
    {

        cResult.AppendLine("&green\t" + cDesc + ":\t" + cValue);

    }
*/

    private static void GetFinalStatus(StringBuilder fStat, string
iStat, StringBuilder tStamp)
    {
        //DateTime CurrTime = DateTime.Now;
        fStat.Append(iStat + " ");
        fStat.Append(tStamp);
        //fStat.AppendFormat("{0:G}", CurrTime);
        //fStat.AppendLine();

    }

    private static void GetTimeStamp(StringBuilder dTime)
    {
        DateTime CurrTime = DateTime.Now;
        dTime.AppendFormat("{0:G}", CurrTime);
    }

    private static void WriteFile(string oFile, StringBuilder
SrvString, StringBuilder StatString)
    {
        FileStream f = new FileStream(oFile, FileMode.Append);
        StreamWriter s = new StreamWriter(f);

        s.WriteLine(SrvString);
        s.WriteLine(StatString);
        s.Close();
        f.Close();
    }
}



More information about the Xymon mailing list