forked from alertlogic/al-aws-collector-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatistics_templates.js
136 lines (122 loc) · 4.03 KB
/
statistics_templates.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* -----------------------------------------------------------------------------
* @copyright (C) 2018, Alert Logic, Inc
* @doc
*
* Statistics functions - to be used from the collectors code,
* as base for custom statistics funs
*
* @end
* -----------------------------------------------------------------------------
*/
const AWS = require('aws-sdk');
const async = require('async');
const moment = require('moment');
const m_alAws = require('./al_aws');
const AWS_STATISTICS_PERIOD_MINUTES = 15;
const MAX_ERROR_MSG_LEN = 1024;
/**
* @typedef {Object} Stat
* @property {string} Label - status of healtcheck (ok, warning, error).
* @property {array} Datapoints - array of datapoints.
*
*/
/**
* @typedef {Object} StatError
* @property {string} Label - status of healtcheck (ok, warning, error).
* @property {string} StatisticsError - description of error happened during getting statistic
*
*/
/**
* All custom statistic funs should follow the same interface as this function.
*
* @function
*
* @param {function} callback - callback, which is called by health check when it's done.
*
* @returns {function} callback(err, result)
*
* {Error} err - The Error object if an error occurred, null otherwise
* {Stat|StatError} result - The Stat object, or StatError if error occured
*
*/
// function customStatFunExample(callback) {
// return callback(null, {
// Label : 'CustomLabel',
// Datapoints : [
// {'Timestamp':'2017-11-21T16:40:00Z','Sum':1,'Unit':'Count'}
// ]
// });
// }
var getMetricStatistics = function (params, callback) {
var cloudwatch = new AWS.CloudWatch({apiVersion: '2010-08-01'});
cloudwatch.getMetricStatistics(params, function(err, data) {
if (err) {
return callback(null, {
Label: params.MetricName,
StatisticsError: JSON.stringify(err).slice(0, MAX_ERROR_MSG_LEN)
});
} else {
return callback(null, {
Label: data.Label,
Datapoints: data.Datapoints
});
}
});
};
var getLambdaMetrics = function (functionName, metricName, callback) {
var params = {
Dimensions: [
{
Name: 'FunctionName',
Value: functionName
}
],
MetricName: metricName,
Namespace: 'AWS/Lambda',
Statistics: ['Sum'],
StartTime: moment().subtract(AWS_STATISTICS_PERIOD_MINUTES, 'minutes').toISOString(),
EndTime: new Date(),
Period: 60*AWS_STATISTICS_PERIOD_MINUTES /* 15 mins as seconds */
};
return getMetricStatistics(params, callback);
};
var getKinesisMetrics = function (streamName, metricName, callback) {
var params = {
Dimensions: [
{
Name: 'StreamName',
Value: streamName
}
],
MetricName: metricName,
Namespace: 'AWS/Kinesis',
Statistics: ['Sum'],
StartTime: moment().subtract(AWS_STATISTICS_PERIOD_MINUTES, 'minutes').toISOString(),
EndTime: new Date(),
Period: 60*AWS_STATISTICS_PERIOD_MINUTES /* 15 mins as seconds */
};
return getMetricStatistics(params, callback);
};
var getAllKinesisMetricsFuns = function(streamName) {
[
function(callback) {
return getKinesisMetrics(streamName, 'IncomingRecords', callback);
},
function(callback) {
return getKinesisMetrics(streamName, 'IncomingBytes', callback);
},
function(callback) {
return getKinesisMetrics(streamName, 'ReadProvisionedThroughputExceeded', callback);
},
function(callback) {
return getKinesisMetrics(streamName, 'WriteProvisionedThroughputExceeded', callback);
}
]
}
module.exports = {
getMetricStatistics : getMetricStatistics,
getLambdaMetrics : getLambdaMetrics,
getKinesisMetrics : getKinesisMetrics,
// functions which return funs:
getAllKinesisMetricsFuns : getAllKinesisMetricsFuns
};