Custom Powershell Plugin

Hello everyone,

I am slowly discovering Icinga . I wrote a powershell script for monitoring file age .
It was working fine however I realized that it was giving an error like this in
var/log/icinga2/icinga2.log

[2023-09-28 09:51:14 +0200] warning/GraphiteWriter: Ignoring invalid perfdata for checkable ‘Hostname!File-Age’ and command ‘windows-file-age’ with value: age=s;0;450

I converted my powershell script to .exe file and I am using like this . I read somewhere that this is more healthy way but I am not sure about it .

Create Custom Icinga Powershell Modules and Plugins

Should I follow this way or converting ps1 to exe and using like this is better way ?

This was the script I wrote in Powershell . It was working fine except not returning age value (I was able to see size value in performance data) in performance data . However I didn’t mind it first because I was able to see age value in output and it was returning critical when it exceeds threshold value .

My script is below here :

$opt_w = 240
$opt_c = 600
$opt_W = 0
$opt_C = 0
$opt_f = “”

$opt_V = $false
$opt_h = $false
$opt_i = $false

$PROGNAME = “check_file_age”

function print_usage {
Write-Host “Usage:”
Write-Host " $PROGNAME [-w ] [-c ] [-W ] [-C ] [-i] -f "
Write-Host " $PROGNAME [-h | --help]"
Write-Host " $PROGNAME [-V | --version]"
}

function print_help {
Write-Host “$PROGNAME”
Write-Host “Copyright (c) 2023 Gurhan Ocaktan”
Write-Host “”
print_usage
Write-Host “”
Write-Host " -i | --ignore-missing : return OK if the file does not exist"
Write-Host " File must be no more than this many seconds old (default: warn 240 secs, crit 600)"
Write-Host " File must be at least this many bytes long (default: crit 0 bytes)"
Write-Host “”
# Note: The “support()” function call is omitted as it is not provided in the original script.
}

$opt_f = $args[0] -replace ’ ', ‘’

Process arguments

for ($i = 1; $i -lt $args.Length; $i++) {
$arg = $args[$i]
switch ($arg) {
“-c” {
$opt_c = [int]$args[$i + 1]
$i++
break
}
“-w” {
$opt_w = [int]$args[$i + 1]
$i++
break
}
“-h” {
print_help
exit
}
“-V” {
Write-Host “$PROGNAME version 2.3.3”
exit
}
default {
Write-Host “Unknown argument: $arg”
exit
}
}
}

Check that file(s) exist

$filelist = Get-ChildItem $opt_f
$counter = 0
$high_water_mark = 0
$result = “OK”

foreach ($file in $filelist) {
if (-not (Test-Path $file)) {
if ($opt_i) {
Write-Host “FILE_AGE OK: $($file.FullName) doesn’t exist, but ignore-missing was set”
$this_result = “OK”
$this_level = 0
} else {
Write-Host “FILE_AGE CRITICAL: File not found - $($file.FullName)”
$this_result = “CRITICAL”
$this_level = 2
}

    if ($high_water_mark -lt $this_level) {
        $high_water_mark = $this_level
        $counter = 1
        $result = $this_result
    } elseif ($high_water_mark -eq $this_level) {
        $counter++
    }
    continue
}

$age = (Get-Date) - $file.LastWriteTime
$size = $file.Length

$perfdata = "age=${age.TotalSeconds}s;${opt_w};${opt_c} size=${size}B;${opt_W};${opt_C};0 "

$this_result = 'OK'
$this_level = 0

if (($opt_c -and $age.TotalSeconds -gt $opt_c) -or ($opt_C -and $size -lt $opt_C)) {
    $this_result = 'CRITICAL'
    $this_level = 2
} elseif (($opt_w -and $age.TotalSeconds -gt $opt_w) -or ($opt_W -and $size -lt $opt_W)) {
    $this_result = 'WARNING'
    $this_level = 1
}

if ($high_water_mark -lt $this_level) {
    $high_water_mark = $this_level
    $counter = 1
    $result = $this_result
} elseif ($high_water_mark -eq $this_level) {
    $counter++
}

Write-Host "FILE_AGE ${this_result}: $($file.FullName) is $($age.TotalSeconds) seconds old and $size bytes"

}

$summary = “${result}: $counter files are $result”

if ($filelist.Count -eq 1) {
Write-Host “$summary | $perfdata”
} else {
Write-Host “$summary”
Write-Host “$perfdata”
}

exit $this_level