In previous articles we introduced the command-line utility “Monitis-Top”, written in VBScript. Monitis Top is a tool that can be used to query your active monitors from the command-line. We promised that we would provide the code of this client utility in C# in addition to the VBScript source, and here it is. The code utilizes Microsoft .NET Framework 4.0, so that is a requirement if you want to use this software or want to make modifications.
We’ll go over the source code below. The complete source code – including the Visual Studio 2010 solution – can be downloaded from Monitis’ Github open source repository.
The class hierarchy uses an AgentList class that is derived from a List<Agent> class and is responsible for retrieving the agents from the Monitis interface. The Agent class maintains the list of Monitor objects and is responsible for retrieving the monitor data from Monitis. The Monitor class is an abstract class and maintains the list of Metric object that store the actual results and values of each monitor.
[gist] https://gist.github.com/1320844 [/gist]
We have four different Monitor classes derived from the base Monitor class; GlobalMonitor, used for the overall CPU, Memory, and Drive counters; ProcessMonitor for the metrics of running processes; ExternalMonitor for the predefined external monitors; and FullpageMonitor for the full page results. We’ll discuss the GlobalMonitor class as the other monitor classes follow the same principles.
Depending on the type of monitor, the parent Agent object calls the appropriate function. We’ll discuss the Agent.GetGlobalMonitors meth od.
This method uses the list of requested monitors from the command line to retrieve each monitor from Monitis by looping through the ShowMonitors
list and performing a Linq to XML query to filter just the monitor we’re
interested in: var allMonitors = from monitorNode in xml.Descendants(m)
[gist] https://gist.github.com/1320841 [/gist]
The base Monitor class from which the GlobalMonitor class is derived contains the properties for each monitor’s Id, Name, parent Agent object, and the MonitorDefitions. We’ll discuss the MonitorDefinitions later.
[gist] https://gist.github.com/1320852 [/gist]
The main method in this class is GetMetrics. It retrieves the results from Monitis and enumerates each supported monitored in the MonitorDefinitions list. For each entry in this list it creates a Metric object and stores the actual metric values.
The MonitorDefinitions class is used by each Monitor object and contains the properties that specify the available Monitis API details the monitor supports. The Metric property correlates to the Monitis name of the monitor result, and the Names and Suffixes specify the names of each performance counter within the result and what suffix (MB, %, etc) to use.
The function AddNamePair allows for adding a Name and Suffix pair to this list.
[gist] https://gist.github.com/1320863 [/gist]
The full source code is available for download from GistHub (under Monitis’ Windows Monitoring Scripts)