Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .mdlrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# https://github.com/markdownlint/markdownlint/blob/master/docs/RULES.md
rules "~MD013", "~MD022", "~MD024", "~ND032"
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ inherit_from: .rubocop_todo.yml

# Support all non EOL rubies
AllCops:
TargetRubyVersion: 2.3
TargetRubyVersion: 2.4

MethodLength:
Max: 200
Expand Down
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# Change Log

This project adheres to [Semantic Versioning](http://semver.org/).

This CHANGELOG follows the format listed at [Our CHANGELOG Guidelines ](https://github.com/sensu-plugins/community/blob/master/HOW_WE_CHANGELOG.md).
This CHANGELOG follows the format listed at [Our CHANGELOG Guidelines](https://github.com/sensu-plugins/community/blob/master/HOW_WE_CHANGELOG.md).
Which is based on [Keep A Changelog](http://keepachangelog.com/)

## [Unreleased]

### Changes
- update powershell checks to provide the proper exitcode (@derekgroh)
- update tests to favor Pester instead of Ruby (@derekgroh)
### Breaking Changes
- update metrics-windows-network.ps1 -Interface argument handling to work with Interfaces names that contain underscores
Expand Down
103 changes: 44 additions & 59 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Sensu-Plugins-Windows
# Sensu-Plugins-Windows

[![Build Status](https://travis-ci.org/sensu-plugins/sensu-plugins-windows.svg?branch=master)](https://travis-ci.org/sensu-plugins/sensu-plugins-windows)
[![Gem Version](https://badge.fury.io/rb/sensu-plugins-windows.svg)](http://badge.fury.io/rb/sensu-plugins-windows)
Expand All @@ -13,44 +13,43 @@ These files provide basic Checks and Metrics for a Windows system.

### Ruby

* bin/check-windows-cpu-load.rb
* bin/check-windows-disk.rb
* bin/check-windows-process.rb
* bin/check-windows-processor-queue-length.rb
* bin/check-windows-ram.rb
* bin/check-windows-service.rb
* bin/metric-windows-cpu-load.rb
* bin/metric-windows-disk-usage.rb
* bin/metric-windows-network.rb
* bin/metric-windows-processor-queue-length.rb
* bin/metric-windows-ram-usage.rb
* bin/metric-windows-uptime.rb
* bin/powershell_helper.rb
* bin/check-windows-cpu-load.rb
* bin/check-windows-disk.rb
* bin/check-windows-process.rb
* bin/check-windows-processor-queue-length.rb
* bin/check-windows-ram.rb
* bin/check-windows-service.rb
* bin/metric-windows-cpu-load.rb
* bin/metric-windows-disk-usage.rb
* bin/metric-windows-network.rb
* bin/metric-windows-processor-queue-length.rb
* bin/metric-windows-ram-usage.rb
* bin/metric-windows-uptime.rb
* bin/powershell_helper.rb

### Powershell

* bin/check-windows-cpu-load.ps1
* bin/check-windows-disk.ps1
* bin/check-windows-disk-writeable.ps1
* bin/check-windows-pagefile.ps1
* bin/check-windows-process.ps1
* bin/check-windows-processor-queue-length.ps1
* bin/check-windows-ram.ps1
* bin/check-windows-service.ps1
* bin/metric-windows-cpu-load.ps1
* bin/metric-windows-disk-usage.ps1
* bin/metric-windows-network.ps1
* bin/metric-windows-processor-queue-length.ps1
* bin/metric-windows-ram-usage.ps1
* bin/metric-windows-uptime.ps1
* bin/check-windows-directory.ps1
* bin/check-windows-event-log.ps1
* bin/check-windows-log.ps1

* bin/check-windows-cpu-load.ps1
* bin/check-windows-disk.ps1
* bin/check-windows-disk-writeable.ps1
* bin/check-windows-pagefile.ps1
* bin/check-windows-process.ps1
* bin/check-windows-processor-queue-length.ps1
* bin/check-windows-ram.ps1
* bin/check-windows-service.ps1
* bin/metric-windows-cpu-load.ps1
* bin/metric-windows-disk-usage.ps1
* bin/metric-windows-network.ps1
* bin/metric-windows-processor-queue-length.ps1
* bin/metric-windows-ram-usage.ps1
* bin/metric-windows-uptime.ps1
* bin/check-windows-directory.ps1
* bin/check-windows-event-log.ps1
* bin/check-windows-log.ps1

## Usage

##### Example 1:
### Example 1

Execute Powershell functions using the helper (No copy needed), see example below:

Expand All @@ -61,53 +60,39 @@ Execute Powershell functions using the helper (No copy needed), see example belo
"command": "c:\\opt\\sensu\\embedded\\bin\\ruby C:\\opt\\sensu\\embedded\\bin\\powershell_helper.rb check-windows-ram.ps1 90 95",
"interval": 30,
"type": "check",
"handler": "win_metrics",
"subscribers": ["win_metrics"]
"handler": "win_memory",
"subscribers": ["win_memory"],
"runtime_assets": ["sensu-plugins-windows", "sensu-ruby-runtime"]
}
}
}
```

##### Example 2:

- Copy either the Ruby or Powershell files on a Sensu Client, typically under C:\etc\sensu\plugins.
### Example 2

- You should also include the full escaped path to the ruby interpreter in the check's command configuration, see example below:
Execute Powershell functions directly, see example below:

```json
{
"checks": {
"cpu_percent": {
"command": "c:\\opt\\sensu\\embedded\\bin\\ruby C:\\opt\\sensu\\etc\\plugins\\metric-windows-cpu-load.rb",
"command": "Powershell.exe -NonInteractive -NoProfile -ExecutionPolicy Bypass -NoLogo -File check-windows-ram.ps1 90 95",
"interval": 30,
"type": "metric",
"handler": "win_metrics",
"subscribers": ["win_metrics"]
"subscribers": ["win_metrics"],
"runtime_assets": ["sensu-plugins-windows"]
}
}
}
```

You should also include the full escaped path to the ruby interpreter in the check's command configuration, see example below:

```json
{
"checks": {
"cpu_percent": {
"command": "c:\\opt\\sensu\\embedded\\bin\\ruby C:\\opt\\sensu\\etc\\plugins\\metric-windows-cpu-load.rb",
"interval": 30,
"type": "metric",
"handler": "win_metrics",
"subscribers": ["win_metrics"]
}
}
}
```

## Dependencies
* Powershell checks require Powershell version 3.0 or higher.

* Powershell checks require Powershell version 3.0 or higher.

## Troubleshooting

* Failures to pull counter data with messages like below, might be due to corrupt performance counters. See [Here](https://support.microsoft.com/en-us/help/2554336/how-to-manually-rebuild-performance-counters-for-windows-server-2008-6) for more information. Short answer on fix is `lodctr /R` in an Admin elevated command prompt

`Check failed to run: undefined method length' for nil:NilClass, "c:/opt/sensu/plugins/check-windows-ram.rb:45:inacquire_ram_usage'", "c:/opt/sensu/plugins/check-windows-ram.rb:54:in run'", "c:/opt/sensu/embedded/lib/ruby/gems/2.0.0/gems/sensu-plugin-1.`
Expand Down Expand Up @@ -148,4 +133,4 @@ Invoke-Pester

## Installation

[Installation and Setup](http://sensu-plugins.io/docs/installation_instructions.html)
[Install Sensu plugins](https://docs.sensu.io/sensu-go/latest/plugins/install-plugins/)
27 changes: 16 additions & 11 deletions bin/check-windows-cpu-load.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,34 @@ Param(
[int]$CRITICAL
)

. (Join-Path $PSScriptRoot perfhelper.ps1)
# Function to help the exitcode be seen by Sensu
function ExitWithCode
{
param
(
$exitcode
)

$host.SetShouldExit($exitcode)
exit
}

$ThisProcess = Get-Process -Id $pid
$ThisProcess.PriorityClass = "BelowNormal"

$perfCategoryID = Get-PerformanceCounterByID -Name 'Processor Information'
$perfCounterID = Get-PerformanceCounterByID -Name '% Processor Time'

$localizedCategoryName = Get-PerformanceCounterLocalName -ID $perfCategoryID
$localizedCounterName = Get-PerformanceCounterLocalName -ID $perfCounterID

$Value = [System.Math]::Round((Get-Counter "\$localizedCategoryName(_total)\$localizedCounterName" -SampleInterval 1 -MaxSamples 1).CounterSamples.CookedValue)
$Value = [System.Math]::Round((Get-Counter '\Processor(_Total)\% Processor Time').CounterSamples.CookedValue)

If ($Value -ge $CRITICAL) {
Write-Host CheckWindowsCpuLoad CRITICAL: CPU at $Value%.
Exit 2 }
ExitWithCode 2
}

If ($Value -ge $WARNING) {
Write-Host CheckWindowsCpuLoad WARNING: CPU at $Value%.
Exit 1
ExitWithCode 1
}

Else {
Write-Host CheckWindowsCpuLoad OK: CPU at $Value%.
Exit 0
ExitWithCode 0
}
16 changes: 14 additions & 2 deletions bin/check-windows-directory.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,25 @@ Param(
[string]$Dir
)

# Function to help the exitcode be seen by Sensu
function ExitWithCode
{
param
(
$exitcode
)

$host.SetShouldExit($exitcode)
exit
}

$ThisDir = Test-Path -Path $Dir

#Shows diretory if it exist
if ($ThisDir) {
"CheckDirectory OK: Directory exist"
EXIT 0
ExitWithCode 0
}else {
"CheckDirectory CRITICAL: Directory doesn't exist"
EXIT 2
ExitWithCode 2
}
25 changes: 20 additions & 5 deletions bin/check-windows-disk.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# Powershell 3.0 or above
#
# USAGE:
# Powershell.exe -NonInteractive -NoProfile -ExecutionPolicy Bypass -NoLogo -File C:\\etc\\sensu\\plugins\\check-windows-disk.ps1 90 95 ab
# Powershell.exe -NonInteractive -NoProfile -ExecutionPolicy Bypass -NoLogo -File check-windows-disk.ps1 -Warning 90 -Critical 95
#
# NOTES:
#
Expand All @@ -38,13 +38,28 @@ Param(
[string]$IGNORE
)

# Function to help the exitcode be seen by Sensu
function ExitWithCode
{
param
(
$exitcode
)

$host.SetShouldExit($exitcode)
exit
}

# Set the process priority
$ThisProcess = Get-Process -Id $pid
$ThisProcess.PriorityClass = "BelowNormal"

# Set $IGNORE and $INCLUDE values if not provided.
If ($IGNORE -eq "") {
$IGNORE = "ab"
}

# Select all fixed local disks (3) which are $INCLUDE and not $IGNORE
$AllDisks = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType = 3" | Where-Object { $_.DeviceID -notmatch "[$IGNORE]:"}

$crit = 0
Expand All @@ -70,15 +85,15 @@ foreach ($ObjDisk in $AllDisks) {

if ($crit -ne 0) {
if ($warn -ne 0 ){
Write-Host "CheckDisk CRITICAL: $crit disks in critical state `n$critDisks;`n$warn disks in warning state:`n$warnDisks"
Write-Host "CheckDisk CRITICAL: $crit disks in critical state `n$critDisks;`nCheckDisk WARNING: $warn disks in warning state:`n$warnDisks"
} else {
Write-Host "CheckDisk CRITICAL: $crit disks in critical state `n$critDisks"
}
exit 2
ExitWithCode 2
} elseif ($warn -ne 0) {
Write-Host "CheckDisk WARNING: $warn disks in warning state `n$warnDisks"
exit 1
ExitWithCode 1
}

Write-Host "CheckDisk OK: All disk usage under $WARNING%."
Exit 0
ExitWithCode 0
17 changes: 14 additions & 3 deletions bin/check-windows-event-log.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ Param(
[string]$Pattern
)

# Function to help the exitcode be seen by Sensu
function ExitWithCode
{
param
(
$exitcode
)

$host.SetShouldExit($exitcode)
exit
}
#Search for pattern inside of File
$ThisEvent = Get-WinEvent $LogName -ErrorAction SilentlyContinue | Where {$_.Message -like "*$($Pattern)*"}

Expand All @@ -40,12 +51,12 @@ $CountWarns=($ThisEvent | Where{$_.LevelDisplayName -eq 'Warning'}).count
#Prints count of how many ciritials and warnings
If($CountCrits -eq 0 -And $CountWarns -eq 0){
"CheckLog OK: $CountCrits criticals $CountWarns warnings"
EXIT 0
ExitWithCode 0
}ElseIF ($CountCrits -gt 0) {
"CheckLog CRITICAL: $CountCrits criticals $CountWarns warnings"
EXIT 2
ExitWithCode 2
}
Else {
"CheckLog WARNING: $CountCrits criticals $CountWarns warnings"
EXIT 1
ExitWithCode 1
}
16 changes: 14 additions & 2 deletions bin/check-windows-http.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ Param(
[string]$CheckAddress
)

# Function to help the exitcode be seen by Sensu
function ExitWithCode
{
param
(
$exitcode
)

$host.SetShouldExit($exitcode)
exit
}

$ThisProcess = Get-Process -Id $pid
$ThisProcess.PriorityClass = "BelowNormal"

Expand All @@ -48,9 +60,9 @@ if (!$Available) {
if ($Available) {
if ($Available.statuscode -eq 200) {
Write-Host OK: $CheckAddress is available!
Exit 0
ExitWithCode 0
} else {
Write-Host CRITICAL: URL $CheckAddress is not accessible!
Exit 2
ExitWithCode 2
}
}
Loading