-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnodeChecker.js
178 lines (164 loc) · 5.31 KB
/
nodeChecker.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
function initCheckers(api, next)
{
api.nodeChecker = {};
api.nodeChecker.apiData = {}; // cache for checker varaible storage
api.nodeChecker.checkers = {}; // list of checkers (like actions)
/////////////////////////////////////////////////////////////////////////////////////
// new modules
api.mysql = require('mysql');
api.https = require('https');
api.querystring = require('querystring');
/////////////////////////////////////////////////////////////////////////////////////
// the actual check
api.runCheck = function(api, check){
var cacheName = "nodeCheckerData_" + check.name;
api.cache.load(api, cacheName, function(data){
try{
if (data == null){ data = []; }
var startTime = new Date().getTime();
check.params._data = data;
api.nodeChecker.checkers[check.type].check(api, check.params, function(response){
response.timeStamp = new Date().getTime();
if(response.dataOverride != true){
data.push(response);
}else{
data = response._data;
}
while( data.length > check.entriesToKeep){
data.shift();
}
api.cache.save(api, cacheName, data, null, function(resp){
var requestDurationSeconds = (response.timeStamp - startTime)/1000;
api.sendSocketCheckResuts(check, response);
api.log("checked -> "+check.name+":"+check.type+" in "+requestDurationSeconds+"s", "magenta");
setTimeout(api.runCheck, (check.frequencyInSeconds * 1000), api, check);
});
});
}catch(e){
api.log(e, "red");
api.log(check.name+":"+check.type+" is not a check I know how to do. Check checks.json", "red");
api.log(" > "+check.name+" will not be processed", "red");
}
});
}
/////////////////////////////////////////////////////////////////////////////////////
// send socket people a message
api.sendSocketCheckResuts = function(check, response){
var message = {
context: "check",
type: check.type,
requestDurationSeconds: check.requestDurationSeconds,
name: check.name,
number: response.number,
error: response.error,
check: response.check,
serverTime: new Date()
};
var connection = {
id: 1,
type: "socket",
room: check.name,
messageCount: 0,
public: {id: 1 }
}
api.socketServer.socketRoomBroadcast(api, connection, message);
connection.room = "all";
api.socketServer.socketRoomBroadcast(api, connection, message);
}
/////////////////////////////////////////////////////////////////////////////////////
// add a util for param sanitization
api.utils.checkParamChecker = function(api, required_params, params, mode){
var error = false;
if(mode == null){mode = "all";}
if(mode == "all"){
required_params.forEach(function(param){
if(error == false && (params[param] === undefined || (params[param] != null && params[param].length == 0))){
error = param + " is a required parameter for this action";
}
});
}
if(mode == "any"){
var paramString = "";
var found = false;
required_params.forEach(function(param){
if(paramString != ""){paramString = paramString + ",";}
paramString = paramString + " " + param;
if(params[param] != null){
found = true;
}
});
if(found == false)
{
error = "none of the required params for this action were provided. Any of the following are required: " + paramString;
}
}
return error;
}
/////////////////////////////////////////////////////////////////////////////////////
// Setup Message Queue
api.messages = {};
api.messages.messages = []; // {message: string, timestamp: datetime}
api.messages.messagesToKeep = api.configData.messagesToKeep;
/////////////////////////////////////////////////////////////////////////////////////
// Load checkers
api.fs.readdirSync("./checkers").forEach( function(file) {
if (file != ".DS_Store"){
var checkerName = file.split(".")[0];
api.nodeChecker.checkers[checkerName] = require("./checkers/" + file)["checker"];
api.nodeChecker.apiData[checkerName] = {};
api.log("checker loaded: " + checkerName, "yellow");
}
});
/////////////////////////////////////////////////////////////////////////////////////
// load and run checks!
try{
api.checks = JSON.parse(api.fs.readFileSync('./checks.json','utf8'));
}catch(e){
console.log(e);
api.log("Loading example checks.");
api.checks = [
{
"name":"random_numbers",
"type":"randomNumber",
"frequencyInSeconds":2,
"entriesToKeep":100,
"axisLabel" : "Random Number",
"params":{}
},
{
"name":"ping_google_com",
"type":"ping",
"frequencyInSeconds":10,
"entriesToKeep":100,
"axisLabel": "ping duration (ms)",
"params":{
"hostname":"google.com"
}
},
{
"name":"http_google_com",
"type":"httpRequest",
"frequencyInSeconds":10,
"entriesToKeep":100,
"axisLabel": "http request time (ms)",
"params":{
"hostname":"http://www.google.com",
"matcher":"</div>"
}
}
];
}
api.checks.forEach(function(check){
process.nextTick(function() { api.runCheck(api, check) });
api.log("loaded check: "+check.name, "magenta");
});
next();
}
/////////////////////////////////////////////////////////////////////////////////////
// GO!
var actionHero = require("actionHero").actionHero;
actionHero.start({}, function(api){
initCheckers(api, function(){
api.log("Loading complete!", ['green', 'bold']);
});
});