More Ansible goodness this week. We’ve been working on a basic playbook to set up the innovatively-named Monit monitoring tool to keep an eye on our webservers and give them a kick up the backside if they’re misbehaving.
It’s based on a very useful Ansible Galaxy role, pgolm’s Monit, which installs and configures the tool. However, the role’s documentation doesn’t necessarily make it obvious how to get the best from Monit, so here’s an example playbook for monitoring PHP, MySQL and Nginx to get things started:
- hosts: hosts
roles:
- pgolm.monit
vars:
monit_cycle: 60
monit_webinterface_enabled: true
monit_webinterface_port: 2812
monit_services:
- name: php5-fpm
type: process
target: /var/run/php5-fpm.pid
start: "/etc/init.d/php5-fpm start"
stop: "/etc/init.d/php5-fpm stop"
- name: nginx
type: process
target: /var/run/nginx.pid
start: /etc/init.d/nginx start
stop: /etc/init.d/nginx stop
rules:
- "if totalcpu > 80% for 3 cycles then alert"
- "if totalcpu > 80% for 6 cycles then restart"
- name: mysql
type: process
target: /var/run/mysqld/mysqld.pid
start: "/etc/init.d/mysql start"
stop: "/etc/init.d/mysql stop"
rules:
- "if totalcpu > 80% for 3 cycles then alert"
- "if totalcpu > 80% for 6 cycles then restart"
A quick explanation of the variables:
Rules can be written with the assistance of any of the following noise keywords (not actually read by the machine, but they really help to structure your code): if
, and
, with(in)
, has
, us(ing|e)
, on(ly)
, then
, for
, of
.
Hopefully that should be enough to get started using pgolm’s Ansible Monit role. We have, however, only scratched the surface of what can be achieved using Monit. Other interesting areas include monitoring checksums of important files to detect changes, and testing the outputs of scripts. Ansible makes it beautifully simple to do all of this stuff.
Connect on LinkedIn. Follow me on Twitter. Grab the RSS Feed