-
Notifications
You must be signed in to change notification settings - Fork 3
/
ambari-service-check.sh
160 lines (133 loc) · 5.52 KB
/
ambari-service-check.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
usage() {
echo "
This script triggers service checks for components which are not in maintenance mode
Usage: sh ambari-service-check.sh -u <user> -p <password> -s <all|comma-separated list> [-t <ambariServerHost>] [-n <ambariServerPort>] [-c <empty | path to cert file>] \n
If not specified, default value for -t : localhost
If not specified, default value for -n : 8080(when SSL disabled) and 8443(when SSL enabled)
Mention -c at the end of the command as shown in the examples
Example: Trigger Service Check for all components which are not in Maintenance Mode
sh ambari-service-check.sh -u admin -p admin -s all
Example: Trigger Service Check for only for HIVE, HDFS, and KNOX
sh ambari-service-check.sh -u admin -p admin -s hive,hdfs,knox
Example: Trigger Service Check for only for HIVE, HDFS, and KNOX when ssl is enabled
sh ambari-service-check.sh -u admin -p admin -s hive,hdfs,knox -c
Example: Trigger Service Check for only for HIVE, HDFS, and KNOX when ssl is enabled and you want to specify cert file
sh ambari-service-check.sh -u admin -p admin -s hive,hdfs,knox -c /path/to/cert/file
" 1>&2; exit 1;
}
sslEnabled=false;
result='';
certPath='';
sslFlag='';
if [ "$#" -lt 1 ]; then
usage
exit 1
fi
while getopts "u:p:s:t::n::c" opt; do
case "${opt}" in
u)
ambariUser=${OPTARG}
if [ $ambariUser = "" ]; then
usage
fi
;;
p)
ambariPassword=${OPTARG}
if [ $ambariPassword = "" ]; then
usage
fi
;;
t)
ambariHost=${OPTARG}
;;
n)
ambariPort=${OPTARG}
;;
s)
serviceCheck=`echo ${OPTARG} | tr [a-z] [A-Z]`
if [ -z "$serviceCheck" ]; then
usage
fi
;;
c)
sslEnabled=true;
;;
*)
echo "Invalid option specified."
usage
;;
esac
done
shift $((OPTIND-1))
# Set Default Values
if $sslEnabled; then
certPath=$1
if [ -z "$certPath" ]; then
sslFlag=" -k "
else
sslFlag=" --cacert $certPath ";
fi
fi
if [ -z "$ambariHost" ]; then
ambariHost=`hostname`
fi
if $sslEnabled; then
if [ -z "$ambariPort" ]; then
ambariPort="8443"
fi
ambariURL="https://$ambariHost:$ambariPort";
else
if [ -z "$ambariPort" ]; then
ambariPort="8080"
fi
ambariURL="http://$ambariHost:$ambariPort";
fi
if [ -z "$clusterName" ]; then
result=`curl $sslFlag -s -u $ambariUser:$ambariPassword "$ambariURL/api/v1/clusters"`;
if [ -z "$result" ] ; then
echo "Error: The following command did not yield results."
echo "curl $sslFlag -s -u $ambariUser:$ambariPassword \"$ambariURL/api/v1/clusters\""
exit;
fi
clusterName=`curl $sslFlag -s -u $ambariUser:$ambariPassword "$ambariURL/api/v1/clusters" | python -mjson.tool | perl -ne '/"cluster_name":.*?"(.*?)"/ && print "$1\n"'`;
fi
#Prepare list of services for which checks should be triggered
if [ "$serviceCheck" == "ALL" ] ; then
aliveServices=`curl $sslFlag -s -u $ambariUser:$ambariPassword "$ambariURL/api/v1/clusters/$clusterName/services?fields=ServiceInfo/service_name&ServiceInfo/maintenance_state=OFF" | python -mjson.tool | perl -ne '/"service_name":.*?"(.*?)"/ && print "$1\n"'`
else
IFS=","
aliveServices=($serviceCheck)
unset IFS
fi
#Prepare curl request input
postBody=
for service in ${aliveServices[@]};
do
userChoice="Y"
if [ "$service" == "ZOOKEEPER" ]; then
postBody="{\"RequestInfo\":{\"context\":\"$service Service Check\",\"command\":\"${service}_QUORUM_SERVICE_CHECK\"},\"Requests/resource_filters\":[{\"service_name\":\"$service\"}]}"
elif [ "$service" == "KERBEROS" ]; then
existingCredentials=`curl $sslFlag -s -u $ambariUser:$ambariPassword -H "X-Requested-By:X-Requested-By" -X GET "$ambariURL/api/v1/clusters/$clusterName/credentials/kdc.admin.credential" | python -mjson.tool | grep status`
if [ ! -z "$existingCredentials" ]; then
echo "KERBEROS service check requires KDC admin principal and password."
read -p "Press 'y' to perform KERBEROS service check, To skip, press 'n' : " userChoice
if [ "$userChoice" == "y" -o "$userChoice" == "Y" ] ; then
read -p "Enter Kerberos Admin Principal:" princ
read -sp "Enter Kerberos Admin password:" krbpwd
kerberosPost="{\"Credential\" : { \"principal\" : \"$princ\", \"key\" : \"$krbpwd\", \"type\" : \"temporary\"} }"
curl $sslFlag -s -u $ambariUser:$ambariPassword -H "X-Requested-By:X-Requested-By" -X POST --data "$kerberosPost" "$ambariURL/api/v1/clusters/$clusterName/credentials/kdc.admin.credential"
fi
fi
postBody="{\"RequestInfo\":{\"context\":\"$service Service Check\",\"command\":\"${service}_SERVICE_CHECK\"},\"Requests/resource_filters\":[{\"service_name\":\"$service\"}]}"
if [ "$userChoice" == "Y" -o "$userChoice" == "y" ] ; then
echo "\nInitiating service check for $service\n"
curl $sslFlag -s -u $ambariUser:$ambariPassword -H "X-Requested-By:X-Requested-By" -X POST --data "$postBody" "$ambariURL/api/v1/clusters/$clusterName/requests"
fi
else
postBody="{\"RequestInfo\":{\"context\":\"$service Service Check\",\"command\":\"${service}_SERVICE_CHECK\"},\"Requests/resource_filters\":[{\"service_name\":\"$service\"}]}"
fi
if [ "$service" != "KERBEROS" ] ; then
echo "Initiating service check for $service"
curl $sslFlag -s -u $ambariUser:$ambariPassword -H "X-Requested-By:X-Requested-By" -X POST --data "$postBody" "$ambariURL/api/v1/clusters/$clusterName/requests"
fi
done;