Forum Discussion

marting's avatar
3 years ago

NetScans - Advanced naming of discovered devices??

When creating/modifying a NetScan: are there any options for regex or anything like that within the Rename Discovered Devices field?

Hoping to use the ##REVERSEDNS## but strip out the domain name (i.e. end up with hostname instead of hostname.domain.local)

For bonus points, would like to capitalize the name as well but that's icing.

 

1 Reply

  • Anonymous's avatar
    Anonymous

    Not that i'm aware of. You could pretty easily write a property source to do this though. This property source does something similar, it takes the snmp obtained sysName and makes it the display name:

    /*******************************************************************************
     * © 2007-2019 - LogicMonitor, Inc. All rights reserved.
     ******************************************************************************/
    import com.santaba.agent.groovyapi.snmp.Snmp
    import groovy.json.JsonOutput
    import groovy.json.JsonSlurper
    import org.apache.commons.codec.binary.Hex
    import org.apache.http.client.methods.*
    import org.apache.http.entity.ContentType
    import org.apache.http.entity.StringEntity
    import org.apache.http.impl.client.CloseableHttpClient
    import org.apache.http.impl.client.HttpClients
    import org.apache.http.util.EntityUtils
    import javax.crypto.Mac
    import javax.crypto.spec.SecretKeySpec
    
    lm_id = hostProps.get("weenig.displayName.lmaccess.id")
    lm_key = hostProps.get("weenig.displayName.lmaccess.key")
    lm_account = hostProps.get("weenig.displayName.lmaccount")
    if (!(lm_id) || !(lm_key) || !(lm_account)){
      println("Missing API credentials.")
      return 3
    }
    
    device_id = hostProps.get("system.deviceId")
    curr_name = hostProps.get("system.displayName")
    try {
      sysname = hostProps.get("system.sysname")
    } catch (Exception ex) {return 2}
    
    def LMAPI(String _verb, _accessId, _accessKey, _account, GString _resourcePath, String _queryParameters, String _data){
        responseDict = [:]
        url = 'https://' + _account + '.logicmonitor.com' + '/santaba/rest' + _resourcePath + _queryParameters
        StringEntity params = new StringEntity(_data, ContentType.APPLICATION_JSON)
        epoch = System.currentTimeMillis()
        requestVars = _verb + epoch + _data + _resourcePath
        hmac = Mac.getInstance('HmacSHA256')
        secret = new SecretKeySpec(_accessKey.getBytes(), 'HmacSHA256')
        hmac.init(secret)
        hmac_signed = Hex.encodeHexString(hmac.doFinal(requestVars.getBytes()))
        signature = hmac_signed.bytes.encodeBase64()
        if (_verb == 'PATCH'){
            CloseableHttpClient httpclient = HttpClients.createDefault()
            http_request = new HttpPatch(url)
            http_request.addHeader("Authorization", "LMv1 " + _accessId + ":" + signature + ":" + epoch)
            http_request.setHeader("Accept", "application/json")
            http_request.setHeader("Content-type", "application/json")
            http_request.setHeader("X-Version", "2")
            http_request.setEntity(params)
            response = httpclient.execute(http_request)
            responseBody = EntityUtils.toString(response.getEntity())
            code = response.getStatusLine().getStatusCode()
        }
        responseDict['code'] = code ?: null
        responseDict['body'] = responseBody ?: null
        return responseDict
    }
    
    if (curr_name != sysname) {
        resourcePath = "/device/devices/${device_id}"
        queryParameters = "?patchFields=displayName"
        data = JsonOutput.toJson([displayName: sysname])
        result = LMAPI("PATCH", lm_id, lm_key, lm_account, resourcePath, queryParameters, data)
        println("displayName.from=system.sysname")
        if (result.code.toString() != '200')
        {
            errorMessage = new JsonSlurper().parseText(result.body)['errorMessage']
            println("Error THX-1138 occurred. See http://bitly.com/98K8eH for more details.\nFailed to update/set \"displayName\" (http.${result.code.toString()}): ${errorMessage}")
            return 1
        }
    }
    
    return 0