3

I have a directory on my server and I want to monitor the number of files inside this directory with Monit... How can this be done?

Giacomo1968
  • 3,522
  • 25
  • 38

1 Answers1

3

There should be some better way to do that, but this works:

  • Create your monitoring program like this, for example in /tmp/monit-num-files.sh:

    #!/bin/bash
    
    maxfiles=80
    dir="/tmp"
    
    if [ $(ls $dir|wc -l) -ge $maxfiles ]; then
      exit 1
    else
      exit 0
    fi
    
  • Then add this to your Monit configuration.

    check program number-of-files with path "/tmp/monit-num-files.sh"
       if status != 0 then alert
    

This alerts if the number of files in /tmp is more or equal 80. Directories are counted as files (but this can be changed easily).

If you want to change the file limit or the directory simply change the variables "maxfiles" and "dir" in the monitoring program.

Giacomo1968
  • 3,522
  • 25
  • 38
unlink
  • 690
  • 7
  • 12