Chapter 15: Final Project - System Monitoring Dashboard
Combine your PowerShell knowledge to build a system monitoring dashboard that tracks system performance, logs data, and provides real-time alerts.
In this final project, we�ll put together the PowerShell skills covered in previous chapters to create a practical, real-time system monitoring dashboard. This script will track key performance metrics, log data for historical analysis, and provide alerts if certain thresholds are met. A system monitoring dashboard can be invaluable for administrators managing server health and performance.
Project Overview and Key Metrics
The system monitoring dashboard will track key performance metrics, including:
- CPU usage
- Memory usage
- Disk space usage
- Network utilization
The dashboard will log these metrics to a file and send alerts if any metric exceeds a defined threshold, allowing administrators to respond proactively to potential issues.
Retrieving System Performance Metrics
To retrieve system metrics, we�ll use PowerShell�s Get-Counter
cmdlet and WMI (Windows Management Instrumentation) classes. Here�s how to gather the main metrics:
# CPU usage
$cpuUsage = (Get-Counter '\Processor(_Total)\% Processor Time').CounterSamples.CookedValue
# Memory usage
$memoryInfo = Get-WmiObject -Class Win32_OperatingSystem
$memoryUsage = [math]::round((($memoryInfo.TotalVisibleMemorySize - $memoryInfo.FreePhysicalMemory) / $memoryInfo.TotalVisibleMemorySize) * 100, 2)
# Disk space usage
$diskUsage = Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" | Select-Object DeviceID, @{Name='FreeSpaceGB';Expression={[math]::round($_.FreeSpace / 1GB, 2)}}, @{Name='SizeGB';Expression={[math]::round($_.Size / 1GB, 2)}}, @{Name='UsedPercent';Expression={[math]::round(($_.Size - $_.FreeSpace) / $_.Size * 100, 2)}}
# Network utilization
$networkUtilization = Get-Counter '\Network Interface(*)\Bytes Total/sec'
This code captures CPU usage, memory usage, disk usage, and network utilization, storing each in variables for further analysis and logging.
Setting Up Alert Thresholds
Define thresholds for each metric to trigger alerts when system performance falls outside acceptable limits. For example:
$cpuThreshold = 85 # Alert if CPU usage exceeds 85%
$memoryThreshold = 80 # Alert if memory usage exceeds 80%
$diskThreshold = 90 # Alert if any disk usage exceeds 90%
You can adjust these values based on your system�s requirements and typical workload.
Logging Data for Historical Analysis
To analyze trends, save the collected data to a CSV file for each run of the monitoring script. Use the Export-Csv
cmdlet to append each data point:
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$data = [PSCustomObject]@{
Timestamp = $timestamp
CPUUsage = $cpuUsage
MemoryUsage = $memoryUsage
DiskUsage = $diskUsage | ForEach-Object { "$($_.DeviceID): $($_.UsedPercent)%" }
}
$data | Export-Csv -Path "C:\SystemLogs\SystemMetrics.csv" -Append -NoTypeInformation
This example appends the latest system metrics to SystemMetrics.csv
with a timestamp for easy tracking over time.
Sending Real-Time Alerts
To notify administrators of critical performance issues, the script can send alerts via email when a metric exceeds its threshold. Configure the Send-MailMessage
cmdlet with your SMTP settings:
if ($cpuUsage -gt $cpuThreshold) {
Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "High CPU Usage Alert" -Body "CPU usage is at $cpuUsage%." -SmtpServer "smtp.example.com"
}
This example sends an email if CPU usage exceeds the defined threshold. You can add similar alerts for memory and disk usage.
Scheduling the Monitoring Script
To automate monitoring, schedule the script to run at regular intervals using Task Scheduler. Refer back to Chapter 11 for instructions on setting up scheduled tasks in Task Scheduler to automate execution of the monitoring script.
Final Thoughts and Customization Ideas
This system monitoring dashboard can be customized further by adding more metrics, improving alert conditions, or using data visualization tools to create a live monitoring dashboard. Consider exporting data to a central database or visualizing trends with Power BI or other reporting tools.
Summary and Project Wrap-Up
In this final project, we combined PowerShell techniques to build a system monitoring dashboard, covering real-time data collection, logging, alerts, and automation. This dashboard project demonstrates the powerful capabilities of PowerShell for system administration tasks, and we encourage you to continue exploring and customizing PowerShell for your unique use cases. Congratulations on reaching the end of this PowerShell journey!