From e1a81030304588baf82a70a9d6e0a2c797015187 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Tue, 13 Jan 2015 12:01:11 -0500 Subject: [PATCH] add dogapi.service_check method --- README.md | 2 ++ lib/api/service_check.js | 47 ++++++++++++++++++++++++++++++++++++++++ lib/constants.js | 13 +++++++++++ lib/index.js | 5 +++++ 4 files changed, 67 insertions(+) create mode 100644 lib/api/service_check.js create mode 100644 lib/constants.js diff --git a/README.md b/README.md index 2791d25..25dbf58 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,8 @@ DD_API_KEY=YOUR_KEY_HERE DD_APP_KEY=YOUR_KEY_HERE node app.js * method used to take a snapshot of a datadog graph * `dogapi.snapshot_status(snapshot_url, [callback])` * method used to check the status of a datadog snapshot +* `dogapi.service_check(status, check, host, [[extra], [callback]])` + * method used to post a new service check ## Sample Usage: diff --git a/lib/api/service_check.js b/lib/api/service_check.js new file mode 100644 index 0000000..c762981 --- /dev/null +++ b/lib/api/service_check.js @@ -0,0 +1,47 @@ +var util = require('util'); + +var constants = require('../constants'); + +var service_check_api = function(){}; + + +service_check_api.prototype.service_check = function(status, check, host, extra, callback){ + /* + * service_check_api.service_check(options, callback]) + * + * used to post a service check + * + * `status` the `dogapi.constant.STATUSES` status code for the check + * `check` the name of the check + * `host` the host associated with the check + * `extra` an object of optional arguments `timestamp`, `message` or `tags` + * `callback` is an optional function to call with the results of the api call + * callback(error, result, status_code) + */ + if(typeof extra === 'function'){ + callback = extra; + extra = {}; + } + + if(constants.ALL_STATUSES.indexOf(status) < 0){ + throw new Error(util.format('Unknown service_check status %s', status)); + } + + var body = { + check: check, + status: status, + host_name: host, + timestamp: parseInt(extra['timestamp'] || (new Date().getTime() / 1000)), + }; + + if(extra['message']){ + body['message'] = extra['message']; + } + if(extra['tags']){ + body['tags'] = extra['tags']; + } + + this.request('POST', '/check_run', {body: body}, callback); +}; + +return module.exports = service_check_api; diff --git a/lib/constants.js b/lib/constants.js new file mode 100644 index 0000000..59c145a --- /dev/null +++ b/lib/constants.js @@ -0,0 +1,13 @@ +module.exports.STATUSES = { + OK: 0, + WARNING: 1, + CRITICAL: 2, + UNKNOWN: 3, +}; + +module.exports.ALL_STATUSES = [ + module.exports.STATUSES.OK, + module.exports.STATUSES.WARNING, + module.exports.STATUSES.CRITICAL, + module.exports.STATUSES.UNKNOWN, +]; diff --git a/lib/index.js b/lib/index.js index 1089c73..28e0784 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,6 +1,7 @@ var extend = require('extend'); var http_client = require('./http_client.js'); +var constants = require('./constants.js'); var alert_api = require('./api/alert.js'); var dash_api = require('./api/dash.js'); var screen_api = require('./api/screen.js'); @@ -8,6 +9,7 @@ var event_api = require('./api/event.js'); var tag_api = require('./api/tag.js'); var metric_api = require('./api/metric.js'); var search_api = require('./api/search.js'); +var service_check_api = require('./api/service_check.js'); var snapshot_api = require('./api/snapshot.js'); @@ -24,6 +26,9 @@ extend(dogapi.prototype, tag_api.prototype, metric_api.prototype, search_api.prototype, + service_check_api.prototype, snapshot_api.prototype); +dogapi.constants = constants; + return module.exports = dogapi;