7 years ago
Script to rename devices
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.id)
// Use regex to get the system property you want, here I wanted sysname
sysName = items.systemProperties =~/(?<=sysname, value:)([^\]]*)/
items.displayName = sysName[0][0]
items = JsonOutput.toJson(items)
StringEntity params = new StringEntity(items,ContentType.APPLICATION_JSON);
println items
epoch = System.currentTimeMillis(); //get current time
resourcePath = "/device/devices/" + deviceID
requestVars = "PUT" + epoch + items + 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();