It is frequently necessary to collect performance counters to troubleshoot problems on servers. Unfortunately, quite often the condition we are trying to troubleshoot is transient, and by the time an administrator is alerted to the problem it has already passed. This document describes a method to set up automated performance monitoring that can be triggered by a particular performance condition being met.
It involves setting up a performance alert to write an event to the application log, and then using the latest version of task scheduler to start a batch job based upon the appearance of that event.
This article applies to Windows 2008 and 2008 R2, and Exchange 2007 and 2010.
Part 1. Setting an alert condition
First, set up alert condition by following http://technet.microsoft.com/en-us/library/cc722414.aspx
To create a Data Collector Set to monitor Performance counters
- To open Windows Performance Monitor, click Start, click in the Start Search box, type perfmon, and then press ENTER.
- In the Windows Performance Monitor navigation pane, expand Data Collector Sets, right-click User Defined, point to New, and click Data Collector Set. The Create new Data Collector Set Wizard starts.
- Enter a name for your Data Collector Set.
- Select the Create manually option and click Next.
- Select the Performance Counter Alert option and click Next.
- Click Add to open the Add Counters dialog box. When you are finished adding counters, click OK to return to the wizard.
- Define alerts based on the values of performance counters you have selected.
- From the list of Performance counters, select the counter to monitor and trigger an alert.
- From the Alert when drop-down, choose whether to alert when the performance counter value is above or below the limit.
- In the Limit box, enter the threshold value. Eg, if you want to gather performance data when cpu utilisation is greater than 95%, enter “95”
- When you are finished defining alerts, click Next to continue configuration.
- After clicking Next, you can configure the Data Collector Set to run as a particular user. Click the Change button to enter the user name and password for a different user than the default listed.
- Click Finish to return to Windows Performance Monitor.
- To view the properties of the Data Collector Set or make additional changes, select Open properties for this data collector set. For more information about the properties of the Data Collector Set, see Data Collector Set Properties.
- To start the Data Collector Set immediately (and begin saving data to the location specified in Step 8), select Start this data collector set now.
- To save the Data Collector Set without starting collection, select Save and close.
To configure alert actions
- Expand Reliability and Performance in the navigation pane.
- Expand Data Collector Sets, expand User Defined, and click the name of the Data Collector Set with performance counter alerts.
- In the console pane, right-click the name of a Data Collector whose type is Alert and click Properties.
- On the Data Collector Properties page, click the Alerts tab. The data collectors and alerts already configured should appear.
- Click the Alert Action tab to choose to write an entry to the event log Applications and Services Logs/Microsoft/Windows/Diagnosis-PLA/Operational when the alert criteria are met. You can also start a Data Collector Set when the alert criteria are met; Don’t do this.
- Click the Alert Task tab to choose a Windows Management Interface (WMI) task and arguments to run when the alert criteria are met. Don’t do this.
This will give you an alert that writes a 2031 event to the log named in step 15, above.
Part 2. Creating the batch file
First, set the PowerShell environment. Open the Exchange Shell and run the following:
Then download and extract the ExPerfWiz powershell script from the Microsoft website:
Make sure to download the latest version – 1.3.7 or higher.
Then Create a batch file to run the experfwiz script:
- Open notepad
- Copy the text below into the open document, and save it as C:\perfwiz\experfwiz.bat, or something equally usable. Try and avoid spaces or non standard characters in your path.
PowerShell.exe -command “. ‘c:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1′; Connect-ExchangeServer -auto; c:\experfwiz\experfwiz.ps1 -duration 00:15:00 -interval 1 -quiet”
- You will need to check:
Location of Exchange binaries for the RemoteExchange.ps1 script.
Location of ExPerfWiz script
Version of ExPerfWiz script – only 1.3.7 and later support the –quiet switch.
Change duration and interval to something suitable. In the example above the duration is 15 minutes, and the interval is two seconds.
Part 3. Scheduling the batch file
The batch file will be run by setting up a scheduled task that will run every time an event id is logged. The Event ID that you will use to trigger the Log:
Event id: 2031
Now to create a Scheduled Task.
- If Task Scheduler is not open, start Task Scheduler. For more information, see Start Task Scheduler.
- Find and click the task folder in the console tree that you want to create the task in. If you want to create the task in a new task folder, see Create a New Task Folder to create the folder.
- In the Actions Pane, click Create Task.
- On the General tab of the Create Task dialog box, enter a name for the task. Make sure it is running under an admin account, and that it is set to run only when the user is logged on. You will need to remain logged on to the server for the duration.
- On the Triggers tab of the Create Task dialog box, click the New… button to create a trigger for the task, and supply information about the trigger in the New Trigger dialog box. Select On an Event to start the task.Fill in the details for the event as shown in the picture below.
- On the Actions tab of the Create Task dialog box, click the New… button to create an action for the task, and supply information about the action in the New Action dialog box. Select Start a Program, and browse to the batch file you created in part 2, above. Once this action is saved you can, if you choose, set it to also send you an email when the condition occurs.
- Click the OK button on the Create Task dialog box.
Part 4. Final checks, and what to do next.
- Make sure that the data collector set is running.
- Make sure that the batch file will work by starting it manually. Check the c:\perflogs folder for the generated log file.
- Make sure that the process works – do this by setting the data collector set to alert on a commonly met condition, such as cpu utilization = 20%. Check that:
- The event is logged in the Applications and Services Logs/Microsoft/Windows/Diagnosis-PLA/Operational event log.
- The scheduled task is triggered by selecting the task, and checking the “history” tab in the lower window of the scheduler console.
- The perfmon log is generated and saved to the C:\perflogs directory.
When the condition is met, the task triggered, the batch file ran and the log generated, why not use PAL to analyze it. I would.
The batch file doesn’t have to point to ExPerfWiz, of course. It could quite easily point to a batch file that triggers logman, or you could avoid using a batch file altogether and set perfmon up to trigger a data collector set instead of an event. For more details on using logman with a batchfile see our nutshell “Scripting Perfmon for Win XP through to Win 2008”
Thanks to Mike Lagase for the excellent ExPerfWiz script, and Amit Tank at exchangeshare.wordpress.com for his article on scheduling Exchange tasks. And obviously thanks to technet, where I’ve ripped great chunks of this from.
- Exchange 2007/2010 Performance Data Collection Script
- Windows Performance Monitor
- Scheduling and Managing Data in Windows Performance Monitor
if you want the script to mail you a little email to say it’s done, or similar, then you’ll want to read Morgen Simonsen’s blog, here:
you may also find it useful to have a mail method that avoids CDOSYS altogether: