|
| 1 | +#!/bin/bash |
| 2 | +showhelp () { |
| 3 | +cat <<eof |
| 4 | +
|
| 5 | + Check to see if the defined amount of spouts are actually running. |
| 6 | +
|
| 7 | + Parameters: |
| 8 | + --topology-name | -n : The name of the topology |
| 9 | + --spout-name | -s : The name of the spout to check, if not specified, it will print a list of spouts |
| 10 | + --url | -u : The url to connect to storm, defaults to localhost:8080 |
| 11 | + --expected-par | -p : The expected amount of spout instances, defaults to 1 |
| 12 | + --help | -h : Shows help |
| 13 | + --debug | -d : Enable debugging, aka echo some params |
| 14 | +
|
| 15 | + Example usage: |
| 16 | + ./check_topology_spouts -n intercad-topology -b update_incident_spout |
| 17 | + |
| 18 | +eof |
| 19 | + exit 3 |
| 20 | +} |
| 21 | + |
| 22 | +debug () { |
| 23 | + if $debug |
| 24 | + then |
| 25 | + echo $@ |
| 26 | + fi |
| 27 | +} |
| 28 | + |
| 29 | +# Params |
| 30 | +defaults () { |
| 31 | + # Not specified params |
| 32 | + error_spouts='' |
| 33 | + good_spouts=0 |
| 34 | + # Required params |
| 35 | + if [[ -z "${topology_name}" ]] |
| 36 | + then |
| 37 | + message="Unknown: You need to specify --topology-name of use --help" |
| 38 | + exitstatus='3' |
| 39 | + quit |
| 40 | + fi |
| 41 | + # Optional params |
| 42 | + if [[ -z "${url}" ]] |
| 43 | + then |
| 44 | + url='localhost:8080' |
| 45 | + fi |
| 46 | + if [[ -z "${debug}" ]] |
| 47 | + then |
| 48 | + debug=false |
| 49 | + fi |
| 50 | + if [[ -z "${expected_par}" ]] |
| 51 | + then |
| 52 | + expected_par='1' |
| 53 | + fi |
| 54 | + if [[ -z "${spout_name}" ]] |
| 55 | + then |
| 56 | + spout_name='ALL' |
| 57 | + fi |
| 58 | +} |
| 59 | + |
| 60 | +is_spout_id_defined () { |
| 61 | + spout_id_test=$@ |
| 62 | + spout_ids=$( echo $get_spouts | jq ".spouts[].spoutId" -r ) |
| 63 | + return_value=1 |
| 64 | + for element in $spout_ids |
| 65 | + do |
| 66 | + debug 'Checking if spout_id is defined' |
| 67 | + if [ $element == $spout_id_test ] |
| 68 | + then |
| 69 | + return_value=0 |
| 70 | + debug 'it is!' |
| 71 | + break |
| 72 | + fi |
| 73 | + done |
| 74 | + return $return_value |
| 75 | +} |
| 76 | + |
| 77 | +get_data () { |
| 78 | + # Get the id of the topology |
| 79 | + get_topology_id=$( curl -s http://${url}/api/v1/topology/summary ) |
| 80 | + # Check if the topology_name is defined |
| 81 | + get_topologies_specified=$( echo $get_topology_id | jq ".topologies[].id" -r ) |
| 82 | + debug $get_topologies_specified |
| 83 | + if [[ -z $get_topologies_specified ]] |
| 84 | + then |
| 85 | + message="Error: Topology ${topology_name} not defined" |
| 86 | + exitstatus='2' |
| 87 | + quit |
| 88 | + fi |
| 89 | + topology_id=$( echo $get_topology_id | jq ".topologies | map(select(.name == \"${topology_name}\" )) | .[].id" -r ) |
| 90 | + debug $topology_id |
| 91 | + |
| 92 | + debug "curl -s http://${url}/api/v1/topology/${topology_id}" |
| 93 | + get_spouts=$( curl -s http://${url}/api/v1/topology/${topology_id} ) |
| 94 | + if [[ "$spout_name" == "ALL" ]] |
| 95 | + then |
| 96 | + message="Unknown: Available spouts:\n" |
| 97 | + spout_ids=$( echo $get_spouts | jq ".spouts[].spoutId" -r ) |
| 98 | + message="${message}${spout_ids}" |
| 99 | + exitstatus="3" |
| 100 | + quit |
| 101 | + else |
| 102 | + spout_id=$spout_name |
| 103 | + fi |
| 104 | +} |
| 105 | + |
| 106 | +do_main_check_one () { |
| 107 | + # Check if the spout is defined in the topology |
| 108 | + if is_spout_id_defined $spout_id |
| 109 | + then |
| 110 | + # Get data on the spout |
| 111 | + get_spout_tasks=$( echo $get_spouts | jq " .spouts | map(select(.spoutId == \"${spout_id}\" )) | .[].tasks" -r ) |
| 112 | + get_spout_last_error=$( echo $get_spouts | jq " .spouts | map(select(.spoutId == \"${spout_id}\" )) | .[].lastError" -r ) |
| 113 | + get_spout_executors=$( echo $get_spouts | jq " .spouts | map(select(.spoutId == \"${spout_id}\" )) | .[].executors" -r ) |
| 114 | + debug ============== |
| 115 | + debug $spout_id |
| 116 | + debug $get_spout_tasks |
| 117 | + debug $get_spout_executors |
| 118 | + if [[ ! -z $get_spout_last_error ]] |
| 119 | + then |
| 120 | + debug $get_spout_last_error |
| 121 | + else |
| 122 | + debug No lastError |
| 123 | + fi |
| 124 | + debug ============== |
| 125 | + # Do the actual check |
| 126 | + if [[ $get_spout_tasks == $expected_par ]] |
| 127 | + then |
| 128 | + message="Ok: Spout: ${spout_name} is running $get_spout_tasks tasks." |
| 129 | + exitstatus='0' |
| 130 | + else |
| 131 | + message="Critical: Spout: ${spout_name} is running $get_spout_tasks tasks!" |
| 132 | + exitstatus='2' |
| 133 | + fi |
| 134 | + else |
| 135 | + message="Critical: Spout not found!" |
| 136 | + exitstatus='2' |
| 137 | + fi |
| 138 | +} |
| 139 | + |
| 140 | +quit () { |
| 141 | + echo -e "${message}" && exit $exitstatus |
| 142 | +} |
| 143 | + |
| 144 | +# Start casing |
| 145 | +while test -n "$1" |
| 146 | +do |
| 147 | + case "$1" in |
| 148 | + --help|-h) |
| 149 | + showhelp |
| 150 | + ;; |
| 151 | + --topology-name|-n) |
| 152 | + shift |
| 153 | + topology_name=$1 |
| 154 | + shift |
| 155 | + ;; |
| 156 | + --url|-u) |
| 157 | + shift |
| 158 | + url=$1 |
| 159 | + shift |
| 160 | + ;; |
| 161 | + --debug|-d) |
| 162 | + shift |
| 163 | + debug=true |
| 164 | + ;; |
| 165 | + --expected-par|-p) |
| 166 | + shift |
| 167 | + expected_par=$1 |
| 168 | + shift |
| 169 | + ;; |
| 170 | + --spout-name|-s) |
| 171 | + shift |
| 172 | + spout_name=$1 |
| 173 | + shift |
| 174 | + ;; |
| 175 | + *) |
| 176 | + showhelp |
| 177 | + ;; |
| 178 | + esac |
| 179 | +done |
| 180 | + |
| 181 | +defaults |
| 182 | +get_data |
| 183 | +do_main_check_one |
| 184 | +quit |
| 185 | + |
0 commit comments