-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
182 lines (143 loc) · 3.75 KB
/
index.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
exports.isMonitoringModule = true;
exports.hasCron = true;
exports.snapshotData = true;
var async = require('async');
var responseMessaging = require('monitor-response');
var pingdomApi = null;
var PingdomCheck = null;
var pingdomChecks = [];
/**
* Module config:
*
* "monitor-pingdom" : {
* "user": "",
* "pass": "",
* "appkey": "",
* "cronTime": ""
* }
*/
// Tables this module need to function
exports.tables = [
{
name: 'pingdomChecks',
index: [
'monitorClientId',
'delete'
]
}
];
exports.init = function (db) {
pingdomApi = require('pingdom-api')(this.config);
PingdomCheck = require('./models/pingdomCheck');
PingdomCheck.init(db);
}
exports.executeCron = function (cb) {
// Reset
pingdomChecks = [];
var actions = [];
// mark existing pingdomChecks toDelete (afterwards deleting old pingdomChecks)
actions.push(PingdomCheck.markToDelete());
// Get all pingdomChecks
actions.push(getChecks());
// Insert/update all pingdomChecks
actions.push(insertOrUpdatePingdomChecks(cb));
// Execute all functions in 'actions' array
async.waterfall(
actions,
function(err, result){
if(err) {
// console.log(err);
} else {
// console.log('DONE');
// Delete old pingdomChecks
PingdomCheck.deleteMarked();
}
}
);
}
getChecks = function() {
return function(callback) {
pingdomApi.getChecks()
.spread(function(checks, response){
pingdomChecks = pingdomChecks.concat(checks);
return callback(null);
})
.catch(function(err){
return callback(err);
});
}
}
// insert or update all pingdomChecks
insertOrUpdatePingdomChecks = function(cb) {
return function(callBack) {
var checkActions = [];
// Insert each instance
for(var i in pingdomChecks) {
checkActions.push(insertOrUpdatePingdomCheck(pingdomChecks[i], cb));
}
// Execute all functions in 'checkActions' array
async.waterfall(
checkActions,
function(err, result){
if(err)
callBack(err);
else
callBack(null);
}
);
}
}
// insert or update loadBalancer
insertOrUpdatePingdomCheck = function(check, cb) {
return function(callback) {
PingdomCheck.insertOrUpdateChecks(check.id, check, function(err, pingdomCheck){
if(err) callback(err);
else {
// PindomCheck is linked to monitorClient -> create moduleData and callback
if(pingdomCheck.monitorClientId && pingdomCheck.monitorClientId != "") {
var monitorClientId = pingdomCheck.monitorClientId;
delete pingdomCheck.monitorClientId;
var moduleData = {
monitorClientId: monitorClientId,
data: pingdomCheck
};
cb(null, moduleData); // callback to moduleManager
}
callback(null);
}
});
}
}
exports.getRoutes = function () {
return [
{method: 'GET', pattern: '/module/monitorPingdom', function: routeGetAll},
{method: 'POST', pattern: '/module/monitorPingdom/update', function: routeUpdate}
];
}
var routeGetAll = function(req, res, next) {
// Protected route (only admins)
if(req.user && req.user.role != 'admin')
return res.status(401).json(responseMessaging.format(401));
PingdomCheck.getAll(function(err, pingdomChecks){
if(err)
res.status(500).json(responseMessaging.format(500, {}, [err]));
else
res.status(200).json(responseMessaging.format(200, pingdomChecks));
});
}
// Update pingdomCheck
var routeUpdate = function(req, res, next) {
// Protected route (only admins)
if(req.user && req.user.role != 'admin')
return res.status(401).json(responseMessaging.format(401));
var id = req.body.id;
var data = {
monitorClientId: req.body.monitorClientId
};
PingdomCheck.update(id, data, function(err, pingdomCheck){
if(err)
res.status(500).json(responseMessaging.format(500, {}, [err]));
else
res.status(200).json(responseMessaging.format(200, pingdomCheck));
});
};