# Custom Powershell Plugin

**URL:** <https://community.icinga.com/t/custom-powershell-plugin/12677>\
**Category:** Icinga 2\
**Tags:** icinga2\
**Created:** [September 28, 2023, 9:26am UTC](https://community.icinga.com/t/custom-powershell-plugin/12677 "2023-09-28T09:26:11Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![gurhan](https://community.icinga.com/letter_avatar_proxy/v4/letter/g/f17d59/32.png) [@gurhan](https://community.icinga.com/u/gurhan)\
**Post date:** [September 28, 2023, 9:26am UTC](https://community.icinga.com/t/custom-powershell-plugin/12677/1 "2023-09-28T09:26:12Z")

</div>

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](https://icinga.com/blog/2020/08/20/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
