Counting the number of files in a Windows client

This forum was archived to /woltlab and is now in read-only mode.
  • Hello,

    after finally having Icinga2 running and being able to monitor a Windows client with the default tools, I need to count files in a folder on that Windows client. How?

    I only have a basic idea how that might be done: Maybe the Icinga2 server should execute a .cmd script which returns the number of files in a folder? With "set count=0 & for %x in (*) do @(set /a count+=1 >nul)" and "echo %count%" I already can see the number of files in a folder. But how to execute that script from the Icinga2 server and read-in the result? And is this even the right way or should the client send the data so the server, instead of the server asking the client? A threshold when a certain number of files is reached in the folder should send an alarm.

  • Hey,

    the usual way is to have the client do all the work and only send performance data and check result back. I wrote a blog post on monitoring Powershell scripts with Icinga 2 (works just as well for cmd, you'll just need to make a few adjustments), regarding thresholds and performance data you might want to read the monitoring guidelines.

  • Actually this is possible with the standard bat

    Here a quick and dirty script:

    1. @echo off
    2. dir /a-d c:\somefolder | find /c ":" > NUMfiles.###
    3. set /p count=<NUMfiles.###
    4. echo %count%
  • crunsher, thanks, but systemctl status icinga2 says there's a problem with PLUGIN_DIR (Not defined? Not sure about the syntax value, maybe define it as PLUGIN_DIR = "C:\Program Files\ICINGA2"? I put the check_process.ps1in C:\Program Files\ICINGA2\sbin where the other default check_***.exe files were put buy the WIndows's setup, is that correct?

    Edit: I changed the path to: vars.powershell_script = "C:\Program Files\ICINGA2\sbin" + "\check_process.ps1"but systemctl status icinga2 says:

    1. vars.powershell_script = "C:\Program Files\ICINGA2
    2. Mai 09 13:23:17 icinga icinga2[10785]: ^^

    birkch, thanks, is this the same as my mentioned "set count=0 & for %x in (*) do @(set /a count+=1 >nul) and echo %count%"? I see your script uses set. How does this work?

  • Hi, i have made a small check for this. Its still work in progress.

    Edit: link to script added: check_files.ps1

    and here are the much cleaner Icingacommands for executing:

    The post was edited 1 time, last by unic ().

  • birkch, so I'd put this in C:\Program Files\ICINGA2\sbin? But then I need to get it to work somehow.

    Wolfgang, the blog post contains already an apply Service "check_powerpoint" {

    unic, I tried your code but I don't see any "apply Service ..." in there so I added:

    1. apply Service "check filecount" {
    2. import "generic-service"
    3. check_command = "files-windows"
    4. assign where host.vars.os == "Windows"
    5. }

    but unfortunately I get "execvpe(C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe) failed: No such file or directory", but the file is there.

  • Yes you put the script in the same directory as the standard windows monitoring plugins.

    for a simple bat plugin the following *could* work (this is not tested)

    1. object CheckCommand "check_files_num" {
    2. command = [ PluginDir + "/check_files_num.bat" ]
    3. }

    apply Service "check filecount" {
    import "generic-service"
    check_command = "files-windows"
    assign where host.vars.os == "Windows"

    be aware that you need the command_endpoint definition here. (this has to be executed on the client)

  • Quote

    be aware that you need the command_endpoint definition here. (this has to be executed on the client)

    birkch, ok, I added it. This is how it looks now:

    1. apply Service "check filecount" {
    2. import "generic-service"
    3. check_command = "files-windows"
    4. command_endpoint = host.vars.remote_client
    5. assign where host.vars.os == "Windows"
    6. }

    sru, ok, I added the arguments, this is how it looks now:

    but now it says:

    Check command 'files-windows' does not exist. (But as one can see above object CheckCommand "files-windows" { is there, and check_files.ps1 is also in the sbin folder.)

    PS: If I google for "vars.powershell_script" only this forum post and the blog come up XD.

  • root@icinga:/etc/icinga2# icinga2 object list --type CheckCommand | grep files-windows
    Object 'files-windows' of type 'CheckCommand':
    * __name = "files-windows"
    * name = "files-windows"
    * templates = [ "files-windows", "plugin-check-command", "plugin-check-command" ]

  • Not sure how you mean, but I added the apply Service "check filecount" { to the C:\ProgramData\icinga2\etc\icinga2\conf.d\services.conf and the 3 object CheckCommands to the C:\ProgramData\icinga2\etc\icinga2\conf.d\commands.conf now and restarted the icinga2 service on the Windows client (> Restart-Service icinga2) and the icinga2 server service on the Linux VM. Now I'm getting: File or folder () not found - Permissions or path correct ?.


    On the Linux Icinga server in the /etc/icinga2/zones.conf the Windows client is set and I can see its disk size, cpu usage, ping4, ping6, uptime, users, ram usage, so, so far I'd it's working. If only the powershell scripts would be working now, that's basically essential.

  • Looks like the Icinga2 configuration is working now, but you need to use the correct parameters.

    the service variable "$files_windows_args$" needs to be: "-unit count -path c:\windows\temp" if you want to check the tempfolder for example. I think you can add "-recurse yes" to count files recursively

    Sadly all Icinga return messages are in German in this check.

  • If I change

                                    value = "$files_windows_args$"

    to say

                                    value = "-unit count -path C:\Users\"

    I get Remote Icinga instance 'DOMAIN-NAME1' is not connected to 'icinga' and all services and the services mentioned above ("disk size, cpu usage, [...]") have the same error too now.

    PS: Does not Linux server still need the 3 object CheckCommand s in its commands.conf or only the Windows client now?

    Edit: Looks like the Linux server needs them too. These object CheckCommands should be the same on both the Server and Windows client?