Search code examples
monit

Can Monit send an alert if a folder size exceeds a certain number?


I'm monitoring a file size using Monit the following way:

check file mydb with path /data/mydatabase.db
  if size > 1 GB then alert

Can I do something similar to monitor a directory size?


Solution

  • Don't think they have one but my workaround is to use check program the following way:

    check program the_folder_size 
      with path "/path/to/bashscript /path/to/folder_to_monitor 500"
      if status != 0 then alert
    

    where the bash script takes in two arguments, the path to the folder to monitor and the size (in kB) which the folder isn't allowed to exceed. The echo part is to make monit output the folder size in the monit web gui.

    #!/bin/bash
    FOLDER_PATH=$1
    REFERENCE_SIZE=$2
    SIZE=$(/usr/bin/du -s $FOLDER_PATH  | /usr/bin/awk '{print $1}')
    echo "$FOLDER_PATH  -  ${SIZE}kB"
    if [[ $SIZE -gt $(( $REFERENCE_SIZE )) ]]; then exit 1;fi