Xymon Mailing List Archive search

windows C# code

7 messages in this thread

list Jeff Newman · Tue, 22 May 2007 19:55:20 -0500 ·
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();
    }
}
list Henrik Størner · Wed, 23 May 2007 07:31:31 +0200 ·
quoted from Jeff Newman
On Tue, May 22, 2007 at 07:55:20PM -0500, Jeff Newman wrote:
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
Hobbit's ncv module breaks when you use the color-icon markers.
Didn't know that, but I checked the code and it will include the
color in the ds name, as you've found.

The quick fix is to change your code so it always has the data
you want to store for graphs with no color-icons in front of the
ds name. E.g. your output could be

    &yellow Heap size is 99 MB

    BytesinallHeaps: 99
    TimeinGC: 200

I'll update the ncv module to skip any color markers in front of the ds
names.


Regards,
Henrik
list Jeff Newman · Wed, 23 May 2007 12:49:56 -0500 ·
Henrik,

Thanks. I'll wait for the ncv module patch to become available. If you
could let me know when it is available (or where to check) that would
be great!

With much apprieciation,
Jeff
quoted from Henrik Størner


On 5/23/07, Henrik Stoerner <user-ce4a2c883f75@xymon.invalid> wrote:
On Tue, May 22, 2007 at 07:55:20PM -0500, Jeff Newman wrote:
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
Hobbit's ncv module breaks when you use the color-icon markers.
Didn't know that, but I checked the code and it will include the
color in the ds name, as you've found.

The quick fix is to change your code so it always has the data
you want to store for graphs with no color-icons in front of the
ds name. E.g. your output could be

   &yellow Heap size is 99 MB

   BytesinallHeaps: 99
   TimeinGC: 200

I'll update the ncv module to skip any color markers in front of the ds
names.


Regards,
Henrik

list Jeff Newman · Tue, 29 May 2007 14:38:21 -0500 ·
Henrik,

Where should I watch for the patch:

http://www.hswn.dk/beta/snapshot/
or
http://www.hobbitmon.com/hobbitsw/patches/
?

Thanks,
quoted from Jeff Newman
Jeff


On 5/23/07, Henrik Stoerner <user-ce4a2c883f75@xymon.invalid> wrote:
On Tue, May 22, 2007 at 07:55:20PM -0500, Jeff Newman wrote:
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
Hobbit's ncv module breaks when you use the color-icon markers.
Didn't know that, but I checked the code and it will include the
color in the ds name, as you've found.

The quick fix is to change your code so it always has the data
you want to store for graphs with no color-icons in front of the
ds name. E.g. your output could be

   &yellow Heap size is 99 MB

   BytesinallHeaps: 99
   TimeinGC: 200

I'll update the ncv module to skip any color markers in front of the ds
names.


Regards,
Henrik

list Jeff Newman · Wed, 13 Jun 2007 11:02:14 -0500 ·
Henrik,

Any update on the patch?

Thanks,
Jeff
quoted from Jeff Newman

On 5/29/07, Jeff Newman <user-e96740e73ca8@xymon.invalid> wrote:
Henrik,

Where should I watch for the patch:

http://www.hswn.dk/beta/snapshot/
or
http://www.hobbitmon.com/hobbitsw/patches/
?

Thanks,
Jeff


On 5/23/07, Henrik Stoerner <user-ce4a2c883f75@xymon.invalid> wrote:
On Tue, May 22, 2007 at 07:55:20PM -0500, Jeff Newman wrote:
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
Hobbit's ncv module breaks when you use the color-icon markers.
Didn't know that, but I checked the code and it will include the
color in the ds name, as you've found.

The quick fix is to change your code so it always has the data
you want to store for graphs with no color-icons in front of the
ds name. E.g. your output could be

   &yellow Heap size is 99 MB

   BytesinallHeaps: 99
   TimeinGC: 200

I'll update the ncv module to skip any color markers in front of the ds
names.


Regards,
Henrik

list Jeff Newman · Fri, 6 Jul 2007 14:27:43 -0500 ·
Henrik,

Any update on the patch?

Thanks,
Jeff


On 5/23/07, Henrik Stoerner <user-ce4a2c883f75@xymon.invalid> wrote:
On Tue, May 22, 2007 at 07:55:20PM -0500, Jeff Newman wrote:
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
Hobbit's ncv module breaks when you use the color-icon markers.
Didn't know that, but I checked the code and it will include the
color in the ds name, as you've found.

The quick fix is to change your code so it always has the data
you want to store for graphs with no color-icons in front of the
ds name. E.g. your output could be

   &yellow Heap size is 99 MB

   BytesinallHeaps: 99
   TimeinGC: 200

I'll update the ncv module to skip any color markers in front of the ds
names.


Regards,
Henrik

list Henrik Størner · Fri, 6 Jul 2007 22:41:18 +0200 ·
On Fri, Jul 06, 2007 at 02:27:43PM -0500, Jeff Newman wrote:
Any update on the patch?
It's in the current snapshot. You can grab the hobbitd/rrd/do_ncv.c file
from the snapshot and just copy that on top of your hobbit-4.2.0
sourcetree.


Regards,
Henrik