Monitis–Where You Can Monitor Exchange 2010 with PowerShell


Did you know that you can use Monitis to monitor your critical applications such as Exchange?
Microsoft Exchange is a popular and powerful enterprise messaging platform and provides critical collaboration tools. It’s very popular with companies, and it is important to ensure that every aspect of the messaging infrastructure is working flawlessly. And you do that (rather, we do it for you) by monitoring the most important parameters.

Just add the following information to our recent trove of blog posts on ways to improve IT performance and administration via Monitis. So far, we’ve written a book full of posts on such topics as Cacti, SNMP, Monitis and what’s between them, adding Monitis website monitors using Excel, how to manage Monitis monitors with VBScript…the list goes on.

Monitoring via Exchange

Exchange management can be carried out using either Management Console or Management Shell. Management Console is a graphical user interface that looks very familiar to most Windows administrators. Management Shell is essentially a PowerShell console boosted with a lot of Exchange related cmdlets. You can find more on Management Shell here.

Management Shell is even more powerful than Management Console and you can use it to perform any action and get any configuration and status value. For example using simple PowerShell scripts you can monitor outbound message queues and get notified when too many messages are not being sent.

PowerShell and ExecutionPolicy

By default PowerShell requires that all scripts are signed to be run, this means that your scripts won’t run unless you sign them. In order to run your custom script you will have to change the execution policy.

You can check the current execution policy settings with the command Get-ExecutionPolicy inside a PowerShell window. Possible values are Restricted (the default), RemoteSigned and Unrestricted.

Using RemoteSigned you will be able to run custom scripts saved on your local drives, if you plan to save your scripts on network drives you will have to use Unrestricted. In order to configure the appropriate execution policy you will have to run the following command: Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

API Access

The first thing you will need in order to monitor Exchange 2010 data is the Monitis AI 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.  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 PowerShell code does it all for you:

$secretKey = "5CK8P217FA21Q9ARDDRGUJSAK"
#Requesting token
$url = "" + $apiKey + "&secretkey=" + $secretKey
$wc = new-object net.webclient
$resp = $wc.DownloadString($url).ToString()
$pos = $resp.IndexOf(":") + 2
$token = $resp.Substring($pos, $resp.Length - $pos - 2)

#Adding custom monitor
$nvc = new-object System.Collections.Specialized.NameValueCollection
$nvc.Add('apikey', $apikey)
$nvc.Add('validation', 'token')
$nvc.Add('authToken', $token)
$nvc.Add('timestamp', (get-date).touniversaltime().ToString("yyyy-MM-dd HH:mm:ss"))
$nvc.Add('action', 'addMonitor')
$nvc.Add('monitorParams', 'exchange_queues:Exchange+data:Number of messages:3:false;')
$nvc.Add('resultParams', 'queued:Queued:N%2FA:2;')
$nvc.Add('name', 'Exchange-queues' )
$nvc.Add('tag', '[exchange+sample]' )

$wc = new-object net.webclient
$wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
$resp = $wc.UploadValues('', $nvc)


 Save this code to a file with a .ps1 extension (such as CreateCustMon.ps1).  Then open a PowerShell window, change to the folder where the script is saved, and then run it executing the following command:



Uploading values to a Custom Monitor

Once your custom monitor is created you can start uploading Exchange data 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. Get queue status and the values you need
  4. Upload the value to the Custom Monitor

The following script does exactly that:

