├── LICENSE ├── README.md ├── check_elasticsearch └── check_elasticsearch.conf /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2017 Lyon Bros. Enterprises, LLC 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Nagios check script for elasticsearch 2 | ===================================== 3 | 4 | This is a simple script to check the status of an elasticsearch cluster. 5 | It now supports elasticsearch 6.3.2 with or without x-pack authentication. 6 | 7 | Usage: 8 | 9 | ./check_elasticsearch -H es1.mysite.com -P 9200 -o /tmp [ -a ] [ -u -p ] 10 | 11 | This will output the standard Nagios format: 12 | 13 | OK - elasticsearch (elasticsearch) is running. status: green; timed_out: false; number_of_nodes: 1; number_of_data_nodes: 1; active_primary_shards: 2; active_shards: 2; relocating_shards: 0; initializing_shards: 0; unassigned_shards: 0 | 'active_primary'=2 'active'=2 'relocating'=0 'init'=0 14 | 15 | `OK` / `WARNING` / `CRITICAL` correspond to the status being "green", "yellow", or "red" respectively. 16 | 17 | If you have a Graphite server you can send data to it via: 18 | 19 | ./check_elasticsearch -H es1.mysite.com -P 9200 -c graphite.foo.com -C 2003 20 | 21 | This will output data to the following metric prefix: 22 | 23 | system.$cluster_name.cluster.app.elasticsearch.cluster 24 | 25 | Removed reference to Chef cookbook since this fork won't support it. 26 | 27 | Elastic authentication via certificate and private kay 28 | Usage: 29 | ./check_elasticsearch -H elasticsearch.mydomain -s --certificate /mypath/certs/admin.crt.pem --private-key /mypath/certs/private/admin.key.pem 30 | 31 | 32 | Script is a modified version of check\_phpfpm by [MAB](https://github.com/mabitt/mab-nagios-plugins). 33 | 34 | Enjoy. 35 | 36 | # License 37 | 38 | MIT. 39 | -------------------------------------------------------------------------------- /check_elasticsearch: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # This program is free software; you can redistribute it and/or modify 4 | # it under the terms of the GNU General Public License as published by 5 | # the Free Software Foundation; either version 2 of the License, or 6 | # (at your option) any later version. 7 | # 8 | # This program is distributed in the hope that it will be useful, 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | # GNU General Public License for more details. 12 | # 13 | # You should have received a copy of the GNU General Public License 14 | # along with this program; if not, write to the Free Software 15 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 16 | 17 | PROGNAME=`basename $0` 18 | VERSION="Version 0.4.0" 19 | AUTHOR="Andrew Lyon, Based on Mike Adolphs (http://www.matejunkie.com/) check_nginx.sh code. Authentication support added by Ryan Gallant, CA support added by Michael Koch." 20 | 21 | ST_OK=0 22 | ST_WR=1 23 | ST_CR=2 24 | ST_UK=3 25 | epoch=`date +%s` 26 | hostname="localhost" 27 | user=elastic 28 | pass=changeme 29 | authentication="False" 30 | use_ca="False" 31 | use_cert="False" 32 | use_key="False" 33 | use_no_check="False" 34 | port=9200 35 | status_page="_cluster/health" 36 | output_dir=/tmp 37 | scheme=http 38 | carbon_server="" 39 | carbon_port="" 40 | carbon_key="system.:::name:::.cluster.app.elasticsearch.cluster" 41 | use_proxy="on" 42 | use_jq="False" 43 | verbose="False" 44 | wget_add_args=() 45 | jq="jq -r" 46 | 47 | 48 | print_version() { 49 | echo "$VERSION $AUTHOR" 50 | } 51 | 52 | print_help() { 53 | print_version $PROGNAME $VERSION 54 | echo "" 55 | echo "$PROGNAME is a Nagios plugin to check the cluster status of elasticsearch." 56 | echo "It also parses the status page to get a few useful variables out, and return" 57 | echo "them in the output." 58 | echo "" 59 | echo "$PROGNAME -H localhost -P 9200 -o /tmp" 60 | echo "" 61 | echo "Options:" 62 | echo " -h/--help)" 63 | echo " Print help." 64 | echo " -H/--hostname)" 65 | echo " Defines the hostname. Default is: localhost" 66 | echo " -P/--port)" 67 | echo " Defines the port. Default is: 9200" 68 | echo " -s/--secure)" 69 | echo " Use TLS. Defaults to false." 70 | echo " -o/--output-directory)" 71 | echo " Specifies where to write the tmp-file that the check creates." 72 | echo " Default is: /tmp" 73 | echo " -u/--username)" 74 | echo " Username for elasticsearch. Turns on authentication mode when set" 75 | echo " -p/--password)" 76 | echo " Password for elasticsearch. Turns on authentication mode when set" 77 | echo " -a/--auth)" 78 | echo " Turns on authentication mode with default credentials." 79 | echo " -c/--ca-certificate)" 80 | echo " Uses the provided CA certificate." 81 | echo " -C/--certificate)" 82 | echo " Uses the provided certificate." 83 | echo " -K/--private-key)" 84 | echo " Uses the provided private key." 85 | echo " -N/--no-check-certificate)" 86 | echo " Don't check the server certificate against the available certificate authorities." 87 | echo " -k/--ok-on-yellow)" 88 | echo " Exit with OK state if the cluster is yellow." 89 | echo " -x/--proxy)" 90 | echo " System proxy off or on. Default is: on" 91 | echo " --carbon-server)" 92 | echo " Defines the carbon server. Default is Null" 93 | echo " --carbon-port)" 94 | echo " Defines the carbon port. Default is Null" 95 | echo " --carbon-key)" 96 | echo " Defines the carbon key. Default is system.:::name:::.cluster.app.elasticsearch.cluster" 97 | echo " -V/--verbose)" 98 | echo " Enable verbose mode." 99 | exit $ST_UK 100 | } 101 | 102 | while test -n "$1"; do 103 | case "$1" in 104 | --help|-h) 105 | print_help 106 | exit $ST_UK 107 | ;; 108 | --version|-v) 109 | print_version $PROGNAME $VERSION 110 | exit $ST_UK 111 | ;; 112 | --hostname|-H) 113 | hostname=$2 114 | shift 115 | ;; 116 | --ok-on-yellow|-k) 117 | ok_on_yellow="True" 118 | ;; 119 | --secure|-s) 120 | scheme=https 121 | ;; 122 | --port|-P) 123 | port=$2 124 | shift 125 | ;; 126 | --proxy|-x) 127 | use_proxy=$2 128 | shift 129 | ;; 130 | --password|-p) 131 | pass=$2 132 | authentication="True" 133 | shift 134 | ;; 135 | --username|-u) 136 | user=$2 137 | authentication="True" 138 | shift 139 | ;; 140 | --auth|-a) 141 | authentication="True" 142 | ;; 143 | --ca-certificate|-c) 144 | ca_cert=$2 145 | use_ca="True" 146 | shift 147 | ;; 148 | --certificate|-C) 149 | cert=$2 150 | use_cert="True" 151 | shift 152 | ;; 153 | --private-key|-K) 154 | private_key=$2 155 | use_key="True" 156 | shift 157 | ;; 158 | --no-check-certificate|-N) 159 | use_no_check="True" 160 | ;; 161 | --output-directory|-o) 162 | output_dir=$2 163 | shift 164 | ;; 165 | --carbon-server) 166 | carbon_server=$2 167 | shift 168 | ;; 169 | --carbon-port) 170 | carbon_port=$2 171 | shift 172 | ;; 173 | --carbon-key) 174 | carbon_key=$2 175 | shift 176 | ;; 177 | --verbose|-V) 178 | verbose="True" 179 | ;; 180 | --use-jq) 181 | use_jq="True" 182 | ;; 183 | *) 184 | echo "Unknown argument: $1" 185 | print_help 186 | exit $ST_UK 187 | ;; 188 | esac 189 | shift 190 | done 191 | 192 | if [ "$authentication" = "True" ]; then 193 | pass="--password=${pass}" 194 | user="--user=${user}" 195 | wget_add_args+=("--auth-no-challenge") 196 | fi 197 | 198 | if [ "$use_ca" = "True" ]; then 199 | ca_cert="--ca-certificate=${ca_cert}" 200 | fi 201 | 202 | if [ "$use_cert" = "True" ]; then 203 | cert="--certificate=${cert}" 204 | fi 205 | 206 | if [ "$use_key" = "True" ]; then 207 | private_key="--private-key=${private_key}" 208 | fi 209 | 210 | if [ "$use_proxy" = "off" ]; then 211 | proxy="--proxy=${use_proxy}" 212 | fi 213 | 214 | if [ "$use_no_check" = "True" ]; then 215 | wget_add_args+=("--no-check-certificate") 216 | fi 217 | 218 | if [ "$verbose" != "True" ]; then 219 | wget_add_args+=("-q") 220 | fi 221 | 222 | get_status() { 223 | filename=$(mktemp -u -p "$output_dir" --suffix="-${PROGNAME}") 224 | 225 | # If authentication via private key ist defined 226 | if [ -n "${private_key}" ] 227 | then 228 | wget -t 3 -T 3 ${cert} ${private_key} $scheme://${hostname}:${port}/${status_page}?pretty=true -O ${filename} ${proxy} ${wget_add_args[@]} 229 | elif [ "${authentication}" = "True" ]; then 230 | wget -t 3 -T 3 ${ca_cert} ${user} ${pass} $scheme://${hostname}:${port}/${status_page}?pretty=true -O ${filename} ${proxy} ${wget_add_args[@]} 231 | else 232 | wget -t 3 -T 3 ${ca_cert} $scheme://${hostname}:${port}/${status_page}?pretty=true -O ${filename} ${proxy} ${wget_add_args[@]} 233 | fi 234 | } 235 | 236 | get_val() { 237 | filename=$1 238 | key=$2 239 | ty=$3 240 | 241 | if [[ $use_jq == "True" ]]; then 242 | ${jq} ".${key}" ${filename} 243 | else 244 | line=$(grep '"'"${key}"'"' ${filename}) 245 | if [ "${ty}" == "string" ]; then 246 | echo ${line} | awk -F '"' '{print $4}' 247 | else 248 | echo ${line} | awk '{print $3}' | sed 's|[\r",]||g' 249 | fi 250 | fi 251 | } 252 | 253 | get_vals() { 254 | name=$(get_val ${filename} "cluster_name" "string") 255 | status=$(get_val ${filename} "status" "string") 256 | timed_out=$(get_val ${filename} "timed_out") 257 | number_nodes=$(get_val ${filename} "number_of_nodes") 258 | number_data_nodes=$(get_val ${filename} "number_of_data_nodes") 259 | active_primary_shards=$(get_val ${filename} "active_primary_shards") 260 | active_shards=$(get_val ${filename} "active_shards") 261 | relocating_shards=$(get_val ${filename} "relocating_shards") 262 | initializing_shards=$(get_val ${filename} "initializing_shards") 263 | delayed_unassigned_shards=$(get_val ${filename} "delayed_unassigned_shards") 264 | unassigned_shards=$(get_val ${filename} "unassigned_shards") 265 | rm -f ${filename} 266 | 267 | # Determine the Nagios Status and Exit Code 268 | if [ "$status" = "red" ]; then 269 | NAGSTATUS="CRITICAL" 270 | EXST=$ST_CR 271 | elif [ "$status" = "yellow" ]; then 272 | if [ -z "$ok_on_yellow" ]; then 273 | NAGSTATUS="WARNING" 274 | EXST=$ST_WR 275 | else 276 | NAGSTATUS="OK" 277 | EXST=$ST_OK 278 | fi 279 | elif [ "$status" = "green" ]; then 280 | NAGSTATUS="OK" 281 | EXST=$ST_OK 282 | else 283 | NAGSTATUS="UNKNOWN" 284 | EXST=$ST_UK 285 | fi 286 | } 287 | 288 | do_output() { 289 | output="elasticsearch ($name) is running. \ 290 | status: $status; \ 291 | timed_out: $timed_out; \ 292 | number_of_nodes: $number_nodes; \ 293 | number_of_data_nodes: $number_data_nodes; \ 294 | active_primary_shards: $active_primary_shards; \ 295 | active_shards: $active_shards; \ 296 | relocating_shards: $relocating_shards; \ 297 | initializing_shards: $initializing_shards; \ 298 | delayed_unassigned_shards: $delayed_unassigned_shards; \ 299 | unassigned_shards: $unassigned_shards " 300 | } 301 | 302 | do_perfdata() { 303 | #perfdata="'idle'=$iproc 'active'=$aproc 'total'=$tproc" 304 | perfdata="'active_primary'=$active_primary_shards 'active'=$active_shards 'relocating'=$relocating_shards 'init'=$initializing_shards 'delay_unass'=$delayed_unassigned_shards 'unass'=$unassigned_shards" 305 | } 306 | 307 | do_graphite() { 308 | if [ "$carbon_server" != "" -a "$carbon_port" != "" ]; then 309 | key=$(echo $carbon_key | sed "s/:::name:::/$name/") 310 | echo "$key.cluster.status $EXST $epoch" | nc -w 2 $carbon_server $carbon_port 311 | echo "$key.cluster.nodes.ttl $number_nodes $epoch" | nc -w 2 $carbon_server $carbon_port 312 | echo "$key.cluster.nodes.data $number_data_nodes $epoch" | nc -w 2 $carbon_server $carbon_port 313 | echo "$key.cluster.shards.active $active_shards $epoch" | nc -w 2 $carbon_server $carbon_port 314 | echo "$key.cluster.shards.active_primary $active_primary_shards $epoch" | nc -w 2 $carbon_server $carbon_port 315 | echo "$key.cluster.shards.initializing $initializing_shards $epoch" | nc -w 2 $carbon_server $carbon_port 316 | echo "$key.cluster.shards.relocating $relocating_shards $epoch" | nc -w 2 $carbon_server $carbon_port 317 | echo "$key.cluster.shards.delayed_unassigned $delayed_unassigned_shards $epoch" | nc -w 2 $carbon_server $carbon_port 318 | echo "$key.cluster.shards.unassigned $unassigned_shards $epoch" | nc -w 2 $carbon_server $carbon_port 319 | unset key 320 | fi 321 | } 322 | 323 | # Here we go! 324 | which wget >/dev/null 2>&1 325 | if [ "$?" != "0" ]; then 326 | echo "CRITICAL - wget is not installed" 327 | exit $ST_CR 328 | fi 329 | get_status 330 | if [ ! -s "$filename" ]; then 331 | echo "CRITICAL - Could not connect to server $hostname" 332 | rm -f "$filename" 333 | exit $ST_CR 334 | else 335 | get_vals 336 | if [ -z "$name" ]; then 337 | echo "CRITICAL - Error parsing server output" 338 | exit $ST_CR 339 | else 340 | do_output 341 | do_perfdata 342 | do_graphite 343 | fi 344 | fi 345 | 346 | COMPARE=$listql 347 | 348 | echo "${NAGSTATUS} - ${output} | ${perfdata}" 349 | exit $EXST 350 | -------------------------------------------------------------------------------- /check_elasticsearch.conf: -------------------------------------------------------------------------------- 1 | object CheckCommand "elastic-search" { 2 | import "plugin-check-command" 3 | command = [ PluginDir + "/check_elasticsearch" ] 4 | 5 | arguments = { 6 | "--hostname" = { 7 | value = "$elasticsearch_hostname$" 8 | description = "Defines the hostname. Default is: localhost" 9 | } 10 | "--port" = { 11 | value = "$elasticsearch_port$" 12 | description = "Defines the port. Default is: 9200" 13 | } 14 | "--secure" = { 15 | value = "$elasticsearch_secure$" 16 | description = "Use TLS. Defaults to false." 17 | } 18 | "--output-directory" = { 19 | value = "$elasticsearch_soutput-directory$" 20 | description = "Specifies where to write the tmp-file that the check creates. Default is: /tmp" 21 | } 22 | "--username" = { 23 | value = "$elasticsearch_username$" 24 | description = "Username for elasticsearch. Turns on authentication mode when set." 25 | } 26 | "--password" = { 27 | value = "$elasticsearch_password$" 28 | description = "Password for elasticsearch. Turns on authentication mode when set." 29 | } 30 | "--auth" = { 31 | value = "$elasticsearch_auth$" 32 | description = "Turns on authentication mode with default credentials." 33 | } 34 | "--ca-certificate" = { 35 | value = "$elasticsearch_ca$" 36 | description = "Uses the provided CA certificate." 37 | } 38 | "--use-jq" = { 39 | value = "$elasticsearch_use_jq$" 40 | description = "Uses jq command to parse the elastic output." 41 | } 42 | } 43 | vars.elasticsearch_hostname = "$address$" 44 | } 45 | --------------------------------------------------------------------------------