-
-
Save Kurukshetran/0e0b5e9e44c15af9b3f57e6c42ca71c5 to your computer and use it in GitHub Desktop.
AMQ Monitoring and Email Alerts script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import base64,json | |
import urllib2 | |
import smtplib | |
from email.mime.text import MIMEText | |
from AMQMOnitorConf import global_conf | |
import logging | |
import socket | |
logging.basicConfig(filename='AMQMonitor.log', level=logging.INFO,format='%(asctime)s - %(levelname)s - %(message)s') | |
#Sends Email | |
def send_mail(resourceDetailsJson,alert): | |
alertBodyTemplate="Hi Team,\nThe AMQ instance on %s requires attention.\nBelow resource has raised alert:\nResource Name:%s\nThreshold Value:%s\nCurrent Value:%s\nThanks & Regards\nAMQ Monitor script"; | |
currentVal=resourceDetailsJson["value"][alert["param"]] | |
if(alert["condition"]=="percent"): | |
percentAgainstVal=resourceDetailsJson["value"][alert["percentageAgainstParam"]] | |
percentVal=(float(currentVal)/percentAgainstVal)*100 | |
currentVal=str(percentVal)+ "%"; | |
alertBody=alertBodyTemplate%(socket.gethostname(),alert["param"],alert["thresholdVal"],currentVal) | |
print(alertBody) | |
msg=MIMEText(alertBody) | |
msg['from']=socket.gethostname()+"@yourdomain.com" | |
msg['subject']= "AMQ Alert:"+socket.gethostname() | |
msg["to"]=alert["emailList"] | |
s=smtplib.SMTP("localhost") | |
s.sendmail(socket.gethostname()+"@yourdomain.com",alert["emailList"].split(",") , msg.as_string()) | |
logging.info("Alert Email sent") | |
s.quit(); | |
return; | |
#Condition checks current value and checks if value is within specified threshold as per conditions | |
def condition_check(resourceDetailsJson,alert): | |
logging.info("Checking alert condition "+str(alert)) | |
paramVal=resourceDetailsJson["value"][alert["param"]] | |
if alert["condition"]=="gt": | |
if(paramVal>alert["thresholdVal"]): | |
return True; | |
elif alert["condition"]=="lt": | |
if(paramVal<alert["thresholdVal"]): | |
return True; | |
elif alert["condition"]=="eq": | |
if(paramVal==alert["thresholdVal"]): | |
return True; | |
elif alert["condition"]=="percent": | |
percentAgainstVal=resourceDetailsJson["value"][alert["percentageAgainstParam"]] | |
percentVal=(float(paramVal)/percentAgainstVal)*100 | |
logging.info("Param Val: %s Percent against val: %s Percentage:%s"%(paramVal,percentAgainstVal,percentVal)) | |
if(percentVal>float(alert["thresholdVal"])): | |
return True; | |
else: | |
logging.error("Please speicify correct condition for alert "+ str(alert)); | |
return False; | |
#Calls the JolokiaURL and adds BasicAuth header to every reqest and dumps data as JSON | |
def read_request(requestURL): | |
base64string = base64.b64encode('%s:%s' % (global_conf["jolokiaUserName"], global_conf["jolokiaPassword"])) | |
opener=urllib2.build_opener() | |
opener.addheaders=[("Authorization", "Basic %s" % base64string)] | |
try: | |
readRequest=opener.open(requestURL) | |
if(readRequest.getcode()!=200): | |
logging.warning("Request Did not complete normaly for"+ requestURL); | |
readData=readRequest.read() | |
jsonData=json.loads(readData); | |
return jsonData; | |
except urllib2.HTTPError as e: | |
if e.code== 404: | |
logging.error("Check for correct URL") | |
return None | |
if e.code==401 or e.code==403: | |
logging.error("Check jolokiaUsername and jolokiaPassword ") | |
return None | |
except urllib2.URLError as u: | |
logging.error("There was error connecting to broker"); | |
logging.error("Please check if broker is up") | |
return None | |
#Script execution starts here | |
jsondata=read_request(global_conf["jolokiaBase"]) | |
memoryJson=read_request(global_conf["jolokiaBase"]+"/read/java.lang:type=Memory/HeapMemoryUsage") | |
if(memoryJson==None): | |
detailsJson={"value":{"Broker Connectivity":"DOWN","currentVal":"DOWN"}} | |
brokerDownAlert={"param":"Broker Connectivity","condition":"available","thresholdVal":"UP","emailList":"[email protected]"} | |
send_mail(detailsJson,brokerDownAlert) | |
exit(); | |
for alert in global_conf["jvmAlerts"]: | |
if(condition_check(memoryJson,alert)): | |
send_mail(memoryJson,alert) | |
for queueName,alertsArray in global_conf["queueAlert"].iteritems(): | |
logging.info("Fetching details for Queue:"+queueName) | |
queueURL=global_conf["jolokiaBase"]+"/read/org.apache.activemq:type=Broker,brokerName="+global_conf["brokerName"]+",destinationType=Queue,destinationName="+queueName | |
queueDetailsJson=read_request(queueURL) | |
if queueDetailsJson["status"]!=200: | |
logging.error("Queue does not exist") | |
else: | |
for alert in alertsArray: | |
if(condition_check(queueDetailsJson,alert)): | |
send_mail(queueDetailsJson,alert) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Replace localhost with your servername | |
#Change username and password accordingly | |
#Alert can be applied on JVM Heap and Queue parameters | |
#Alert format is | |
# param=Name of parameter on whose value you want to put alert | |
# thresholdVal= Value againsts which current value of param will be evaluated as per condition | |
# condition= denotes how to compare thresholdVal and current value of parameter. | |
# Possible values are as below: | |
# lt - Alert will be raised if current value is less than thresholdVal | |
# gt - Alert will be raised if current value is greater than thresholdVal | |
# eq - Alert will be raised if current value is equal to thresholdVal | |
# percent - Alert will be raised if current value percenatge are greater than thresholdVal | |
# For this condition one extra parameter is required in alert configuration perecntAgainstParam. This is value which | |
# will be used to calculate percentage on current value. For example to calculate percent usage of JVM heap param will be used | |
# percentAgainstParam will be max and percentage usage will be calculated like (param/percentAgainstParam)*100 | |
# emailList= comma separated list of emails to notify on alert | |
#JVMAlerts: Alerts in this section can be used for Heap memory usage | |
# Possible parameters are as below | |
# used: int | |
# init: int | |
# max:int | |
# commited:int | |
#QueueAlerts: Alerts used to monitor individual queue parameters | |
# Possible values are as below: | |
# MemoryUsageByteCount: int, | |
# ProducerCount: int, | |
# UseCache: boolean, | |
# CursorMemoryUsage: int | |
# DLQ: boolean, | |
# ProducerFlowControl: boolean, | |
# MaxAuditDepth: int, | |
# MaxPageSize: int, | |
# AlwaysRetroactive: boolean, | |
# MemoryPercentUsage: float, | |
# PrioritizedMessages: boolean, | |
# MaxEnqueueTime: int, | |
# MemoryLimit: int, | |
# CursorFull: false, | |
# Paused: boolean, | |
# MemoryUsagePortion: int, | |
# InFlightCount: int, | |
# Options: string, | |
# MinMessageSize: int, | |
# AverageBlockedTime: int, | |
# Name: string, | |
# MaxProducersToAudit: int, | |
# ExpiredCount: int, | |
# ForwardCount: int, | |
# CursorPercentUsage: int, | |
# BlockedSends: int, | |
# AverageMessageSize: int, | |
# EnqueueCount: int, | |
# MaxMessageSize: int, | |
# ConsumerCount: int | |
global_conf={ | |
"jolokiaBase":"http://localhost:8161/api/jolokia", | |
"jolokiaUserName":"admin", | |
"jolokiaPassword":"admin", | |
"brokerName":"localhost", | |
"brokerDownAlertEmail":"[email protected]", | |
"jvmAlerts":[ | |
{"param":"used","thresholdVal":"0.2","condition":"percent","emailList":"[email protected]","percentageAgainstParam":"max"} | |
], | |
"queueAlert":{ | |
"cbr.queue":[ | |
{"param":"ConsumerCount","thresholdVal":200,"condition":"lt","emailList":"[email protected]"}, | |
{"param":"QueueSize","thresholdVal":1,"condition":"eq","emailList":"[email protected]"} | |
], | |
"queue2":[] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment