Esempio n. 1
0
    def send_regi(cls, type='regi'):
        auth = cls.get_auth()

        url = 'http://' + str(CONFIG.get_rest_ip()) + ':' + str(
            CONFIG.get_rest_port()) + '/event'

        req_body = {'url': url}
        req_body_json = json.dumps(req_body)

        header = {
            'Content-Type': 'application/json',
            'Authorization': base64.b64encode(auth)
        }

        cls.CLI_LOG.cli_log(
            '---------------------------SEND CMD---------------------------')

        try:
            if type == 'regi':
                url = CONFIG.get_regi_uri()
            else:
                url = CONFIG.get_unregi_uri()

            cls.CLI_LOG.cli_log('URL = ' + url)
            cls.CLI_LOG.cli_log('AUTH = ' + auth)

            myResponse = requests.get(url,
                                      headers=header,
                                      data=req_body_json,
                                      timeout=CONFIG.get_rest_timeout())

            cls.CLI_LOG.cli_log('HEADER = ' +
                                json.dumps(header, sort_keys=True, indent=4))
            cls.CLI_LOG.cli_log('BODY = ' +
                                json.dumps(req_body, sort_keys=True, indent=4))

        except:
            # req timeout
            LOG.exception_err_write()
            return False

        cls.CLI_LOG.cli_log(
            '---------------------------RECV RES---------------------------')
        cls.CLI_LOG.cli_log('RESPONSE CODE = ' + str(myResponse.status_code))

        if myResponse.status_code == 200:
            result = json.loads(myResponse.content)

            if result['Result'] == 'SUCCESS':
                return True
            else:
                return False
        else:
            return False
Esempio n. 2
0
    def send_rest(cls, cmd):
        auth = cls.get_auth()

        tmp = cmd.split(' ')
        cmd = tmp[0]
        param = ''

        if len(tmp) == 2:
            param = tmp[1]

        req_body = {'command': cmd, 'system': cls.selected_sys, 'param': param}
        req_body_json = json.dumps(req_body)

        header = {'Content-Type': 'application/json', 'Authorization': base64.b64encode(auth)}

        cls.CLI_LOG.cli_log('---------------------------SEND CMD---------------------------')

        try:
            url = CONFIG.get_cmd_addr()
            cls.CLI_LOG.cli_log('URL = ' + url)
            cls.CLI_LOG.cli_log('AUTH = ' + auth)

            myResponse = requests.get(url, headers=header, data=req_body_json, timeout=CONFIG.get_rest_timeout())

            cls.CLI_LOG.cli_log('COMMAND = ' + cmd)
            cls.CLI_LOG.cli_log('SYSTEM = ' + cls.selected_sys)
            cls.CLI_LOG.cli_log('HEADER = ' + json.dumps(header, sort_keys=True, indent=4))
            cls.CLI_LOG.cli_log('BODY = ' + json.dumps(req_body, sort_keys=True, indent=4))

        except:
            # req timeout
            LOG.exception_err_write()
            return -1, None

        cls.CLI_LOG.cli_log('---------------------------RECV RES---------------------------')
        cls.CLI_LOG.cli_log('RESPONSE CODE = ' + str(myResponse.status_code))

        try:
            cls.CLI_LOG.cli_log(
                'BODY = ' + json.dumps(json.loads(myResponse.content.replace("\'", '"')), sort_keys=True, indent=4))
        except:
            cls.CLI_LOG.cli_log('BODY = ' + myResponse.content)

        return 1, myResponse
