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
<span style="font-size:10px;"><span style="color:#008000;">/*</span>
<span style="color:#008000;"> ConfigSource </span>
<span style="color:#008000;"> Retrieves and stores the JSON document for alert rules.</span>
<span style="color:#008000;"> Define the following custom properties on a collector object:</span>
<span style="color:#008000;"> api.accessId</span>
<span style="color:#008000;"> api.accessKey</span>
<span style="color:#008000;"> api.accountName</span>
<span style="color:#008000;"> Mosh Jahán </span>
<span style="color:#008000;"> Rentokil Initial</span>
<span style="color:#008000;"> May 2017</span>
<span style="color:#008000;">*/</span>
<span style="color:#0000ff;">import</span> org.apache.http.HttpEntity
<span style="color:#0000ff;">import</span> org.apache.http.client.methods.CloseableHttpResponse
<span style="color:#0000ff;">import</span> org.apache.http.client.methods.HttpGet
<span style="color:#0000ff;">import</span> org.apache.http.impl.client.CloseableHttpClient
<span style="color:#0000ff;">import</span> org.apache.http.impl.client.HttpClients
<span style="color:#0000ff;">import</span> org.apache.http.util.EntityUtils
<span style="color:#0000ff;">import</span> javax.crypto.Mac;
<span style="color:#0000ff;">import</span> javax.crypto.spec.SecretKeySpec;
<span style="color:#0000ff;">import</span> org.apache.commons.codec.binary.Hex;
<span style="color:#2b91af;">def</span> debugMode = <span style="color:#0000ff;">false</span>;
<span style="color:#2b91af;">def</span> apiAccessId;
<span style="color:#2b91af;">def</span> apiAccessKey;
<span style="color:#2b91af;">def</span> apiAccountName;
<span style="color:#0000ff;">if</span> (debugMode == <span style="color:#0000ff;">false</span>)
{
<span style="color:#0000ff;">try</span>
{
apiAccessId = hostProps.get(<span style="color:#a31515;">"api.accessId"</span>);
}
<span style="color:#0000ff;">catch</span> (all)
{
println <span style="color:#a31515;">"(debug::fatal) Missing custom property api.accessId"</span>;
<span style="color:#0000ff;">return</span> 1;
}
<span style="color:#0000ff;">try</span>
{
apiAccessKey = hostProps.get(<span style="color:#a31515;">"api.accessKey"</span>);
}
<span style="color:#0000ff;">catch</span> (all)
{
println <span style="color:#a31515;">"(debug::fatal) Missing custom property api.accessKey"</span>;
<span style="color:#0000ff;">return</span> 1;
}
<span style="color:#0000ff;">try</span>
{
apiAccountName = hostProps.get(<span style="color:#a31515;">"api.accountName"</span>);
}
<span style="color:#0000ff;">catch</span> (all)
{
println <span style="color:#a31515;">"(debug::fatal) Missing custom property api.accountName"</span>;
<span style="color:#0000ff;">return</span> 1;
}
}
<span style="color:#0000ff;">else</span>
{
<span style="color:#008000;">// Hardcode values for debug here</span>
apiAccessId = <span style="color:#a31515;">""</span>;
apiAccessKey = <span style="color:#a31515;">""</span>;
apiAccountName = <span style="color:#a31515;">""</span>;
}
<span style="color:#2b91af;">def</span> resourcePath = <span style="color:#a31515;">"/setting/alert/rules"</span>;
<span style="color:#2b91af;">def</span> url = <span style="color:#a31515;">"https://"</span> + apiAccountName + <span style="color:#a31515;">".logicmonitor.com"</span> + <span style="color:#a31515;">"/santaba/rest"</span> + resourcePath;
<span style="color:#008000;">// Calculate signature</span>
epoch = System.currentTimeMillis();
requestVars = <span style="color:#a31515;">"GET"</span> + epoch + resourcePath;
hmac = Mac.getInstance(<span style="color:#a31515;">"HmacSHA256"</span>);
secret = <span style="color:#0000ff;">new</span> SecretKeySpec(apiAccessKey.getBytes(), <span style="color:#a31515;">"HmacSHA256"</span>);
hmac.init(secret);
hmacSigned = Hex.encodeHexString(hmac.doFinal(requestVars.getBytes()));
signature = hmacSigned.bytes.encodeBase64();
<span style="color:#008000;">// HTTP GET</span>
CloseableHttpClient httpclient = HttpClients.createDefault();
httpGet = <span style="color:#0000ff;">new</span> HttpGet(url);
httpGet.addHeader(<span style="color:#a31515;">"Authorization"</span> , <span style="color:#a31515;">"LMv1 "</span> + apiAccessId + <span style="color:#a31515;">":"</span> + signature + <span style="color:#a31515;">":"</span> + epoch);
response = httpclient.execute(httpGet);
responseBody = EntityUtils.toString(response.getEntity());
statusCode = response.getStatusLine().getStatusCode();
<span style="color:#008000;">// Output response</span>
<span style="color:#0000ff;">if</span> (statusCode == 200)
{
println <span style="color:#a31515;">"AlertRulesJSON:"</span> + responseBody;
}
<span style="color:#0000ff;">else</span>
{
println <span style="color:#a31515;">"Request failed "</span> + statusCode;
}
httpclient.close();
<span style="color:#0000ff;">return</span> 0;</span>

Mosh
Posted 9 years ago·Last reply 9 years ago
3 comments
Andrey_Kitsen
·9 years agoPerfect.
Mosh
OP9 years agoLM Exchange Code: JRYWGC
Andrey_Kitsen
·9 years ago@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.