(**kwargs)
Perform a http get request and validate the return code
Perform a http get request and validate the return code
Perform a http get request and validate the return code
def check(**kwargs):
    """ Perform a http get request and validate the return code """
    jdata = kwargs['jdata']
    logger = kwargs['logger']
    headers = {}
    if 'extra_headers' in jdata['data']:
        headers = ParseHeaders(jdata['data']['extra_headers'])
    headers['host'] = jdata['data']['host']
    timeout = 3.00
    url = jdata['data']['url']
    try:
        result = requests.get(
            url, timeout=timeout, headers=headers, allow_redirects=True, verify=False, stream=True)
    except Exception as e:
        line = 'http-keyword: Reqeust to {0} sent for monitor {1} - ' \
               'had an exception: {2}'.format(url, jdata['cid'], e)
        logger.debug(line)
        return False
        
    stream = cStringIO.StringIO()
    length = 0
    for chunk in result.iter_content(8192, decode_unicode=False):
        stream.write(chunk)
        length += len(chunk)
        if length > get_max_size():
            break
        
    retext = stream.getvalue()
    stream.close()
    result.close()
    retext = retext.decode('utf8')
    if jdata['data']['regex'] == "True":
        match = re.search(jdata['data']['keyword'], retext)
        if match:
            if jdata['data']['present'] == "True":
                return True
            else:
                return False
        else:
            if jdata['data']['present'] == "False":
                return True
            else:
                return False
    else:
        if jdata['data']['keyword'] in retext:
            if jdata['data']['present'] == "True":
                return True
            else:
                return False
        else:
            if jdata['data']['present'] == "False":
                return True
            else:
                return False
def check ( * * kwargs ) : jdata = kwargs [ 'jdata' ] logger = kwargs [ 'logger' ] headers = { } if 'extra_headers' in jdata [ 'data' ] : headers = ParseHeaders ( jdata [ 'data' ] [ 'extra_headers' ] ) headers [ 'host' ] = jdata [ 'data' ] [ 'host' ] timeout = 3.00 url = jdata [ 'data' ] [ 'url' ] try : result = requests . get ( url , timeout = timeout , headers = headers , allow_redirects = True , verify = False , stream = True ) except Exception as e : line = 'http-keyword: Reqeust to {0} sent for monitor {1} - ' 'had an exception: {2}' . format ( url , jdata [ 'cid' ] , e ) logger . debug ( line ) return False stream = cStringIO . StringIO ( ) length = 0 for chunk in result . iter_content ( 8192 , decode_unicode = False ) : stream . write ( chunk ) length += len ( chunk ) if length > get_max_size ( ) : break retext = stream . getvalue ( ) stream . close ( ) result . close ( ) retext = retext . decode ( 'utf8' ) if jdata [ 'data' ] [ 'regex' ] == "True" : match = re . search ( jdata [ 'data' ] [ 'keyword' ] , retext ) if match : if jdata [ 'data' ] [ 'present' ] == "True" : return True else : return False else : if jdata [ 'data' ] [ 'present' ] == "False" : return True else : return False else : if jdata [ 'data' ] [ 'keyword' ] in retext : if jdata [ 'data' ] [ 'present' ] == "True" : return True else : return False else : if jdata [ 'data' ] [ 'present' ] == "False" : return True else : return False