Esempio n. 3
0
    def get_event_list(cls):
        auth = cls.get_auth()

        url = 'http://' + str(CONFIG.get_rest_ip()) + ':' + str(
            CONFIG.get_rest_port()) + '/event'

        req_body = {'url': url}
        req_body_json = json.dumps(req_body)

        header = {
            'Content-Type': 'application/json',
            'Authorization': base64.b64encode(auth)
        }

        cls.CLI_LOG.cli_log(
            '---------------------------SEND CMD---------------------------')

        try:
            url = CONFIG.get_event_list_uri()

            cls.CLI_LOG.cli_log('URL = ' + url)
            cls.CLI_LOG.cli_log('AUTH = ' + auth)

            myResponse = requests.get(url,
                                      headers=header,
                                      data=req_body_json,
                                      timeout=CONFIG.get_rest_timeout())

            cls.CLI_LOG.cli_log('HEADER = ' +
                                json.dumps(header, sort_keys=True, indent=4))
            cls.CLI_LOG.cli_log('BODY = ' +
                                json.dumps(req_body, sort_keys=True, indent=4))

        except:
            # req timeout
            LOG.exception_err_write()
            return False

        cls.CLI_LOG.cli_log(
            '---------------------------RECV RES---------------------------')
        cls.CLI_LOG.cli_log('RESPONSE CODE = ' + str(myResponse.status_code))

        try:
            res = json.loads(myResponse.content.replace("\'", '"'))
            cls.CLI_LOG.cli_log('BODY = ' +
                                json.dumps(res, sort_keys=True, indent=4))

            cls.HISTORY_LOG.write_history(
                "--- Current Event History Start ---")

            for line in res['Event list']:
                cls.HISTORY_LOG.write_history(
                    '[OCCUR_TIME : %s][%s][%s][%s] %s', line['time'],
                    line['system'], line['item'],
                    line['pre_grade'] + '->' + line['grade'], line['reason'])

            cls.HISTORY_LOG.write_history("--- Current Event History END ---")

        except:

            cls.CLI_LOG.cli_log('BODY = ' + myResponse.content)

        result = json.loads(myResponse.content)

        if myResponse.status_code == 200 and result['Result'] == 'SUCCESS':
            return True
        else:
            return False
Esempio n. 4
0
    def get_event_list(cls):
        auth = cls.get_auth()

        url = 'http://' + str(CONFIG.get_rest_ip()) + ':' + str(
            CONFIG.get_rest_port()) + '/event'

        req_body = {'url': url}
        req_body_json = json.dumps(req_body)

        header = {
            'Content-Type': 'application/json',
            'Authorization': base64.b64encode(auth)
        }

        cls.CLI_LOG.cli_log(
            'get_event_list:: SEND CMD ---------------------------')

        try:
            url = CONFIG.get_event_list_uri()

            cls.CLI_LOG.cli_log('URL = ' + url)
            cls.CLI_LOG.cli_log('AUTH = ' + auth)

            myResponse = requests.get(url,
                                      headers=header,
                                      data=req_body_json,
                                      timeout=CONFIG.get_rest_timeout())

            cls.CLI_LOG.cli_log('HEADER = ' +
                                json.dumps(header, sort_keys=True, indent=4))
            cls.CLI_LOG.cli_log('BODY = ' +
                                json.dumps(req_body, sort_keys=True, indent=4))

        except:
            # req timeout
            LOG.exception_err_write()
            return False

        cls.CLI_LOG.cli_log(
            'get_event_list:: RECV RES ---------------------------')
        cls.CLI_LOG.cli_log('RESPONSE CODE = ' + str(myResponse.status_code))
        cls.CLI_LOG.cli_log('RESPONSE BODY = ' + str(myResponse.content))

        try:
            res = json.loads(myResponse.content.replace("\'", '"'))
            cls.CLI_LOG.cli_log('BODY = ' +
                                json.dumps(res, sort_keys=True, indent=4))

            cls.HISTORY_LOG.write_history(
                "[%s] --- Current Event History Begin ---",
                str(datetime.now()))

            for line in res['event_list']:
                reason_str = ''
                if type(line['reason']) == list:
                    if len(line['reason']) > 0:
                        reason_str = '\n-- ' + '\n-- '.join(line['reason'])
                else:
                    reason_str = str(line['reason'])

                cls.HISTORY_LOG.write_history(
                    '[%s] %s %s changed from %s to %s %s', line['time'],
                    line['system'], line['item'], line['pre_grade'],
                    line['grade'], reason_str)

            cls.HISTORY_LOG.write_history(
                "[%s] --- Current Event History End ---", str(datetime.now()))

        except:
            LOG.exception_err_write()

        result = json.loads(myResponse.content)

        if myResponse.status_code == 200 and result['Result'] == 'SUCCESS':
            return True
        else:
            return False