if ( (Get-PSSnapin -Name Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction SilentlyContinue) -eq $null ) {
  Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
. $env:ExchangeInstallPath\bin\RemoteExchange.ps1
Connect-ExchangeServer -auto

$secretKey = "5CK8P2176A21Q9ARDDRGUJBAK"

#Requesting token
$url = "" + $apiKey + "&secretkey=" + $secretKey
$wc = new-object net.webclient
$resp = $wc.DownloadString($url).ToString()
$pos = $resp.IndexOf(":") + 2
$token = $resp.Substring($pos, $resp.Length - $pos - 2)

#Requests the monitor list in order to find the MonitorID
$tag = '[exchange+sample]'
$url = '' + $apiKey + "&tag=" + $tag
$wc = new-object net.webclient
$resp = $wc.DownloadString($url).ToString()
$posL = $resp.IndexOf('id') + 5
$posR = $resp.IndexOf('","')
$monitorID = $resp.Substring($posL, $posR-$posL)

$messages = (Get-TransportServer | Get-Queue | Measure-Object MessageCount -sum).sum

#Uploading data
$nvc = new-object System.Collections.Specialized.NameValueCollection
$nvc.Add('apikey', $apikey)
$nvc.Add('validation', 'token')
$nvc.Add('authToken', $token)
$nvc.Add('timestamp', ((get-date).touniversaltime().ToString("yyyy-MM-dd HH:mm:ss").ToString()))
$nvc.Add('action', 'addResult')
$nvc.Add('monitorId', $monitorID)
$nvc.Add('checktime', ([int][double]::Parse((get-date((get-date).touniversaltime()) -UFormat %s))).ToString() + '000')
$nvc.Add('results', ('queued:' + $messages.tostring()) + ';' )

$wc = new-object net.webclient
$wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
$resp = $wc.UploadValues('', $nvc)

Save this code to a file with a .ps1 extension (such as PushDataExch.ps1). Then open a PowerShell window, change to the folder where the script is saved and run it executing the following command:



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 as frequently as you need.
In this case you will have to configure the following Action in your task:

  • Command: powershell

  • Arguments: -Command “& { c:\scripts\PushDataExch.ps1}”

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.

Monitis Custom Monitor

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

What else to monitor in Exchange 2010

Now that you have learned the basics of interacting with your Monitis account from PowerShell, you can customize your scripts to monitor the most critical metrics of Exchange 2010.

In the example script, the value that is uploaded to Monitis is in the line that reads:

$messages = (Get-TransportServer ....

You can replace the value of this variable with the sample commands that follows:

(get-counter '\Processor(_Total)\% Processor Time').CounterSamples[0].CookedValue

This counter specifies the processor use of each or all processors. If these values are consistently higher than 50%–60%, consider upgrade options or segmenting workloads.

(get-counter '\PhysicalDisk(0 C:)\% Disk time').CounterSamples[0].CookedValue

This counter analyzes the percentage of elapsed time that the selected disk spends on servicing read or write requests. Ideally, this value should remain below 50%.

(get-counter '\PhysicalDisk(0 C:)\Avg. Disk Queue Length').CounterSamples[0].CookedValue

This counter has different performance indicators depending on the monitored disk drive
(Database or Transaction Log volume). On disk drives storing the Exchange Server
database, this value should be below the number of spindled drives divided by 2. On
disk drives storing transaction log data, this value should be below 1.

(get-counter '\Memory\Pages/Sec').CounterSamples[0].CookedValue

The values of this counter should range from 5 to 20. Values consistently higher than 10 are indicative of potential performance problems, whereas values consistently higher than 20 might cause noticeable and significant performance hits. The trend of these values is impacted by the amount of physical memory installed in the server.

(get-counter '\Network Interface(<your adapter name>)\Bytes Received/sec').CounterSamples[0].CookedValue
(get-counter '\Network Interface(<your adapter name>)\Bytes Sent/sec').CounterSamples[0].CookedValue

This indicates the rate at which the network adapter is processing data bytes. This counter includes all application and file data, in addition to protocol information such as packet headers.

The processing rate for a 100-megabytes per second (MBps) network adapter should be below 6–7 MBps. For a 1000-megabits per second (Mbps) network adapter, the rate should be below 60–70 Mbps.  And, for Mailbox servers, gigabit Ethernet (1 gigabit per second (Gbps)) is recommended.

(Get-Counter '\MSExchange Database(Information Store)\Log Bytes Write/sec').CounterSamples[0].CookedValue

The rate of bytes written to the log, should be less than 10 million at all times.
With each log file being 1 million bytes in size, 10 million bytes/sec would yield 10 logs per second. This may indicate a large message being sent or a looping message.

(Get-Counter '\MSExchange ADAccess Domain Controllers(<your domain controller name>)\Long running LDAP operations/Min').CounterSamples[0].CookedValue

This indicates the number of LDAP operations on this domain controller that took longer than the specified threshold per minute.  It should be less than 50 at all times, higher values may indicate issues with Active Directory resources.

(Get-Counter '\MSExchange RpcClientAccess\Active User Count').CounterSamples[0].CookedValue

This indicates the number of unique users that have shown some activity in the last 2 minutes.

(Get-Counter '\MSExchange RpcClientAccess\Connection Count').CounterSamples[0].CookedValue

The above represents the total number of client connections maintained.

(Get-Counter '\MSExchange RpcClientAccess\RPC Operations/sec').CounterSamples[0].CookedValue

The above shows the rate at which RPC operations occur, per second.

(Get-Counter '\MSExchange RpcClientAccess\User Count').CounterSamples[0].CookedValue

The above shows the number of users connected to the service.

Monitis also allows you to display data in charts, in the case of the last counter you can obtain a chart like this:

Monitis Charts

It seems like we’re running through MS programs in our effort to show you how you can Monitor everyday applications with Monitis. Recently, we expounded on Excel. And we promise to keep adding to your knowledge bank with even more instructive posts!

Monitis Monitoring Platform

You might also like