From 18180281804c09349e36813e8c1b4fc5f860cb05 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 21 Mar 2015 09:27:30 -0400 Subject: [PATCH] add serviceCheck api --- lib/api/index.js | 3 ++- lib/api/serviceCheck.js | 55 ++++++++++++++++++++++++++++++++++++++++ lib/api/service_check.js | 47 ---------------------------------- lib/index.js | 4 +++ 4 files changed, 61 insertions(+), 48 deletions(-) create mode 100644 lib/api/serviceCheck.js delete mode 100644 lib/api/service_check.js diff --git a/lib/api/index.js b/lib/api/index.js index c83395a..3d0faa8 100644 --- a/lib/api/index.js +++ b/lib/api/index.js @@ -1,7 +1,8 @@ var api = { tag: require("./tag"), metric: require("./metric"), - event: require("./event") + event: require("./event"), + serviceCheck: require("./serviceCheck") }; module.exports = function(obj){ diff --git a/lib/api/serviceCheck.js b/lib/api/serviceCheck.js new file mode 100644 index 0000000..5db6ea0 --- /dev/null +++ b/lib/api/serviceCheck.js @@ -0,0 +1,55 @@ +var client = require("../client"); + +/*section: serviceCheck + *comment: | + * post an update to a service check + *params: + * check: the check name (e.g. "app.ok") + * hostName: the name of the host submitting the check + * status: one of `dogapi.OK`, `dogapi.WARNING`, `dogapi.CRITICAL` or `dogapi.UNKNOWN` + * parameters: | + * optional, an object containing any of the following + * * timestamp: POSIX timestamp for when the check happened + * * message: string message to accompany the check + * * tags: an array of "tag:value"'s associated with the check + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * var check = "app.ok"; + * var hostName = "some.machine"; + * dogapi.serviceCheck.check( + * check, hostName, dogapi.WARNING, function(err, res){ + * console.dir(res); + * }); + * ``` + */ +function check(check, hostName, status, parameters, callback){ + if(arguments.length < 5 && typeof arguments[3] === "function"){ + callback = parameters; + parameters = {}; + } + + if(typeof parameters !== "object"){ + parameters = {}; + } + + parameters.check = check; + parameters.host_name = hostName, + parameters.status = status; + + var params = { + body: parameters + }; + client.request("POST", "/check_run", params, callback); +}; + + +module.exports = { + check: check +}; diff --git a/lib/api/service_check.js b/lib/api/service_check.js deleted file mode 100644 index c762981..0000000 --- a/lib/api/service_check.js +++ /dev/null @@ -1,47 +0,0 @@ -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/index.js b/lib/index.js index 2f2234f..893bb13 100644 --- a/lib/index.js +++ b/lib/index.js @@ -33,3 +33,7 @@ function initialize(options){ module.exports.client = require("./client"), module.exports.initialize = initialize; +module.exports.OK = 0; +module.exports.WARNING = 1; +module.exports.CRITICAL = 2; +module.exports.UNKNOWN = 3;