Everything about Web and Network Monitoring

Home > Server Management > Windows Servers Monitoring > How to Easily Monitor Windows Performance Counters with VBScript on Monitis

How to Easily Monitor Windows Performance Counters with VBScript on Monitis

Monitis provides you with the ability to monitor almost any detail on your Windows server (also see our post How to Monitor Windows Servers with VBScript, WMI and Monitis).

Using a simple Visual Basic Script (VBS), you will be able to read the value of any Performance Counter on your Windows Server and send it to a Custom Monitor.

It’s easy as pie. Keep reading for the recipe!

About Performance Counters

 

Performance Counters are used to provide information as to how well the operating system or an application, service, or driver is performing.  Performance Counters are highly detailed, and their counter value covers Processor, Disk, Network interface and a lot of other parameters.  The data from these counters can help determine system bottlenecks, and you can fine tune system and application performance.

You can access data from Performance Counters using interactive applications, such as Performance Monitor, or Windows Management Instrumentation (WMI) programs.  All Performance Counters’s value are made available through WMI via the classes named Win32_PerfFormattedData*.  The values’s names and instances are the same, which you can see within Performance Monitor and other interactive tools.

Also, you can browse available classes and values using WMI Explorer, and you can download it from this address: http://www.ks-soft.net/hostmon.eng/wmi/index.htm.

In this article, we will monitor the counter Connections Established in the Tcpip_TCPv4 class.
Monitoring established connections is important because it can give you a precise idea about what the usage pattern looks like. It can also give you a clue that your server is under attack or it is being exploited.  Of course, if you’re using Monitis to monitor your Performance Counters and something should go wrong, we’ll notify you immediately via a variety of ways — including cell phone, instant messaging, email, SMS, Twitter and URL callback.

You can learn more about monitoring Performance Counters with WMI here: http://msdn.microsoft.com/en-us/library/aa392397%28v=vs.85%29.aspx#using_raw_performance_data_classes

API Access

 

The first thing you will need in order to monitor WMI data is the Monitis API Key and Secret Key.  The API Key is an alphanumeric code that allows you to access the Monitis API urls and transmit or receive data about your Monitis services.  The Secret Key is an alphanumeric code that allows you to digitally sign your information to ensure that only you can transmit data to your Monitis account.

Your API Key may be disclosed to anyone, but your Secret Key must be maintained private and should not be shared or transmitted.  How safe is that? Very. To obtain your Monitis API Key and Secret Key, log into your account and from the top menu bar, go to:

  • Tools,
  • API
  • API Key

Both your API Key and your Secret Key will appear.

Create a Custom Monitor

 

A Custom Monitor is needed to display the values you get from external sources.

In order to create it you need go through the following steps:

  1. Connect to Monits API and obtain an authorization token
  2. Create the Custom Monitor tailored to your needs.

The following VBScript code does it all for you:

'You API key and Secret Key
apiKey = "your API key here"
secretKey = "your secret key here"

'Connecting to WMI, "." is for local computer, specify computer name for another computer
computer = "."
Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" + computer + "\root\cimv2")

'Finds current timezone to obtain GMT date
dtGMT = now
Set oRes = oWMI.ExecQuery("Select LocalDateTime from Win32_OperatingSystem")
For each oEntry in oRes
  dtGMT = DateAdd("n", -CInt(right(oEntry.LocalDateTime, 4)), dtGMT)
next

'Initialize HTTP connection object
Set objHTTP = CreateObject("Microsoft.XMLHTTP")

