• 0
Sign in to follow this  
MrTK

Script to rename devices

Question

This script makes the devices displayName the same as the sysName. It can be easily modified to update any property for any other property, or appending.  We use a couple different variations of this script to keep our standard naming conventions consistent throughout LogicMonitor (and our CMDB).

Made for API v1, I have not looked at how this might be different/easier with v2.  Just sharing in case others find it useful.  

Script was adapted from a script example @Chengjie Zhu posted.  

 

import org.apache.http.client.methods.HttpGet
import org.apache.http.client.methods.HttpPut
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;
import org.apache.commons.codec.binary.Hex;
import groovy.json.*;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.ContentType;


//define credentials and url
def accessId = '##accessid##';
def accessKey = '##accesskey##';
def account = '##subdomain##';
// Change GroupID

def GroupID = '3919'
def resourcePath = "/device/groups/" + GroupID+ "/devices"
def queryParams = '?filter=systemProperties.name:system.sysname&size=200'
def url = "https://" + account + ".logicmonitor.com" + "/santaba/rest" + resourcePath + queryParams
def deviceID = []
//def sysName = []

//get current time
epoch = System.currentTimeMillis();

//calculate signature
requestVars = "GET" + epoch + 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();

// HTTP Get
CloseableHttpClient httpclient = HttpClients.createDefault();
httpGet = new HttpGet(url);
httpGet.addHeader("Authorization" , "LMv1 " + accessId + ":" + signature + ":" + epoch);
response = httpclient.execute(httpGet);
responseBody = EntityUtils.toString(response.getEntity());

JsonSlurper slurper = new JsonSlurper()
def json = slurper.parseText(responseBody)
def items = json.data.items

// Iterate all of devices in the parent group
// Please note: It does not work on the group's child groups.

for (i=0; i<items.size(); i++){
    deviceID.add(items[i].id)
    // Use regex to get the system property you want, here I wanted sysname
    sysName = items[i].systemProperties =~/(?<=sysname, value:)([^\]]*)/
    items[i].displayName = sysName[0][0]
    items[i] = JsonOutput.toJson(items[i])
    StringEntity params = new StringEntity(items[i],ContentType.APPLICATION_JSON);
    println items[i]
    epoch = System.currentTimeMillis(); //get current time
    resourcePath =  "/device/devices/" + deviceID[i]
    requestVars = "PUT" + epoch +  items[i] + resourcePath;

    // Construct signature
    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();

    // HTTP PUT
    url = "https://" + account + ".logicmonitor.com" + "/santaba/rest" + resourcePath;
    httpPut = new HttpPut(url);
    httpPut.addHeader("Authorization" , "LMv1 " + accessId + ":" + signature + ":" + epoch);
    httpPut.setHeader("Accept", "application/json");
    httpPut.setHeader("Content-type", "application/json");
    httpPut.setEntity(params);
    responsePut = httpclient.execute(httpPut);
    responseBodyPut = EntityUtils.toString(responsePut.getEntity());
    codePut = responsePut.getStatusLine().getStatusCode();

    // Print Response
    println "Status:" + codePut;
    println "Response body:" + responseBodyPut;
}

httpclient.close();

 

Share this post


Link to post
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this