Auto Monitor New Users Or Directories

Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Auto Monitor New Users Or Directories

Lorne Wanamaker
Hi,

I have a server where new directories are created automatically in /var/www /htmleach of these contains a site and is attached to a new system user also created at the same time.

I do not have a way to limit these directories for disk use, so would like to be able to monitor them for size and alert if they go over lets say 1GB. Thing is I cannot be creating a new rule in monit for every new user or directory.

I can't find anything on how I might accomplish this. Has anybody done anything similar with monit and mind sharing an example?
Reply | Threaded
Open this post in threaded view
|

Re: Auto Monitor New Users Or Directories

Henning Bopp (boppy)
Hi Lorne,

I am using a `check program` for this (info: my Monit has 1 minute cycle):

```
check program "dirsizer" path "/srv/monit/dirsizer" timeout 360 seconds
  every 60 cycles # Runs once an hour
  if status != 0 then alert
```


while "/srv/monit/dirsizer" might be like:

```
#!/usr/bin/env bash

checkdir="/var/www/html"
sizes=$(du -d 1 -t 1G -h "${checkdir}" 2>/dev/null | head -n -1 | sort -rh)
chars=$(wc -c <<< "$sizes")

if [[ "${chars}" -gt "1" ]]; then
    echo "$(head -n10 <<< "$sizes")"
    exit 1
fi

exit 0
```

In this case, you will receive an alert if there is at least one
directory with >= 1G of size. You will also get the 10 biggest
directories above 1G in the programs output to review on the web front
end AND included in the alert email (if the LIMITS allow it). Since
Monit is configured to alert only on status!=0, you might even echo
the sizes without an alert, so you can review them any time on the
web-frontend of Monit.

As this request seems to refer to some kind a shared hosting, you have
to keep an eye on the execution time. In my scenario there are not
that many small files that slow down du significantly. But with
multiple CMS' installs, you might run into a timeout. Good thing:
You'll receive an alert about this! ;)

Reply | Threaded
Open this post in threaded view
|

Re: Auto Monitor New Users Or Directories

Lorne Wanamaker
That is perfect! I was thinking of calling a script but was not quite sure how to go about it.

Thanks!

On Sun, Nov 3, 2019 at 9:31 AM Henning Bopp (boppy) <[hidden email]> wrote:
Hi Lorne,

I am using a `check program` for this (info: my Monit has 1 minute cycle):

```
check program "dirsizer" path "/srv/monit/dirsizer" timeout 360 seconds
  every 60 cycles # Runs once an hour
  if status != 0 then alert
```


while "/srv/monit/dirsizer" might be like:

```
#!/usr/bin/env bash

checkdir="/var/www/html"
sizes=$(du -d 1 -t 1G -h "${checkdir}" 2>/dev/null | head -n -1 | sort -rh)
chars=$(wc -c <<< "$sizes")

if [[ "${chars}" -gt "1" ]]; then
    echo "$(head -n10 <<< "$sizes")"
    exit 1
fi

exit 0
```

In this case, you will receive an alert if there is at least one
directory with >= 1G of size. You will also get the 10 biggest
directories above 1G in the programs output to review on the web front
end AND included in the alert email (if the LIMITS allow it). Since
Monit is configured to alert only on status!=0, you might even echo
the sizes without an alert, so you can review them any time on the
web-frontend of Monit.

As this request seems to refer to some kind a shared hosting, you have
to keep an eye on the execution time. In my scenario there are not
that many small files that slow down du significantly. But with
multiple CMS' installs, you might run into a timeout. Good thing:
You'll receive an alert about this! ;)