集群监控页面爬取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()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值