monit.rst
changeset 2410 3a97cc7e1b39
child 2411 417b9ee21460
equal deleted inserted replaced
2409:6163ed9ff6de 2410:3a97cc7e1b39
       
     1 
       
     2 =======
       
     3  monit
       
     4 =======
       
     5 .. contents::
       
     6    :local:
       
     7 
       
     8 Official docs
       
     9 =============
       
    10 
       
    11 https://mmonit.com/monit/documentation/monit.html
       
    12   Main docs.
       
    13 https://mmonit.com/wiki/Monit/ConfigurationExamples
       
    14   Real-world configuration examples.
       
    15 https://mmonit.com/wiki/Monit/FAQ
       
    16   FAQ
       
    17 
       
    18 Debugging monit
       
    19 ===============
       
    20 
       
    21 Run standalone::
       
    22 
       
    23   monit -c /path/to/monitrc
       
    24 
       
    25 Check syntax::
       
    26 
       
    27   monit -t
       
    28   monit -t -c /path/to/monitrc
       
    29 
       
    30 Override log file location in config::
       
    31 
       
    32   set log /var/log/monit.log
       
    33 
       
    34 or with CLI option::
       
    35 
       
    36   monit -l /var/log/monit.log
       
    37 
       
    38 Do not go into background::
       
    39 
       
    40   monit -I
       
    41 
       
    42 Make verbose output::
       
    43 
       
    44   monit -v
       
    45   monit -vv
       
    46 
       
    47 Full length commant may look like::
       
    48 
       
    49   sudo monit -v status
       
    50   sudo monit -Iv validate
       
    51   sudo monit -Ivv -c /path/to/monitrc
       
    52 
       
    53 To debug start/stop scripts write wrapper that redirects STDIO to file::
       
    54 
       
    55   #/bin/sh
       
    56   exec 1>my.log
       
    57   exec 2>my.log
       
    58   echo "$@"
       
    59   exec "$@"
       
    60 Limiting server to ``localhost``::
       
    61 
       
    62   set httpd port 2812
       
    63     use address localhost
       
    64     allow localhost
       
    65     allow admin:monit
       
    66 
       
    67 Monit modes
       
    68 ===========
       
    69 
       
    70 * In ``active`` mode (the default), Monit will pro-actively monitor a service and in case of
       
    71 problems raise alerts and/or restart the service.
       
    72 * In ``passive`` mode, Monit will passively monitor a service and will raise alerts, but will not
       
    73 try to fix a problem by executing start, stop or restart.
       
    74 * In ``manual`` mode, Monit will enter active mode only if a service was started via Monit.
       
    75 
       
    76 Example::
       
    77 
       
    78   check process App with pidfile /var/run/app.pid
       
    79     start program = "/usr/bin/app start"
       
    80     stop program = "/usr/bin/app stop"
       
    81     mode passive
       
    82 
       
    83 Alerting
       
    84 ========
       
    85 ::
       
    86 
       
    87   check process memcached with match memcached
       
    88     start program = "/usr/bin/systemctl start memcached"
       
    89     stop program = "/usr/bin/systemctl stop memcached"
       
    90     if failed host 127.0.0.1 port 11211 protocol MEMCACHE then restart
       
    91     if cpu > 70% for 2 cycles then alert
       
    92     if cpu > 98% for 5 cycles then restart
       
    93     if 2 restarts within 3 cycles then timeout
       
    94 
       
    95   check process myapp with pidfile /run/myapp.pid
       
    96     if does not exist then alert
       
    97 
       
    98   check filesystem Ubuntu with path /dev/sda1
       
    99     if space usage > 90% then alert
       
   100   check filesystem Home with path /dev/sda3
       
   101     if space usage > 90% then alert
       
   102 
       
   103   check host app_name with address 127.0.0.1
       
   104     start "/sbin/start app_name"
       
   105     stop "/sbin/stop app_name"
       
   106     if failed port 80 protocol HTTP
       
   107       request /ok
       
   108       with timeout 5 seconds
       
   109       then restart
       
   110 
       
   111 Running monit by systemd
       
   112 ========================
       
   113 ::
       
   114 
       
   115   [Unit]
       
   116   Description=Pro-active monitoring utility for unix systems
       
   117   After=network.target
       
   118   Documentation=man:monit(1) https://mmonit.com/wiki/Monit/HowTo
       
   119 
       
   120   [Service]
       
   121   Type=simple
       
   122   KillMode=process
       
   123   ExecStart=/usr/local/bin/monit -I
       
   124   ExecStop=/usr/local/bin/monit quit
       
   125   ExecReload=/usr/local/bin/monit reload
       
   126   Restart = on-abnormal
       
   127   StandardOutput=null
       
   128 
       
   129   [Install]
       
   130   WantedBy=multi-user.target
       
   131 
       
   132 https://mmonit.com/wiki/Monit/Systemd
       
   133   Official docs.
       
   134 
       
   135 Monitor systemd process
       
   136 =======================
       
   137 
       
   138 With pid-file::
       
   139 
       
   140   check process nginx with pidfile /var/run/nginx.pid
       
   141     start program = "/bin/systemctl start nginx"
       
   142     stop program = "/bin/systemctl stop nginx"
       
   143 
       
   144 If process without pid-file::
       
   145 
       
   146   check program MyApp with path "systemctl --quiet is-active MyApp"
       
   147     if status != 0 then ...
       
   148 
       
   149 There is a way to tell systemd to create pid-file if process doesn't to it itself::
       
   150 
       
   151   ExecStartPost=/bin/sh -c "echo $MAINPID > /run/myapp.pid"
       
   152 
       
   153 By matching program name::
       
   154 
       
   155   check process MyApp matching 'myapp.*'
       
   156      start program = /bin/app
       
   157      stop program = something..
       
   158 
       
   159 Can systemd replace monit?
       
   160 ==========================
       
   161 
       
   162 No. Systemd has only basic check if process is running. Imaging that process is stuck. You need some
       
   163 active probes, like HTTP health endpoint.
       
   164 
       
   165 Systemd has limited capabilities for notifying with ``OnFailure``::
       
   166 
       
   167   [Service]
       
   168   Restart=always
       
   169   RestartSecs=30
       
   170 
       
   171   [Unit]
       
   172   OnFailure=...
       
   173 
       
   174 Reverse proxy for monit
       
   175 =======================
       
   176 
       
   177 Ngnix::
       
   178 
       
   179   server {
       
   180     listen   80;
       
   181     server_name  my.server.name;
       
   182     location /monit/ {
       
   183       allow 127.0.0.1;
       
   184       allow 192.0.0.0/8;
       
   185       deny all;
       
   186 
       
   187       proxy_pass http://127.0.0.1:2812;
       
   188       proxy_set_header Host $host;
       
   189       rewrite ^/monit/(.*) /$1 break;
       
   190       proxy_ignore_client_abort on;
       
   191     }
       
   192   }
       
   193