Skip to content

Commit 4aa1d36

Browse files
committed
1.1.0 Added node(s) check
1 parent 4834ded commit 4aa1d36

File tree

1 file changed

+73
-5
lines changed

1 file changed

+73
-5
lines changed

check_rancher2.sh

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@
3030
# 20180926 beta7 Handle a workflow in status 'updating' as warning, not critical #
3131
# 20181107 beta8 Missing pod check type in help, documentation completed #
3232
# 20181109 1.0.0 Do not alert for succeeded pods #
33-
##########################################################################################
34-
# todos:
35-
# - check type: nodes (inside a given cluster)
33+
# 20190308 1.1.0 Added node(s) check #
3634
##########################################################################################
3735
# (Pre-)Define some fixed variables
3836
STATE_OK=0 # define the exit code if status is OK
@@ -41,7 +39,7 @@ STATE_CRITICAL=2 # define the exit code if status is Critical
4139
STATE_UNKNOWN=3 # define the exit code if status is Unknown
4240
export PATH=/usr/local/bin:/usr/bin:/bin:$PATH # Set path
4341
proto=http # Protocol to use, default is http, can be overwritten with -S parameter
44-
version=1.0.0
42+
version=1.1.0
4543

4644
# Check for necessary commands
4745
for cmd in jshon curl [
@@ -54,7 +52,7 @@ do
5452
done
5553
#########################################################################
5654
# We all need help from time to time
57-
help="check_rancher2 v ${version} (c) 2018 Claudio Kuenzler (published under GPLv2)\n
55+
help="check_rancher2 v ${version} (c) 2018-2019 Claudio Kuenzler (published under GPLv2)\n
5856
Usage: $0 -H Rancher2Address -U user-token -P password [-S] -t checktype [-c cluster] [-p project] [-w workload]\n
5957
\nOptions:\n
6058
\t-H Address of Rancher 2 API (e.g. rancher.example.com)\n
@@ -71,6 +69,7 @@ Usage: $0 -H Rancher2Address -U user-token -P password [-S] -t checktype [-c clu
7169
\nCheck Types:\n
7270
\tinfo -> Informs about available clusters and projects and their API ID's. These ID's are needed for specific checks.\n
7371
\tcluster -> Checks the current status of all clusters or of a specific cluster (defined with -c clusterid)\n
72+
\tnode -> Checks the current status of all nodes or of nodes in a specific cluster (defined with -c clusterid)\n
7473
\tproject -> Checks the current status of all projects or of a specific project (defined with -p projectid)\n
7574
\tworkload -> Checks the current status of all or a specific (-w workloadname) workload within a project (-p projectid must be set!)\n
7675
\tpod -> Checks the current status of all or a specific (-o podname -n namespace) pod within a project (-p projectid must be set!)\n
@@ -221,6 +220,75 @@ else
221220
fi
222221
;;
223222

223+
# --- node status check --- #
224+
node)
225+
if [[ -z $clustername ]]; then
226+
227+
# Check status of all nodes in all clusters
228+
api_out_nodes=$(curl -s -u "${apiuser}:${apipass}" "${proto}://${apihost}/v3/nodes")
229+
declare -a node_names=( $(echo "$api_out_nodes" | jshon -e data -a -e nodeName -u) )
230+
declare -a node_status=( $(echo "$api_out_nodes" | jshon -e data -a -e state -u) )
231+
declare -a node_cluster_member=( $(echo "$api_out_nodes" | jshon -e data -a -e clusterId -u) )
232+
233+
i=0
234+
for node in ${node_names[*]}
235+
do
236+
for status in ${node_status[$i]}
237+
do
238+
if [[ ${status} != active ]]; then
239+
nodeerrors[$i]="${node} in cluster ${node_cluster_member[$i]} is ${node_status[$i]} -"
240+
fi
241+
done
242+
let i++
243+
done
244+
245+
if [[ ${#nodeerrors[*]} -gt 0 ]]
246+
then
247+
echo "CHECK_RANCHER2 CRITICAL - ${nodeerrors[*]}|'nodes_total'=${#node_names[*]};;;; 'node_errors'=${#nodeerrors[*]};;;;"
248+
exit ${STATE_CRITICAL}
249+
else
250+
echo "CHECK_RANCHER2 OK - All ${#node_names[*]} nodes are active|'nodes_total'=${#node_names[*]};;;; 'node_errors'=${#nodeerrors[*]};;;;"
251+
exit ${STATE_OK}
252+
fi
253+
254+
else
255+
256+
# Check status of all nodes in a specific clusters
257+
api_out_nodes=$(curl -s -u "${apiuser}:${apipass}" "${proto}://${apihost}/v3/nodes/?clusterId=${clustername}")
258+
259+
# Check if that given cluster name exists
260+
if [[ -n $(echo "$api_out_nodes" | grep -i "error") ]]
261+
then echo "CHECK_RANCHER2 CRITICAL - Cluster $clustername not found. Hint: Use '-t info' to identify cluster and project names."; exit ${STATE_CRITICAL}
262+
fi
263+
264+
declare -a node_names=( $(echo "$api_out_nodes" | jshon -e data -a -e nodeName -u) )
265+
declare -a node_status=( $(echo "$api_out_nodes" | jshon -e data -a -e state -u) )
266+
267+
i=0
268+
for node in ${node_names[*]}
269+
do
270+
for status in ${node_status[$i]}
271+
do
272+
if [[ ${status} != active ]]; then
273+
nodeerrors[$i]="${node} in cluster ${clustername} is ${node_status[$i]} -"
274+
fi
275+
done
276+
let i++
277+
done
278+
279+
if [[ ${#nodeerrors[*]} -gt 0 ]]
280+
then
281+
echo "CHECK_RANCHER2 CRITICAL - ${nodeerrors[*]}|'nodes_total'=${#node_names[*]};;;; 'node_errors'=${#nodeerrors[*]};;;;"
282+
exit ${STATE_CRITICAL}
283+
else
284+
echo "CHECK_RANCHER2 OK - All ${#node_names[*]} nodes are active|'nodes_total'=${#node_names[*]};;;; 'node_errors'=${#nodeerrors[*]};;;;"
285+
exit ${STATE_OK}
286+
fi
287+
288+
fi
289+
;;
290+
291+
224292
# --- project status check --- #
225293
project)
226294
if [[ -z $projectname ]]; then

0 commit comments

Comments
 (0)