Forum Discussion

pranshulgarg's avatar
2 months ago

API to fetch device id and remove it from monitoring

Hi Community,

I am looking for API to automatically delete device from monitoring. Can someone please guide me how to start working on it.

I have generated accessID and accessKey to authenticate my API call. 

Thanks

Pranshul

7 Replies

  • You'll find the Swagger docs for the API here.  That should give you an idea of the API calls you are able to make: https://www.logicmonitor.com/support/rest-api-v3-swagger-documentation

    There is some documentation here on how to work with the API: https://www.logicmonitor.com/support/rest-api-developers-guide/overview/using-logicmonitors-rest-api

    For example, you can call /device/devices/<deviceid> with a DELETE method call and that will delete the device from monitoring. You do need to know what the deviceID is though, so you might need to first search for the device by name using a GET request to the /devices/devices endpoint with a filter 

    One word of warning with using accessID and accessKey... it can be a little complex to get the authentication working property as you have to generate a signature based on your request, time, endpoint you are querying and your credentials.  You might be best off starting with using a Bearer token as you can just supply that in the headers.

    What system/language are you hoping to integrate this with?  Have you seen the Logic Monitor PowerShell module?   If you're just looking to script something you can run yourself, this might be a good option.

    https://github.com/logicmonitor/lm-powershell-module

     

  • Hi Dave_Lee​ 

    Can you please help me with entire process step by step as I am very new to Logic Monitoring.

    You can ping me on MS Teams

    Thanks

    Pranshul

    • Dave_Lee's avatar
      Dave_Lee
      Icon for Advisor rankAdvisor

      It's probably best to cover it here then anyone else who stumbles across this in future can also benefit.

      What are you trying to achieve?  I can give you some sample PowerShell that would show how to query the API for a single device and then make an API call to delete that device.  I'm not sure if that would cover what you need or not?

      Dave

      • Dave_Lee's avatar
        Dave_Lee
        Icon for Advisor rankAdvisor

        But also, if you're wanting to do this from a PowerShell script, then using the LogicMonitor PowerShell module might be the best way if you're not confident with working with APIs directly.

  • Dave_Lee​  - I am confident with working APIs directly. Please guide me with API that can be executed from local windows laptop to fetch device id against a given device name. 

    Need your help for Step-2 and Step-3. Let's start with Step-2 first.

    Step-1 

    AccessID and AccessKey created. 

    Step-2

    Execute API to fetch deviceID ( let say deviceName=ABC)

    Step-3

    Execute API to delete Device using fetched deviceID

     

     

    • Dave_Lee's avatar
      Dave_Lee
      Icon for Advisor rankAdvisor

      LogicMonitor provide the following documentation for their API endpoints.  They documented it using Swagger, so should be familiar as it's a fairly common API documentation framework.  https://www.logicmonitor.com/swagger-ui-master/dist/#/Devices

      Hopefully the following will give you a steer in the right direction.  For me, it was generating the signature for the authentication header that caused me the most problems.  You might want to look at using a Bearer token rather than AccessId/AccessKey (aka LMv1 token) as the Bearer token is much easier to use.

      For step-2

      You call the device/devices API and pass in a filter to narrow down your search to a specific device.   This page provides examples of how to structure filters in your API call:  https://www.logicmonitor.com/support/rest-api-basic-filters

      The section headed up "Filters for /device/devices API Endpoints" shows some examples of the filters that you can use.  For example, if you want to use displayname, you can do a GET request like thi

      https://<account>.logicmonitor.com/device/devices?filter=displayName~"*Member"

      If found, this will return a JSON payload with information about the device, including it's ID.  You'll need to parse the JSON output to get the ID for the device.

      If you know the device ID already, you could bypass this step and go straight to the delete API call.  All you need is the device ID.

      For step-3

      Now you can make an DELTE request to the device/devices endpoint, passing in the ID of the device to delete the device

      https://<account>.logicmonitor.com/device/devices/18904

       

      Authentication / Headers

      You should add the following headers for all requests:

      • X-Version = 3
      • Content-Type = application/json
      • Authorization = <token>

      For authentication, given that you want to use the AccessID and AccessKey method, you are using the "LMv1 Token" authentication method (not related to the API version, this works fine for API v3).  You will have to construct the value of <token> including "LMv1 ", then your Access ID, then a Base64 encoded signature generated from your access key and the details of your request, followed by a timestamp.

      You should end up with something in the following format

      LMv1 AccessId:Signature:Timestamp

      This page will give you some guidance on how to construct this.  Exactly how you do this will depend on the system/language you are using.  https://www.logicmonitor.com/support/rest-api-authentication

      Alternatively, an easier option is to generate a Bearer token on your account.  You can then provide this bearer token direction in the Authorization header, without having to mess about with having to generate a signature etc.