Forum Discussion

Mosh's avatar
Mosh
Icon for Professor rankProfessor
8 years ago

ConfigSource for Alert Rules Configuration

There is no way to backup Alert Rules configuration in LogicMonitor so I came up with this ConfigSource for LMConfig to keep track of configuration changes to Alert Rules.
 

Groovy Script

/*

  ConfigSource 

  Retrieves and stores the JSON document for alert rules.

  Define the following custom properties on a collector object:

		api.accessId
		api.accessKey
		api.accountName


  Mosh Jahán 
  Rentokil Initial
  May 2017

*/

import org.apache.http.HttpEntity
import org.apache.http.client.methods.CloseableHttpResponse
import org.apache.http.client.methods.HttpGet
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;

def debugMode = false;

def apiAccessId;
def apiAccessKey;
def apiAccountName;


if (debugMode == false)
{
	try 
	{
	  apiAccessId = hostProps.get("api.accessId");
	}
	catch (all)
	{
	  println "(debug::fatal) Missing custom property api.accessId";
	  return 1;
	}

	try 
	{
	  apiAccessKey = hostProps.get("api.accessKey");
	}
	catch (all)
	{
	  println "(debug::fatal) Missing custom property api.accessKey";
	  return 1;
	}

	try 
	{
	  apiAccountName = hostProps.get("api.accountName");
	}
	catch (all)
	{
	  println "(debug::fatal) Missing custom property api.accountName";
	  return 1;
	}
}
else
{
  // Hardcode values for debug here

	apiAccessId = "";
	apiAccessKey = "";
	apiAccountName = "";	
}


def resourcePath = "/setting/alert/rules";

def url = "https://" + apiAccountName + ".logicmonitor.com" + "/santaba/rest" + resourcePath;


// Calculate signature

epoch = System.currentTimeMillis();

requestVars = "GET" + epoch + resourcePath;

hmac = Mac.getInstance("HmacSHA256");

secret = new SecretKeySpec(apiAccessKey.getBytes(), "HmacSHA256");

hmac.init(secret);

hmacSigned = Hex.encodeHexString(hmac.doFinal(requestVars.getBytes()));

signature = hmacSigned.bytes.encodeBase64();


// HTTP GET

CloseableHttpClient httpclient = HttpClients.createDefault();

httpGet = new HttpGet(url);

httpGet.addHeader("Authorization" , "LMv1 " + apiAccessId + ":" + signature + ":" + epoch);

response = httpclient.execute(httpGet);

responseBody = EntityUtils.toString(response.getEntity());
statusCode = response.getStatusLine().getStatusCode();


// Output response

if (statusCode == 200)
{
  println "AlertRulesJSON:" + responseBody;
}
else
{
	println "Request failed " + statusCode;
}

httpclient.close();

return 0;

3 Replies

Replies have been turned off for this discussion
  • @Mosh thx for the submission.

    Instead of posting the entire source code, so that others need to copy/paste it all, submit it to the LM Exchange and post the locator ID here.

    Makes it way easier to share.