UNPKG

cloud-log-collector

Version:

Collect log from mutiple servers

142 lines (112 loc) 3.16 kB
#!/usr/bin/env node var WebSocketClient = require('websocket').client; var Tail = require('tail-file'); var fs = require('fs'); // Load config var conf=JSON.parse(fs.readFileSync('/etc/clc/client.conf')); // log function logClient (data) { try { fs.appendFileSync('/var/clclog/client.log', (new Date()) + " - client - " + data + "\n"); } catch (err) { console.log(err); } } logClient('Client start'); // random function function getRandomInt(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min + 1)) + min; } // nano time function getNanoSecTime() { var hrTime = process.hrtime(); return hrTime[0] * 1000000000 + hrTime[1]; } // Websocket part var client = new WebSocketClient(); var connection={}; var connection_connected=false; client.on('connectFailed', function(error) { logClient('Connect Error: ' + error.toString()); }); client.on('connect', function(connection2) { connection=connection2; connection_connected=true; logClient('WebSocket Client Connected'); connection.on('error', function(error) { logClient("Connection Error: " + error.toString()); }); connection.on('close', function() { logClient('echo-protocol Connection Closed'); connection_connected=false; }); connection.on('message', function(message) { if (message.type === 'utf8') { if (message.utf8Data=="ping") { connection.sendUTF("pong"); return; } if (message.utf8Data=="pong") return; try{ var data=JSON.parse(message.utf8Data); if ('error' in data) logClient('error ws message : '+data['error']); } catch(err) { logClient('error bad ws message'); } } }); function sendPing() { if (connection_connected) { connection.sendUTF("ping"); setTimeout(sendPing, getRandomInt(0, 120*1000)); } } sendPing(); }); //manage queu var message_to_send=[]; function process_queue () { if (!connection_connected) return; while ((message_to_send.length!=0)&&(connection_connected)) { try { connection.sendUTF(JSON.stringify(message_to_send.shift())); } catch (err) { logClient(err); } } } function auto_connect () { if (!connection_connected) { logClient('WebSocket Client Connection try'); client.connect('wss://'+conf['server']['hostname']+':'+conf['server']['port']+'/', 'echo-protocol'); } process_queue(); } setInterval(auto_connect, getRandomInt(0, 60*1000)); auto_connect(); //tail part function launch_tail(_file) { if (fs.existsSync(_file)) { logClient("tail file '"+_file+"'"); } var tail = new Tail(_file); tail.on("line", function(data) { message_to_send.push({'id':getNanoSecTime(),'SecretClientKey':conf['client']['SecretClientKey'],'file':_file,'data':data}); process_queue(); }); tail.on("error", function(error) { if (fs.existsSync(_file)) { logClient("tail error : " + error); } setTimeout(function(){ launch_tail(_file); }, getRandomInt(0, 2*1000)); }); tail.start(); } for (var i=0;i<conf['client']['watcher'].length;i++) launch_tail(conf['client']['watcher'][i]);