library/roles/keepalived: Manage the notify script. Add a nagios check that runs via NRPE.

This commit is contained in:
Andrea Dell'Amico 2016-08-08 18:18:41 +02:00
parent ca2bbc03cd
commit c23202cd4c
5 changed files with 113 additions and 0 deletions

View File

@ -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 }}'

View File

@ -0,0 +1,57 @@
#!/bin/bash
#
# echo $1 $2 is in $3 state > /var/run/keepalive.state
#
###############################################################
# Check Keepalived State #
# #
# Author: Zhivko Todorov <ztodorov@neterra.net> #
# 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

View File

@ -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

View File

@ -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

View File

@ -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