Created
January 11, 2019 06:14
-
-
Save codebudo/83e65df458788f418561c0d708af993a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const request = require('request-promise'); | |
| const twilio = require('twilio'); | |
| const log = console.log; // switch to winston or something | |
| // Your Account SID from www.twilio.com/console | |
| let accountSid = process.env.TWILIO_SID; | |
| // Your Auth Token from www.twilio.com/console | |
| let authToken = process.env.TWILIO_AUTH_TOKEN; | |
| const services = ['obliviousio', 'proxy', 'api', 'obliviousbox']; | |
| function getStatus(){ | |
| return new Promise( (accept, reject) => { | |
| request('https://www.oblivious.io/status') | |
| .then( (data) => { | |
| let d; | |
| try{ | |
| d = JSON.parse(data); | |
| } catch(err){ | |
| log('json parse error'); | |
| } | |
| let servicesDown = []; | |
| services.map( (i) => { | |
| // If the service is in the list and has a port and address, we're solid | |
| log(i); | |
| if( d[i] && d[i].port && d[i].address ){ | |
| // success, ignore | |
| } else { | |
| // failure, send alert | |
| log(i + ' is down!'); | |
| servicesDown.push(i); | |
| } | |
| }); | |
| log(data); | |
| accept(servicesDown); | |
| }) | |
| .catch( (e) => { | |
| // error | |
| reject(e); | |
| }); | |
| }); | |
| } | |
| function sendAlert(msg){ | |
| var client = new twilio(accountSid, authToken); | |
| client.messages.create({ | |
| body: msg, | |
| to: '+19259637417', // Text this number | |
| from: '+14253827973' // From a valid Twilio number | |
| }) | |
| .then((message) => console.log(message.sid)) | |
| .catch( (err) => {log('error:', err); }); | |
| } | |
| // exports for webtask.io environment | |
| module.exports = function (context, cb) { | |
| if( context && context.secrets | |
| && context.secrets.TWILIO_SID && context.secrets.TWILIO_AUTH_TOKEN | |
| ){ | |
| accountSid = context.secrets.TWILIO_SID; | |
| authToken = context.secrets.TWILIO_AUTH_TOKEN; | |
| } | |
| getStatus().then( (down) => { | |
| down.map( (i) => { | |
| sendAlert(i + ' is down!'); | |
| }) | |
| cb(null, down); | |
| }) | |
| .catch( (err) => { | |
| cb(err); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment