I would like to count the number of files in a folder on a ftp server of certain types, e.g. count all xml files. As far as I can see the "FTP SERVER FILE COUNT SENSOR" can not filter by filename. Is there another sensor I can use?


Article Comments

Hello Johannes,

I'm afraid there is no native sensor for this. I would recommend to take a look here if this is an option for you.


Kind regards

Felix Wiesneth - Team Tech Support


Jul, 2020 - Permalink

I created a custom sensor using Powershell, Nuttercode-Prtg and PSFTP.

Param (
    [Parameter(Mandatory=$True,Position=0)][String]$Hostname,
    [Parameter(Mandatory=$True,Position=1)][String]$Username,
    [Parameter(Mandatory=$True,Position=2)][String]$Password,
    [Parameter(Mandatory=$True,Position=3)][String]$Path,
    [Parameter(Mandatory=$False,Position=4)][String]$Filter = "*",
    [Parameter(Mandatory=$False,Position=5)][switch]$Recurse = $false
)

$ErrorActionPreference = "Stop"
Import-Module PSFTP

$sensor = New-PRTGSensor
[int]$itemCount = 0

try {
    $garbage = Set-FTPConnection -Session Session -Server $Hostname -Credentials (New-Object -TypeName System.Management.Automation.PSCredential -Argumentlist $Username, ($Password | ConvertTo-SecureString -AsPlainText -Force))
    $session = Get-FtpConnection -Session Session
    $itemCount = @(
        $(
            if( $Recurse ) {
                Get-FTPChildItem -Session $Session -Path $Path -Recurse
            }
            else {
                Get-FTPChildItem -Session $Session -Path $Path
            }
        ) | ? { $_.Name -like $Filter }
    ).Count
}
catch {
    $sensor.has_error = $true
    $sensor.error_code = 1
    $sensor = $sensor | Set-PRTGSensorText -text "An error occurend while running this sensor. Message: $_"
}

$sensor |
    Add-PRTGChannel -name "File Count" -value $itemCount -unit Count |
Convert-PRTGSensorToXML

Jul, 2020 - Permalink

Hello Johannes,

thank you for sharing your solution.


Kind regards

Felix Wiesneth - Team Tech Support


Jul, 2020 - Permalink