'Request a token to use in following calls
url = "http://www.monitis.com/api?action=authToken&apikey=" + apiKey + "&secretkey=" + secretKey
objHTTP.open "GET", url, False
objHTTP.send
resp = objHTTP.responseText
token = DissectStr(resp, "authToken"":""", """")

'Adds the custom monitor
url = "http://www.monitis.com/customMonitorApi"
monitorParams = "PerfCtrs_data:Connections:Connections+established:3:false;"
resultParams = "connections:Connections:N%2FA:2;"
name = "Connections established"
tag = "[perfctrs+sample]"
objHTTP.open "POST", url, False
objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
postData = "apikey=" + apiKey + "&validation=token&authToken=" + token + "&timestamp=" + FmtDate(dtGMT) + "&action=addMonitor&monitorParams=" + monitorParams + "&resultParams=" + resultParams + "&name=" + name + "&tag=" + tag
objHTTP.send postData
resp = objHTTP.responseText

Set objHTTP = Nothing

Function DissectStr(cString, cStart, cEnd)
  dim nStart, nEnd
  nStart = InStr(cString, cStart)
  if nStart = 0 then
    DissectStr = ""
  else
    nStart = nStart + len(cStart)
    if cEnd = "" then
  	nEnd = len(cString)
    else
  	nEnd = InStr(nStart, cString, cEnd)
  	if nEnd = 0 then nEnd = nStart else nEnd = nEnd - nStart
    end if
	DissectStr = mid(cString, nStart, nEnd)
  end if
End Function

Function FmtDate(dt)
  FmtDate = cstr(Datepart("yyyy", dt)) + "-" + right("0" + cstr(Datepart("m", dt)),2) + "-" +  right("0" + cstr(Datepart ("d", dt)),2) + " " + right("0" + cstr(Datepart("h", dt)),2) + ":" + right("0" + cstr(Datepart("n", dt)),2) + ":" + right("0" + cstr(Datepart("S", dt)),2)
end function

Save this code to a file with a .vbs extension (such as createCustMon.vbs) and save it, then run it executing the following command from a command prompt:

cscript createCustMon.vbs

Uploading Performance Counters values to Custom Monitor

 

Once your custom monitor is created you can start uploading Performance Counters values into it.

Take the following steps:

  1. Connect to Monits API and obtain an authToken
  2. Get the list of Custom Monitors and locate the ID of the monitor you want to add data to
  3. Execute your WMI query and get the necessary Performance Counter values
  4. Upload the value to the Custom Monitor

The following script does exactly that:

'You API key and Secret Key
apiKey = "your API key here"
secretKey = "your secret key here"

'Connecting to WMI, "." is for local computer, specify computer name for another computer
computer = "."
Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" + computer + "\root\cimv2")

'Finds current timezone to obtain GMT date
dtGMT = now
Set oRes = oWMI.ExecQuery("Select LocalDateTime from Win32_OperatingSystem")
For each oEntry in oRes
  dtGMT = DateAdd("n", -CInt(right(oEntry.LocalDateTime, 4)), dtGMT)
next

'Initialize HTTP connection object
Set objHTTP = CreateObject("Microsoft.XMLHTTP")

'Request a token to use in following calls
url = "http://www.monitis.com/api?action=authToken&apikey=" + apiKey + "&secretkey=" + secretKey
objHTTP.open "GET", url, False
objHTTP.send
resp = objHTTP.responseText
token = DissectStr(resp, "authToken"":""", """")

'Requests the monitor list in order to find the MonitorID
tag = "[perfctrs+sample]"
url = "http://www.monitis.com/customMonitorApi?action=getMonitors&apikey=" + apiKey + "&tag=" + tag + "&output=xml"
objHTTP.open "GET", url, False
objHTTP.send
resp = objHTTP.responseText

Set oResp = CreateObject("Microsoft.XMLDOM")
oResp.async = False
oResp.LoadXML(resp)

for each oNode in oResp.documentElement.childnodes
  if oNode.selectSingleNode("name").text = "Connections established" then
     monitorID = oNode.selectSingleNode("id").text
     exit for
  end if
next

wscript.echo "Monitor ID: " + monitorID

'Executes the WMI Query to obtain the data
Set oRes = oWMI.ExecQuery ("select ConnectionsEstablished from Win32_PerfFormattedData_Tcpip_TCPv4")
For each oEntry in oRes
  value = oEntry.ConnectionsEstablished
next

wscript.echo "Connections established: " & value

'Posts data
url = "http://www.monitis.com/customMonitorApi"
action = "addResult"
results = "connections:" & value & ";"
unixDate = CStr(DateDiff("s", "01/01/1970 00:00:00", DateSerial(Year(dtGMT), Month(dtGMT), Day(dtGMT)) + TimeSerial(Hour(dtGMT), Minute(dtGMT), Second(dtGMT)))) + "000"
objHTTP.open "POST", url, False
objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
postData = "apikey=" + apiKey + "&validation=token&authToken=" + token + "&timestamp=" + FmtDate(dtGMT) + "&action=" + action + "&monitorId=" + monitorID + "&checktime=" + unixDate + "&results=" + results
objHTTP.send postData
resp = objHTTP.responseText

Function DissectStr(cString, cStart, cEnd)
  'Generic string manipulation function to extract value from JSON output
  dim nStart, nEnd
  nStart = InStr(cString, cStart)
  if nStart = 0 then
    DissectStr = ""
  else
    nStart = nStart + len(cStart)
    if cEnd = "" then
  	nEnd = len(cString)
    else
  	nEnd = InStr(nStart, cString, cEnd)
  	if nEnd = 0 then nEnd = nStart else nEnd = nEnd - nStart
    end if
    DissectStr = mid(cString, nStart, nEnd)
  end if
End Function

Function FmtDate(dt)
  FmtDate = cstr(Datepart("yyyy", dt)) + "-" + right("0" + cstr(Datepart("m", dt)),2) + "-" +  right("0" + cstr(Datepart ("d", dt)),2) + " " + right("0" + cstr(Datepart("h", dt)),2) + ":" + right("0" + cstr(Datepart("n", dt)),2) + ":" + right("0" + cstr(Datepart("S", dt)),2)
end function

Save this code to a file with a .vbs extension (such as pushDataPerfCtrs.vbs). Then run it executing the following command from a command prompt:

cscript pushDataPerfCtrs.vbs

Testing the Monitor

 

Each time you run the second script, a value is uploaded to your Custom Monitor.
You can use Windows Task Scheduler to run the script automatically and as frequently as you need.

Add your Custom Monitor widget to your Monitis account like this:

From the Monitors menu, select and click Manage Monitors. Select Create Custom Monitor, then click Add to Window.
WMI Monitor

Refresh the page after adding new data to see them show up inside the widget.

We at Monitis hope this information on monitoring Performance Counters with VBScript will help you see how important — and easy — it is to use an independent monitoring service for servers. That’s what we do best. If you don’t believe us, check out the testimony of customers who have found extraordinary value in hosted monitoring. For example, one customer, DCmedia Hosting,  says Monitis’s “unique interface is easy to use and informative at the same time making setting up monitoring in a browser anywhere a breeze.”

 

Post Tagged with ,
Hovhannes Avoyan

About Hovhannes Avoyan

Monitis CEO – Hovhannes is an international entrepreneur with a recognized and respected reputation in the high tech industry. His technical expertise, combined with his drive to build the best business/product, has positioned him as a visionary international extension of Silicon Valley.

Web & Cloud
Monitoring