diff --git a/keepalived/defaults/main.yml b/keepalived/defaults/main.yml index 3f692919..8a4f8aa6 100644 --- a/keepalived/defaults/main.yml +++ b/keepalived/defaults/main.yml @@ -15,6 +15,9 @@ keepalived_floating_ip1: 127.0.0.1 keepalived_script1_name: chk_haproxy keepalived_inst_priority: 100 +keepalived_nagios_check: False +keepalived_notify_script: /usr/local/bin/keepalived_notify + keepalived_scripts: - name: '{{ keepalived_script1_name }}' script: 'killall -0 haproxy' @@ -28,6 +31,7 @@ keepalived_instances: interface: eth0 state: MASTER vrouter_id: 51 + notify: '{{ keepalived_notify_script }}' priority: '{{ keepalived_inst_priority }}' v_addr: - '{{ keepalived_floating_ip1 }}' diff --git a/keepalived/files/check_keepalived_state b/keepalived/files/check_keepalived_state new file mode 100644 index 00000000..23d3859a --- /dev/null +++ b/keepalived/files/check_keepalived_state @@ -0,0 +1,57 @@ +#!/bin/bash +# +# echo $1 $2 is in $3 state > /var/run/keepalive.state +# + +############################################################### +# Check Keepalived State # +# # +# Author: Zhivko Todorov # +# Date: 01-Dec-2015 # +# Version: 0.0.1 # +# License: GPL # +############################################################### + + +# set to 'true' if the host is supposed to be in MASTER state +# or set to 'false' if the host is supposed to be in BACKUP state +# nrpe cannot receive external variables UNLESS is forced in config +MASTER='true' + +# checking if there are alive keepalived processes so we can trust the content of the notify 'state' file +KEEPALIVENUM=`ps uax|grep '/usr/sbin/keepalived --dont-fork'|grep -v grep|wc -l|tr -d "\n"` + +if [ $KEEPALIVENUM -gt 0 ]; then + + KEEPALIVESTATE=`cat /var/run/keepalive.state` + + if [ "$MASTER" == "true" ]; then + + if [[ $KEEPALIVESTATE == *"MASTER"* ]];then + echo $KEEPALIVESTATE + exit 0 + fi + + if [[ $KEEPALIVESTATE == *"BACKUP"* ]];then + echo $KEEPALIVESTATE + exit 0 + fi + + else + + if [[ $KEEPALIVESTATE == *"BACKUP"* ]];then + echo $KEEPALIVESTATE + exit 0 + fi + + if [[ $KEEPALIVESTATE == *"MASTER"* ]];then + echo $KEEPALIVESTATE + exit 2 + fi + + fi +fi + +echo "Keepalived is in UNKNOWN state" +exit 3 + diff --git a/keepalived/tasks/main.yml b/keepalived/tasks/main.yml index ed63310e..ca0d8a79 100644 --- a/keepalived/tasks/main.yml +++ b/keepalived/tasks/main.yml @@ -12,6 +12,20 @@ template: src=keepalived.conf.j2 dest=/etc/keepalived/keepalived.conf notify: restart keepalived + - name: Install the keepalived notify scripts + template: src=keepalived_notify.sh.j2 dest={{ item.notify }} owner=root group=root mode=0754 + with_items: '{{ keepalived_instances }}' + + - name: Install the keepalived NRPE nagios check + copy: src=check_keepalived_state dest={{ nagios_plugins_dir }}/check_keepalived_state owner=root group=root mode=0555 + with_items: '{{ keepalived_instances }}' + when: keepalived_nagios_check + + - name: Install the keepalived NRPE command configuration + template: src=keepalived-nrpe.cfg.j2 dest={{ nrpe_include_dir }}/keepalived-nrpe.cfg owner=root group=root mode=0444 + notify: Reload NRPE server + when: keepalived_nagios_check + - name: Ensure that keepalived is started and enabled service: name=keepalived state=started enabled=yes when: keepalived_enabled @@ -30,6 +44,18 @@ apt: name={{ item }} state=absent with_items: '{{ keepalived_pkgs }}' + - name: Remove the keepalived notify scripts + file: dest={{ item.notify }} state=absent + with_items: '{{ keepalived_instances }}' + + - name: Remove the keepalived NRPE check + file: dest={{ nagios_plugins_dir }}/check_keepalived_state state=absent + with_items: '{{ keepalived_instances }}' + + - name: Remove the keepalived NRPE command configuration + file: dest={{ nrpe_include_dir }}/keepalived-nrpe.cfg state=absent + notify: Reload NRPE server + tags: keepalived when: not keepalived_install diff --git a/keepalived/templates/keepalived-nrpe.cfg.j2 b/keepalived/templates/keepalived-nrpe.cfg.j2 new file mode 100644 index 00000000..fdf2cdba --- /dev/null +++ b/keepalived/templates/keepalived-nrpe.cfg.j2 @@ -0,0 +1,6 @@ +# +command[check_keepalived]={{ nagios_plugins_dir }}/check_keepalived_state + +# Restart keepalived (via handler) +command[global_restart_keepalived]=/usr/bin/sudo /etc/init.d/keepalived restart + diff --git a/keepalived/templates/keepalived_notify.sh.j2 b/keepalived/templates/keepalived_notify.sh.j2 new file mode 100644 index 00000000..6370a3e1 --- /dev/null +++ b/keepalived/templates/keepalived_notify.sh.j2 @@ -0,0 +1,20 @@ +#!/bin/bash +# +# echo $1 $2 is in $3 state > /var/run/keepalive.state +# +export PATH="/sbin:/usr/sbin:/bin:/usr/bin:$PATH" +TYPE=$1 +NAME=$2 +STATE=$3 + +{% if keepalived_notify_extra_params is defined %} +{{ keepalived_notify_extra_params }} +{% endif %} + + +{% if keepalived_nagios_check %} +echo $TYPE $NAME is in $STATE state > /var/run/keepalive.state +{% endif %} + +exit 0 +