3

I've been using monit to monitor and restart Apache and MySQL for several months now and all has been working fine until today when something on the server caused the memory utilization to exceed 90%, MySQL stopped and monit then tried to continually restart however there was insufficient memory to be able to restart it.

A full server restart sorted everything so now running as normal again.

My question is can I get monit to monitor the server ram utilization and free up ram or restart the server etc when it exceeds 90% for example?

DoubleSpeed
  • 43
  • 1
  • 5
  • Why not fix the underlying problem where Apache's able to fire up enough processes to eat up all the server's RAM? Look into the max clients settings. – ceejayoz May 19 '15 at 18:18

1 Answers1

7

Capacity planing

A note about memory consumption of MySQL and Apache. Both are more or less deterministic and you ought to do some capacity planning beforehand, not just restarting the server every time it couldn't survive a load spike because of misconfiguration. In case of MySQL sum up all allocated buffers, caches, threads cache (number of ready-to-serve threads) and their stack size, etc and you get the number. MySQLTuner can do it for you -- the line looks something like (besides other important metrics):

[OK] Maximum possible memory usage: 352.0M (48% of installed RAM)

In case of Apache it's harder, but empirical way should be the way to go. Having limited number of workers in Apache configuration, you just need to estimate an average memory consumption per worker -- look at ps, top, etc. Then multiply the value by the number of workers. If you run something like mod_php then there's also a way to restrict memory usage at the side of your runtime.

Monit side

Monit can restart a service (e.g. MySQL) when it matches a certain limit (e.g. consumed memory). But it will be a mistake to do in most cases, because MySQL maintains buffers and cache and serves clients. Restarting would cause performance degradation and service outage. Use it only when you can't restrict memory consumption in any other way. For example:

check process mysql with pidfile /var/run/mysqld/mysqld.pid
  start program = "/etc/init.d/mysql start"
  stop program = "/etc/init.d/mysql stop"

  if totalmem > 256 MB for 10 cycles then restart

  group database

It's also possible to monitor system-wide memory consumption, but be aware that Linux memory model is complex and it's really hard to tell what amount of memory a process really consumes. System-wide metrics like free memory can also be misguiding because values of disk cache (cached memory), shared library memory, and many other aspects should be taken into account.

Here's example from Monit's default Debain default config:

check system myhost.mydomain.tld
  if loadavg (1min) > 4 then alert
  if loadavg (5min) > 2 then alert
  if memory usage > 75% then alert
  if cpu usage (user) > 70% then alert
  if cpu usage (system) > 30% then alert
  if cpu usage (wait) > 20% then alert

You can use exec action (then <ACTION>) to restart your server, but I really don't recommend you to follow this path, but rather do some capacity planning.

Community
  • 1
  • 1
saaj
  • 23,253
  • 3
  • 104
  • 105