在Nagios社区中上找了下相关用于监控elasticsearch索引的脚本,再经过修改下,可以在平时用于传入elasticsearch的监听ip用于在Nagios中使用监控elasticsearch健康状态的脚本。
#!/bin/bash
#check_elasticsearch_health.sh
#Memo for Nagios outputs
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
#Position parameter judgment
if [ $# -lt 1 ];then
echo "Please enter host address"
echo "ex> $0 HOST_ADDRESS"
exit $STATE_UNKNOWN
fi
if [ $# -gt 1 ]; then
echo "The input host address are too much"
echo "ex> $0 localhost"
exit $STATE_UNKNOWN
fi
type curl >/dev/null 2>&1 || { echo >&2 "This plugin require curl but it's not installed."; exit $STATE_UNKNOWN; }
#检查是否有安装curl
HOST=$1
STATUS=$(curl -s $HOST:9200/_cluster/health?pretty | grep status | awk '{print $3}' | cut -d\" -f2)
#单节点的健康检测,平时可以根据需要监控内容来修改api获取相应的值
if [[ $STATUS && "$STATUS" != "green" ]]; then
echo "CRITICAL - Status is $STATUS"
exit $STATE_CRITICAL
fi
if [[ "$STATUS" == "green" ]]; then
echo "OK - Status is $STATUS"
exit $STATE_OK
fi
echo "UNKNOW - No data were returned by elastisearch on host $HOST"
exit $STATE_UNKNOWN