Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var url = require('url');
var https = require('https');
var config = require('./config');
var _ = require('lodash');
var escapeStringRegexp = require('escape-string-regexp');
var hookUrl;

var baseSlackMessage = {}
Expand Down Expand Up @@ -233,12 +234,31 @@ var handleCloudWatch = function(event, context) {
var region = event.Records[0].EventSubscriptionArn.split(":")[3];
var subject = "AWS CloudWatch Notification";
var alarmName = message.AlarmName;
var metricName = message.Trigger.MetricName;
var trigger = message.Trigger;
var alarmExpression;

if (typeof trigger.Metrics === 'object') {
// This is a CloudWatch metric math alarm. Instead of a MetricName and
// statistic there is an array of Metrics where the first element is the
// math expression. Need to process each metric in the list of Metrics
// and replace occurences of it in the alarm expression
alarmExpression = trigger.Metrics[0].Expression;
var triggerMetricsLength = trigger.Metrics.length;
for (var i = 1; i < triggerMetricsLength; i++) {
var metric = trigger.Metrics[i];
alarmExpression = alarmExpression.replace(
new RegExp(escapeStringRegexp(metric.Id), 'g'),
metric.MetricStat.Stat + ':' + metric.MetricStat.Metric.MetricName
);
}
} else {
// This is a standard CloudWatch alarm on a single metric
alarmExpression = trigger.Statistic + ":" + trigger.MetricName;
}
var oldState = message.OldStateValue;
var newState = message.NewStateValue;
var alarmDescription = message.AlarmDescription;
var alarmReason = message.NewStateReason;
var trigger = message.Trigger;
var color = "warning";

if (message.NewStateValue === "ALARM") {
Expand All @@ -257,8 +277,7 @@ var handleCloudWatch = function(event, context) {
{ "title": "Alarm Description", "value": alarmDescription, "short": false},
{
"title": "Trigger",
"value": trigger.Statistic + " "
+ metricName + " "
"value": alarmExpression + " "
+ trigger.ComparisonOperator + " "
+ trigger.Threshold + " for "
+ trigger.EvaluationPeriods + " period(s) of "
Expand Down Expand Up @@ -364,7 +383,7 @@ var processEvent = function(event, context) {
try {
eventSnsMessage = JSON.parse(eventSnsMessageRaw);
}
catch (e) {
catch (e) {
}

if(eventSubscriptionArn.indexOf(config.services.codepipeline.match_text) > -1 || eventSnsSubject.indexOf(config.services.codepipeline.match_text) > -1 || eventSnsMessageRaw.indexOf(config.services.codepipeline.match_text) > -1){
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "lambda-cloudwatch-slack",
"version": "0.3.0",
"description": "Better Slack notifications for AWS CloudWatch",
"authors": [
"authors": [
"Christopher Reichert <creichert07@gmail.com>",
"Cody Reichert <codyreichert@gmail.com>",
"Alexandr Promakh <s-promakh@ya.ru>"
Expand All @@ -12,6 +12,7 @@
},
"dependencies": {
"aws-sdk": "^2.4.0",
"escape-string-regexp": "^2.0.0",
"https": "^1.0.0",
"lodash": "^4.15.0",
"url": "^0.11.0"
Expand Down
3 changes: 2 additions & 1 deletion scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ $NODE_LAMBDA run -x test/context.json -j test/sns-codepipeline-event-stage-start
$NODE_LAMBDA run -x test/context.json -j test/sns-codepipeline-event-stage-succeeded.json
$NODE_LAMBDA run -x test/context.json -j test/sns-codepipeline-event-stage-failed.json
$NODE_LAMBDA run -x test/context.json -j test/sns-cloudwatch-event.json
$NODE_LAMBDA run -x test/context.json -j test/sns-cloudwatch-event-metricmath.json
$NODE_LAMBDA run -x test/context.json -j test/sns-event.json
$NODE_LAMBDA run -x test/context.json -j test/sns-elastic-beanstalk-event.json
$NODE_LAMBDA run -x test/context.json -j test/sns-codedeploy-event.json
$NODE_LAMBDA run -x test/context.json -j test/sns-codedeploy-configuration.json
$NODE_LAMBDA run -x test/context.json -j test/sns-elasticache-event.json
$NODE_LAMBDA run -x test/context.json -j test/sns-autoscaling-event.json
$NODE_LAMBDA run -x test/context.json -j test/sns-autoscaling-event.json
18 changes: 18 additions & 0 deletions test/sns-cloudwatch-event-metricmath.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"Records": [
{
"EventSource": "aws:sns",
"EventVersion": "1.0",
"EventSubscriptionArn": "arn:aws:sns:us-east-1:123456789123:cloudwatch-alarms:00000000-0000-0000-0000-000000000000",
"Sns": {
"Type": "Notification",
"MessageId": "00000000-0000-0000-0000-000000000000",
"TopicArn": "arn:aws:sns:us-east-1:123456789123:cloudwatch-alarms",
"Subject": "ALARM: \"Sample Metric Math Alert\" in US East (N. Virginia)",
"Message": "{\"AlarmName\":\"Sample Metric Math Alert\",\"AlarmDescription\":null,\"AWSAccountId\":\"946184294613\",\"NewStateValue\":\"ALARM\",\"NewStateReason\":\"Threshold Crossed: 1 out of the last 1 datapoints [8133.333333333447 (07/08/19 18:43:00)] was greater than the threshold (1000.0) (minimum 1 datapoint for OK -> ALARM transition).\",\"StateChangeTime\":\"2019-08-07T18:45:24.269+0000\",\"Region\":\"US East (N. Virginia)\",\"OldStateValue\":\"OK\",\"Trigger\":{\"Period\":60,\"EvaluationPeriods\":1,\"ComparisonOperator\":\"GreaterThanThreshold\",\"Threshold\":1000.0,\"TreatMissingData\":\"- TreatMissingData: missing\",\"EvaluateLowSampleCountPercentile\":\"\",\"Metrics\":[{\"Expression\":\"m1*100*m2 - MIN(m1)\",\"Id\":\"e2\",\"Label\":\"Expression2\",\"ReturnData\":true},{\"Id\":\"m1\",\"MetricStat\":{\"Metric\":{\"Dimensions\":[{\"value\":\"i-0a5c8ea70646e7b05\",\"name\":\"InstanceId\"}],\"MetricName\":\"CPUUtilization\",\"Namespace\":\"AWS/EC2\"},\"Period\":60,\"Stat\":\"Average\"},\"ReturnData\":false},{\"Id\":\"m2\",\"MetricStat\":{\"Metric\":{\"Dimensions\":[{\"value\":\"i-0a5c8ea70646e7b05\",\"name\":\"InstanceId\"}],\"MetricName\":\"NetworkOut\",\"Namespace\":\"AWS/EC2\"},\"Period\":60,\"Stat\":\"Average\"},\"ReturnData\":false}]}}",
"Timestamp": "2019-08-07T18:45:24.355Z",
"MessageAttributes": {}
}
}
]
}