elastic: install elasticsearch_shards Nagios check

check_elasticsearch_shards: Fix undefined value when output exceed one line
This commit is contained in:
David Prevot 2024-01-10 14:29:05 +01:00
parent 72727a8332
commit 50d33da3c5
4 changed files with 69 additions and 4 deletions

View file

@ -64,8 +64,19 @@ if [ ! -e "/usr/bin/jq" ]; then
exit 3
fi
nb_shards=$(curl -s -k -u ${CREDENTIALS} https://127.0.0.1:9200/_cat/allocation/${NODE_ID}?h=shards)
nb_shards_max=$(curl -s -k -u ${CREDENTIALS} https://127.0.0.1:9200/_cluster/settings?flat_settings | jq -r ".persistent[]" )
if [ -z ${CREDENTIALS} ]; then
nb_shards=$(curl -s 127.0.0.1:9200/_cat/allocation/${NODE_ID}?h=shards | head -n1)
nb_shards_max=$(curl -s 127.0.0.1:9200/_cluster/settings?flat_settings | jq -r ".persistent[]" )
else
nb_shards=$(curl -s -k -u ${CREDENTIALS} https://127.0.0.1:9200/_cat/allocation/${NODE_ID}?h=shards | head -n1)
nb_shards_max=$(curl -s -k -u ${CREDENTIALS} https://127.0.0.1:9200/_cluster/settings?flat_settings | jq -r ".persistent[]" )
fi
re='^[0-9]+$'
if ! [[ $nb_shards_max =~ $re ]] ; then
printf "UNKNWON - max_shards_per_node (NaN)"
exit 3
fi
nb_shards_avail=$(( $nb_shards_max - $nb_shards ))
@ -79,7 +90,7 @@ fi
nb_shards_crit=$(( $nb_shards_warn / 4 ))
# debug
# debug
# printf '%d out of %d, avail: %d, warn: %d, crit: %d' $nb_shards $nb_shards_max $nb_shards_avail $nb_shards_warn $nb_shards_crit
if [ "${nb_shards_avail}" -le "${nb_shards_crit}" ]; then

View file

@ -1,7 +1,11 @@
---
- name: restart elasticsearch
ansible.builtin.systemd:
daemon_reload: yes
name: elasticsearch
state: restarted
- name: restart nagios-nrpe-server
ansible.builtin.service:
name: nagios-nrpe-server
state: restarted

View file

@ -19,3 +19,5 @@
- ansible.builtin.include: curator.yml
when: elasticsearch_curator | bool
- ansible.builtin.include: nrpe.yml

View file

@ -0,0 +1,48 @@
---
- name: is NRPE present ?
ansible.builtin.stat:
path: /etc/nagios/nrpe.d/evolix.cfg
check_mode: no
register: nrpe_evolix_config
tags:
- elasticsearch
- nrpe
- block:
- name: Install dependencies
ansible.builtin.apt:
name:
- jq
- ansible.builtin.include_role:
name: evolix/remount-usr
- name: Copy Nagios check for Elasticsearch shards
ansible.builtin.copy:
src: check_elasticsearch_shards
dest: /usr/local/lib/nagios/plugins/
mode: "0755"
# - name: Create a password for NRPE
# ansible.builtin.command:
# cmd: "apg -n 1 -m 16 -M lcN"
# register: elasticsearch_nrpe_password
# check_mode: no
# changed_when: False
# - name: Create nrpe user
# todo:
- name: Add NRPE check
ansible.builtin.lineinfile:
name: /etc/nagios/nrpe.d/evolix.cfg
regexp: '^command\[check_elasticsearch_shards\]='
line: 'command[check_elasticsearch_shards]=/usr/local/lib/nagios/plugins/check_elasticsearch_shards --crendentials TODO(user:password) --nodeid TODO(nodeid)'
notify: restart nagios-nrpe-server
when:
- nrpe_evolix_config.stat.exists
tags:
- elasticsearch
- nrpe