Skip to content

Instantly share code, notes, and snippets.

@gbiellem
Last active February 27, 2017 07:10
Show Gist options
  • Save gbiellem/de8908f20c47e3d573750596a1a17640 to your computer and use it in GitHub Desktop.
Save gbiellem/de8908f20c47e3d573750596a1a17640 to your computer and use it in GitHub Desktop.
Disk and Services Report
<#
Send Slack Notification in Disk gets below 20%
Send Slack Notification in any service that match the service name prefixs are not running (skip of service start is not Automatic)
#>
Function SlackDiskReport {
param(
[Parameter(Mandatory, ValueFromPipeLine)]
$disk
)
process {
$percentageFree = [Math]::round((($disk.freespace/$disk.size) * 100))
$freeGB = [math]::round($disk.freespace /1Gb, 0)
if ($percentageFree -lt 21) {
$color= 'good'
if ($percentageFree -lt 20) {
$color = 'warning'
}
if ($percentageFree -lt 10) {
$color = 'danger'
}
$msg = "{0} has {1}% free disk space ({2} GB)" -f $Env:COMPUTERNAME, $percentageFree, $freeGB
$attachment = @{
'title' = 'Disk space warning'
'color' = $color
'fallback' = $msg
'text' = $msg
}
$report = @{'attachments' = @($attachment)}
$body = ConvertTo-Json -Depth 10 -InputObject $report
IWR -UseBasicParsing -Uri $url -Body $body -Method Post
}
}
}
Function FilterServicesByPrefix {
param (
[Parameter(Mandatory, ValueFromPipeLine)]
[System.ServiceProcess.ServiceController] $service,
[Parameter(Mandatory)]
[ValidateCount(1, 1000000)]
[string[]] $prefix
)
process {
$prefix | % { if ( $service.name -match "^$_") { return $service }}
}
}
Function SlackServiceReport {
param(
[Parameter(Mandatory, ValueFromPipeLine)]
[System.ServiceProcess.ServiceController] $service
)
process {
if (($service.Status -ne "Running") -and ($service.StartType -eq "Automatic" )) {
$attachment = @{
'title' = $service.Name
'color' = 'danger'
'fallback' = $service.Name + " is " + $service.Status.ToString()
'text' = $service.Name + " is " + $service.Status.ToString()
}
$report = @{'attachments' = @($attachment)}
$body = ConvertTo-Json -Depth 10 -InputObject $report
IWR -UseBasicParsing -Uri $url -Body $body -Method Post
}
}
}
$url = Get-Content -Path 'C:\Monitron\slackwebhookurl.txt'
Get-Service | FilterServicesByPrefix -prefix @("particular", "nservicebus", "website.backend") | SlackServiceReport
Get-WmiObject -class Win32_logicalDisk -Filter "DeviceID='C:'" | SlackDiskReport
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment