Muhammad Tahir
Muhammad Tahir

Reputation: 5184

Monit not working with Redis

I am configuring Monit to monitor Redis and Redis Sentinel. But every time Monit is run, it logs that both Redis and Redis Sentinel are not running and tries to run them although both of them are already done. It fails in starting the Redis processes.

Whereas it is working perfectly for system checks and nginx and some other processes.

Versions:

Redis: Redis server v=2.8.4 sha=00000000:0 malloc=jemalloc-3.4.1 bits=64 build=a44a05d76f06a5d9
Monit: version 5.6

Here is my configuration of Monit for Redis (Redis Sentinel has almost same configuration):

# redis
check process redis with pidfile /var/run/redis-server.pid
  group cache
  group redis
  start program = "/etc/init.d/redis-server start"
  stop program  = "/etc/init.d/redis-server stop"
  if failed host 127.0.0.1 port 6379 then restart
  if totalmem > 500 Mb then alert
  if cpu > 60% for 2 cycles then alert
  if cpu > 98% for 5 cycles then restart
  if 2 restarts within 2 cycles then alert
  depend redis_bin
  depend redis_rc

check file redis_bin with path /usr/bin/redis-server
  group redis
  include /etc/monit/templates/rootbin

check file redis_rc with path /etc/init.d/redis-server
  group redis
  include /etc/monit/templates/rootbin

Here are the logs of Monit:

[PKT Aug 19 17:00:07] error    : 'redis' process is not running
[PKT Aug 19 17:00:07] info     : 'redis' trying to restart
[PKT Aug 19 17:00:07] info     : 'redis' start: /etc/init.d/redis-server
[PKT Aug 19 17:00:37] error    : 'redis' failed to start

I think this cause of this issue is other processes that are working fine are owned by root along with all the directories and bin files. But everything related to Redis is own by user "redis" from group "redis". And this is why Monit is not able to start or stop Redis.

But I have tried to use it by adding user in start and stop settings like this:

start program = "/etc/init.d/redis-server start"
  as uid redis and gid redis
stop program  = "/etc/init.d/redis-server stop"
  as uid redis and gid redis

But this didn't work either.

What am I doing wrong here? What is the correct way to monitor Redis with Monit?

Upvotes: 4

Views: 2408

Answers (2)

Jason
Jason

Reputation: 1028

This is my monitrc entry for redis. See if this works for you.

check process redis matching "redis"
     start program = "/usr/bin/sudo /bin/systemctl start redis"
     stop program = "/usr/bin/sudo /bin/systemctl stop redis"
     if failed host localhost port 6379 protocol redis then alert
     if failed host localhost port 6379 for 3 cycles then restart
     if 3 restarts within 10 cycles then timeout

Upvotes: 0

Rabea
Rabea

Reputation: 2034

A couple of changes I made to make sure that Monit has access to start/stop the service and also making sure I have the right PID file that Monit checks once it restarts the service. In my case , this was the configuration:

# redis
check process redis with pidfile /var/run/redis/redis-server.pid
  group cache
  group redis
  start program = "/usr/sbin/service redis-server start" with timeout 60 seconds
  stop program  = "/usr/sbin/service redis-server stop" with timeout 60 seconds
  if failed host 127.0.0.1 port 6379 then restart
  if totalmem > 500 Mb then alert
  if cpu > 60% for 2 cycles then alert
  if cpu > 98% for 5 cycles then restart
  if 2 restarts within 2 cycles then alert
  depend redis_bin
  depend redis_rc

check file redis_bin with path /usr/bin/redis-server
  group redis
  include /etc/monit/templates/rootbin

check file redis_rc with path /etc/init.d/redis-server
  group redis
  include /etc/monit/templates/rootbin
  • The generated PID file path is different on my machine, I recommend you make sure it is the right path on your side.
  • I gave the start/stop operation 60 seconds instead of the default 30 second
  • A side note: I used /usr/sbin/service redis-server instead of /etc/init.d/redis-server but this shouldnt make a difference, I tried both of them, and they both work.

Monit allows before timing out, just to make sure it has a good amount of time to respond as a service.

The output is:

[EST Nov 30 16:25:22] error    : 'redis' process is not running
[EST Nov 30 16:25:22] info     : 'redis' trying to restart
[EST Nov 30 16:25:22] info     : 'redis' start: /usr/sbin/service
[EST Nov 30 16:27:22] info     : 'redis' process is running with pid 24864

Upvotes: 2

Related Questions