-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter2moog.js
339 lines (250 loc) · 10.6 KB
/
twitter2moog.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/share/moogsoft/connectors/node/bin/node
// -----------------------------------------------------------
// twitter2Moog
//
// Uses the twitter API to turn relevsnt tweets
// into Moog comapatible events.
//
// (c) Moogsoft Ltd. 2014
//
// This script is contributed and as such is
// supported only on a best effort basis.
//
// -----------------------------------------------------------
// Required built in modules
var net = require('net');
//var twitter = require('/usr/share/moogsoft/connectors/node/node_modules/ntwitter');
var twitter = require('./node_modules/ntwitter');
// Process any command line arguemnts
var appName="Twitter";
var scriptName=process.argv[1];
var cmdLineOpts=getOpts(process.argv.slice(2));
// Use the command line args or the defaults.
var defaultConfigFile="./twitter2moog.conf";
var defaultUtilsFile="./twitter2moog.utils";
var defaultLogLevel="info";
var defaultRetries=3;
var retryCount=0;
var moogRetryCount=0;
var configFile=cmdLineOpts.config ? cmdLineOpts.config : defaultConfigFile;
var utilsFile=cmdLineOpts.utils ? cmdLineOpts.utils : defaultUtilsFile;
var logLevel=cmdLineOpts.loglevel ? cmdLineOpts.loglevel : defaultLogLevel ;
// Source in the specific modules.
var t2mConfig = require(configFile);
var utils = require(utilsFile);
// Set some logging defaults.
var logger=new utils.Logger;
logger.setLogLevel(logLevel);
logger.warning("======== "+ appName + " 2 Incident.Moog Connector =========");
// Globals for the connections
var twitterConnection;
var moogConnection;
//
// Set some actions when signals are recieved.
//
process.on('SIGINT', function() {
exit_app("SIGINT recieved, exiting",3);
});
process.on('SIGTERM', function() {
exit_app("SIGTERM recieved, exiting",3);
});
//
// Configuration checks
//
// Check that we have enough config to proceed.
if ( typeof t2mConfig.twitterConf === 'undefined' || typeof t2mConfig.moogServer === 'undefined' ) {
exit_app("Could not find required configuration for Twitter and Incident.MOOG in " + configFile ,2);
}
// Twitter config
var twitterStreamType=t2mConfig.twitterConf.streamType ? t2mConfig.twitterConf.streamType : "statuses/filter";
var twitterStreamFilter=t2mConfig.twitterConf.streamFilter || exit_app("A stream filter must be configured in " + configFile,2);
var maxRetryCount=t2mConfig.twitterConf.retries || defaultRetries;
// Twitter API credentials
if ( typeof t2mConfig.twitterConf.credentials === 'undefined' ) {
exit_app("Counld not find any Twitter API credentials in " + configFile,2);
}
var twitterConsumerKey=t2mConfig.twitterConf.credentials.consumer_key || exit_app("No Twitter consumer key found in " + configFile,2);
var twitterConsumerSecret=t2mConfig.twitterConf.credentials.consumer_secret || exit_app("No Twitter consumer secret found in " + configFile,2);
var twitterAccessTokenKey=t2mConfig.twitterConf.credentials.access_token_key || exit_app("No Twitter access token key found in " + configFile,2);
var twitterAccessTokenSecret=t2mConfig.twitterConf.credentials.access_token_secret || exit_app("No Twitter access token secret found in " + configFile,2);
// Moog configuration
var moogServer=t2mConfig.moogServer.host || exit_app("Config: No Moog server host found",2);
var moogPort=t2mConfig.moogServer.port || exit_app("Config: No Moog server port found",2);
var moogMaxRetryCount=t2mConfig.moogServer.retries || defaultRetries;
logger.warning("Configuration checks passed...");
logger.warning("----------------------------------------------------");
logger.warning("Moog server " + moogServer + ":" + moogPort);
logger.warning("Moog server connection retry count: " + moogMaxRetryCount);
logger.warning("Twitter API connection retry count: " + maxRetryCount);
logger.warning("LogLevel set to " + logLevel);
// Show what stream filters we are looking for.
var numFilters=0;
for ( var sf in twitterStreamFilter ) {
if ( typeof twitterStreamFilter[sf] !== 'function' ) {
logger.warning("Filtering Tweets on : " + "[ " + sf + " ] : " + twitterStreamFilter[sf].join(", "));
numFilters++;
}
}
if ( numFilters === 0 ) {
exit_app("A set of filter cirteria need to be supplied in " + configFile,2);
}
logger.warning("----------------------------------------------------");
//
// Establish the connections to the and Moog server
//
connectToMoog();
// Functions
function connectToMoog() {
moogRetryCount++;
if ( moogRetryCount > moogMaxRetryCount ) {
exit_app("Could not connect to the Moog Server - retry count exceeded",1);
}
logger.warning("Connecting to the Moog Server (attempt " + moogRetryCount + " of " + moogMaxRetryCount + ")");
moogConnection = net.connect(moogPort,moogServer, function() {
logger.info("Connected to the Moog Server " + moogServer +":"+moogPort);
listenForTweets();
});
moogConnection.on('error', function(err) {
if (err.message.indexOf('REFUSED')) {
logger.warning("Connection to the Moog Server " + moogServer + ":" + moogPort + " refused - retrying");
setTimeout( function() { connectToMoog() }, 5000 );
}
else {
logger.warning("Connection to the Moog Server failed - retrying");
setTimeout( function() { connectToMoog() }, 5000 );
}
});
moogConnection.on('end', function() {
logger.warning("Connection to the Moog Server closed - retrying");
setTimeout( function() { connectToMoog() }, 5000 );
});
}
function listenForTweets() {
retryCount++;
if ( retryCount > maxRetryCount ) {
exit_app("Could not connect to the Twitter API - retry count exceeded",1);
}
logger.warning("Connecting to the Twitter API (attempt " + retryCount + " of " + maxRetryCount+")");
twitterConnection = new twitter({
consumer_key: twitterConsumerKey,
consumer_secret: twitterConsumerSecret,
access_token_key: twitterAccessTokenKey,
access_token_secret: twitterAccessTokenSecret
});
// Let's verify we are ok
twitterConnection.verifyCredentials(function (err, data) {
if ( err ) {
exit_app("Cound not verify connection to Twitter API, exiting",2);
}
else {
logger.warning("Connection to the Twitter API verified");
}
})
// Get a stream of tweets based on the filters defined.
twitterConnection.stream(twitterStreamType,twitterStreamFilter , function(tweetStream)
{
tweetStream.on('data',function(t) {
processTweet(t);
});
tweetStream.on('end',function(res) {
logger.warning("Twitter API connection closed (end) retrying - attempt " + retryCount + " of " + maxRetryCount);
listenForTweets();
});
tweetStream.on('error',function(source,error) {
logger.warning("Twitter API connection error : http error " + error ) ;
});
});
}
function processTweet(t) {
var tweet=t;
if ( typeof tweet.text === 'undefined' ||
( typeof tweet.user === 'undefined' || typeof tweet.user.followers_count === 'undefined' ) ) {
logger.warning("Received a tweet without enough data to construct an event");
return;
}
// We want a unique id for this tweet for the signature.
var tweetText=tweet.text;
var tweetFollowers=tweet.user.followers_count;
logger.debug("Tweet recieved: " + tweetText);
// We will use the recieved/processed time rather than the tweet created time.
var tweetTime = Math.round(Date.now() / 1000)
// We want a unique id for this tweet for the signature.
var tweetId=tweet.id || tweet.text.length + tweetFollowers ;
// Create an event, populate it and send it to the LAM.
var tweetEvent=new utils.MoogEvent;
tweetEvent.signature="@TWITTER->" + tweetId;
tweetEvent.source="twitter";
tweetEvent.external_id=tweetId;
tweetEvent.description=tweetText;
tweetEvent.first_occurred=tweetTime;
tweetEvent.agent_time=tweetTime;
tweetEvent.severity=getTweetSeverity(tweetFollowers);
// Send the event to the LAM.
moogConnection.write(JSON.stringify(tweetEvent) + "\n");
}
function getTweetSeverity(numFoll) {
// Simple follower based severity
if ( numFoll <= 10 ) { return 1; }
if ( numFoll > 10 && numFoll <= 50) { return 2; }
if ( numFoll > 50 && numFoll <= 100) { return 3; }
if ( numFoll > 100 && numFoll <= 200) { return 4; }
if ( numFoll > 200 ) { return 5; }
}
function exit_app(m,c) {
try {
// Try and close any open connections.
moogConnection.destroy();
twitterConnection.destroy();
}
catch(e) { }
logger.critical(m);
logger.critical("=============== "+ appName + " 2 Incident.Moog Exited ====================");
process.exit(c);
}
function getOpts(opts) {
// The options beginning with a "-" are
// flag, the ones that aren't are values.
// create an object with flags and values.
var options={};
for ( var optIdx = 0; optIdx < opts.length ; optIdx++ ) {
var flagRe=/^(?:-{1,2})(\w+)$/gi;
var valueRe=/^[^\-]/;
var option = opts[optIdx];
var isFlag=flagRe.exec(option);
if ( isFlag !== null && isFlag.length > 1 ) {
var optionName=isFlag[1].toLowerCase();
if ( optionName === 'h' || optionName === 'help' ) {
var helpText="";
helpText += "\n============= "+appName+" 2 Incident.Moog Connector =========\n";
helpText += "Usage: " + scriptName + "\n\n";
helpText += "[ --config <file> ]\t:\tconfig file name (defaults to " + defaultConfigFile + ")\n";
helpText += "[ --utils <file> ]\t:\tutils file name (defaults to " + defaultUtilsFile + "\n";
helpText += "[ --loglevel (info|warn|debug|all) ]\t:\tlog message level (defaults to info)\n";
helpText +="\n";
console.log(helpText);
process.exit(0);
}
// We've got a flag, check the next arg to
// see if it's a value (i.e. not a flag.
var valueIdx=optIdx + 1;
if ( valueIdx < opts.length ) {
var value = opts[valueIdx];
var isValue=valueRe.test(value);
if ( isValue === true ) {
// We've got a value, set it, and increment the index.
// to skip over this value on the next loop.
options[optionName]=value;
optIdx++;
}
else {
// We got antother flag, so just set this.
options[optionName]=true;
}
}
else {
options[optionName]=true;
}
}
}
return(options);
}