Run all service checks in bulk

  • 0

Run all service checks in bulk

In this blogs I tried to explain that how you can use ambari API to trigger all Service Checks with a single command.

In order to check the status and stability of any service in your cluster you need to run the service checks that are included in Ambari. Usually each Service provides its own service check in ambari and to run a service check you have to select the service (e.g. HDFS) in Ambari and click “Run Service Check” in the “Actions” dropdown menu.

But its a tedious job to run every service check one by one in case if we have many services. So I created this script by using Ambari API to start all available service checks via single script. Only thing you need to pass required parameters according to your env.

Example:

[s0998dnz@m1.hdp22 ~]$ ./run_all_service_checks.sh
Enter Ambari server name : m1.hdp22
Enter Ambari admin's User Name: saurkuma
Enter Password for saurkuma : 
Your cluster name is: HDPTST
There are following running services :
FALCON
FLUME
HBASE
HDFS
HIVE
KAFKA
KNOX
MAHOUT
MAPREDUCE2
OOZIE
PIG
RANGER
RANGER_KMS
SLIDER
SPARK
SQOOP
STORM
TEZ
YARN
ZOOKEEPER
{
  "href" : "http://m1.hdp22:8080/api/v1/clusters/HDPTST/requests/1315",
  "Requests" : {
    "id" : 1315,
    "status" : "Accepted"
  }
}{
  "href" : "http://m1.hdp22:8080/api/v1/clusters/HDPTST/requests/1316",
  "Requests" : {
    "id" : 1316,
    "status" : "Accepted"
  }
}{
  "href" : "http://m1.hdp22:8080/api/v1/clusters/HDPTST/requests/1317",
  "Requests" : {
    "id" : 1317,
    "status" : "Accepted"
  }
}{
  "href" : "http://m1.hdp22:8080/api/v1/clusters/HDPTST/requests/1318",
  "Requests" : {
    "id" : 1318,
    "status" : "Accepted"
  }
}{
  "href" : "http://m1.hdp22:8080/api/v1/clusters/HDPTST/requests/1319",
  "Requests" : {
    "id" : 1319,
    "status" : "Accepted"
  }
}{
  "href" : "http://m1.hdp22:8080/api/v1/clusters/HDPTST/requests/1320",
  "Requests" : {
    "id" : 1320,
    "status" : "Accepted"
  }
}{
  "href" : "http://m1.hdp22:8080/api/v1/clusters/HDPTST/requests/1321",
  "Requests" : {
    "id" : 1321,
    "status" : "Accepted"
  }
}{
  "href" : "http://m1.hdp22:8080/api/v1/clusters/HDPTST/requests/1322",
  "Requests" : {
    "id" : 1322,
    "status" : "Accepted"
  }
}{
  "href" : "http://m1.hdp22:8080/api/v1/clusters/HDPTST/requests/1323",
  "Requests" : {
    "id" : 1323,
    "status" : "Accepted"
  }
}{
  "href" : "http://m1.hdp22:8080/api/v1/clusters/HDPTST/requests/1324",
  "Requests" : {
    "id" : 1324,
    "status" : "Accepted"
  }
}{
  "href" : "http://m1.hdp22:8080/api/v1/clusters/HDPTST/requests/1325",
  "Requests" : {
    "id" : 1325,
    "status" : "Accepted"
  }
}{
  "href" : "http://m1.hdp22:8080/api/v1/clusters/HDPTST/requests/1326",
  "Requests" : {
    "id" : 1326,
    "status" : "Accepted"
  }
}{
  "href" : "http://m1.hdp22:8080/api/v1/clusters/HDPTST/requests/1327",
  "Requests" : {
    "id" : 1327,
    "status" : "Accepted"
  }
}{
  "href" : "http://m1.hdp22:8080/api/v1/clusters/HDPTST/requests/1328",
  "Requests" : {
    "id" : 1328,
    "status" : "Accepted"
  }
}{
  "href" : "http://m1.hdp22:8080/api/v1/clusters/HDPTST/requests/1329",
  "Requests" : {
    "id" : 1329,
    "status" : "Accepted"
  }
}{
  "href" : "http://m1.hdp22:8080/api/v1/clusters/HDPTST/requests/1330",
  "Requests" : {
    "id" : 1330,
    "status" : "Accepted"
  }
}{
  "href" : "http://m1.hdp22:8080/api/v1/clusters/HDPTST/requests/1331",
  "Requests" : {
    "id" : 1331,
    "status" : "Accepted"
  }
}{
  "href" : "http://m1.hdp22:8080/api/v1/clusters/HDPTST/requests/1332",
  "Requests" : {
    "id" : 1332,
    "status" : "Accepted"
  }
}{
  "href" : "http://m1.hdp22:8080/api/v1/clusters/HDPTST/requests/1333",
  "Requests" : {
    "id" : 1333,
    "status" : "Accepted"
  }
}{
  "href" : "http://m1.hdp22:8080/api/v1/clusters/HDPTST/requests/1334",
  "Requests" : {
    "id" : 1334,
    "status" : "Accepted"
  }
}

Now if you will login to your ambari server you will see all service checks are running. So now you will be thinking which script is doing this magic,so don’t worry here is script. You use it and enjoy.

[s0998dnz@m1.hdp22 ~]$ cat run_all_service_checks.sh 
#!/usr/bin/env bash
###########################
## Saurabh Singh ###
### Version 1.0 ####
###########################
echo -n "Enter Ambari server name : "
read "server"
AMBARI_HOST=$server
echo -n "Enter Ambari admin's User Name: "
read "user"
echo -n "Enter Password for $user : "
read -s "pwd"
LOGIN=$user
PASSWORD=$pwd
if [ -e "~/.ambari_login" ]; then
    . ~/.ambari_login
fi

cluster_name=$(curl -s -u $LOGIN:$PASSWORD --insecure "http://$AMBARI_HOST:8080/api/v1/clusters"  | python -mjson.tool | perl -ne '/"cluster_name":.*?"(.*?)"/ && print "$1\n"')
if [ -z "$cluster_name" ]; then
    exit
fi
echo -e "\nYour cluster name is: $cluster_name"

running_services=$(curl -s -u $LOGIN:$PASSWORD --insecure "http://$AMBARI_HOST:8080/api/v1/clusters/$cluster_name/services?fields=ServiceInfo/service_name&ServiceInfo/maintenance_state=OFF" | python -mjson.tool | perl -ne '/"service_name":.*?"(.*?)"/ && print "$1\n"')
if [ -z "$running_services" ]; then
    exit
fi
echo "There are following running services :
$running_services"

post_body=
for s in $running_services; do
    if [ "$s" == "ZOOKEEPER" ]; then
        post_body="{\"RequestInfo\":{\"context\":\"$s Service Check\",\"command\":\"${s}_QUORUM_SERVICE_CHECK\"},\"Requests/resource_filters\":[{\"service_name\":\"$s\"}]}"

    else
        post_body="{\"RequestInfo\":{\"context\":\"$s Service Check\",\"command\":\"${s}_SERVICE_CHECK\"},\"Requests/resource_filters\":[{\"service_name\":\"$s\"}]}"
    fi
    curl -s -u $LOGIN:$PASSWORD --insecure -H "X-Requested-By:X-Requested-By" -X POST --data "$post_body"  "http://$AMBARI_HOST:8080/api/v1/clusters/$cluster_name/requests"
done

As always I welcome your valuable feedback or any suggestion.