check_nt "COUNTER" to access custom counters (via

Hello,

I am having trouble accessing custom windows performance counters via check_nt (NC_Net on XP and 2003); keeps returning not found (-1). I am able to access the counter using NC_Net’s “Performance Counter Viewer”, so I hope that I am simply using incorrect syntax.

These work (return 1):
./check_nt -H 192.168.1.91 -p 1248 -v COUNTER -l “\Server\Server Sessions”,“Server Sessions: %.f” -w 20 -c 30
./check_nt -H 192.168.1.91 -p 1248 -v COUNTER -l “\Server\Server Sessions”, -w 20 -c 30

These fail (return -1):
./check_nt -H 192.168.1.91 -p 1248 -v COUNTER -l “\TestCounterCategory\TestCounter”, -w 20 -c 30
./check_nt -H 192.168.1.91 -p 1248 -v COUNTER -l “TestCounterCategory\TestCounter”, -w 20 -c 30

./check_nt -H 192.168.1.91 -p 1248 -v COUNTER -l “\TestCounterCategory\TestCounter”, -w 20 -c 30
./check_nt -H 192.168.1.91 -p 1248 -v COUNTER -l “TestCounterCategory\TestCounter”, -w 20 -c 30

./check_nt -H 192.168.1.91 -p 1248 -v COUNTER -l “^TestCounterCategory^TestCounter”, -w 20 -c 30
./check_nt -H 192.168.1.91 -p 1248 -v COUNTER -l “TestCounterCategory^TestCounter”, -w 20 -c 30

Using “COUNTER” flag:
shatterit.com/nc_net/files/r … om_Counter

Name of the counter using the NC_Net performance counter viewer:
TestCounterCategory:TestCounter:0

Code snippet:
using System.Diagnostics;

PerformanceCounter _counter;

// see: msdn2.microsoft.com/en-us/2b0byczf(VS.80.aspx
_counter = new PerformanceCounter( “TestCounterCategory”, “TestCounter”, false );
_counter.RawValue = 0

Full source code:
using System;
using System.Diagnostics;
using System.Threading;

namespace PCTest
{
class App
{
private delegate void RunDelegate();

    private Thread             _counterThread;
    private PerformanceCounter _counter;
    private bool               _counterRunning;

    public App()
    {
        try
        {
            _counter = new PerformanceCounter( "TestCounterCategory", "TestCounter", false );        
            _counter.RawValue = 0;                
        }
        catch(Exception e:evil:{ throw new ApplicationException( "Missing counters", ex );}
    }

    ~App()
    {
        _counter.RawValue = 0;            
    }

    private void Run()
    {                        
        while(_counterRunning)
        {
            _counter.RawValue += 2;                
            Thread.Sleep(1000);
        }            
    }

    public void BeginRun()
    {
        _counterRunning = true;
        _counterThread = new Thread( new ThreadStart( this.Run ) );
        _counterThread.Start();
                    
        Console.WriteLine("PCTest running...Press any key to exit");
        Console.ReadLine();

        _counterRunning = false;
        _counterThread.Join();                            
    }

	[STAThread]
	static void Main(string] args)
	{
        try
        {
            if( args.Length == 0 )
            {
                App app = new App();
                app.BeginRun();
            }
            else if( args[0] == "-i" )
                App.Install();
            else if( args[0] == "-u" )			
                App.Uninstall();
            else
                App.Usage();
        }
        catch( ApplicationException ex )
        {
            Console.WriteLine( ex.Message );
            App.Usage();
        }
        catch( Exception ex )
        {
            Console.WriteLine("-----------------------------------------");
            Console.WriteLine( ex.Message );
            Console.WriteLine( ex.StackTrace );
            Console.WriteLine("-----------------------------------------");
            App.Usage();
        }
	}

    public static void Usage()
    {
        Console.WriteLine( "PCTest.exe -i] -u]" );
        Console.WriteLine( "  -i install performnance counter" );
        Console.WriteLine( "  -u uninstall performnance counter" );
    }

    public static void Install()
    {
        // Remove an existing performance category and its counters
        if( PerformanceCounterCategory.Exists( "TestCounterCategory" ) )
            throw new ApplicationException( "Performance counters have already been installed." );

        // Create a set of performance counter for each task
        CounterCreationDataCollection ccdc = new CounterCreationDataCollection();
                            
        // Trans execs per hour
        CounterCreationData ccd = new CounterCreationData( "TestCounter",
            "TestCounterDesc",
            PerformanceCounterType.NumberOfItems64 );
        ccdc.Add( ccd );
                                
        // Create category
        PerformanceCounterCategory.Create( "TestCounterCategory",
            "TestCounterCategoryDesc",
            ccdc );
    }

    public static void Uninstall()
    {
        // Remove an existing performance category and its counters
        if( PerformanceCounterCategory.Exists( "TestCounterCategory" ) )
            PerformanceCounterCategory.Delete( "TestCounterCategory" );                    
    }
}

}

Thanks for your time,
Erik

So, it appears from the lack of responses that most people don’t access custom performance counters. Wonder if I am headed down a dark and lonely path… :shock:

P.S. Cross posted on:
nagios-forum.co.uk/forum/vie … 392ac91ef1

Has anyone successfully accessed custom Windows (C#/.Net) performance counters?

Ok, I was hesitant to post the solution given how obvious (in hindsight) it was. It turned out to be a windows permissions issue. I was using local system user for the NC_Net service and a different user for the service I was trying to monitor. Sigh…

What made this (slightly) trickier to debug was that during testing my app and NC_net’s test perf counter viewer were running under my credentials, but the NC_Net service was running under the local system account. So, I could see them via the NC_Net perf viewer, but NC_Net couldn’t find them, so the check_nt plugin returned -1.

In my (weak) defense, I had seen the following error in the event log, but it was a bit misleading…

Description:
Exeption occured during Counter check :Category does not exist.::>\CounterCategory(testcounterinstance)\TestCounter

Hope this is helpful to someone else someday…

Cheers,
Erik

While this isn’t Eriks issue, I think tis probably a good thing to mention. In the count samples Erik listed the port variable was used with port 1248. I was looking for some samples of how to use the COUNTER variable and spent about 30 minutes trying to make this work. Finally I realized my port is 12489, not 1248.