クラスタモニタリングページhiveステータスを取得し、シミュレーションページhiveを再起動

3991 ワード

lowコード、バックアップのみ
# coding=utf-8
import httplib
import gzip
import StringIO
import json
import os


#  cookie
def get_cookie():
    header = {'Host':' [ ip]: [ ]',
          'Authorization':'Basic YWRtaW46YWRtaW4=',    
        'Accept-Language':'zh-CN,zh;q=0.8',
        'Connection':'keep-alive',
        'Accept-Encoding':'gzip, deflate, sdch',
        'Accept':'application/json, text/javascript, */*; q=0.01',
        'Cookie':'AMBARISESSIONID=c4futirz7ka3mraa6c7j1dy',
        'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
        'Referer':'http:// [ ip]: [ ]/',
        'X-Requested-By':'X-Requested-By',
        'X-Requested-With':'XMLHttpRequest'
        }
     
    con = httplib.HTTPConnection(' [ ip]: [ ]')            
    # print(con)
    con.request(method='GET',url='/api/v1/users/admin?fields=*,privileges/PrivilegeInfo/cluster_name,privileges/PrivilegeInfo/permission_name&_=1568710093248',headers=header)
    res = con.getresponse()
    # print(res)
    content = res.read()
    # print(content)
    con.close()
    
    
    if res.getheader('Set-Cookie')!=None:
        cookie = res.getheader('Set-Cookie').split(';')[0]
        return cookie
    else:
        print 'got no cookie'
        exit(1)


#  
def get_hive_state():
    header = {'Host':' [ ip]: [ ]',
        'Accept-Language':'zh-CN,zh;q=0.8',
        'Connection':'keep-alive',
        'Accept-Encoding':'gzip, deflate, sdch',
        'Accept':'application/json, text/javascript, */*; q=0.01',
        'Cookie':'AMBARISESSIONID=1qh9ilh89pxic11guank00tiwf',
        'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
        'Referer':'http:// [ ip]: [ ]/',
        'X-Requested-By':'X-Requested-By',
        'X-Requested-With':'XMLHttpRequest'
        }
    con = httplib.HTTPConnection(' [ ip]: [ ]')
    con.request(method='GET', url='/api/v1/clusters/[other]/HIVE_SERVER', headers=header)
    res = con.getresponse()
    res.encoding = 'utf-8'
    content = res.read()
    data = StringIO.StringIO(content)
    gzipper = gzip.GzipFile(fileobj=data)
    html = gzipper.read()
    context = json.loads(html)
    hive_state = context['HostRoles']['state']
    return hive_state


#  ,  
def restart_hive():
    hive_state = get_hive_state()
    cookie = get_cookie()
    
    if hive_state == 'STARTED':
        print("the hive progress is running")
        return "the hive progress is running"
    elif hive_state == 'INSTALLED':
        host_ip=' [ ip]: [ ]'
        host_name='****'
        cluster_name='****'
        API='''curl  http://%s/api/v1/clusters/[other]/HIVE_SERVER? -H "X-Requested-By: X-Requested-By"  -H "Cookie: %s" -d '{"RequestInfo":{"context":"  HiveServer2","operation_level":{"level":"HOST_COMPONENT","cluster_name":"%s","host_name":"%s","service_name":"HIVE"}},"Body":{"HostRoles":{"state":"STARTED"}}}' -X PUT'''%(host_ip, cookie, cluster_name, host_name)
        os.system(API)
        print("hive progress will be restartted, please check in http:// [ ip]: [ ]")
        return "hive progress will be restartted, please check in http:// [ ip]: [ ]"
   

if __name__ == '__main__':
    restart_hive()