集群监控页面爬取hive状态, 并模拟页面重启hive

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()
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页