From 47487efeef071b50c881fe6888c2b84f34b1d3f1 Mon Sep 17 00:00:00 2001 From: Ismail Date: Thu, 23 Jun 2016 11:55:48 +0800 Subject: [PATCH 1/3] Clone from ashiina --- README.md | 2 +- handler.js | 2 +- sns_sample_event.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 28aa6cb..c0d23ba 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# lambda-cloudwatch-slack +# lambda-cloudwatch-slack Amazon Lambda to receive alerts from CloudWatch and post to Slack. ## What is This? diff --git a/handler.js b/handler.js index 79afc71..b5fd452 100644 --- a/handler.js +++ b/handler.js @@ -1,4 +1,4 @@ -console.log('[Amazon CloudWatch Notification]'); +console.log(' [Amazon CloudWatch Notification]'); /* configuration for each condition. diff --git a/sns_sample_event.js b/sns_sample_event.js index c63b9d5..5ff6a02 100644 --- a/sns_sample_event.js +++ b/sns_sample_event.js @@ -6,7 +6,7 @@ module.exports = { "EventSubscriptionArn": "arn:aws:sns:EXAMPLE", "Sns": { "Type": "Notification", - "MessageId": "xxxxx-xxx-xxx-xxxxxxxx", + "MessageId": "xxxxxx-xxx-xxx-xxxxxxxx", "TopicArn": "arn:aws:sns:EXAMPLE", "Subject": "ALARM: 'Elasticache-FreeableMemory-CRITICAL' in APAC - Tokyo", "Message": " {'AlarmName':'Elasticache-FreeableMemory-INFO','AlarmDescription':'Elasticache-FreeableMemory-INFO','AWSAccountId':'1234567890','NewStateValue':'ALARM','NewStateReason':'Threshold Crossed: 1 datapoint (8.60604416E8) was less than or equal to the threshold (1.0E9).','StateChangeTime':'1970-01-01T00:00:00.000+0000','Region':'APAC - Tokyo','OldStateValue':'OK','Trigger':{'MetricName':'FreeableMemory','Namespace':'AWS/ElastiCache','Statistic':'AVERAGE','Unit':null,'Dimensions':[],'Period':60,'EvaluationPeriods':1,'ComparisonOperator':'LessThanOrEqualToThreshold','Threshold':1.0E9}}", From 6fe437cbecf408f77b430a9b144457299ee7aa47 Mon Sep 17 00:00:00 2001 From: Ismail Al Jubbah Date: Wed, 29 Jun 2016 17:33:33 +0800 Subject: [PATCH 2/3] Add ALARM,OK code for AWS CloudWatch Add ALARM,OK code for AWS CloudWatch --- handler.js | 43 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/handler.js b/handler.js index b5fd452..d32c590 100644 --- a/handler.js +++ b/handler.js @@ -1,4 +1,4 @@ -console.log(' [Amazon CloudWatch Notification]'); +console.log('[Amazon CloudWatch Notification]'); /* configuration for each condition. @@ -6,23 +6,24 @@ console.log(' [Amazon CloudWatch Notification]'); */ var ALARM_CONFIG = [ { - condition: "INFO", - channel: "#test", - mention: " ", - color: "#FF9F21", + condition: "OK", + channel: "#cloudwatch", + mention: "<@here>", + color: "#21ff9f", severity: "INFO" }, { - condition: "CRITICAL", - channel: "#general", - mention: "<@channel> ", + condition: "ALARM", + channel: "#cloudwatch", + mention: "<@here>", color: "#F35A00", severity: "CRITICAL" } + ]; var SLACK_CONFIG = { - path: "YOUR_PATH", + path: "/YOUR_PATH", }; var http = require ('https'); @@ -41,10 +42,9 @@ exports.handler = function(event, context) { var color; // create post message - var alarmMessage = " *[Amazon CloudWatch Notification]* \n"+ - "Subject: "+subject+"\n"+ - "Message: "+message+"\n"+ - "Timestamp: "+timestamp; + var s1 = subject.split(' '); + var s2 = s1[1].split('awsroute53'); + var alarmMessage = "`"+s2[0]+"`"; // check subject for condition for (var i=0; i < ALARM_CONFIG.length; i++) { @@ -52,8 +52,12 @@ exports.handler = function(event, context) { console.log(row); if (subject.match(row.condition)) { console.log("Matched condition: "+row.condition); - - alarmMessage = row.mention+" "+alarmMessage+" "; + if(row.condition=="ALARM") + alarmMessage = row.mention+": "+alarmMessage+" "+" is not responding for the last 5 mins.\n"; + else if(row.condition=="OK") + alarmMessage = row.mention+": "+alarmMessage+" "+" is up now.\n"; + else + alarmMessage = alarmMessage; channel = row.channel; severity = row.severity; color = row.color; @@ -72,14 +76,7 @@ exports.handler = function(event, context) { "fallback": alarmMessage, "text": alarmMessage, "mrkdwn_in": ["text"], - "username": "AWS-CloudWatch-Lambda-bot", - "fields": [ - { - "title": "Severity", - "value": severity, - "short": true - } - ], + "username": "AWS-CloudWatch", "color": color } ], From 993b893476d689626d9e05a020b7cc5a3972f4bb Mon Sep 17 00:00:00 2001 From: Ismail Al Jubbah Date: Wed, 29 Jun 2016 17:35:01 +0800 Subject: [PATCH 3/3] Set code to ALARM --- sns_sample_event.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sns_sample_event.js b/sns_sample_event.js index 5ff6a02..cbf04c4 100644 --- a/sns_sample_event.js +++ b/sns_sample_event.js @@ -1,14 +1,14 @@ -module.exports = { - Records:[ +{ + "Records":[ { "EventSource": "aws:sns", "EventVersion": "1.0", "EventSubscriptionArn": "arn:aws:sns:EXAMPLE", "Sns": { "Type": "Notification", - "MessageId": "xxxxxx-xxx-xxx-xxxxxxxx", + "MessageId": "xxxxx-xxx-xxx-xxxxxxxx", "TopicArn": "arn:aws:sns:EXAMPLE", - "Subject": "ALARM: 'Elasticache-FreeableMemory-CRITICAL' in APAC - Tokyo", + "Subject": "ALARM: This-is-a-test-msg in APAC - Tokyo", "Message": " {'AlarmName':'Elasticache-FreeableMemory-INFO','AlarmDescription':'Elasticache-FreeableMemory-INFO','AWSAccountId':'1234567890','NewStateValue':'ALARM','NewStateReason':'Threshold Crossed: 1 datapoint (8.60604416E8) was less than or equal to the threshold (1.0E9).','StateChangeTime':'1970-01-01T00:00:00.000+0000','Region':'APAC - Tokyo','OldStateValue':'OK','Trigger':{'MetricName':'FreeableMemory','Namespace':'AWS/ElastiCache','Statistic':'AVERAGE','Unit':null,'Dimensions':[],'Period':60,'EvaluationPeriods':1,'ComparisonOperator':'LessThanOrEqualToThreshold','Threshold':1.0E9}}", "Timestamp": "1970-01-01T00:00:00.000Z", "SignatureVersion": "1", @@ -28,4 +28,4 @@ module.exports = { } } ] -}; +}