forked from alertlogic/al-aws-collector-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhealth_checks.js
112 lines (100 loc) · 3.27 KB
/
health_checks.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
/* -----------------------------------------------------------------------------
* @copyright (C) 2018, Alert Logic, Inc
* @doc
*
* Halth check functions common for all collectors
*
* @end
* -----------------------------------------------------------------------------
*/
const AWS = require('aws-sdk');
const async = require('async');
/**
* checks status of CF, returns error in case if it's in failed state, returns error.
* All custom healthchecks should follow the same interface as this function.
*
* @function
*
* @param {Object} event - checkin event for Lambda function, can be used to send needed parameters for health checks. Example: stackName.
* @param {Object} context - context of Lambda's function.
* @param {function} callback - callback, which is called by health check when it's done.
*
* @returns {function} callback(err)
*
* {ErrorMsg} err - The ErrorMsg object if an error occurred, null otherwise.
*
*/
function checkCloudFormationStatus(stackName, callback) {
var cloudformation = new AWS.CloudFormation();
cloudformation.describeStacks({StackName: stackName}, function(err, data) {
if (err) {
return callback(errorMsg('ALAWS00001', stringify(err)));
} else {
var stackStatus = data.Stacks[0].StackStatus;
if (stackStatus === 'CREATE_COMPLETE' ||
stackStatus === 'UPDATE_COMPLETE' ||
stackStatus === 'UPDATE_IN_PROGRESS' ||
stackStatus === 'UPDATE_ROLLBACK_COMPLETE' ||
stackStatus === 'UPDATE_ROLLBACK_IN_PROGRESS' ||
stackStatus === 'UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS' ||
stackStatus === 'REVIEW_IN_PROGRESS') {
return callback(null);
} else {
return callback(errorMsg('ALAWS00002', 'CF stack has wrong status: ' + stackStatus));
}
}
});
}
function getHealthStatus(context, customChecks, callback) {
async.parallel([
function(asyncCallback) {
checkCloudFormationStatus(process.env.stack_name, asyncCallback);
}
].concat(customChecks),
function(errMsg) {
var status = {};
if (errMsg) {
console.warn('ALAWS00001 Health check failed with', errMsg);
status = {
status: errMsg.status,
error_code: errMsg.code,
details: [errMsg.details]
};
} else {
status = {
status: 'ok',
details: []
};
}
return callback(null, status);
});
}
function stringify(jsonObj) {
return JSON.stringify(jsonObj, null, 0);
}
/**
* @typedef {Object} ErrorMsg
* @property {string} status - status of healtcheck (ok, warning, error).
* @property {string} code - unique error code.
* @property {string} details - description of particular error.
*
*/
/**
* @function
*
* @param {string} code - error code
* @param {string} message - error message
*
* @returns {ErrorMsg} returns error message object
*/
function errorMsg(code, message) {
return {
status: 'error',
code: code,
details: message
};
}
module.exports = {
getHealthStatus : getHealthStatus,
errorMsg : errorMsg
};