I would like to handle a kind of chain action in monit.
My tries (so far):
check process myprocess with pidfile /run/my.pid
start program = "/path/to/binary start" with timeout 60 seconds
stop program = "/path/to/binary stop" with timeout 60 seconds
if not exist for 3 cycles then restart
if not exist then alert
if 3 restarts within 3 cycles then timeout
Does not alert and keeps in state "running" on failing PID but restarts after the 3 cycles.
check process myprocess with pidfile /run/my.pid
start program = "/path/to/binary start" with timeout 60 seconds
stop program = "/path/to/binary stop" with timeout 60 seconds
if not exist for 3 cycles then restart
if children < 1 for 1 cycles then alert
if 3 restarts within 3 cycles then timeout
No alert of children < 1 but restart afer 5.
monit.log
[CEST Aug 1 15:09:30] error : 'myprocess' process is not running
monit summary
Process 'myprocess' Running
Here ist monit -v part:
Existence = if does not exist 3 times within 3 cycle(s) then restart else
if succeeded 1 times within 1 cycle(s) then alert
Pid = if changed 1 times within 1 cycle(s) then alert
Ppid = if changed 1 times within 1 cycle(s) then alert
Children = if less than 1 1 times within 1 cycle(s) then alert else if
succeeded 1 times within 1 cycle(s) then alert
Timeout = If restarted 3 times within 3 cycle(s) then unmonitor
So the question: is it possible to send an alert and change the status to 'not running' within 1 cycle and restart after 3?
Now, If we want Monit to also start at boot so I need to edit the service configuration file at /etc/default/monit and ensure “START” is set to “yes”. And that's it. It will automatically restart any service or script. Moreover it is very simple to setup.
You can enable web interface and use it to view reports. By default, Monit is setup on port 2812. In the example above, the username is set to 'admin', and the password is set to 'monit'. Now you can log in to http://[YOUR-IP-ADDRESS]:2812 using admin:monit as credentials.
Monit will detach from the terminal and run as a background process, i.e. as a daemon process. As a daemon, Monit runs in cycles; It monitor services, then goes to sleep for a configured period, then wakes up and start monitoring again in an endless loop.
The monit configuration can be re-read after installing the PSM by entering the command “monit reload”, or by rebooting the machine. You can obtain the status of the PSM and other important services via the monit web interface at http:// <server ip address>:2812 or on the command line.
EDIT (IMPORTANT): See comments below for newer (as per Feb. 2019) versions of Monit, where this behaviour has been improved.
This line:
if does not exist for 3 cycles then restart
Means the following:
Do not perform any action until you have checked 3 times that the service does not exist, then restart it. This behaviour is described in monit's documentation as Failure Tolerance:
FAILURE TOLERANCE
By default the action is executed if it matches and the service set in an error state. However, you can require a test to fail more than once before the error event is triggered and the service state changed to failed. This is useful to avoid getting alerts on spurious errors, which can happen, especially with network tests.
Syntax:
FOR CYCLES ... or:
[TIMES WITHIN] CYCLES ...
Accordingly, Monit wont change the service's status until it fails within the next X cycles. In order to confirm this statement, just remove the fault tolerance for this service and use only:
if does not exist then alert
stop manually the service and confirm that the command
monit status
shows now the status "Does not exist" as soon as you stop it.
So, back to your questions:
SMTP SERVER CONFIGURATION
set mailserver smtp.gmail.com PORT 587 USERNAME "[email protected]" PASSWORD "xxxxx" using TLSV1 with timeout 30 seconds
(Be aware that in gmail you must activate the access for "unsecure" apps in order to allow monit to use the stmp service)
and
EMAIL RECIPIENT
set alert [email protected]
both in the file /etc/monit/monitrc. Refer to the official documentation for more information about these two lines.
References:
Monit's documentation: https://mmonit.com/monit/documentation/monit.html
Hope it helps!
Regards
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With