Starting services via Ambari REST API

How to start, stop and check services via curl command

Keeping on curl based commands from Ambari REST API, it is also possible start and stop services of your Big Data cluster, and not only to collect metrics from Ambari.
For example,  ambari-service.sh [start|stop|check] a given service:

cesar@lemmy ~/local/ambari $ ./ambari-service.sh 
Usage: ambari-service.sh [ZOOKEEPER|HDFS|YARN|MAPREDUCE2|HBASE|HIVE|SPARK|KAFKA|FLINK|ZK-NIFI|ZEPPELIN] [start|stop|check]

Available services:
 "AMBARI_METRICS"
 "FLINK"
 "HBASE"
 "HDFS"
 "HIVE"
 "KAFKA"
 "MAPREDUCE2"
 "PIG"
 "SLIDER"
 "SPARK"
 "TEZ"
 "YARN"
 "ZEPPELIN"
 "ZK-NIFI"
 "ZOOKEEPER"

For example:

# We can check any of the available services
cesar@lemmy ~/local/ambari $ ./ambari-service.sh HIVE check
Checking HIVE
    "maintenance_state" : "OFF",
    "state" : "STARTED"

# Stopping a STARTED service such as HIVE
cesar@lemmy ~/local/ambari $ ./ambari-service.sh HIVE stop
Stopping HIVE
.
.

# During some seconds the state is STOPPING
cesar@lemmy ~/local/ambari $ ./ambari-service.sh HIVE check
Checking HIVE
    "maintenance_state" : "OFF",
    "state" : "STOPPING"

# When service is stopped the state is INSTALLED
cesar@lemmy ~/local/ambari $ ./ambari-service.sh HIVE check
Checking HIVE
    "maintenance_state" : "OFF",
    "state" : "INSTALLED"

# Then we can start the 
cesar@lemmy ~/local/ambari $ ./ambari-service.sh HIVE start
Starting HIVE
.
.

# During some seconds the state is STARTING
cesar@lemmy ~/local/ambari $ ./ambari-service.sh HIVE check
Checking HIVE
    "maintenance_state" : "OFF",
    "state" : "STARTING"

# And finally the state is STARTED
cesar@lemmy ~/local/ambari $ ./ambari-service.sh HIVE check
Checking HIVE
    "maintenance_state" : "OFF",
    "state" : "STARTED"

And the ambari-service.sh script:

#!/bin/bash

SERVER=ambari.planetexpress.net
PORT=8080
USERNAME=admin
PASSWORD=robot1729
CLUSTERNAME=bender
SERVICE=$1

ENDPOINT="http://${SERVER}:${PORT}/api/v1/clusters/${CLUSTERNAME}/services/${SERVICE}"

case "$2" in
  start)
        echo "Starting $SERVICE"
        curl -u $USERNAME:$PASSWORD -i -H 'X-Requested-By: ambari' -X PUT -d '{"RequestInfo": {"context" :"Starting service via REST"}, "Body": {"ServiceInfo": {"state": "STARTED"}}}' $ENDPOINT
        ;;
  stop)
        echo "Stopping $SERVICE"
        curl -u $USERNAME:$PASSWORD -i -H 'X-Requested-By: ambari' -X PUT -d '{"RequestInfo": {"context" :"Starting service via REST"}, "Body": {"ServiceInfo": {"state": "INSTALLED"}}}' $ENDPOINT
        ;;
  check)
        echo "Checking $SERVICE"
        curl --silent -u ${USERNAME}:${PASSWORD} -X GET ${ENDPOINT}?fields=ServiceInfo | grep state
        ;;
  *)
        echo 'Usage: ambari-service.sh [ZOOKEEPER|HDFS|YARN|MAPREDUCE2|HBASE|HIVE|SPARK|KAFKA|FLINK|ZK-NIFI|ZEPPELIN] [start|stop|check]'
        echo
        echo 'Available services:' 
        curl --silent -u $USERNAME:$PASSWORD -X GET http://${SERVER}:${PORT}/api/v1/clusters/${CLUSTERNAME}/services | grep service_name| awk -F":" '{print $2}'
        ;;
esac

Links:

https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=41812517

00

Más entradas de blog

thumbnail

Añadir comentarios