diff options
| author | 2026-10-22 20:23:48 +0000 | |
|---|---|---|
| committer | 2026-10-22 20:23:48 +0000 | |
| commit | bdbdec2d56a9db989328394b3c828f5f0239b8b0 (patch) | |
| tree | 275a77b5d67bb943abaa1ef2c71596da78cd1ee6 /roles/monit/templates/monitrc.j2 | |
| parent | feat: add acme_client (diff) | |
| download | infra-bdbdec2d56a9db989328394b3c828f5f0239b8b0.tar.gz | |
feat: add monit
Diffstat (limited to 'roles/monit/templates/monitrc.j2')
| -rw-r--r-- | roles/monit/templates/monitrc.j2 | 194 |
1 files changed, 194 insertions, 0 deletions
diff --git a/roles/monit/templates/monitrc.j2 b/roles/monit/templates/monitrc.j2 new file mode 100644 index 0000000..4a521e3 --- /dev/null +++ b/roles/monit/templates/monitrc.j2 | |||
| @@ -0,0 +1,194 @@ | |||
| 1 | # -*- Conf -*- | ||
| 2 | # {{ ansible_managed }} | ||
| 3 | |||
| 4 | set daemon 30 with start delay 10 | ||
| 5 | set log syslog | ||
| 6 | |||
| 7 | set pidfile /var/run/monit.pid | ||
| 8 | set idfile /var/lib/monit/id | ||
| 9 | set statefile /var/lib/monit/state | ||
| 10 | |||
| 11 | set limits { | ||
| 12 | programOutput: 4 KB | ||
| 13 | # programOutput: 512 B, # check program's output truncate limit | ||
| 14 | # sendExpectBuffer: 256 B, # limit for send/expect protocol test | ||
| 15 | # fileContentBuffer: 512 B, # limit for file content test | ||
| 16 | # httpContentBuffer: 1 MB, # limit for HTTP content test | ||
| 17 | # networkTimeout: 5 seconds # timeout for network I/O | ||
| 18 | # programTimeout: 300 seconds # timeout for check program | ||
| 19 | # stopTimeout: 30 seconds # timeout for service stop | ||
| 20 | # startTimeout: 30 seconds # timeout for service start | ||
| 21 | # restartTimeout: 30 seconds # timeout for service restart | ||
| 22 | # execTimeout: 0 seconds # timeout for test action exec | ||
| 23 | } | ||
| 24 | |||
| 25 | ## Set global SSL options (just most common options showed, see manual for | ||
| 26 | ## full list). | ||
| 27 | # | ||
| 28 | set ssl { | ||
| 29 | verify : enable, | ||
| 30 | selfsigned : reject | ||
| 31 | } | ||
| 32 | |||
| 33 | set mailserver localhost | ||
| 34 | |||
| 35 | ## By default Monit will drop alert events if no mail servers are available. | ||
| 36 | ## If you want to keep the alerts for later delivery retry, you can use the | ||
| 37 | ## EVENTQUEUE statement. The base directory where undelivered alerts will be | ||
| 38 | ## stored is specified by the BASEDIR option. You can limit the queue size | ||
| 39 | ## by using the SLOTS option (if omitted, the queue is limited by space | ||
| 40 | ## available in the back end filesystem). | ||
| 41 | # | ||
| 42 | # set eventqueue | ||
| 43 | # basedir /var/monit # set the base directory where events will be stored | ||
| 44 | # slots 100 # optionally limit the queue size | ||
| 45 | # | ||
| 46 | # | ||
| 47 | |||
| 48 | ## Monit by default uses the following format for alerts if the mail-format | ||
| 49 | ## statement is missing:: | ||
| 50 | ## --8<-- | ||
| 51 | ## set mail-format { | ||
| 52 | ## from: Monit <monit@$HOST> | ||
| 53 | ## subject: monit alert -- $EVENT $SERVICE | ||
| 54 | ## message: $EVENT Service $SERVICE | ||
| 55 | ## Date: $DATE | ||
| 56 | ## Action: $ACTION | ||
| 57 | ## Host: $HOST | ||
| 58 | ## Description: $DESCRIPTION | ||
| 59 | ## | ||
| 60 | ## Your faithful employee, | ||
| 61 | ## Monit | ||
| 62 | ## } | ||
| 63 | ## --8<-- | ||
| 64 | ## | ||
| 65 | ## You can override this message format or parts of it, such as subject | ||
| 66 | ## or sender using the MAIL-FORMAT statement. Macros such as $DATE, etc. | ||
| 67 | ## are expanded at runtime. For example, to override the sender, use: | ||
| 68 | # | ||
| 69 | # set mail-format { from: monit@foo.bar } | ||
| 70 | |||
| 71 | set alert martin@michalec.dev not on { instance, action } | ||
| 72 | |||
| 73 | set httpd unixsocket /var/run/monit/monit.sock | ||
| 74 | |||
| 75 | set onreboot nostart # start, nostart, laststart | ||
| 76 | |||
| 77 | check system $HOST | ||
| 78 | if loadavg (1min) per core > 2 for 5 cycles then alert | ||
| 79 | if loadavg (5min) per core > 1.5 for 10 cycles then alert | ||
| 80 | if cpu usage > 95% for 10 cycles then alert | ||
| 81 | if memory usage > 75% then alert | ||
| 82 | if swap usage > 25% then alert | ||
| 83 | |||
| 84 | ## Check if a file exists, checksum, permissions, uid and gid. In addition | ||
| 85 | ## to alert recipients in the global section, customized alert can be sent to | ||
| 86 | ## additional recipients by specifying a local alert handler. The service may | ||
| 87 | ## be grouped using the GROUP option. More than one group can be specified by | ||
| 88 | ## repeating the 'group name' statement. | ||
| 89 | # | ||
| 90 | # check file apache_bin with path /usr/local/apache/bin/httpd | ||
| 91 | # if failed checksum and | ||
| 92 | # expect the sum 8f7f419955cefa0b33a2ba316cba3659 then unmonitor | ||
| 93 | # if failed permission 755 then unmonitor | ||
| 94 | # if failed uid "root" then unmonitor | ||
| 95 | # if failed gid "root" then unmonitor | ||
| 96 | # alert security@foo.bar on { | ||
| 97 | # checksum, permission, uid, gid | ||
| 98 | # } with the mail-format { subject: Alarm! } | ||
| 99 | # group server | ||
| 100 | # | ||
| 101 | |||
| 102 | ## Check that a process is running, in this case Apache, and that it respond | ||
| 103 | ## to HTTP and HTTPS requests. Check its resource usage such as cpu and memory, | ||
| 104 | ## and number of children. If the process is not running, Monit will restart | ||
| 105 | ## it by default. In case the service is restarted very often and the | ||
| 106 | ## problem remains, it is possible to disable monitoring using the TIMEOUT | ||
| 107 | ## statement. This service depends on another service (apache_bin) which | ||
| 108 | ## is defined above. | ||
| 109 | # | ||
| 110 | # check process apache with pidfile /usr/local/apache/logs/httpd.pid | ||
| 111 | # start program = "/etc/init.d/httpd start" with timeout 60 seconds | ||
| 112 | # stop program = "/etc/init.d/httpd stop" | ||
| 113 | # if cpu > 60% for 2 cycles then alert | ||
| 114 | # if cpu > 80% for 5 cycles then restart | ||
| 115 | # if totalmem > 200.0 MB for 5 cycles then restart | ||
| 116 | # if children > 250 then restart | ||
| 117 | # if disk read > 500 kb/s for 10 cycles then alert | ||
| 118 | # if disk write > 500 kb/s for 10 cycles then alert | ||
| 119 | # if failed host www.tildeslash.com port 80 protocol http and request "/somefile.html" then restart | ||
| 120 | # if failed port 443 protocol https with timeout 15 seconds then restart | ||
| 121 | # if 3 restarts within 5 cycles then unmonitor | ||
| 122 | # depends on apache_bin | ||
| 123 | # group server | ||
| 124 | # | ||
| 125 | |||
| 126 | ## Check filesystem permissions, uid, gid, space usage, inode usage and disk I/O. | ||
| 127 | ## Other services, such as databases, may depend on this resource and an automatically | ||
| 128 | ## graceful stop may be cascaded to them before the filesystem will become full and data | ||
| 129 | ## lost. | ||
| 130 | # | ||
| 131 | # check filesystem datafs with path /dev/sdb1 | ||
| 132 | # start program = "/bin/mount /data" | ||
| 133 | # stop program = "/bin/umount /data" | ||
| 134 | # if failed permission 660 then unmonitor | ||
| 135 | # if failed uid "root" then unmonitor | ||
| 136 | # if failed gid "disk" then unmonitor | ||
| 137 | # if space usage > 80% for 5 times within 15 cycles then alert | ||
| 138 | # if space usage > 99% then stop | ||
| 139 | # if inode usage > 30000 then alert | ||
| 140 | # if inode usage > 99% then stop | ||
| 141 | # if read rate > 1 MB/s for 5 cycles then alert | ||
| 142 | # if read rate > 500 operations/s for 5 cycles then alert | ||
| 143 | # if write rate > 1 MB/s for 5 cycles then alert | ||
| 144 | # if write rate > 500 operations/s for 5 cycles then alert | ||
| 145 | # if service time > 10 milliseconds for 3 times within 5 cycles then alert | ||
| 146 | # group server | ||
| 147 | # | ||
| 148 | |||
| 149 | ## Check a file's timestamp. In this example, we test if a file is older | ||
| 150 | ## than 15 minutes and assume something is wrong if its not updated. Also, | ||
| 151 | ## if the file size exceed a given limit, execute a script | ||
| 152 | # | ||
| 153 | # check file database with path /data/mydatabase.db | ||
| 154 | # if failed permission 700 then alert | ||
| 155 | # if failed uid "data" then alert | ||
| 156 | # if failed gid "data" then alert | ||
| 157 | # if timestamp > 15 minutes then alert | ||
| 158 | # if size > 100 MB then exec "/my/cleanup/script" as uid dba and gid dba | ||
| 159 | # | ||
| 160 | |||
| 161 | ## Check directory permission, uid and gid. An event is triggered if the | ||
| 162 | ## directory does not belong to the user with uid 0 and gid 0. In addition, | ||
| 163 | ## the permissions have to match the octal description of 755 (see chmod(1)). | ||
| 164 | # | ||
| 165 | # check directory bin with path /bin | ||
| 166 | # if failed permission 755 then unmonitor | ||
| 167 | # if failed uid 0 then unmonitor | ||
| 168 | # if failed gid 0 then unmonitor | ||
| 169 | # | ||
| 170 | |||
| 171 | ## Check a remote host availability by issuing a ping test and check the | ||
| 172 | ## content of a response from a web server. Up to three pings are sent and | ||
| 173 | ## connection to a port and an application level network check is performed. | ||
| 174 | # | ||
| 175 | # check host myserver with address 192.168.1.1 | ||
| 176 | # if failed ping then alert | ||
| 177 | # if failed port 3306 protocol mysql with timeout 15 seconds then alert | ||
| 178 | # if failed port 80 protocol http | ||
| 179 | # and request /some/path with content = "a string" | ||
| 180 | # then alert | ||
| 181 | # | ||
| 182 | |||
| 183 | ## Check a network link status (up/down), link capacity changes, saturation | ||
| 184 | ## and bandwidth usage. | ||
| 185 | # | ||
| 186 | # check network public with interface eth0 | ||
| 187 | # if link down then alert | ||
| 188 | # if changed link then alert | ||
| 189 | # if saturation > 90% then alert | ||
| 190 | # if download > 10 MB/s then alert | ||
| 191 | # if total uploaded > 1 GB in last hour then alert | ||
| 192 | # | ||
| 193 | |||
| 194 | include /etc/monit.d/* | ||
