├── .gitignore ├── LICENSE ├── README.md ├── index.js ├── package.json └── test └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | 10 | pids 11 | logs 12 | results 13 | 14 | npm-debug.log 15 | 16 | node_modules 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Forbes Lindesay 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Uptime Robot 2 | 3 | A simple node.js and browserify API for [uptime robot](http://uptimerobot.com/api) 4 | 5 | $ npm install uptime-robot 6 | 7 | Currently, only some methods are implemented, but pull requests for the missing ones are welcome. 8 | 9 | This library works in the browser using browserify. You can see a demo by cloning this repo and running `npm run test-browser`. 10 | 11 | All methods also return a [Promise](https://www.promisejs.org/) if no callback is provided. 12 | 13 | ## Example 14 | 15 | ```javascript 16 | var Client = require('uptime-robot'); 17 | var cl = new Client('api-key'); 18 | cl.getMonitors({customUptimeRatio: [1, 7, 30]}, function (err, res) { 19 | if (err) throw err; 20 | console.dir(res); 21 | }); 22 | ``` 23 | 24 | ## API 25 | 26 | ### cl.getMonitors(options, fn(err, monitors)) 27 | 28 | options: 29 | 30 | - see https://uptimerobot.com/api 31 | 32 | ### cl.newMonitor(options, fn(err)) 33 | 34 | options: 35 | 36 | - friendlyName - required 37 | - url - required 38 | - type - required (Default: 1) 39 | - subType - optional (required for port monitoring) 40 | - port - optional (required for port monitoring) 41 | - keywordType - optional (required for keyword monitoring) 42 | - keywordValue - optional (required for keyword monitoring) 43 | - httpUsername - optional 44 | - httpPassword - optional 45 | - alertContacts - optional (array of alert contact ids) 46 | - interval - optional (in minutes) 47 | 48 | ### cl.editMonitor(options, fn(err)) 49 | 50 | options: 51 | 52 | - monitorID - required 53 | - friendlyName - optional 54 | - url - optional 55 | - subType - optional (used only for port monitoring) 56 | - port - optional (used only for port monitoring) 57 | - keywordType - optional (used only for keyword monitoring) 58 | - keywordValue - optional (used only for keyword monitoring) 59 | - httpUsername - optional 60 | - httpPassword - optional 61 | - alertContacts - optional (array of alert contact ids) 62 | - interval - optional (in minutes) 63 | 64 | ### cl.deleteMonitor(id, fn(err)) 65 | 66 | options: 67 | 68 | - monitorID - required 69 | 70 | ### cl.resetMonitor(id, fn(err)) 71 | 72 | options: 73 | 74 | - monitorID - required 75 | 76 | 77 | ### cl.getAlertContacts(options, fn(err, alertContacts)) 78 | 79 | options: 80 | 81 | - alertContacts - optional (array of alert contact ids) 82 | - offset - optional (record to start paginating. Default: 0) 83 | - limit - optional (number of records to return. Default and max: 50) 84 | 85 | 86 | ### cl.getAllAlertContactIds(fn(err, alertContacts)) 87 | 88 | - alertContacts: array of all alert contact ids 89 | 90 | ## License 91 | 92 | MIT 93 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var request = require('then-jsonp'); 4 | var IS_BROWSER = require('is-browser'); 5 | 6 | var base = 'https://api.uptimerobot.com/'; 7 | 8 | module.exports = Client; 9 | function Client(apiKey) { 10 | if (apiKey === '' || typeof apiKey !== 'string') { 11 | throw new Error('Uptime Robot API Key must be provided'); 12 | } 13 | this.request = function (method, params, callback) { 14 | params.apiKey = apiKey; 15 | params.format = 'json'; 16 | if (!IS_BROWSER) params.noJsonCallback = '1'; 17 | return request('GET', base + method, { 18 | qs: params, 19 | callbackName: 'jsonUptimeRobotApi', 20 | callbackParameter: false, 21 | skipJsonpOnServer: true 22 | }).then(function (res) { 23 | if (res.stat === 'fail') { 24 | throw makeError(res); 25 | } else { 26 | return res; 27 | } 28 | }).nodeify(callback); 29 | }; 30 | } 31 | 32 | function makeError(res) { 33 | var err = new Error(res.message); 34 | err.name = 'UptimeRobotServerError'; 35 | err.code = res.id; 36 | return err; 37 | } 38 | 39 | Client.prototype.getMonitors = function (options, callback) { 40 | if (typeof options === 'function') { 41 | callback = options; 42 | options = {}; 43 | } 44 | options = options || {}; 45 | if (!options.logs && options.alertContacts) throw new Error('logs is required if alert contacts is true.'); 46 | var params = {}; 47 | if (options.monitors) params.monitors = options.monitors.join('-'); 48 | if (options.customUptimeRatio) params.customUptimeRatio = options.customUptimeRatio.join('-'); 49 | if (options.logs) params.logs = '1'; 50 | if (options.alertContacts) params.alertContacts = '1'; 51 | if (options.showMonitorAlertConcats) params.showMonitorAlertConcats = '1'; 52 | if (options.showTimezone) params.showTimezone = '1'; 53 | if (options.responseTimes) params.responseTimes = '1'; 54 | 55 | return this.request('getMonitors', params).then(function (res) { 56 | var monitors = res.monitors.monitor; 57 | monitors.forEach(function (monitor) { 58 | if (monitor.customuptimeratio) 59 | monitor.customuptimeratio = monitor.customuptimeratio.split('-'); 60 | else 61 | monitor.customuptimeratio = []; 62 | if (monitor.log) 63 | monitor.log.forEach(function (log) { 64 | log.datetime = parseDate(log.datetime); 65 | }); 66 | }); 67 | return monitors; 68 | }).nodeify(callback); 69 | }; 70 | 71 | Client.prototype.newMonitor = function (options, callback) { 72 | if (!options.friendlyName) throw new Error('friendlyName is required'); 73 | if (!options.url) throw new Error('url is required'); 74 | var params = { 75 | monitorFriendlyName: options.friendlyName, 76 | monitorURL: options.url, 77 | monitorType: options.type || '1', 78 | monitorSubType: options.subType, 79 | monitorPort: options.port, 80 | monitorKeywordType: options.keywordType, 81 | monitorKeywordValue: options.keywordValue, 82 | monitorHTTPUsername: options.httpUsername, 83 | monitorHTTPPassword: options.httpPassword, 84 | monitorAlertContacts: (options.alertContacts || []).join('-'), 85 | monitorInterval: options.interval 86 | }; 87 | return this.request('newMonitor', params).nodeify(callback); 88 | }; 89 | 90 | Client.prototype.editMonitor = function (options, callback) { 91 | if (!options.monitorID) throw new Error('monitorID is required'); 92 | var params = { 93 | monitorID: options.monitorID, 94 | monitorFriendlyName: options.friendlyName, 95 | monitorURL: options.url, 96 | monitorSubType: options.subType, 97 | monitorPort: options.port, 98 | monitorKeywordType: options.keywordType, 99 | monitorKeywordValue: options.keywordValue, 100 | monitorHTTPUsername: options.httpUsername, 101 | monitorHTTPPassword: options.httpPassword, 102 | monitorAlertContacts: (options.alertContacts || []).join('-'), 103 | monitorInterval: options.interval 104 | }; 105 | return this.request('editMonitor', params).nodeify(callback); 106 | }; 107 | 108 | Client.prototype.deleteMonitor = function (id, callback) { 109 | return this.request('deleteMonitor', { monitorID: id }).nodeify(callback); 110 | }; 111 | 112 | Client.prototype.resetMonitor = function (id, callback) { 113 | return this.request('resetMonitor', { monitorID: id }).nodeify(callback); 114 | }; 115 | 116 | Client.prototype.getAlertContacts = function (options, callback) { 117 | if (typeof options === 'function') { 118 | callback = options; 119 | options = {}; 120 | } 121 | options = options || {}; 122 | var params = {}; 123 | if (options.alertContacts) params.alertcontacts = options.alertContacts.join('-'); 124 | if (options.offset) params.offset = options.offset; 125 | if (options.limit) params.limit = options.limit; 126 | 127 | return this.request('getAlertContacts', params).then(function (res) { 128 | return res.alertcontacts.alertcontact; 129 | }).nodeify(callback); 130 | }; 131 | 132 | Client.prototype.getAllAlertContactIds = function (callback) { 133 | return this.getAlertContacts().then(function(alertContacts) { 134 | return alertContacts.map(function (c) { return c.id; }); 135 | }).nodeify(callback); 136 | }; 137 | 138 | 139 | var datePattern = /^(\d\d)\/(\d\d)\/(\d\d\d\d) (\d\d):(\d\d):(\d\d)$/; 140 | function parseDate(str) { 141 | var match = datePattern.exec(str); 142 | var month = +match[1]; 143 | var day = +match[2]; 144 | var year = +match[3]; 145 | var hour = +match[4]; 146 | var minute = +match[5]; 147 | var second = +match[6]; 148 | 149 | return new Date(year, month - 1, day, hour, minute, second); 150 | } 151 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "uptime-robot", 3 | "version": "1.3.0", 4 | "description": "A simple node.js API for uptime robot", 5 | "repository": { 6 | "type": "git", 7 | "url": "https://github.com/ForbesLindesay/uptime-robot.git" 8 | }, 9 | "author": "ForbesLindesay", 10 | "license": "MIT", 11 | "dependencies": { 12 | "is-browser": "^2.0.1", 13 | "then-jsonp": "^1.0.1" 14 | }, 15 | "scripts": { 16 | "test": "node test", 17 | "test-browser": "run-browser test/index.js" 18 | }, 19 | "devDependencies": { 20 | "run-browser": "^2.0.0" 21 | } 22 | } -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var assert = require('assert'); 4 | var Client = require('../'); 5 | var cl = new Client('u193485-e7bb953d295bd66420f2f5d6'); 6 | 7 | cl.getMonitors({customUptimeRatio: [1, 7, 30]}, function (err, res) { 8 | if (err) throw err; 9 | assert(Array.isArray(res)); 10 | assert(res.length === 1); 11 | assert(res[0].id === '776540955'); 12 | console.log('getMonitors', res); 13 | console.log('getMonitors passed'); 14 | }); 15 | 16 | cl.resetMonitor('776540955', function (err, res) { 17 | if (err) throw err; 18 | assert(res.stat === 'ok'); 19 | console.log('resetMonitor', res); 20 | console.log('resetMonitor passed'); 21 | }); 22 | 23 | process && process.exit && process.exit(0); 24 | --------------------------------------------------------------------------------