Last updated at Mon, 30 Oct 2017 13:28:07 GMT
We are often asked at Support questions such as, “How do we use Logentries to monitor Windows performance stats?” or “Can Logentries get alerts based on CPU utilization on Windows?” The simple answer is yes, with the help of our Logentries Agent for Windows and PowerShell we can.
Here is a 5 minute tutorial to get you monitoring your CPU, Memory and Disk Space with Logentries.
Installing the Logentries Agent
You will need an account to follow this tutorial, if you dont have an account yet, sign up now. It’s quick, easy, and free!
Scroll down to the “Installation” section here and choose the Windows tab. Next download and install the agent on your Windows server.
Setting up a PowerShell script
Create a new script called monitor.ps1 on your Windows server and add the following text.
# Current Load as a percentage
$load = (Get-WmiObject win32_processor | select -exp LoadPercentage)
# Memory Usage as a percentage
$memory = (Get-WmiObject win32_operatingsystem | Foreach {"{0:N2}" -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize)})
# Free Disk as a percentage
$disk = (Get-WmiObject Win32_Volume -Filter "DriveLetter = 'C:'" | Foreach {"{0:N2}" -f (($_.FreeSpace / $_.Capacity)*100)})
$d = "load=$load | memory=$memory | disk=$disk"
$file = "C:\Users\Administrator\Desktop\monitor.log"
if ((Get-Item $file).length -gt 5Mb) {
Remove-Item $file
}
$d | Out-File $file -Append utf8
We use Windows Management Instrumentation (WMI), a component on Windows machines aimed at managing local and remote computers and collecting information, to get the current load, memory and free disk as a percentage and write it out to a file at C:\monitor.log
As sample output of the command is as follows:
load=5 | memory=36 | disk=29
Make the script run every X mins
Having a script that can get this information is all and good, but we need a way for it to run in the background automatically. To do this we’ll set up a task using Windows Task Scheduler to make our script run every 5 minutes and gather our system information.
Follow these steps to open and set up Task Scheduler:
- Click the Start button
- Click Control Panel,
- Click System and Security
- Click Administrative Tools
- Double-click Task Scheduler
- If you’re prompted for an administrator password or confirmation, type the password or provide confirmation
To schedule a program to start automatically:
- Click the Action menu, and then click Create Basic Task.
- Type a name for the task and an optional description, and then click Next.
- On the following screen, choose Daily as the schedule you want to use, and then click Next.
- Click Start a program
- The program/script we want to use run is powershell.exe with an argument specifying the location of your script
- Click Next then, finish
- You will get a prompt to edit it your script further, choose yes
- We will schedule our task to run daily, but to repeat every N minutes. In the image below we repeat the task every 1 minute. this can be adjusted to your own specification.
Using the Windows Logentries Agent to follow the file
We will now make the Logentries Agent follow your file by opening the Windows LE Agent, clicking follow file and pointing it to the location of the file that is being appended to above. (In the example we used C:/monitor.log)
Using our Tags & Alerts to monitor the Windows Server
Log into your account at https://Logentries.com. You should see your Windows server in the list of hosts with a log being followed called “monitor.log”. Here is where the real power of Logentries comes in, we’re going to Create a Tag / Alert called windows_load which will match the string load and a value that is greater than 80:
- Click on the Tags & Alerts tab.
- Select or create a label for this pattern match: load>80
- Choose your log: monitor.log which this tag will be applied to
Next we will create the alert, which is used to notify you via email, webhook or via the Logentries iPhone app:
- Simply press the “Add Alert” button at the bottom of the screen
- Start filling out the form
- Select the frequency of which the notification will trigger
- Select how often you wish to be alerted
- Insert an email which you want the notification to be sent to when this event occurs
Once complete you will have a monitoring system on your Windows machine which will alert you whenever your load, disk or memory usage is above the given threshold.