Forum Discussion

EvanTempel's avatar
EvanTempel
Icon for Neophyte rankNeophyte
2 months ago

LM API

Interesting in hearing some use cases you guys have for the LM API?

I have a few scripts setup for mass device manipulation in the case of a change, mass auto discovery, automatically add a Windows server based off AD group, and automated internal ping scans based off a few parameters.

Curious to see use cases you guys are using.

  • As part of a MSP, the majority of the API work I used to do is related to onboarding new customers/environments. Had several scripts to add standard groups, add devices, deploy dashboard templates (some predate dashboard tokens), specific customizations, etc. Had some script that checks for "invalid" configurations, either items that cause problems (like device names with newlines in them from CSV import) or not following our conventions/policies like location property not matching CMDB, hoststatus check disabled, wrong collector, etc. Also had some limited reporting stuff and integrations into other systems like ticketing.

  • We've build a whole suite of products around the LogicMonitor API!  Alert integration, CMDB sync, customer reporting, fully-automated provisioning. It really is very powerful.

    To make things easier for us and the .NET development community, we've created an MIT-licensed, open source library (in .NET terms, a "nuget").  You can find that here: https://www.nuget.org/packages/LogicMonitor.Api 

    There's even a free (as in beer) Datmart for SQL Server or Postgres, which you can find here: https://www.panoramicdata.com/open-source/logicmonitor-datamart/ .  Runs in docker, or you can even use the library for free as part of your own commercial product (we certainly do).

    Most of our pro services end up using it in some way or another!

    Automate all the things!

  • Some of the use cases that I personally have done:

    1) Make device turn-ups in one system reach out to LM via API to create devices and set custom properties.

    2) Use the API to create devices from info in an EMS (when Netscan isn't sufficient).

    3) Grab all collectors, pull their resource context, pull custom properties from the resource context, then set or update custom properties on the collector objects, for more custom "collector down" alert integrations.

    4) Scan resources for 90+ day host down, and use team-specific custom properties to generate custom device list CSVs to different teams.

    5) Rename devices based on a concatenation of existing snmp info and resource properties.

    6) Switch preferred collector to autobalanced mode on every resource in a specified auto-balanced collector group, due to old manual UI settings blocking the devices from UI-based re-balance.

    7) Edit netscans in bulk, because updating 90 netscans in the UI would be terrible.

    etc etc

     

    • Barb's avatar
      Barb
      Icon for Advisor rankAdvisor

      Point 5 is exactly what i need pretty please :)😇

  • This is fantastic thou alot more than i need in the moment to solve this little script. I have actually got it working - very pleased with myself indeed! 

    This script will change the name (aka hostname) of all devices in a group to the FQDN displayname.somedomain.local

    Sharing is caring :)

    Add your account info

    Change to your group ID 

    Change somedomain.local to your domain

    import requests
    import json
    import hashlib
    import base64
    import time
    import hmac
    
    
    # Account Info
    AccessId = ''
    AccessKey = ''
    Company = ''
    
    # Request Info - Change Group ID
    httpVerb = 'GET'
    resourcePath = '/device/groups/2474/devices'
    queryParam = '?fields=id,name,displayName'
    
    # Construct URL
    url = 'https://' + Company + '.logicmonitor.com/santaba/rest' + resourcePath + queryParam
    
    # Get current time in milliseconds
    epoch = str(int(time.time() * 1000))
    
    # Concatenate Request details
    requestVars = httpVerb + epoch + resourcePath
    
    # Construct signature
    hmac1 = hmac.new(AccessKey.encode(), msg=requestVars.encode(), digestmod=hashlib.sha256).hexdigest()
    signature = base64.b64encode(hmac1.encode())
    
    # Construct headers
    auth = 'LMv1 ' + AccessId + ':' + signature.decode() + ':' + epoch
    headers = {'Content-Type': 'application/json', 'Authorization': auth}
    
    # Make request
    response = requests.get(url, headers=headers)
    
    # Parse response
    jsonResponse = json.loads(response.content)
    
    # Loop through each device & update name field. Change somedomain.local
    for i in jsonResponse['data']['items']:
        deviceId = str(i['id'])
        deviceName = str(i['displayName']) + ".somedomain.local"
        nameChanged = '"' + deviceName + '"'
    
        # Request Info
        httpVerb = 'PATCH'
        resourcePath = '/device/devices/' + deviceId
        queryParams = '?patchFields=name'
        data = '{"name":' + nameChanged + '}'
        # Construct URL
        url = 'https://' + Company + '.logicmonitor.com/santaba/rest' + resourcePath + queryParams
    
        # Get current time in milliseconds
        epoch = str(int(time.time() * 1000))
    
        # Concatenate Request details
        requestVars = httpVerb + epoch + data + resourcePath
    
        # Construct signature
    hmac1 = hmac.new(AccessKey.encode(), msg=requestVars.encode(), digestmod=hashlib.sha256).hexdigest()
    signature = base64.b64encode(hmac1.encode())
    
    # Construct headers
    auth = 'LMv1 ' + AccessId + ':' + signature.decode() + ':' + epoch
    headers = {'Content-Type': 'application/json', 'Authorization': auth}
    
    # Make request
    response = requests.patch(url, data=data, headers=headers)
    print(response.content)
    print('Response:', '%s Property has been updated' % i)

     

  • Big ended use here...

    • Import devices from Nagios directly
    • Alert rules suppression
    • Duplicate esc chains
    • List all device props ie wmi.password ec

    List keeps going... But if i look at the code base we have 200+ scripts that use LM API one way or another

    One handy one which broke I have not had time to look was datasources -> github so we essentially had source control of everything in portal 

  • While you are on the scripts topic :) I'm needing some help please :) I'm trying to rename a group of devices to use FQDN on the name property and its not quiet working. deviceName gets the response i want, it returns 'server.somedomain.local' but when i try to use deviceName in the data variable value of "name" its not working it returns '("name":deviceName)' I know I'm missing something simple ??

    #Loop through each device & update name
    for i in jsonResponse['data']['items']:
        deviceId = str(i['id'])
        deviceName = str(i['displayName']) + ".somedommain.local"
    
        #Request Info
        httpVerb ='PATCH'
        resourcePath = '/device/devices/'+deviceId
        queryParams ='?patchFields=name'
        data = '("name":deviceName)'
    
        #Construct URL
        url = 'https://'+ Company +'.logicmonitor.com/santaba/rest' + resourcePath +queryParams