|
| 1 | +/* |
| 2 | + * Copyright 2012 eBay Software Foundation |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +"use strict"; |
| 18 | + |
| 19 | +var http = require('http'), |
| 20 | + os = require('os'); |
| 21 | + |
| 22 | +exports.version = require('../package.json').version; |
| 23 | + |
| 24 | +/** |
| 25 | + * The ECV check sends a "/tables" request to the running server. Anything other than a valid JSON response is |
| 26 | + * treated as an error. |
| 27 | + */ |
| 28 | +var hostname = os.hostname(); |
| 29 | + |
| 30 | +exports.enable = function(app, port, path, monitor, validator) { |
| 31 | + app.get(path || '/ecv', function(req, res) { |
| 32 | + var tosend = { |
| 33 | + date : new Date , |
| 34 | + port : port |
| 35 | + }; |
| 36 | + var options = options || { |
| 37 | + host: 'localhost', |
| 38 | + port: port, |
| 39 | + path: monitor || '/', |
| 40 | + method: 'GET', |
| 41 | + headers: { |
| 42 | + host:'localhost', |
| 43 | + connection:'close', |
| 44 | + accept: 'application/json' |
| 45 | + |
| 46 | + } |
| 47 | + }; |
| 48 | + var creq = http.request(options, function(cres) { |
| 49 | + cres.setEncoding('utf8'); |
| 50 | + var data = ''; |
| 51 | + cres.on('data', function(chunk) { |
| 52 | + data = data + chunk; |
| 53 | + }); |
| 54 | + |
| 55 | + cres.on('end', function() { |
| 56 | + if(cres.statusCode >= 300) { |
| 57 | + // Not happy |
| 58 | + unhappy(req, res, tosend); |
| 59 | + } |
| 60 | + else { |
| 61 | + try { |
| 62 | + if(validator.apply(this, [data])); |
| 63 | + happy(req, res, tosend); |
| 64 | + } |
| 65 | + catch(e) { |
| 66 | + // Not happy |
| 67 | + unhappy(req, res, tosend); |
| 68 | + } |
| 69 | + } |
| 70 | + }); |
| 71 | + }); |
| 72 | + creq.on('error', function(err) { |
| 73 | + unhappy(req, res, tosend.date); |
| 74 | + }); |
| 75 | + creq.end(); |
| 76 | + }); |
| 77 | +}; |
| 78 | + |
| 79 | +function happy(req, res, tosend) { |
| 80 | + res.writeHead(200, { |
| 81 | + 'content-type': 'text/plain', |
| 82 | + 'cache-control': 'no-cache' |
| 83 | + }); |
| 84 | + res.write('status=AVAILABLE&ServeTraffic=true&ip='+ req.connection.address()['address'] +'&hostname='+ hostname +'&port=' + tosend.port+ '&time=' + tosend.date.toString()); |
| 85 | + res.end(); |
| 86 | +} |
| 87 | + |
| 88 | +function unhappy(req, res, tosend) { |
| 89 | + res.writeHead(500, { |
| 90 | + 'content-type': 'text/plain', |
| 91 | + 'cache-control': 'no-cache' |
| 92 | + }); |
| 93 | + res.write('status=WARNING&ServeTraffic=false&ip='+ req.connection.address()['address'] +'&hostname='+ hostname +'&port=' + tosend.port + '&time=' + tosend.date.toString()); |
| 94 | + res.end(); |
| 95 | +} |
0 commit comments