Forum Discussion

GUaro5555's avatar
2 years ago

Login

HI gurus

Very new to Python and therefore to all this so go easy on me,  I am trying to just login to logicMonitor using python 3

I have my accesskey and all that good stuff, well I think do, but I cant seemed to be able to login into my account.

I have :

signature = base64.b64encode(hmac.new(AccessKey,msg=requestVars,digestmod=hashlib.sha256).hexdigest())

  But i get the expected a "byte" and got str instead.

can someone share their Python code to just login into logicmonitor , so I can learn on how to do it and what am I doing wrong?

Thanks a bunch 

3 Replies

  • You might consider using the SDK, instructions here.  My personal wrapper to streamline the sdk is described a href="https://communities.logicmonitor.com/topic/7713-lm-wrapper-for-the-python-sdk/#comment-86637" rel="">here.

    Here's my LM_GET function, don't remember where I got it. 

    def LM_GET(_lm_id, _lm_key, _lm_account, _resource_path, _query_params = {}, _data = '', _query_size = 1000):
      items = []
      last_item_found = False
      status_code = 0
      _query_params['size'] = _query_size
      err_out = ""
      while not last_item_found:
        _query_params['offset'] = len(items)
        _query_params_string = "?" + "&".join([f"{k}={v}" for k,v in _query_params.items()])
        url = 'https://'+ _lm_account +'.logicmonitor.com/santaba/rest' + _resource_path + _query_params_string
        epoch = str(int(time.time() * 1000))
        requestVars = 'GET' + epoch + _data + _resource_path
        authCode = hmac.new(_lm_key.encode(),msg=requestVars.encode(),digestmod=hashlib.sha256).hexdigest()
        signature = base64.b64encode(authCode.encode())
        auth = 'LMv1 ' + _lm_id + ':' + signature.decode() + ':' + epoch
        headers = {'Content-Type':'application/json','Authorization':auth,'X-Version': '2'}
        response = requests.get(url, data=_data, headers=headers)
        status_code = response.status_code
        if status_code in (200,201):
            current_call_result = json.loads(response.content)
            data = current_call_result['items']
            fetched_count = len(data)
            fetched_items = data
            items += fetched_items
            last_item_found = fetched_count < _query_size
        else:
            current_call_result = response.content
            last_item_found=True
            err_out += f"""Call to {url} results:
        HTTP Response code: {status_code}
        Status code: {status_code}
        Items fetched: {fetched_count}
        Total items fetched: {len(items)}
        Last item found: {last_item_found}\n"""
      return {'code':status_code, 'items':items, 'err_out': err_out}

     

  • Boss

    all this just to login,i am not planning to do anything just login , well I have a lot to read chief, I will keep you posted, Thanks a bunchhhhhhhhhhhhhhhh