aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--roles/monit/handlers/main.yml5
-rw-r--r--roles/monit/tasks/main.yml25
-rw-r--r--roles/monit/templates/monitrc.j2194
-rw-r--r--roles/personal/templates/nginx.conf.j211
4 files changed, 235 insertions, 0 deletions
diff --git a/roles/monit/handlers/main.yml b/roles/monit/handlers/main.yml
new file mode 100644
index 0000000..7f35f22
--- /dev/null
+++ b/roles/monit/handlers/main.yml
@@ -0,0 +1,5 @@
1---
2- name: Restart Monit
3 ansible.builtin.service:
4 name: monit
5 state: restarted
diff --git a/roles/monit/tasks/main.yml b/roles/monit/tasks/main.yml
new file mode 100644
index 0000000..e844bde
--- /dev/null
+++ b/roles/monit/tasks/main.yml
@@ -0,0 +1,25 @@
1---
2- block:
3 - name: Install Monit package
4 ansible.builtin.package:
5 name:
6 - monit
7 - monit-openrc
8 state: present
9
10 - name: Deploy Monit configuration file
11 ansible.builtin.template:
12 src: monitrc.j2
13 dest: /etc/monitrc
14 owner: root
15 group: root
16 mode: '0444'
17 notify: Restart Monit
18
19 - name: Ensure Monit service is enabled and started
20 ansible.builtin.service:
21 name: monit
22 state: started
23
24 tags:
25 - monit
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
4set daemon 30 with start delay 10
5set log syslog
6
7set pidfile /var/run/monit.pid
8set idfile /var/lib/monit/id
9set statefile /var/lib/monit/state
10
11set 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#
28set ssl {
29 verify : enable,
30 selfsigned : reject
31}
32
33set 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
71set alert martin@michalec.dev not on { instance, action }
72
73set httpd unixsocket /var/run/monit/monit.sock
74
75set onreboot nostart # start, nostart, laststart
76
77check 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
194include /etc/monit.d/*
diff --git a/roles/personal/templates/nginx.conf.j2 b/roles/personal/templates/nginx.conf.j2
index fdb869b..6b2d05a 100644
--- a/roles/personal/templates/nginx.conf.j2
+++ b/roles/personal/templates/nginx.conf.j2
@@ -49,6 +49,17 @@ server {
49 location = /private/status { 49 location = /private/status {
50 stub_status; 50 stub_status;
51 } 51 }
52
53 location = /private/monit {
54 return 301 $scheme://$http_host/private/monit/;
55 }
56 location /private/monit/ {
57 proxy_pass http://127.0.0.1:2812/;
58 proxy_set_header Host $http_host;
59 proxy_set_header X-Real-IP $remote_addr;
60 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
61 proxy_set_header X-Forwarded-Proto $scheme;
62 }
52 } 63 }
53 64
54 location /files/ { 65 location /files/ {