From 7b55a272243b3c23b0792d3111ffc8f6d3a70f63 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Wed, 18 Mar 2015 08:51:44 -0400 Subject: [PATCH 01/65] replace old http client library with a new one --- lib/client.js | 122 ++++++++++++++++++++++++++++++++++++++ lib/http_client.js | 145 --------------------------------------------- 2 files changed, 122 insertions(+), 145 deletions(-) create mode 100644 lib/client.js delete mode 100644 lib/http_client.js diff --git a/lib/client.js b/lib/client.js new file mode 100644 index 0000000..3e91e40 --- /dev/null +++ b/lib/client.js @@ -0,0 +1,122 @@ +var extend = require("extend"); +var https = require("https"); +var url = require("url"); +var util = require("util"); + + +/*section: client + *comment: | + * the constructor for _client_ object + *params: + *example: | + * See [client.request](#client-request) + */ +var client = function(){ + this.api_key = null; + this.app_key = null; + this.api_version = "v1"; + this.api_host = "app.datadoghq.com"; +}; + +/*section: client + *comment: | + * used to make a raw request to the datadog api + *params: + * method: | + * http method GET, POST, PUT, DELETE + * path: | + * the api url path e.g. /tags/hosts + * params: | + * an object which allows the keys `query` or `body` + * callback: | + * function to call on success/failure callback(err, result) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.client.request("GET", "/url/path", {}, function(err, results){ + * console.dir(results); + * }); + * ``` + */ +client.prototype.request = function(method, path, params, callback){ + if(arguments.length === 3 && typeof arguments[2] === "function"){ + callback = arguments[2]; + params = {}; + } + + params = params || {}; + var body = (typeof params["body"] === "object")? JSON.stringify(params["body"]) : params["body"]; + var query = { + "api_key": this.api_key, + "application_key": this.app_key, + }; + + if(typeof params["query"] === "object"){ + extend(query, params["query"]); + } + + path = url.format({ + "pathname": util.format("/api/%s%s", this.api_version, path), + "query": query, + }); + + var http_options = { + hostname: this.api_host, + port: 443, + method: method.toUpperCase(), + path: path, + }; + + if(["POST", "PUT"].indexOf(http_options["method"]) >= 0){ + http_options["headers"] = { + "Content-Type": "application/json", + "Content-Length": body.length, + }; + } + + var req = https.request(http_options, function(res){ + res.on("error", function(err){ + if(typeof callback == "function"){ + callback(err, null, res.statusCode); + } + }); + + var data = ""; + res.on("data", function(chunk){ + data += chunk; + }); + + res.on("end", function(){ + var error = null; + try{ data = JSON.parse(data); }catch(e){} + if(data["errors"]){ + error = data["errors"]; + data = null; + } + + if(typeof callback === "function"){ + callback(error, data, res.statusCode); + } + }); + }); + + // This should only occur for errors such as a socket hang up prior to any + // data being received, or SSL-related issues. + req.on("error", function(err){ + if(typeof callback === "function"){ + callback(err, null, 0); + } + }); + + if(["POST", "PUT"].indexOf(http_options["method"]) >= 0){ + req.write(body); + } + req.end() +}; + +module.exports = new client(); diff --git a/lib/http_client.js b/lib/http_client.js deleted file mode 100644 index bfcb8fc..0000000 --- a/lib/http_client.js +++ /dev/null @@ -1,145 +0,0 @@ -var extend = require('extend'); -var https = require('https'); -var url = require('url'); -var util = require('util'); - - -var client = function(options){ - /* - * new http_client([options]) - * - * http client used to make requests to datadog api service - * - * `options` is an optional object of parameters to set: - * { - * 'api_key': 'datadog api key', - * 'app_key': 'datadog app key', - * 'api_version': 'default is v1', - * 'api_host': 'default is app.datadoghq.com', - * } - * - * this client also allows you to provide these options as - * environment variables instead: - * - * api_key: DD_API_KEY - * app_key: DD_APP_KEY - * api_version: DD_API_VERSION - * api_host: DD_API_HOST - */ - options = (options)? options : {}; - - this.api_key = (options['api_key'])? options['api_key'] : process.env['DD_API_KEY']; - if(!this.api_key){ - throw new Error('`api_key` is not present, either provide `api_key` in `options` or use environment variable `DD_API_KEY`'); - } - - this.app_key = (options['app_key'])? options['app_key'] : process.env['DD_APP_KEY']; - if(!this.api_key){ - throw new Error('`app_key` is not present, either provide `app_key` in `options` or use environment variable `DD_APP_KEY`'); - } - - this.api_version = (options['api_version'])? options['api_version'] : process.env['DD_API_VERSION']; - if(!this.api_version){ - this.api_version = 'v1'; - } - - this.api_host = (options['api_host'])? options['api_host'] : process.env['DD_API_HOST']; - if(!this.api_host){ - this.api_host = 'app.datadoghq.com'; - } - - -}; - -client.prototype.request = function(method, path, params, callback){ - /* - * http_client.request(method, path, [[params], callback]) - * - * method used to send an http request to the datadog api service - * - * `method` is the http method to use ('POST', 'GET', 'DELETE', etc) - * `path` the api url to call, e.g '/events', '/series', '/dash', etc - * `params` an optional object of additional options: - * { - * 'query': { 'key': 'value' }, - * 'body': { 'json/object': 'body' } or 'string body', - * } - * `callback` an optional function to obtain the results of the api call - * callback(error, result) - */ - if(arguments.length == 3 && typeof arguments[2] == 'function'){ - callback = arguments[2]; - params = {}; - } - - params = params || {}; - var body = (typeof params['body'] == 'object')? JSON.stringify(params['body']) : params['body']; - var query = { - 'api_key': this.api_key, - 'application_key': this.app_key, - }; - - if(typeof params['query'] == 'object'){ - extend(query, params['query']); - } - - path = url.format({ - 'pathname': util.format('/api/%s%s', this.api_version, path), - 'query': query, - }); - - var http_options = { - hostname: this.api_host, - port: 443, - method: method.toUpperCase(), - path: path, - }; - - if(['POST', 'PUT'].indexOf(http_options['method']) >= 0){ - http_options['headers'] = { - 'Content-Type': 'application/json', - 'Content-Length': body.length, - }; - } - - var req = https.request(http_options, function(res){ - res.on('error', function(err){ - if(typeof callback == 'function'){ - callback(err, null, res.statusCode); - } - }); - - var data = ''; - res.on('data', function(chunk){ - data += chunk; - }); - - res.on('end', function(){ - var error = null; - try{ data = JSON.parse(data); }catch(e){} - if(data['errors']){ - error = data['errors']; - data = null; - } - - if(typeof callback == 'function'){ - callback(error, data, res.statusCode); - } - }); - }); - - // This should only occur for errors such as a socket hang up prior to any - // data being received, or SSL-related issues. - req.on('error', function(err) { - if(typeof callback == 'function'){ - callback(err, null, 0); - } - }); - - if(['POST', 'PUT'].indexOf(http_options['method']) >= 0){ - req.write(body); - } - req.end() -}; - -return module.exports = client; From ad5841aa5300cd211a366eeb300a5f20fd7952a9 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Wed, 18 Mar 2015 08:52:47 -0400 Subject: [PATCH 02/65] setup new dogapi base and port over the tag api --- lib/api/index.js | 9 ++ lib/api/tag.js | 294 +++++++++++++++++++++++++++-------------------- lib/index.js | 65 ++++++----- 3 files changed, 210 insertions(+), 158 deletions(-) create mode 100644 lib/api/index.js diff --git a/lib/api/index.js b/lib/api/index.js new file mode 100644 index 0000000..04d809b --- /dev/null +++ b/lib/api/index.js @@ -0,0 +1,9 @@ +var api = { + tag: require("./tag") +}; + +module.exports = function(obj){ + for(key in api){ + obj[key] = api[key]; + } +}; diff --git a/lib/api/tag.js b/lib/api/tag.js index f155430..e376b8c 100644 --- a/lib/api/tag.js +++ b/lib/api/tag.js @@ -1,169 +1,213 @@ +var client = require("../client"); var util = require('util'); -var tag_api = function(){}; - -tag_api.prototype.all_tags = function(source, callback){ - /* - * tag_api.all_tags([[source], callback]) - * - * method to get all the tags in datadog - * - * `source` a source to limit to - * `callback` is an optional function to call with the results of the api call - * callback(error, result, status_code) - */ - if(arguments.length < 2 && typeof arguments[0] == 'function'){ - callback = arguments[0]; +/*section: tag + *comment: | + * get all host tags + *params: + * source: | + * optional, only show tags for a particular source [default: null] + * callback: | + * function callback(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.tag.get_all(function(err, results){ + * console.dir(results); + * }); + * ``` + */ +function get_all(source, callback){ + if(arguments.length < 2 && typeof arguments[0] === "function"){ + callback = source; source = undefined; } - params = { + var params = { query: { source: source } }; - this.request('GET', '/tags/hosts', params, callback); -}; - -tag_api.prototype.host_tags = function(host, source, callback){ - /* - * tag_api.host_tags(host, [[source], callback]) - * - * method to get the tags associated with a given `host` - * - * `host` the hostname or id to get tags for - * `source` a source to limit the results to - * `callback` is an optional function to call with the results of the api call - * callback(error, result, status_code) - */ - if(arguments.length < 3 && typeof arguments[1] == 'function'){ - callback = arguments[1]; - source = undefined; + client.request("GET", "/tags/hosts", params, callback); +} + +/*section: tag + *comment: | + * get the host tags for a provided host name or host id + *params: + * hostname: | + * the hostname or host id + * options: + * | + * optional, an object of options for the query allowing the following + * * source: the source of the tags (e.g. chef, puppet, users, etc) [default: null] + * * by_source: whether or not to group the results by source [default: false] + * callback: | + * function callback(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.tag.get("host.name", function(err, results){ + * console.dir(results); + * }); + * ``` + */ +function get(hostname, options, callback){ + if(arguments.length < 3 && typeof arguments[1] === "function"){ + callback = options; + options = {}; } + options = options || {}; - params = { + var params = { query: { - source: source, } }; - this.request('GET', util.format('/tags/hosts/%s', host), params, callback); -}; - -tag_api.prototype.host_tags_by_source = function(host, source, callback){ - /* - * tag_api.host_tags_by_source(host, [[source], callback]) - * - * method to return the tags associated with a host, arranged by source - * - * `host` the hostname of id to get tags for - * `source` a source to limit the lookup for - * `callback` is an optional function to call with the results of the api call - * callback(error, result, status_code) - */ - if(arguments.length < 3 && typeof arguments[1] == 'function'){ - callback = arguments[1]; - source = undefined; + if(options.source){ + params.query.source = options.source; } - - params = { - query: { - source: source, - by_source: true, - } - }; - this.request('GET', util.format('/tags/hosts/%s', host), params, callback); + if(options.by_source){ + params.query.by_source = options.by_source; + } + client.request("GET", "/tags/hosts/" + hostname, params, callback); }; -tag_api.prototype.add_tags = function(host, tags, source, callback){ - /* - * tag_api.add_tags(host, tags, [[source], callback]) - * - * add new tags to given `host` - * - * `host` the hostname or id of the machine to add tags for - * `tags` an array of tags to add to the `host` - * `source` the source to associate the tags with, default: user - * `callback` is an optional function to call with the results of the api call - * callback(error, result, status_code) - */ - if(typeof tags != 'object'){ - throw new Error('`tags` parameter must be an array'); - } - if(arguments.length < 4 && typeof arguments[2] == 'function'){ - callback = arguments[2]; +/*section: tag + *comment: | + * assign new host tags to the provided host name or host id + *params: + * hostname: | + * the hostname or host id + * tags: | + * list of `:` tags to assign to the server + * source: | + * optional, the source of the tags (e.g. chef, puppet, etc) [default: users] + * callback: | + * function callback(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.tag.create("host.name", ["role:webserver"], function(err, results){ + * console.dir(results); + * }); + * ``` + */ +function create(hostname, tags, source, callback){ + if(arguments.length < 4 && typeof arguments[2] === "function"){ + callback = source; source = undefined; } - params = { - query: { - source: source, - }, + var params = { body: { tags: tags, - } + source: source + }, }; - this.request('POST', util.format('/tags/hosts/%s', host), params, callback); + client.request("POST", "/tags/hosts/" + hostname, params, callback); }; -tag_api.prototype.update_tags = function(host, tags, source, callback){ - /* - * tag_api.update_tags(host, tags, [[source], callback]) - * - * update the tags associated with the given `host` - * - * `host` is the hostname or id of the machine to update tags for - * `tags` an array of tags to associate with the `host` - * `source` the source to associate the tags with, default: user - * `callback` is an optional function to call with the results of the api call - * callback(error, result, status_code) - */ - if(typeof tags != 'object'){ - throw new Error('`tags` parameter must be an array'); - } - if(arguments.length < 4 && typeof arguments[2] == 'function'){ - callback = arguments[2]; +/*section: tag + *comment: | + * update the host tags for the provided host name or host id + *params: + * hostname: | + * the hostname or host id + * tags: | + * list of `:` tags to assign to the server + * source: | + * optional, the source of the tags (e.g. chef, puppet, etc) [default: users] + * callback: | + * function callback(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.tag.update("host.name", function(err, results){ + * console.dir(results); + * }); + * ``` + */ +function update(hostname, tags, source, callback){ + if(arguments.length < 4 && typeof arguments[2] === "function"){ + callback = source; source = undefined; } - params = { - query: { - source: source, - }, + var params = { body: { tags: tags, - } + source: source + }, }; - - this.request('PUT', util.format('/tags/hosts/%s', host), params, callback); + client.request("PUT", "/tags/hosts/" + hostname, params, callback); }; -tag_api.prototype.detach_tags = function(host, source, callback){ - /* - * tag_api.detach_tags(host, [[source], callback]) - * - * method to remove tags for a given `host` - * - * `host` the hostname or id of the machine to remove the tags for - * `source` the source of the tags - * `callback` is an optional function to call with the results of the api call - * callback(error, result, status_code) - */ - if(arguments.length < 3 && typeof arguments[1] == 'function'){ - callback = arguments[1]; +/*section: tag + *comment: | + * delete the host tags for the provided host name or host id + *params: + * hostname: | + * the hostname or host id + * source: | + * optional, the source of the tags (e.g. chef, puppet, etc) [default: users] + * callback: | + * function callback(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.tag.delete("host.name", function(err, results){ + * console.dir(results); + * }); + * ``` + */ +function delete_tags(hostname, source, callback){ + if(arguments.length < 3 && typeof arguments[1] === "function"){ + callback = source; source = undefined; } - params = { + var params = { query: { - source: source, - }, + source: source + } }; - - this.request('DELETE', util.format('/tags/hosts/%s', host), params, callback); + client.request("DELETE", "/tags/hosts/" + hostname, params, callback); }; -return module.exports = tag_api; +module.exports = { + _client: client, + get_all: get_all, + get: get, + create: create, + update: update, + "delete": delete_tags, +} diff --git a/lib/index.js b/lib/index.js index 7cfc672..2f2234f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,36 +1,35 @@ -var extend = require('extend'); +require("./api")(module.exports); -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'); -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'); -var downtime_api = require('./api/downtime.js'); - - -var dogapi = function(options){ - http_client.call(this, options); +/*section: dogapi + *params: + * options: + * | + * An object which allows you to override the default set parameters for interacting + * with the datadog api. The available options are. + * * api_key: your api key + * * app_key: your app key + * * api_version: the version of the api [default: `v1`] + * * api_host: the host to call [default: `api.datadoghq.com`] + *example: + * | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "", + * app_key: "" + * }; + * dogapi.initialize(options); + * dogapi.event.create(...); + * ``` + */ +function initialize(options){ + options = options || {}; + for(var key in options){ + if(module.exports.client.hasOwnProperty(key)){ + module.exports.client[key] = options[key]; + } + } }; -extend(dogapi.prototype, - http_client.prototype, - alert_api.prototype, - dash_api.prototype, - screen_api.prototype, - event_api.prototype, - tag_api.prototype, - metric_api.prototype, - search_api.prototype, - service_check_api.prototype, - snapshot_api.prototype, - downtime_api.prototype); - -dogapi.constants = constants; - -return module.exports = dogapi; +module.exports.client = require("./client"), +module.exports.initialize = initialize; From 5b868c60dbbb024f97f6cf0086cb50520a0ca33c Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Wed, 18 Mar 2015 08:53:22 -0400 Subject: [PATCH 03/65] add documentation generation --- docs/create.js | 109 ++++++++++++++++++++++++++++++++++++++++++++++++ docs/styles.css | 49 ++++++++++++++++++++++ package.json | 8 +++- 3 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 docs/create.js create mode 100644 docs/styles.css diff --git a/docs/create.js b/docs/create.js new file mode 100644 index 0000000..2e0df72 --- /dev/null +++ b/docs/create.js @@ -0,0 +1,109 @@ +var path = require("path"); + +var docast = require("docast"); +var glob = require("glob"); +var marked = require("marked"); +var yaml = require("js-yaml"); + +marked.setOptions({ + gfm: true, + sanitize: true, + pedantic: false +}); + +var above = path.resolve(__dirname, "../lib/"); +var match = above + "/**/*.js"; + +var docs = {}; +glob(match, function(er, files){ + files.forEach(function(file){ + var comments = docast.parse(file); + for(var i = 0; i < comments.length; ++i){ + var comment = comments[i]; + try{ + comment.doc = yaml.safeLoad(comment.doc); + if(!comment.doc.hasOwnProperty("section")){ + continue; + } + if(!docs[comment.doc.section]){ + docs[comment.doc.section] = {}; + } + + for(var key in comment.doc.params){ + if(comment.doc.params.hasOwnProperty(key)){ + comment.doc.params[key] = comment.doc.params[key].replace("optional", "_optional_"); + comment.doc.params[key] = marked(comment.doc.params[key]); + } + } + if(comment.doc.hasOwnProperty("example")){ + comment.doc.example = marked(comment.doc.example); + } else { + comment.doc.example = ""; + } + + if(comment.doc.hasOwnProperty("comment")){ + comment.doc.comment = marked(comment.doc.comment); + } else { + comment.doc.comment = ""; + } + + docs[comment.doc.section][comment.name] = comment.doc; + } catch(e){} + } + }); + + var output = "\n\n\n\n\n\n\n\n\n"; + output += "
    \n"; + for(var section in docs){ + if(!docs.hasOwnProperty(section)){ + continue; + } + output += "
  • " + section + "
  • \n"; + } + output += "
\n"; + + for(var section in docs){ + if(!docs.hasOwnProperty(section)){ + continue; + } + methods = docs[section]; + + output += "
\n"; + output += "

" + section + "

\n"; + for(var name in methods){ + if(!methods.hasOwnProperty(name)){ + continue; + } + doc = methods[name]; + + var className = section + "-" + name; + output += "
\n"; + + output += "

" + name + "

\n"; + output += "
\n"; + output += "
\n"; + output += doc.comment; + output += "

Parameters:

\n"; + for(var param in doc.params){ + if(!doc.params.hasOwnProperty(param)){ + continue; + } + var comment = doc.params[param]; + output += "
" + param + "
\n"; + output += comment; + } + output += "
\n"; + + output += "
\n"; + output += doc.example; + output += "
\n"; + + output += "
\n"; + output += "
\n"; + + } + output += "
\n"; + } + output += ""; + console.log(output); +}); diff --git a/docs/styles.css b/docs/styles.css new file mode 100644 index 0000000..e758e2c --- /dev/null +++ b/docs/styles.css @@ -0,0 +1,49 @@ + +h2 { + width: 100%; + font-size: 2em; + background-color: #eee; +} + +h3 { + width: 100%; + font-size: 1.5em; +} + +.function { + border-top: solid 1px #eee; +} + +.function .container { + display: flex; +} + +.function .doc { + width: 50%; +} + +.function .doc p { + text-indent: 1.5em; +} + +.function .example { + width: 50%; + flex-grow: 2; + flex-basis: auto; + overflow-x: scroll; +} + +.function .example code { + border-radius: 5px; + border: solid 1px #000; +} + +@media all and (max-width: 800px) { + .function .container { + flex-direction: column; + } + + .function .doc, .function .example { + width: 100%; + } +} diff --git a/package.json b/package.json index d94f0d8..1cfe83a 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "dog api" ], "author": "Brett Langdon (http://brett.is)", - "contributors":[ + "contributors": [ "colinjonesx (http://www.thetestpeople.com)", "dalehamel (http://blog.srvthe.net)" ], @@ -28,5 +28,11 @@ "gitHead": "f388635a5ab4f4da25702dc0999385d437bdf2bc", "dependencies": { "extend": "^2.0.0" + }, + "devDependencies": { + "docast": "^0.1.1", + "glob": "^5.0.3", + "js-yaml": "^3.2.7", + "marked": "^0.3.3" } } From 192ba230beaa35f4a1812874a94a53dddc9757f5 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 21 Mar 2015 08:59:26 -0400 Subject: [PATCH 04/65] fix up documentation template some more --- docs/create.js | 71 +++++++++++++++++++++++++++++++++++-------------- docs/styles.css | 49 ---------------------------------- 2 files changed, 51 insertions(+), 69 deletions(-) delete mode 100644 docs/styles.css diff --git a/docs/create.js b/docs/create.js index 2e0df72..43eb893 100644 --- a/docs/create.js +++ b/docs/create.js @@ -52,15 +52,26 @@ glob(match, function(er, files){ } }); - var output = "\n\n\n\n\n\n\n\n\n"; - output += "
    \n"; + var output = "\n\n\n"; + output += "\n"; + output += "\n\n"; + output += "\n"; + output += "\n"; + output += "\n\n"; + output += "Jump To Top\n"; + output += "
    \n"; + output += "
    \n"; + output += "
    \n"; + output += "

    Node Dogapi

    \n"; + output += "
      \n"; for(var section in docs){ if(!docs.hasOwnProperty(section)){ continue; } - output += "
    • " + section + "
    • \n"; + output += "
    • " + section + "\n"; + output += "
    • \n"; } - output += "
    \n"; + output += "
\n\n\n"; for(var section in docs){ if(!docs.hasOwnProperty(section)){ @@ -68,8 +79,19 @@ glob(match, function(er, files){ } methods = docs[section]; - output += "
\n"; - output += "

" + section + "

\n"; + output += "
\n"; + output += "
\n

" + section + "

\n"; + + output += "
    \n"; + for(var name in methods){ + if(!methods.hasOwnProperty(name)){ + continue; + } + output += "
  • " + name + "
  • \n"; + } + + output += "
\n"; + for(var name in methods){ if(!methods.hasOwnProperty(name)){ continue; @@ -77,33 +99,42 @@ glob(match, function(er, files){ doc = methods[name]; var className = section + "-" + name; - output += "
\n"; + output += "
\n"; + + var definition = name + "("; + if(doc.params && typeof doc.params === "object"){ + definition += Object.keys(doc.params).join(", "); + } + definition += ")"; - output += "

" + name + "

\n"; - output += "
\n"; - output += "
\n"; + output += "

" + definition + "

\n"; + output += "
\n"; output += doc.comment; - output += "

Parameters:

\n"; - for(var param in doc.params){ - if(!doc.params.hasOwnProperty(param)){ - continue; + if(doc.params){ + output += "

Parameters:

\n"; + output += "
\n"; + for(var param in doc.params){ + if(!doc.params.hasOwnProperty(param)){ + continue; + } + var comment = doc.params[param]; + output += "
" + param + "
\n"; + output += "
" + comment + "
\n"; } - var comment = doc.params[param]; - output += "
" + param + "
\n"; - output += comment; + output += "
\n"; } output += "
\n"; - output += "
\n"; + output += "
\n"; output += doc.example; output += "
\n"; output += "
\n"; - output += "
\n"; - } output += "
\n"; } + output += "\n"; + output += "\"Fork"; output += ""; console.log(output); }); diff --git a/docs/styles.css b/docs/styles.css deleted file mode 100644 index e758e2c..0000000 --- a/docs/styles.css +++ /dev/null @@ -1,49 +0,0 @@ - -h2 { - width: 100%; - font-size: 2em; - background-color: #eee; -} - -h3 { - width: 100%; - font-size: 1.5em; -} - -.function { - border-top: solid 1px #eee; -} - -.function .container { - display: flex; -} - -.function .doc { - width: 50%; -} - -.function .doc p { - text-indent: 1.5em; -} - -.function .example { - width: 50%; - flex-grow: 2; - flex-basis: auto; - overflow-x: scroll; -} - -.function .example code { - border-radius: 5px; - border: solid 1px #000; -} - -@media all and (max-width: 800px) { - .function .container { - flex-direction: column; - } - - .function .doc, .function .example { - width: 100%; - } -} From df67f3d90b1aeff23ec6e8e7d2e6d9945a0fdab3 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 21 Mar 2015 08:59:33 -0400 Subject: [PATCH 05/65] add script to build docs --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 1cfe83a..fc139a3 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "Datadog API Node.JS Client", "main": "lib/index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 1", + "docs": "node ./docs/create.js > index.html" }, "repository": { "type": "git", From 306c3699263d063809fb7755633ce7b7a50aa31c Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 21 Mar 2015 09:05:41 -0400 Subject: [PATCH 06/65] re-write metric api --- lib/api/metric.js | 182 +++++++++++++++++++++++++++++++++------------- 1 file changed, 133 insertions(+), 49 deletions(-) diff --git a/lib/api/metric.js b/lib/api/metric.js index 6cdba32..97f530f 100644 --- a/lib/api/metric.js +++ b/lib/api/metric.js @@ -1,59 +1,143 @@ -var metric_api = function(){}; +var client = require("../client"); -metric_api.prototype.add_metric = function(metric, callback){ - /* - * metric_api.add_metric(metric, [callback]) - * - * method used to add a single metric to datadog - * - * `metric` an object representation of the metric - * metric: *required*, the name of the metric - * points: *required*, an array of elements [ timestamp, value ] - * host: name of the host that produced the event - * tags: array of tags to associate with the event - * type: "guage" or "counter" - * - * `callback` an optional function to call with the results - * callback(metric, [callback]) - */ - var metrics = { - 'series': [metric] - }; - this.add_metrics(metrics, callback); -}; -metric_api.prototype.add_metrics = function(metrics, callback){ - /* - * metric_api.add_metrics(metrics, [callback]) - * - * method used to add multiple metrics to datadog - * - * `metrics` an object representation of the metric: - * series: an array of `metrics` to add - * - * `callback` an optional function to call with the results - * callback(metric, [callback]) - */ - if(typeof metrics != 'object'){ - throw new Error('`metrics` parameter must be an object'); +/*section: metric + *comment: | + * submit a new metric + *params: + * metric: the metric name + * points: | + * single datapoint or array of [timestamp, datapoint], if a single point + * is given "now" is used as the timestamp + * extra: | + * optional, object which can contain the following keys + * * host: the host source of the metric + * * tags: array of "tag:value"'s to use for the metric + * * metric_type: which metric type to use ("gauge" or "counter") [default: gauge] + * callback: | + * function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.metric.send("my.metric", 1000, function(err, results){ + * console.dir(results); + * }); + * var now = parseInt(new Date().getTime() / 1000); + * dogapi.metric.send("my.metric", [now, 1000], function(err, results){ + * console.dir(results); + * }); + * ``` + */ +function send(metric, points, extra, callback){ + if(arguments.length < 4 && typeof arguments[2] === "function"){ + callback = tags; + extra = {}; } - if(!metrics['series'] || typeof metrics['series'] != 'object'){ - throw new Error('`metrics["series"]` parameter must be an array'); - } - - for(var i in metrics['series']){ - var metric = metrics['series'][i]; - if(!metric['metric']){ - throw new Error('metric["metric"] is required'); + extra = extra || {}; + var series = [ + { + metric: metric, + points: points, + host: extra.host, + tags: extra.tags, + metric_type: extra.metric_type } + ]; + + send_all(series, callback); +} - if(!metric['points'] || typeof metric['points'] != 'object'){ - throw new Error('metric["points"] must be an array'); +/*section: metric + *comment: | + * send a list of metrics + *params: + * metrics: | + * an array of metrics where each element is an object with the following keys + * * metric: the name of the metric + * * points: a single datapoint or an array of [timestamp, datapoint] (same as `dogapi.metric.send`) + * * tags: an array of "tag:value"'s + * * host: the source hostname to use for the metrics + * * metric_type: the type of metric to use ("gauge" or "counter") [default: gauge] + * callback: | + * function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * var now = parseInt(new Date().getTime() / 1000); + * var metrics = [ + * { + * metric: "my.metric", + * points: [now, 1000], + * tags: ["tag:value"] + * }, + * { + * metric: "another.metric", + * points: 1000 + * } + * ]; + * dogapi.metric.send_all(metrics, function(err, results){ + * console.dir(results); + * }); + * ``` + */ +function send_all(metrics, callback){ + var now = parseInt(new Date().getTime() / 1000); + for(var i = 0; i < metrics.length; ++i){ + if(!Array.isArray(metrics[i].points)){ + metrics[i].points = [now, metrics[i].points]; } } + var params = { + body: metrics + }; + client.request("POST", "/series", params, callback); +} - this.request('POST', '/series', {body: metrics}, callback); -}; +/*section: metric + *comment: | + * make a metric query + *params: + * from: POSIX timestamp for start of query + * to: POSIX timestamp for end of query + * query: the string query to perform (e.g. "system.cpu.idle{*}by{host}") + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * var now = parseInt(new Date().getTime() / 1000); + * var then = now - 3600; // one hour ago + * var query = "system.cpu.idle{*}by{host}"; + * dogapi.metric.query(then, now, query, function(err, res){ + * console.dir(res); + * }); + * ``` + */ +function query(from, to, query, callback){ + var params = { + from: from, + to: to, + query: query + }; + client.request("GET", "/query", params, callback); +} -return module.exports = metric_api; +module.exports = { + send: send, + send_all: send_all +}; From d9ad10a9a8f52873763272f458acdf22206a55a4 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 21 Mar 2015 09:10:48 -0400 Subject: [PATCH 07/65] re-write the event api --- lib/api/event.js | 349 +++++++++++++++++++---------------------------- 1 file changed, 138 insertions(+), 211 deletions(-) diff --git a/lib/api/event.js b/lib/api/event.js index d2a9cb5..8801cf9 100644 --- a/lib/api/event.js +++ b/lib/api/event.js @@ -1,216 +1,143 @@ -var extend = require('extend'); -var util = require('util'); - - -var event_api = function(){}; - -event_api.prototype.stream = function(start, end, filter, callback){ - /* - * event_api.stream( start, end, [[filter], callback] ) - * - * function used to retrieve all events that have occured between - * `start` and `end` (POSIX timestamps) - * - * optionally filter the query with `filter`: - * { - * 'priority': ("low" or "normal"), - * 'sources': ["sources", "as", "a", "list"], - * 'tags': ["tags", "as", "a", "list"], - * } - * - * optionally provide a `callback` function to get the result - * of this api call: - * callback(error, result, status_code) - */ - if(arguments.length < 2){ - throw new Error('parameters `start` and `end` are required'); - } - query = { - start: parseInt(start), - end: parseInt(end), +var client = require("../client"); +var util = require("util"); + +/*section: event + *comment: | + * create a new event + *params: + * title: the title of the event + * text: the body of the event + * properties: | + * an optional object continaing any of the following additional optional properties + * * date_happened: POSIX timestamp of when it happened + * * priority: "normal" or "low" [defualt: "normal"] + * * host: the host name to associate with the event + * * tags: array of "tag:value"'s to associate with the event + * * alert_type: "error", "warning", "info" or "success" [defualt: "info"] + * * aggregation_key: an arbitrary string used to aggregate like events + * * source_type_name: options: "nagios", "hudson", "jenkins", "user", "my apps", "feed", "chef", "puppet", "git", "bitbucket", "fabric", "capistrano" + * callback: | + * function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * var title = "some new event"; + * var text = "IT HAPPENED!"; + * dogapi.event.create(title, text, function(err, res){ + * console.dir(res); + * }); + * title = "another event"; + * text = "IT HAPPENED AGAIN!"; + * var properties = { + * tags: ["some:tag"], + * alert_type: "error" + * }; + * dogapi.event.create(title, text, properties, function(err, res){ + * console.dir(res); + * }); + * ``` + */ +function create(title, text, properties, callback){ + if(arguments.length < 4 && typeof arguments[2] === "function"){ + callback = properties; + properties = {}; + } + if(typeof properties !== "object"){ + properties = {}; + } + + properties.title = tile; + properties.text = text; + + var params = { + body: properties }; - - // this is the only case we have to check - // if we have `event_api(1234, 5678, callback)` then - // we want to push callback back - if(arguments.length == 3 && typeof arguments[2] == 'function'){ - callback = arguments[2]; - filter = {}; - } - - // validate the filters we were given and append to `query` - // if they exist and meet their requirements - if(filter['priority'] && ['low', 'normal'].indexOf(filter['priority'].toLowerCase()) >= 0){ - query['priority'] = filter['priority'].toLowerCase(); - } - if(filter['sources'] && typeof filter['sources'] == 'object'){ - query['sources'] = filter['sources'].join(); - } - if(filter['tags'] && typeof filter['tags'] == 'object'){ - query['tags'] = filter['tags'].join(); - } - - params = { - query: query, + client.request("POST", "/events", params, callback); +} + +/*section: event + *comment: | + * get event details from the provided event id + *params: + * eventId: | + * the id of the event to fetch + * callback: | + * function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.event.get(10005, function(err, res){ + * console.dir(res); + * }); + * ``` + */ +function get(eventId, callback){ + client.request("GET", util.format("/events/%s", eventId), callback); +} + +/*section: event + *comment: | + * query the event stream + *params: + * start: POSIX timestamp for start of query + * end: POSIX timestamp for end of query + * parameters: | + * optional parameters to use for the query + * * priority: "low" or "normal" + * * sources: comma separated list of sources (e.g. "jenkins,user") + * * tags: comma separated list of tags (e.g. "tag:value1,tag:value2") + * callback: | + * function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * var now = parseInt(new Date().getTime() / 1000); + * var then = now - 3600; // an hour ago + * var parameters = { + * tags: "some:tag", + * sources: "jenkins" + * }; + * dogapi.event.query(then, now, parameters, function(err, res){ + * console.dir(res); + * }); + * ``` + */ +function query(start, end, parameters, callback){ + if(arguments.length < 4 && typeof argument[2] === "function"){ + callback = parameters; + parameters = {}; + } + + if(typeof parameters !== "object"){ + parameters = {} + } + parameters.start = start; + parameters.end = end; + + var params = { + query: parameters }; - this.request('GET', '/events', params, callback); -}; -event_api.prototype.polling_stream = function(interval, filter, callback){ - /* - * event_api.polling_stream(interval, [[filter], callback] ) - * - * function used to continuously call `stream` for new events - * - * `interval` seconds between each call - * `filter` is an object to limit the results by - * `callback` is an optional function called with the results of the api call - * callback(error, result, status_code) - */ - if(arguments.length < 3 && typeof arguments[1] == 'function'){ - callback = arguments[1]; - filter = {}; - } - if(typeof filter != 'object'){ - throw new Error('`filter` parameter must be an object'); - } - - var last_run = new Date().getTime() / 1000; - var self = this; - setInterval(function(){ - var start = last_run; - last_run = new Date().getTime() / 1000; - self.stream(start, last_run, filter, callback); - }, interval * 1000); - -}; - -event_api.prototype.get_event = function(event_id, callback){ - /* - * event_api.get_event(event_id, callback) - * - * method used to retrieve a single event's data - * - * `event_id` the id of the event to get the information for - * `callback` an optional function called with the results - * callback(error, result, status_code) - */ - if(!event_id){ - throw new Error('`event_id` parameter is required'); - } + client.request("GET", "/events", params, callback); +} - this.request('GET', util.format('/events/%s', event_id), callback); +module.exports = { + create: create, + get: get, + query: query }; - -event_api.prototype.add_event = function(event, callback){ - /* - * event_api.add_event(event, callback) - * - * method used to add a new event to datadog - * - * `event` is an object containing any of the following: - * title: *required*, string, title for the new event - * text: *required*, string, event message - * date_happened: int, when the event occurred. if unset defaults to the current time. (POSIX timestamp) - * handle: string, user to post the event as. defaults to owner of the application key used to submit. - * priority: string, priority to post the event as. ("normal" or "low", defaults to "normal") - * related_event_id: post event as a child of the given event - * tags: array, tags to post the event with - * host: string, host to post the event with - * device_name: string, device_name to post the event with - * aggregation_ket: string, key to aggregate this event on - * - * `callback` is an optional function for the result - * callback(error, result, status_code) - */ - if(typeof event != 'object'){ - throw new Error('`event` parameter must be an object'); - } - - if(!event['title']){ - throw new Error('`title` property of `event` parameter is required'); - } - - if(!event['text']){ - throw new Error('`text` property of `event` parameter is required'); - } - - if(!event['date_happened']){ - event['date_happened'] = Math.round((new Date()).getTime() / 1000); - } - - if(event['priority']){ - if(['normal', 'low'].indexOf(event['priority'].toLowerCase()) == -1){ - event['priority'] = undefined; - } - } - - if(event['tags']){ - event['tags'] = event['tags'].join(); - } - - this.request('POST', '/events', {body: event}, callback); -}; - -event_api.prototype.add_comment = function(comment, callback){ - /* - * event_api.add_comment(comment, [callback]) - * - * method used to add a new comment to datadog - * - * `comment` is a object representation of the new comment: - * message: *require*, string, comment text body - * handle: string, optional handle name to use instead of owner of the app key - * related_event_id: string, optional event id to attach this comment to - * - * `callback` is an optional function for the result of the call - * callback(error, result, status_code) - */ - if(typeof comment != 'object'){ - throw new Error('`comment` parameter must be an object'); - } - - if(!comment['message']){ - throw new Error('`message` property of `comment` paramter is required'); - } - - this.request('POST', '/comments', {body: comment}, callback); -}; - -event_api.prototype.update_comment = function(comment_id, comment, callback){ - /* - * event_api.update_comment(comment_id, comment, callback) - * - * method used to update a comment that already exists in datadog - * - * `comment_id` is the id of the comment to update - * `comment` an object representation of the comment changes to make - * message: string, the new message text for the comment - * handle: string, the new owner of the comment - * - * `callback` an optional callback to call with the result - * callback(error, result, status_code) - */ - if(typeof comment != 'object'){ - throw new Error('`comment` parameter must be an object'); - } - - this.request('PUT', util.format('/comments/%s', comment_id), {body: comment}, callback); -}; - -event_api.prototype.delete_comment = function(comment_id, callback){ - /* - * event_api.delete_comment(comment_id, callback) - * - * method used to remove a comment from datadog - * - * `comment_id` the comment id for the comment to remove - * `callback` an optional function to handle the result - * callback(error, result, status_code) - * - */ - this.request('DELETE', util.format('/comments/%s', comment_id), callback); -}; - - -return module.exports = event_api; From ac399d191e96f1d5b52f3a38838f32ea8d333f55 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 21 Mar 2015 09:11:05 -0400 Subject: [PATCH 08/65] expose the new metric and event apis --- lib/api/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/api/index.js b/lib/api/index.js index 04d809b..c83395a 100644 --- a/lib/api/index.js +++ b/lib/api/index.js @@ -1,5 +1,7 @@ var api = { - tag: require("./tag") + tag: require("./tag"), + metric: require("./metric"), + event: require("./event") }; module.exports = function(obj){ From 508b4d0d0fcb0215c451918c8c03f7730ab5c983 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 21 Mar 2015 09:11:30 -0400 Subject: [PATCH 09/65] build documentation --- index.html | 553 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 553 insertions(+) create mode 100644 index.html diff --git a/index.html b/index.html new file mode 100644 index 0000000..a7cdbb5 --- /dev/null +++ b/index.html @@ -0,0 +1,553 @@ + + + + + + + + + + +Jump To Top +
+
+
+

Node Dogapi

+ +
+
+
+
+

event

+ +
+

create(title, text, properties, callback)

+
+

create a new event

+

Parameters:

+
+
title
+

the title of the event

+
+
text
+

the body of the event

+
+
properties
+

an optional object continaing any of the following additional optional properties

+
    +
  • date_happened: POSIX timestamp of when it happened
  • +
  • priority: "normal" or "low" [defualt: "normal"]
  • +
  • host: the host name to associate with the event
  • +
  • tags: array of "tag:value"'s to associate with the event
  • +
  • alert_type: "error", "warning", "info" or "success" [defualt: "info"]
  • +
  • aggregation_key: an arbitrary string used to aggregate like events
  • +
  • source_type_name: options: "nagios", "hudson", "jenkins", "user", "my apps", "feed", "chef", "puppet", "git", "bitbucket", "fabric", "capistrano"
  • +
+
+
callback
+

function(err, res)

+
+
+
+
+
 var dogapi = require("dogapi");
+ var options = {
+   api_key: "api_key",
+   app_key: "app_key"
+ };
+ dogapi.initialize(options);
+ var title = "some new event";
+ var text = "IT HAPPENED!";
+ dogapi.event.create(title, text, function(err, res){
+   console.dir(res);
+ });
+ title = "another event";
+ text = "IT HAPPENED AGAIN!";
+ var properties = {
+   tags: ["some:tag"],
+   alert_type: "error"
+ };
+ dogapi.event.create(title, text, properties, function(err, res){
+   console.dir(res);
+ });
+
+
+
+
+

get(eventId, callback)

+
+

get event details from the provided event id

+

Parameters:

+
+
eventId
+

the id of the event to fetch

+
+
callback
+

function(err, res)

+
+
+
+
+
 var dogapi = require("dogapi");
+ var options = {
+   api_key: "api_key",
+   app_key: "app_key"
+ };
+ dogapi.initialize(options);
+ dogapi.event.get(10005, function(err, res){
+   console.dir(res);
+ });
+
+
+
+
+

query(start, end, parameters, callback)

+
+

query the event stream

+

Parameters:

+
+
start
+

POSIX timestamp for start of query

+
+
end
+

POSIX timestamp for end of query

+
+
parameters
+

optional parameters to use for the query

+
    +
  • priority: "low" or "normal"
  • +
  • sources: comma separated list of sources (e.g. "jenkins,user")
  • +
  • tags: comma separated list of tags (e.g. "tag:value1,tag:value2")
  • +
+
+
callback
+

function(err, res)

+
+
+
+
+
 var dogapi = require("dogapi");
+ var options = {
+   api_key: "api_key",
+   app_key: "app_key"
+ };
+ dogapi.initialize(options);
+ var now = parseInt(new Date().getTime() / 1000);
+ var then = now - 3600; // an hour ago
+ var parameters = {
+   tags: "some:tag",
+   sources: "jenkins"
+ };
+ dogapi.event.query(then, now, parameters, function(err, res){
+   console.dir(res);
+ });
+
+
+
+
+
+
+

metric

+ +
+

send(metric, points, extra, callback)

+
+

submit a new metric

+

Parameters:

+
+
metric
+

the metric name

+
+
points
+

single datapoint or array of [timestamp, datapoint], if a single point +is given "now" is used as the timestamp

+
+
extra
+

optional, object which can contain the following keys

+
    +
  • host: the host source of the metric
  • +
  • tags: array of "tag:value"'s to use for the metric
  • +
  • metric_type: which metric type to use ("gauge" or "counter") [default: gauge]
  • +
+
+
callback
+

function(err, res)

+
+
+
+
+
 var dogapi = require("dogapi");
+ var options = {
+   api_key: "api_key",
+   app_key: "app_key"
+ };
+ dogapi.initialize(options);
+ dogapi.metric.send("my.metric", 1000, function(err, results){
+   console.dir(results);
+ });
+ var now = parseInt(new Date().getTime() / 1000);
+ dogapi.metric.send("my.metric", [now, 1000], function(err, results){
+   console.dir(results);
+ });
+
+
+
+
+

send_all(metrics, callback)

+
+

send a list of metrics

+

Parameters:

+
+
metrics
+

an array of metrics where each element is an object with the following keys

+
    +
  • metric: the name of the metric
  • +
  • points: a single datapoint or an array of [timestamp, datapoint] (same as dogapi.metric.send)
  • +
  • tags: an array of "tag:value"'s
  • +
  • host: the source hostname to use for the metrics
  • +
  • metric_type: the type of metric to use ("gauge" or "counter") [default: gauge]
  • +
+
+
callback
+

function(err, res)

+
+
+
+
+
 var dogapi = require("dogapi");
+ var options = {
+   api_key: "api_key",
+   app_key: "app_key"
+ };
+ dogapi.initialize(options);
+ var now = parseInt(new Date().getTime() / 1000);
+ var metrics = [
+   {
+     metric: "my.metric",
+     points: [now, 1000],
+     tags: ["tag:value"]
+   },
+   {
+     metric: "another.metric",
+     points: 1000
+   }
+ ];
+ dogapi.metric.send_all(metrics, function(err, results){
+   console.dir(results);
+ });
+
+
+
+
+

query(from, to, query, callback)

+
+

make a metric query

+

Parameters:

+
+
from
+

POSIX timestamp for start of query

+
+
to
+

POSIX timestamp for end of query

+
+
query
+

the string query to perform (e.g. "system.cpu.idle{*}by{host}")

+
+
callback
+

function(err, res)

+
+
+
+
+
 var dogapi = require("dogapi");
+ var options = {
+   api_key: "api_key",
+   app_key: "app_key"
+ };
+ dogapi.initialize(options);
+ var now = parseInt(new Date().getTime() / 1000);
+ var then = now - 3600; // one hour ago
+ var query = "system.cpu.idle{*}by{host}";
+ dogapi.metric.query(then, now, query, function(err, res){
+   console.dir(res);
+ });
+
+
+
+
+
+
+

tag

+ +
+

get_all(source, callback)

+
+

get all host tags

+

Parameters:

+
+
source
+

optional, only show tags for a particular source [default: null]

+
+
callback
+

function callback(err, res)

+
+
+
+
+
var dogapi = require("dogapi");
+var options = {
+  api_key: "api_key",
+  app_key: "app_key"
+};
+dogapi.initialize(options);
+dogapi.tag.get_all(function(err, results){
+  console.dir(results);
+});
+
+
+
+
+

get(hostname, options, callback)

+
+

get the host tags for a provided host name or host id

+

Parameters:

+
+
hostname
+

the hostname or host id

+
+
options
+

optional, an object of options for the query allowing the following

+
    +
  • source: the source of the tags (e.g. chef, puppet, users, etc) [default: null]
  • +
  • by_source: whether or not to group the results by source [default: false]
  • +
+
+
callback
+

function callback(err, res)

+
+
+
+
+
var dogapi = require("dogapi");
+var options = {
+  api_key: "api_key",
+  app_key: "app_key"
+};
+dogapi.initialize(options);
+dogapi.tag.get("host.name", function(err, results){
+  console.dir(results);
+});
+
+
+
+
+

create(hostname, tags, source, callback)

+
+

assign new host tags to the provided host name or host id

+

Parameters:

+
+
hostname
+

the hostname or host id

+
+
tags
+

list of <tag>:<value> tags to assign to the server

+
+
source
+

optional, the source of the tags (e.g. chef, puppet, etc) [default: users]

+
+
callback
+

function callback(err, res)

+
+
+
+
+
var dogapi = require("dogapi");
+var options = {
+  api_key: "api_key",
+  app_key: "app_key"
+};
+dogapi.initialize(options);
+dogapi.tag.create("host.name", ["role:webserver"], function(err, results){
+  console.dir(results);
+});
+
+
+
+
+

update(hostname, tags, source, callback)

+
+

update the host tags for the provided host name or host id

+

Parameters:

+
+
hostname
+

the hostname or host id

+
+
tags
+

list of <tag>:<value> tags to assign to the server

+
+
source
+

optional, the source of the tags (e.g. chef, puppet, etc) [default: users]

+
+
callback
+

function callback(err, res)

+
+
+
+
+
var dogapi = require("dogapi");
+var options = {
+  api_key: "api_key",
+  app_key: "app_key"
+};
+dogapi.initialize(options);
+dogapi.tag.update("host.name", function(err, results){
+  console.dir(results);
+});
+
+
+
+
+

delete_tags(hostname, source, callback)

+
+

delete the host tags for the provided host name or host id

+

Parameters:

+
+
hostname
+

the hostname or host id

+
+
source
+

optional, the source of the tags (e.g. chef, puppet, etc) [default: users]

+
+
callback
+

function callback(err, res)

+
+
+
+
+
var dogapi = require("dogapi");
+var options = {
+  api_key: "api_key",
+  app_key: "app_key"
+};
+dogapi.initialize(options);
+dogapi.tag.delete("host.name", function(err, results){
+  console.dir(results);
+});
+
+
+
+
+
+
+

client

+ +
+

client()

+
+

the constructor for client object

+
+ +
+
+

request(method, path, params, callback)

+
+

used to make a raw request to the datadog api

+

Parameters:

+
+
method
+

http method GET, POST, PUT, DELETE

+
+
path
+

the api url path e.g. /tags/hosts

+
+
params
+

an object which allows the keys query or body

+
+
callback
+

function to call on success/failure callback(err, result)

+
+
+
+
+
var dogapi = require("dogapi");
+var options = {
+  api_key: "api_key",
+  app_key: "app_key"
+};
+dogapi.initialize(options);
+dogapi.client.request("GET", "/url/path", {}, function(err, results){
+  console.dir(results);
+});
+
+
+
+
+
+
+

dogapi

+ +
+

initialize(options)

+
+

Parameters:

+
+
options
+

An object which allows you to override the default set parameters for interacting +with the datadog api. The available options are.

+
    +
  • api_key: your api key
  • +
  • app_key: your app key
  • +
  • api_version: the version of the api [default: v1]
  • +
  • api_host: the host to call [default: api.datadoghq.com]
  • +
+
+
+
+
+
var dogapi = require("dogapi");
+var options = {
+  api_key: "<API_KEY_HERE>",
+  app_key: "<APP_KEY_HERE>"
+};
+dogapi.initialize(options);
+dogapi.event.create(...);
+
+
+
+
+
+Fork me on GitHub From 18180281804c09349e36813e8c1b4fc5f860cb05 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 21 Mar 2015 09:27:30 -0400 Subject: [PATCH 10/65] 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; From dfdf498da8b64c98cabbc4a6eaeed4014219ec4f Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 21 Mar 2015 09:28:10 -0400 Subject: [PATCH 11/65] build docs --- index.html | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/index.html b/index.html index a7cdbb5..a7040d7 100644 --- a/index.html +++ b/index.html @@ -20,6 +20,8 @@ document.addEventListener("DOMContentLoaded", function(){hljs.initHighlightingOn
  • metric
  • +
  • serviceCheck +
  • tag
  • client @@ -296,6 +298,57 @@ is given "now" is used as the timestamp

  • +
    +
    +

    serviceCheck

    + +
    +

    check(check, hostName, status, parameters, callback)

    +
    +

    post an update to a service check

    +

    Parameters:

    +
    +
    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)

    +
    +
    +
    +
    +
     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);
    + });
    +
    +
    +
    +

    tag

    From 43834e9fecebfcd214f7b62f8c5caadaf050ea23 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 21 Mar 2015 09:30:14 -0400 Subject: [PATCH 12/65] Add dogapi.now helper method --- lib/index.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/index.js b/lib/index.js index 893bb13..4d77121 100644 --- a/lib/index.js +++ b/lib/index.js @@ -31,8 +31,23 @@ function initialize(options){ } }; +/*section: dogapi + *comment: get the current POSIX timestamp + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * dogapi.now(); + * // this is the same as + * parseInt(new Date().getTime() / 1000); + * ``` + */ +function now(){ + return parseInt(new Date().getTime() / 1000); +}; + module.exports.client = require("./client"), module.exports.initialize = initialize; +module.exports.now = now; module.exports.OK = 0; module.exports.WARNING = 1; module.exports.CRITICAL = 2; From c27d73b0eef5bfac701520361b2183f372101f6f Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 21 Mar 2015 09:30:19 -0400 Subject: [PATCH 13/65] build docs --- index.html | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/index.html b/index.html index a7040d7..cdab18b 100644 --- a/index.html +++ b/index.html @@ -572,6 +572,7 @@ dogapi.client.request("GET", "/url/path", {}, function(err,

    dogapi

    initialize(options)

    @@ -601,6 +602,19 @@ dogapi.event.create(...);
    +
    +

    now()

    +
    +

    get the current POSIX timestamp

    +
    +
    +
    var dogapi = require("dogapi");
    +dogapi.now();
    +// this is the same as
    +parseInt(new Date().getTime() / 1000);
    +
    +
    +
    Fork me on GitHub From 7250b5262efe2ee31e2a8a77c62100a0e88cc0f1 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 21 Mar 2015 09:36:46 -0400 Subject: [PATCH 14/65] add user api --- lib/api/index.js | 3 ++- lib/api/user.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 lib/api/user.js diff --git a/lib/api/index.js b/lib/api/index.js index 3d0faa8..94cf122 100644 --- a/lib/api/index.js +++ b/lib/api/index.js @@ -2,7 +2,8 @@ var api = { tag: require("./tag"), metric: require("./metric"), event: require("./event"), - serviceCheck: require("./serviceCheck") + serviceCheck: require("./serviceCheck"), + user: require("./user") }; module.exports = function(obj){ diff --git a/lib/api/user.js b/lib/api/user.js new file mode 100644 index 0000000..580dd83 --- /dev/null +++ b/lib/api/user.js @@ -0,0 +1,33 @@ +var client = require("../client"); + +/*section: user + *comment: invite users via e-mail + *params: + * emails: an array of email addresses to send invites to + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * var emails = ["me@domain.com", "you@domain.com"]; + * dogapi.user.invite(emails, fuction(err, res){ + * console.dir(res): + * }); + * ``` + */ +function invite(emails, callback){ + var params = { + body: { + emails: emails + } + }; + client.request("POST", "/invite_users", params, callback); +}; + +module.exports = { + invite: invite +}; From 001f263963bd29b7d4917d523c92f200bb8fa4fc Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 21 Mar 2015 09:37:46 -0400 Subject: [PATCH 15/65] Add comment for dogapi.initialize --- lib/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/index.js b/lib/index.js index 4d77121..08f06fa 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,6 +1,7 @@ require("./api")(module.exports); /*section: dogapi + *comment: configure the dogapi client with your app/api keys *params: * options: * | From 0a06e34c52f0dd828f89772de31f55e7ec57a0a2 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 21 Mar 2015 09:37:55 -0400 Subject: [PATCH 16/65] build docs --- index.html | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/index.html b/index.html index cdab18b..5b0288c 100644 --- a/index.html +++ b/index.html @@ -24,6 +24,8 @@ document.addEventListener("DOMContentLoaded", function(){hljs.initHighlightingOn
  • tag
  • +
  • user +
  • client
  • dogapi @@ -517,6 +519,41 @@ dogapi.tag.delete("host.name", function(err, results){ +
    +
    +

    user

    + +
    +

    invite(emails, callback)

    +
    +

    invite users via e-mail

    +

    Parameters:

    +
    +
    emails
    +

    an array of email addresses to send invites to

    +
    +
    callback
    +

    function(err, res)

    +
    +
    +
    +
    +
    var dogapi = require("dogapi");
    +var options = {
    +  api_key: "api_key",
    +  app_key: "app_key"
    +};
    +dogapi.initialize(options);
    +var emails = ["me@domain.com", "you@domain.com"];
    +dogapi.user.invite(emails, fuction(err, res){
    +  console.dir(res):
    +});
    +
    +
    +
    +

    client

    @@ -577,6 +614,7 @@ dogapi.client.request("GET", "/url/path", {}, function(err,

    initialize(options)

    +

    configure the dogapi client with your app/api keys

    Parameters:

    options
    From 2251c3825938414d07fcb07d34034a1d9c557cd3 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 21 Mar 2015 09:39:16 -0400 Subject: [PATCH 17/65] remove docs from readme --- README.md | 91 ------------------------------------------------------- 1 file changed, 91 deletions(-) diff --git a/README.md b/README.md index 26d7293..b3cc743 100644 --- a/README.md +++ b/README.md @@ -45,97 +45,6 @@ var app = new dogapi(options); DD_API_KEY=YOUR_KEY_HERE DD_APP_KEY=YOUR_KEY_HERE node app.js ``` -## API - -`dogapi` implements all available functions in the official datadog api, http://docs.datadoghq.com/api/. - -* `dogapi.constants.STATUSES` - * `OK`, `WARNING`, `CRITICAL`, `UNKNOWN` -* `dogapi.stream(start, end, [[filter], callback])` - * function used to retrieve all events that have occured between -* `dogapi.polling_stream(interval, [[filter], callback])` - * function used to continuously call `stream` for new events -* `dogapi.get_event(event_id, callback)` - * method used to retrieve a single event's data -* `dogapi.add_event(event, callback)` - * method used to add a new event to datadog -* `dogapi.add_comment(comment, [callback])` - * method used to add a new comment to datadog -* `dogapi.update_comment(comment_id, comment, callback)` - * method used to update a comment that already exists in datadog -* `dogapi.delete_comment(comment_id, callback)` - * method used to remove a comment from datadog -* `dogapi.add_alert(alert, [callback])` - * add a new alert to datadog -* `dogapi.update_alert(alert_id, alert, [callback])` - * update an existing alert -* `dogapi.get_alert(alert_id, [callback])` - * get the details of an alert from the given id -* `dogapi.delete_alert(alert_id, [callback])` - * delete the given alert from datadog -* `dogapi.get_all_alerts([callback])` - * get the details of all alerts in datadog -* `dogapi.mute_alerts([callback])` - * mute all alerts -* `dogapi.unmute_alerts([callback])` - * unmute all alerts -* `dogapi.get_dashboard(dash_id, [callback])` - * method to get a single dashboard information -* `dogapi.get_all_dashboards([callback])` - * method to retrieve all dashboards in datadog -* `dogapi.create_dashboard(dashboard, [callback])` - * method used to create a new dashboard in datadog -* `dogapi.update_dashboard(dash_id, dashboard, [callback])` - * method used to update the dashboard with the provided `dash_id` -* `dogapi.delete_dashboard(dash_id, [callback])` - * method to remove a dashboard from datadog -* `dogapi.get_screenboard(screen_id, [callback])` - * method to get a single screenboard information -* `dogapi.get_all_screenboards([callback])` - * method to retrieve all screenboards in datadog -* `dogapi.create_screenboard(screenboard, [callback])` - * method used to create a new screenboard in datadog -* `dogapi.update_screenboard(screen_id, screenboard, [callback])` - * method used to update the screenboard with the provided `screen_id` -* `dogapi.delete_screenboard(screen_id, [callback])` - * method to remove a screenboard from datadog -* `dogapi.search(query, [callback])` - * method used to query the api for `metrics` or `hosts` -* `dogapi.add_metric(metric, [callback])` - * method used to add a single metric to datadog -* `dogapi.add_metrics(metrics, [callback])` - * method used to add multiple metrics to datadog -* `dogapi.all_tags([[source], callback])` - * method to get all the tags in datadog -* `dogapi.host_tags(host, [[source], callback])` - * method to get the tags associated with a given `host` -* `dogapi.host_tags_by_source(host, [[source], callback])` - * method to return the tags associated with a host, arranged by source -* `dogapi.add_tags(host, tags, [[source], callback])` - * add new tags to given `host` -* `dogapi.update_tags(host, tags, [[source], callback])` - * update the tags associated with the given `host` -* `dogapi.detach_tags(host, [[source], callback])` - * method to remove tags for a given `host` -* `dogapi.add_snapshot(snapshot, [callback])` - * method used to take a snapshot of a datadog graph -* `dogapi.add_snapshot_from_def(snapshot, [callback])` - * 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 (see `dogapi.constants.STATUSES`) -* `dogapi.schedule_downtime(scope, [options], [callback])` - * method to schedule a new downtime -* `dogapi.update_downtime(downtime_id, [options], [callback])` - * method to update an existing downtime -* `dogapi.cancel_downtime(downtime_id, [callback])` - * method to cancel an existing downtime -* `dogapi.get_downtime(downtime_id, [callback])` - * method to get an existing downtime -* `dogapi.get_all_downtimes([current_only], [callback])` - * method to get all downtimes - ## Sample Usage: **Example:** get all events since this time yesterday: From 7b836fd0c3fabe1fd2c7a8607344a5cae2bd7c57 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 21 Mar 2015 13:03:02 -0400 Subject: [PATCH 18/65] fix example indentation for metric --- lib/api/metric.js | 92 +++++++++++++++++++++++------------------------ 1 file changed, 46 insertions(+), 46 deletions(-) diff --git a/lib/api/metric.js b/lib/api/metric.js index 97f530f..472ae88 100644 --- a/lib/api/metric.js +++ b/lib/api/metric.js @@ -18,19 +18,19 @@ var client = require("../client"); * function(err, res) *example: | * ```javascript - * var dogapi = require("dogapi"); - * var options = { - * api_key: "api_key", - * app_key: "app_key" - * }; - * dogapi.initialize(options); - * dogapi.metric.send("my.metric", 1000, function(err, results){ - * console.dir(results); - * }); - * var now = parseInt(new Date().getTime() / 1000); - * dogapi.metric.send("my.metric", [now, 1000], function(err, results){ - * console.dir(results); - * }); + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.metric.send("my.metric", 1000, function(err, results){ + * console.dir(results); + * }); + * var now = parseInt(new Date().getTime() / 1000); + * dogapi.metric.send("my.metric", [now, 1000], function(err, results){ + * console.dir(results); + * }); * ``` */ function send(metric, points, extra, callback){ @@ -68,27 +68,27 @@ function send(metric, points, extra, callback){ * function(err, res) *example: | * ```javascript - * var dogapi = require("dogapi"); - * var options = { - * api_key: "api_key", - * app_key: "app_key" - * }; - * dogapi.initialize(options); - * var now = parseInt(new Date().getTime() / 1000); - * var metrics = [ - * { - * metric: "my.metric", - * points: [now, 1000], - * tags: ["tag:value"] - * }, - * { - * metric: "another.metric", - * points: 1000 - * } - * ]; - * dogapi.metric.send_all(metrics, function(err, results){ - * console.dir(results); - * }); + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * var now = parseInt(new Date().getTime() / 1000); + * var metrics = [ + * { + * metric: "my.metric", + * points: [now, 1000], + * tags: ["tag:value"] + * }, + * { + * metric: "another.metric", + * points: 1000 + * } + * ]; + * dogapi.metric.send_all(metrics, function(err, results){ + * console.dir(results); + * }); * ``` */ function send_all(metrics, callback){ @@ -114,18 +114,18 @@ function send_all(metrics, callback){ * callback: function(err, res) *example: | * ```javascript - * var dogapi = require("dogapi"); - * var options = { - * api_key: "api_key", - * app_key: "app_key" - * }; - * dogapi.initialize(options); - * var now = parseInt(new Date().getTime() / 1000); - * var then = now - 3600; // one hour ago - * var query = "system.cpu.idle{*}by{host}"; - * dogapi.metric.query(then, now, query, function(err, res){ - * console.dir(res); - * }); + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * var now = parseInt(new Date().getTime() / 1000); + * var then = now - 3600; // one hour ago + * var query = "system.cpu.idle{*}by{host}"; + * dogapi.metric.query(then, now, query, function(err, res){ + * console.dir(res); + * }); * ``` */ function query(from, to, query, callback){ From 6f3b077d49e9940812ead369d8ffbcb827524a57 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 21 Mar 2015 13:04:09 -0400 Subject: [PATCH 19/65] add infrastructure.search api --- lib/api/index.js | 3 ++- lib/api/infrastructure.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 lib/api/infrastructure.js diff --git a/lib/api/index.js b/lib/api/index.js index 94cf122..1125d63 100644 --- a/lib/api/index.js +++ b/lib/api/index.js @@ -3,7 +3,8 @@ var api = { metric: require("./metric"), event: require("./event"), serviceCheck: require("./serviceCheck"), - user: require("./user") + user: require("./user"), + infrastructure: require("./infrastructure") }; module.exports = function(obj){ diff --git a/lib/api/infrastructure.js b/lib/api/infrastructure.js new file mode 100644 index 0000000..4ed7d29 --- /dev/null +++ b/lib/api/infrastructure.js @@ -0,0 +1,36 @@ +var client = require("../client"); + +/*section: infrastructure + *comment: | + * search for metrics or hosts + *params: + * query: | + * the query to use for search see [datadog docs](http://docs.datadoghq.com/api/#search) + * for examples of the query (e.g. "hosts:database", "metrics:system" or "test") + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.infrastructure.search("hosts:database", function(err, res){ + * console.dir(res); + * }); + * ``` + */ +function search(query, callback){ + var params = { + query: { + q: query + } + }; + + client.request("GET", "/search", params, callback); +} + +module.exports = { + search: search +}; From 94f0196ee63e300ee849f5a21b9828effd9278b1 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 21 Mar 2015 13:04:13 -0400 Subject: [PATCH 20/65] build docs --- index.html | 129 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 83 insertions(+), 46 deletions(-) diff --git a/index.html b/index.html index 5b0288c..33c52e7 100644 --- a/index.html +++ b/index.html @@ -18,6 +18,8 @@ document.addEventListener("DOMContentLoaded", function(){hljs.initHighlightingOn
    +
    +
    +

    infrastructure

    + + +

    metric

    @@ -201,19 +238,19 @@ is given "now" is used as the timestamp

    -
     var dogapi = require("dogapi");
    - var options = {
    -   api_key: "api_key",
    -   app_key: "app_key"
    - };
    - dogapi.initialize(options);
    - dogapi.metric.send("my.metric", 1000, function(err, results){
    -   console.dir(results);
    - });
    - var now = parseInt(new Date().getTime() / 1000);
    - dogapi.metric.send("my.metric", [now, 1000], function(err, results){
    -   console.dir(results);
    - });
    +
    var dogapi = require("dogapi");
    +var options = {
    +  api_key: "api_key",
    +  app_key: "app_key"
    +};
    +dogapi.initialize(options);
    +dogapi.metric.send("my.metric", 1000, function(err, results){
    +  console.dir(results);
    +});
    +var now = parseInt(new Date().getTime() / 1000);
    +dogapi.metric.send("my.metric", [now, 1000], function(err, results){
    +  console.dir(results);
    +});
     
    @@ -239,27 +276,27 @@ is given "now" is used as the timestamp

    -
     var dogapi = require("dogapi");
    - var options = {
    -   api_key: "api_key",
    -   app_key: "app_key"
    - };
    - dogapi.initialize(options);
    - var now = parseInt(new Date().getTime() / 1000);
    - var metrics = [
    -   {
    -     metric: "my.metric",
    -     points: [now, 1000],
    -     tags: ["tag:value"]
    -   },
    -   {
    -     metric: "another.metric",
    -     points: 1000
    -   }
    - ];
    - dogapi.metric.send_all(metrics, function(err, results){
    -   console.dir(results);
    - });
    +
    var dogapi = require("dogapi");
    +var options = {
    +  api_key: "api_key",
    +  app_key: "app_key"
    +};
    +dogapi.initialize(options);
    +var now = parseInt(new Date().getTime() / 1000);
    +var metrics = [
    +  {
    +    metric: "my.metric",
    +    points: [now, 1000],
    +    tags: ["tag:value"]
    +  },
    +  {
    +    metric: "another.metric",
    +    points: 1000
    +  }
    +];
    +dogapi.metric.send_all(metrics, function(err, results){
    +  console.dir(results);
    +});
     
    @@ -284,18 +321,18 @@ is given "now" is used as the timestamp

    -
     var dogapi = require("dogapi");
    - var options = {
    -   api_key: "api_key",
    -   app_key: "app_key"
    - };
    - dogapi.initialize(options);
    - var now = parseInt(new Date().getTime() / 1000);
    - var then = now - 3600; // one hour ago
    - var query = "system.cpu.idle{*}by{host}";
    - dogapi.metric.query(then, now, query, function(err, res){
    -   console.dir(res);
    - });
    +
    var dogapi = require("dogapi");
    +var options = {
    +  api_key: "api_key",
    +  app_key: "app_key"
    +};
    +dogapi.initialize(options);
    +var now = parseInt(new Date().getTime() / 1000);
    +var then = now - 3600; // one hour ago
    +var query = "system.cpu.idle{*}by{host}";
    +dogapi.metric.query(then, now, query, function(err, res){
    +  console.dir(res);
    +});
     
    From 8e3e9787b88cda92536835510bc75321ce48cab9 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 21 Mar 2015 13:52:02 -0400 Subject: [PATCH 21/65] ignore .dogapirc file --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 303335e..82cee7b 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,5 @@ logs results npm-debug.log + +.dogapirc From 17da0c0f6b368b8ce8eebab3e70d5e83583689eb Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 21 Mar 2015 14:39:16 -0400 Subject: [PATCH 22/65] add start to cli tool --- bin/dogapi | 70 +++++++++++++++++++++++++++++++++++++++ lib/api/infrastructure.js | 11 +++++- lib/api/metric.js | 39 +++++++++++++++++++++- package.json | 7 +++- 4 files changed, 124 insertions(+), 3 deletions(-) create mode 100755 bin/dogapi diff --git a/bin/dogapi b/bin/dogapi new file mode 100755 index 0000000..d872d89 --- /dev/null +++ b/bin/dogapi @@ -0,0 +1,70 @@ +#!/usr/bin/env node +var dogapi = require("../"); +var minimist = require("minimist"); +var rc = require("rc"); + +var config = rc("dogapi", { + api_key: null, + app_key: null +}); +dogapi.initialize(config); + +var usage = [ + "Usage:", + "${command} --help", + "${command} --version", + "${command} now", + "${command} then " +]; +var help = []; +for(var key in dogapi){ + if(!dogapi.hasOwnProperty(key)){ + continue; + } else if(!dogapi[key].hasOwnProperty("getUsage") || typeof dogapi[key].getUsage !== "function"){ + continue; + } else if(!dogapi[key].hasOwnProperty("handleCli") || typeof dogapi[key].handleCli !== "function"){ + continue; + } + usage = usage.concat(dogapi[key].getUsage()); + + if(dogapi[key].hasOwnProperty("getHelp") && typeof dogapi[key].getHelp === "function"){ + help = help.concat(["\r\n"], dogapi[key].getHelp()); + } +} + +usage = usage.concat(help); +usage = usage.join("\r\n").replace(/\$\{command\}/g, " dogapi"); +var args = minimist(process.argv); + +var command = args._[2]; +var subcommand = args._[3]; + +if(command === "now"){ + console.log(dogapi.now()); +} else if(command === "then" && args._.length > 3){ + console.log(dogapi.now() - parseInt(args._[args._.length - 1])); +} else if(dogapi.hasOwnProperty(command)){ + if(subcommand && dogapi[command].hasOwnProperty(subcomand)){ + dogapi[command].handleCli(subcommand, args, function(err, res){ + if(err){ + console.error(err); + process.exit(1); + } else { + if(res === ""){ + res = "success"; + } + console.log(res); + } + }); + } else { + var commandUsage = ["Usage:"].concat(dogapi[command].getUsage()); + if(dogapi[command].hasOwnProperty("getHelp") && typeof dogapi[command].getHelp === "function"){ + commandUsage = commandUsage.concat(["\r\n"], dogapi[command].getHelp()); + } + console.log(commandUsage.join("\r\n").replace(/\$\{command\}/g, " dogapi")); + } +} else if(args.version){ + console.log(require("../package.json").version); +} else { + console.log(usage); +} diff --git a/lib/api/infrastructure.js b/lib/api/infrastructure.js index 4ed7d29..73e1deb 100644 --- a/lib/api/infrastructure.js +++ b/lib/api/infrastructure.js @@ -32,5 +32,14 @@ function search(query, callback){ } module.exports = { - search: search + search: search, + getUsage: function(){ + return [ + "${command} infrastructure search " + ] + }, + handleCli: function(subcommand, args, callback){ + var query = args._[4]; + search(query, callback); + } }; diff --git a/lib/api/metric.js b/lib/api/metric.js index 472ae88..bf46145 100644 --- a/lib/api/metric.js +++ b/lib/api/metric.js @@ -139,5 +139,42 @@ function query(from, to, query, callback){ module.exports = { send: send, - send_all: send_all + send_all: send_all, + query: query, + getUsage: function(){ + return [ + "${command} metric send [--tags ] [--host ] [--type ]", + "${command} metric query " + ] + }, + getHelp: function(){ + return [ + "Metric Options:", + " --tags a comma separated list of \"tag:value\"'s", + " --host the hostname that should be associated with this metric", + " --type the type of metric \"gauge\" or \"counter\"" + ] + }, + handleCli: function(subcommand, args, callback){ + if(subcommand === "send"){ + var extra = {}; + if(args["--tags"]){ + extra.tags = args["--tags"].split(","); + } + if(args["--host"]){ + extra.host = args["--host"]; + } + if(args["--type"]){ + extra.metric_type = args["--type"]; + } + send(args[""], args[""], extra, callback); + } else if(subcommand === "query" && args._.length > 6){ + var from = parseInt(args._[4]); + var to = parseInt(args._[5]); + var q = args._[6]; + query(from, to, q, callback); + } else { + callback("unknown subcommand or arguments try `dogapi --help` for help", false); + } + } }; diff --git a/package.json b/package.json index fc139a3..2c65bbd 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,9 @@ "test": "echo \"Error: no test specified\" && exit 1", "docs": "node ./docs/create.js > index.html" }, + "bin": { + "dogapi": "bin/dogapi" + }, "repository": { "type": "git", "url": "git://github.com/brettlangdon/node-dogapi" @@ -28,7 +31,9 @@ "readmeFilename": "README.md", "gitHead": "f388635a5ab4f4da25702dc0999385d437bdf2bc", "dependencies": { - "extend": "^2.0.0" + "extend": "^2.0.0", + "minimist": "^1.1.1", + "rc": "^1.0.0" }, "devDependencies": { "docast": "^0.1.1", From 0123b0d25b218168981653f67f8d62e7e5833a1a Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 21 Mar 2015 14:39:30 -0400 Subject: [PATCH 23/65] fix param format for dogapi.metric.query --- lib/api/metric.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/api/metric.js b/lib/api/metric.js index bf46145..ee9f663 100644 --- a/lib/api/metric.js +++ b/lib/api/metric.js @@ -130,9 +130,11 @@ function send_all(metrics, callback){ */ function query(from, to, query, callback){ var params = { - from: from, - to: to, - query: query + query: { + from: from, + to: to, + query: query + } }; client.request("GET", "/query", params, callback); } From 80f84f6a81fc870683aa46c71a4aaffe4fc88e1d Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 21 Mar 2015 14:41:10 -0400 Subject: [PATCH 24/65] remove old api code --- lib/api/alert.js | 123 -------------------------------------------- lib/api/dash.js | 103 ------------------------------------- lib/api/downtime.js | 102 ------------------------------------ lib/api/screen.js | 116 ----------------------------------------- lib/api/search.js | 16 ------ lib/api/snapshot.js | 101 ------------------------------------ 6 files changed, 561 deletions(-) delete mode 100644 lib/api/alert.js delete mode 100644 lib/api/dash.js delete mode 100644 lib/api/downtime.js delete mode 100644 lib/api/screen.js delete mode 100644 lib/api/search.js delete mode 100644 lib/api/snapshot.js diff --git a/lib/api/alert.js b/lib/api/alert.js deleted file mode 100644 index cc7bc9c..0000000 --- a/lib/api/alert.js +++ /dev/null @@ -1,123 +0,0 @@ -var util = require('util'); - - -var alert_api = function(){}; - -alert_api.prototype.add_alert = function(alert, callback){ - /* - * alert_api.add_alert(alert, [callback]) - * - * add a new alert to datadog - * - * `alert` is an object containing: - * query: *required*, the metric to query on - * name: name of the alert - * message: an optional message to include with the alert - * silenced: whether the alert should notify by email and in the stream - * - * `callback` an optional function to get called with the results of the api call - * callback(error, result, status_code) - */ - if(typeof alert != 'object'){ - throw new Error('`alert` parameter must be an object'); - } - - if(!alert['query']){ - throw new Error('`alert["query"]` is required'); - } - - this.request('POST', '/alert', {body: alert}, callback); -}; - -alert_api.prototype.update_alert = function(alert_id, alert, callback){ - /* - * alert_api.update_alert(alert_id, alert, [callback]) - * - * update an existing alert - * - * `alert_id` the id of alert to update - * `alert` is an object containing: - * query: *required*, the metric to query on - * name: name of the alert - * message: an optional message to include with the alert - * silenced: whether the alert should notify by email and in the stream - * - * `callback` an optional function to get called with the results of the api call - * callback(error, result, status_code) - */ - if(typeof alert != 'object'){ - throw new Error('`alert` parameter must be an object'); - } - - if(!alert['query']){ - throw new Error('`alert["query"]` is required'); - } - - this.request('PUT', util.format('/alert/%s', alert_id), {body: alert}, callback); -}; - -alert_api.prototype.get_alert = function(alert_id, callback){ - /* - * alert_api.get_alert(alert_id, [callback]) - * - * get the details of an alert from the given id - * - * `alert_id` the id for the alert to get - * - * `callback` an optional function to call with the results - * callback(error, result, status_code) - */ - this.request('GET', util.format('/alert/%s', alert_id), callback); -}; - -alert_api.prototype.delete_alert = function(alert_id, callback){ - /* - * alert_api.delete_alert(alert_id, [callback]) - * - * delete the given alert from datadog - * - * `alert_id` the id for the alert to get - * - * `callback` an optional function to call with the results - * callback(error, result, status_code) - */ - this.request('DELETE', util.format('/alert/%s', alert_id), callback); -}; - -alert_api.prototype.get_all_alerts = function(callback){ - /* - * alert_api.get_all_alerts([callback]) - * - * get the details of all alerts in datadog - * - * `callback` an optional function to call with the results - * callback(error, result, status_code) - */ - this.request('GET', '/alert', callback); -}; - -alert_api.prototype.mute_alerts = function(callback){ - /* - * alert_api.mute_alerts([callback]) - * - * mute all alerts - * - * `callback` an optional function to call with the results - * callback(error, result, status_code) - */ - this.request('POST', '/mute_alerts', callback); -}; - -alert_api.prototype.unmute_alerts = function(callback){ - /* - * alert_api.unmute_alerts([callback]) - * - * unmute all alerts - * - * `callback` an optional function to call with the results - * callback(error, result, status_code) - */ - this.request('POST', '/unmute_alerts', callback); -}; - -return module.exports = alert_api; diff --git a/lib/api/dash.js b/lib/api/dash.js deleted file mode 100644 index 8abe9c4..0000000 --- a/lib/api/dash.js +++ /dev/null @@ -1,103 +0,0 @@ -var util = require('util'); - -var validate_dashboard = function(dashboard){ - if(typeof dashboard != 'object'){ - throw new Error('`dashboard` parameter must be an object'); - } - - if(dashboard['title'] != 'string'){ - throw new Error('`dashboard["title"]` must be a string'); - } - - if(typeof dashboard['description'] != 'string'){ - throw new Error('`dashboard["description"]` must be a string'); - } - - if(typeof dashboard['graphs'] != 'object'){ - throw new Error('`dashboard["graphs"]` must be an array'); - } - - for(var i in dashboard['graphs']){ - if(!dashboard['graphs'][i]['title']){ - throw new Error(util.format('`dashboard["graphs"][%s]["title"]` is missing', i)); - } - if(!dashboard['graphs'][i]['definition']){ - throw new Error(util.format('`dashboard["graphs"][%s]["definition"]` is missing', i)); - } - } -}; - -var dash_api = function(){}; - -dash_api.prototype.get_dashboard = function(dash_id, callback){ - /* - * dash_api.get_dashboard(dash_id, [callback]) - * - * method to get a single dashboard information - * - * `dash_id` is the id of the dashboard to get information for - * `callback` is an optional function to call with the results of the api call - * callback(error, results, status_code) - */ - this.request('GET', util.format('/dash/%s', dash_id), callback); -}; - -dash_api.prototype.get_all_dashboards = function(callback){ - /* - * dash_api.get_all_dashboards([callback]) - * - * method to retrieve all dashboards in datadog - * - * `callback` is an optional function to call with the results of the api call - * callback(error, result, status_code) - */ - this.request('GET', '/dash', callback); -}; - -dash_api.prototype.create_dashboard = function(dashboard, callback){ - /* - * dash_api.create_dashboard(dashboard, [callback]) - * - * method used to create a new dashboard in datadog - * - * `dashboard` is the definition for the dashboard - * please see (http://docs.datadoghq.com/api/) for more information - * - * `callback` is an optional function to call with the results of the api call - * callback(error, result, status_code) - */ - validate_dashboard(dashboard); - this.request('POST', '/dash', {body: dashboard}, callback); -}; - -dash_api.prototype.update_dashboard = function(dash_id, dashboard, callback){ - /* - * dash_api.update_dashboard(dash_id, dashboard, [callback]) - * - * method used to update the dashboard with the provided `dash_id` - * - * `dash_id` the id of the dashboard to update - * `dashboard` is a definition for the datadog dashboard - * please see (http://docs.datadoghq.com/api/) for more information - * - * `callback` an optional function to call with the results of the api call - * callback(error, result, status_code) - */ - validate_dashboard(dashboard); - this.request('PUT', util.format('/dash/%s', dash_id), {body: dashbboard}, callback); -}; - -dash_api.prototype.delete_dashboard = function(dash_id, callback){ - /* - * dash_api.delete_dashboard(dash_id, [callback]) - * - * method to remove a dashboard from datadog - * - * `dash_id` the id for the dashboard to remove - * `callback` an optional function to call with the results of the api call - * callback(error, result, status_code) - */ - this.request('DELETE', util.format('/dash/%s', dash_id), callback); -}; - -return module.exports = dash_api; diff --git a/lib/api/downtime.js b/lib/api/downtime.js deleted file mode 100644 index 98dcf66..0000000 --- a/lib/api/downtime.js +++ /dev/null @@ -1,102 +0,0 @@ -var util = require('util'); - -var downtime_api = function(){}; - -downtime_api.prototype.schedule_downtime = function(scope, options, callback){ - /* - * downtime_api.schedule_downtime(scope, [options], [callback]) - * - * method to schedule a new downtime - * - * `scope` the scope to schedule downtime for - * `options` optional `start` `end` and `message` paramaters - * `callback` is an optional function to call with the results of the api call - * callback(error, results, status_code) - */ - if(typeof scope !== 'string'){ - throw new Error('scope parameter must be a string'); - } - - if(typeof options === 'function'){ - callback = options; - options = {}; - } - options.scope = scope; - - this.request('POST', '/downtime', {body: options}, callback); -}; - -downtime_api.prototype.update_downtime = function(downtime_id, options, callback){ - /* - * downtime_api.update_downtime(downtime_id, [options], [callback]) - * - * method to update an existing downtime - * - * `downtime_id` the id of the downtime - * `options` optional `scope` `start` `end` and `message` paramaters - * `callback` is an optional function to call with the results of the api call - * callback(error, results, status_code) - */ - - if(typeof options === 'function'){ - callback = options; - options = {}; - } - - this.request('PUT', util.format('/downtime/%s', downtime_id), {body: options}, callback); -}; - -downtime_api.prototype.get_downtime = function(downtime_id, callback){ - /* - * downtime_api.get_downtime(downtime_id, [callback]) - * - * method to get an existing downtime - * - * `downtime_id` the id of the downtime - * `callback` is an optional function to call with the results of the api call - * callback(error, results, status_code) - */ - - this.request('GET', util.format('/downtime/%s', downtime_id), callback); -}; - -downtime_api.prototype.cancel_downtime = function(downtime_id, callback){ - /* - * downtime_api.cancel_downtime(downtime_id, [callback]) - * - * method to cancel an existing downtime - * - * `downtime_id` the id of the downtime - * `callback` is an optional function to call with the results of the api call - * callback(error, results, status_code) - */ - - this.request('DELETE', util.format('/downtime/%s', downtime_id), callback); -}; - -downtime_api.prototype.get_all_downtimes = function(current_only, callback){ - /* - * downtime_api.get_all_downtimes(downtime_id, [current_only], [callback]) - * - * method to get all downtimes - * - * `downtime_id` the id of the downtime - * `current_only` whether or not to get the current downtime only - * `callback` is an optional function to call with the results of the api call - * callback(error, results, status_code) - */ - - if(typeof current_only === 'function'){ - callback = current_only; - current_only = false; - } - - query = {}; - if(current_only){ - query.current_only = true; - } - - this.request('GET', '/downtime', {query: query}, callback); -}; - -return module.exports = downtime_api; diff --git a/lib/api/screen.js b/lib/api/screen.js deleted file mode 100644 index febfb8f..0000000 --- a/lib/api/screen.js +++ /dev/null @@ -1,116 +0,0 @@ -var util = require('util'); - -var validate_screenboard = function(screenboard){ - if(typeof screenboard != 'object'){ - throw new Error('`screenboard` parameter must be an object'); - } - - if(typeof screenboard['board_title'] != 'string'){ - throw new Error('`screenboard["board_title"]` must be a string'); - } - - if(screenboard['width'] != undefined && typeof screenboard['width'] != 'number'){ - throw new Error('`screenboard["width"]` must be a number'); - } - - if(screenboard['height'] != undefined && typeof screenboard['height'] != 'number'){ - throw new Error('`screenboard["height"]` must be a number'); - } - - if(typeof screenboard['widgets'] != 'object'){ - throw new Error('`screenboard["widgets"]` must be an array'); - } - - for(var i in screenboard['widgets']){ - if(!screenboard['widgets'][i]['type']){ - throw new Error(util.format('`screenboard["widgets"][%s]["type"]` is missing', i)); - } - if(!screenboard['widgets'][i]['width']){ - throw new Error(util.format('`screenboard["widgets"][%s]["width"]` is missing', i)); - } - if(!screenboard['widgets'][i]['height']){ - throw new Error(util.format('`screenboard["widgets"][%s]["height"]` is missing', i)); - } - if(!screenboard['widgets'][i]['x']){ - throw new Error(util.format('`screenboard["widgets"][%s]["x"]` is missing', i)); - } - if(!screenboard['widgets'][i]['y']){ - throw new Error(util.format('`screenboard["widgets"][%s]["y"]` is missing', i)); - } - } -}; - -var screen_api = function(){}; - -screen_api.prototype.get_screenboard = function(screen_id, callback){ - /* - * screen_api.get_screenboard(screen_id, [callback]) - * - * method to get a single screenboard information - * - * `screen_id` is the id of the screenboard to get information for - * `callback` is an optional function to call with the results of the api call - * callback(error, results, status_code) - */ - this.request('GET', util.format('/screen/%s', screen_id), callback); -}; - -screen_api.prototype.get_all_screenboards = function(callback){ - /* - * screen_api.get_all_screenboards([callback]) - * - * method to retrieve all screenboards in datadog - * - * `callback` is an optional function to call with the results of the api call - * callback(error, result, status_code) - */ - this.request('GET', '/screen', callback); -}; - -screen_api.prototype.create_screenboard = function(screenboard, callback){ - /* - * screen_api.create_screenboard(screenboard, [callback]) - * - * method used to create a new screenboard in datadog - * - * `screenboard` is the definition for the screenboard - * please see (http://docs.datadoghq.com/api/) for more information - * - * `callback` is an optional function to call with the results of the api call - * callback(error, result, status_code) - */ - validate_screenboard(screenboard); - this.request('POST', '/screen', {body: screenboard}, callback); -}; - -screen_api.prototype.update_screenboard = function(screen_id, screenboard, callback){ - /* - * screen_api.update_screenboard(screen_id, screenboard, [callback]) - * - * method used to update the screenboard with the provided `screen_id` - * - * `screen_id` the id of the screenboard to update - * `screenboard` is a definition for the datadog screenboard - * please see (http://docs.datadoghq.com/api/) for more information - * - * `callback` an optional function to call with the results of the api call - * callback(error, result, status_code) - */ - validate_screenboard(screenboard); - this.request('PUT', util.format('/screen/%s', screen_id), {body: screenboard}, callback); -}; - -screen_api.prototype.delete_screenboard = function(screen_id, callback){ - /* - * screen_api.delete_screenboard(screen_id, [callback]) - * - * method to remove a screenboard from datadog - * - * `screen_id` the id for the screenboard to remove - * `callback` an optional function to call with the results of the api call - * callback(error, result, status_code) - */ - this.request('DELETE', util.format('/screen/%s', screen_id), callback); -}; - -return module.exports = screen_api; diff --git a/lib/api/search.js b/lib/api/search.js deleted file mode 100644 index 416110c..0000000 --- a/lib/api/search.js +++ /dev/null @@ -1,16 +0,0 @@ -var search_api = function(){}; -search_api.prototype.search = function(query, callback){ - /* - * search_api.search(query, [callback]) - * - * method used to query the api for `metrics` or `hosts` - * - * `query` the query to use to search the datadog service - * - * `callback` an optional function called with the results of the search - * callback(error, result, status_code) - */ - this.request('GET', '/search', {query: {'q': query}}, callback); -}; - -return module.exports = search_api; diff --git a/lib/api/snapshot.js b/lib/api/snapshot.js deleted file mode 100644 index 2ce482a..0000000 --- a/lib/api/snapshot.js +++ /dev/null @@ -1,101 +0,0 @@ -var extend = require('extend'); -var util = require('util'); - -var snapshot_api = function(){}; - -snapshot_api.prototype.add_snapshot = function(snapshot, callback){ - /* - * snapshot_api.add_snapshot(snapshot, [callback]) - * - * method used to add a new snapshot to datadog - * - * `snapshot` is an object containing any of the following: - * metric_query: *required*, The metric query. - * start: *required*, int, Start of the query.(POSIX timestamp) - * end: *required*, int, End of the query.(POSIX timestamp) - * event_query: A query that will add event bands to the graph. - * - * `callback` is an optional function for the result - * callback(error, result, status_code) - */ - if(typeof snapshot != 'object'){ - throw new Error('`snapshot` parameter must be an object'); - } - - if(!snapshot['metric_query']){ - throw new Error('`metric_query` property of `snapshot` parameter is required'); - } - - if(!snapshot['start']){ - throw new Error('`start` property of `snapshot` parameter is required'); - } - - if(!snapshot['end']){ - throw new Error('`end` property of `snapshot` parameter is required'); - } - - this.request('GET', '/graph/snapshot', {query: snapshot}, callback); -}; - -snapshot_api.prototype.add_snapshot_from_def = function(snapshot, callback){ - /* - * snapshot_api.add_snapshot_from_def(snapshot, [callback]) - * - * method used to add a new snapshot to datadog based on a graph definition - * https://github.com/DataDog/dogapi/commit/583f13d7bd8de5a86daa2ff53f2d7cf6570e7ab2 - * feature is not currently documented (at time of writing), but very useful. - * - * `snapshot` is an object containing any of the following: - * graph_def: *required*, JSON string dump of an existing graph definition - * start: *required*, int, Start of the query.(POSIX timestamp) - * end: *required*, int, End of the query.(POSIX timestamp) - * - * `callback` is an optional function for the result - * callback(error, result, status_code) - */ - if(typeof snapshot != 'object'){ - throw new Error('`snapshot` parameter must be an object'); - } - - if(!snapshot['graph_def']){ - throw new Error('`graph_def` property of `snapshot` parameter is required'); - } - - if(!snapshot['start']){ - throw new Error('`start` property of `snapshot` parameter is required'); - } - - if(!snapshot['end']){ - throw new Error('`end` property of `snapshot` parameter is required'); - } - - this.request('GET', '/graph/snapshot', {query: snapshot}, callback); -}; - -snapshot_api.prototype.snapshot_status = function(snapshot_url, callback){ - /* - * snapshot_api.add_snapshot(snapshot_url, [callback]) - * - * method used to check the status of a datadog snapshot. - * https://github.com/DataDog/dogapi/blob/master/src/dogapi/http/snapshot.py#L64 - * Snapshot URLs are returned right away, but may be empty if the query or graph is complicated. - * Result will be a json payload, with 403 for incomplete, or 200 for complete. - * Examples: - * * {"status_code":403} - incomplete (still processing, image is empty) - * * {"status_code":200} - image is rendered and ready - * - * `snapshot_url` is a string containing URL returned from a call to add_snapshot or add_snapshot_from_def - * - * `callback` is an optional function for the result - * callback(error, result, status_code) - */ - if(typeof snapshot_url != 'string'){ - throw new Error('`snapshot_url` parameter must be a string'); - } - - url = snapshot_url.split('/snapshot/view/')[1].split('.png')[0] - - this.request('GET', '/graph/snapshot_status/'+url, {}, callback); -}; - -return module.exports = snapshot_api; From 9ccf2563869f2bfeda1caf7b6510f25f97230ae1 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 21 Mar 2015 14:43:49 -0400 Subject: [PATCH 25/65] fix spelling mistake --- bin/dogapi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/dogapi b/bin/dogapi index d872d89..fbae857 100755 --- a/bin/dogapi +++ b/bin/dogapi @@ -44,7 +44,7 @@ if(command === "now"){ } else if(command === "then" && args._.length > 3){ console.log(dogapi.now() - parseInt(args._[args._.length - 1])); } else if(dogapi.hasOwnProperty(command)){ - if(subcommand && dogapi[command].hasOwnProperty(subcomand)){ + if(subcommand && dogapi[command].hasOwnProperty(subcommand)){ dogapi[command].handleCli(subcommand, args, function(err, res){ if(err){ console.error(err); From b61e7e14a9d837cdbb9ae9cb63cc0b074c621fa0 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 21 Mar 2015 14:44:29 -0400 Subject: [PATCH 26/65] add cli commands for user invite --- lib/api/user.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/api/user.js b/lib/api/user.js index 580dd83..3436ca6 100644 --- a/lib/api/user.js +++ b/lib/api/user.js @@ -29,5 +29,13 @@ function invite(emails, callback){ }; module.exports = { - invite: invite + invite: invite, + getUsage: function(){ + return [ + "${command} user invite
    ..." + ]; + }, + handleCli: function(subcommand, args, callback){ + invite(args._.slice(4), callback) + } }; From f40d2f41ac0cf2f7f6d52c65522e246817d7032b Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 21 Mar 2015 15:00:38 -0400 Subject: [PATCH 27/65] add tag commands to cli --- lib/api/tag.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/lib/api/tag.js b/lib/api/tag.js index e376b8c..5d0e634 100644 --- a/lib/api/tag.js +++ b/lib/api/tag.js @@ -210,4 +210,52 @@ module.exports = { create: create, update: update, "delete": delete_tags, + getUsage: function(){ + return [ + "${command} tag get_all [--source ]", + "${command} tag get [--source ] [--by-source]", + "${command} tag delete [--source ]", + "${command} tag create [--source ]", + "${command} tag update [--source ]" + ]; + }, + getHelp: function(){ + return [ + "Tag Commands:", + " get_all get all tags", + " get get all tags for a given host", + " delete delete tags for a given host", + " create add the comma separates \"tag:value\"'s from to ", + " update update the comma separates \"tag:value\"'s from to ", + "", + "Tag Options:", + " --source the source of the tags (e.g. \"chef\", \"user\", \"jenkins\", etc)", + " --by-source whether the results should be grouped by source" + ]; + }, + handleCli: function(subcommand, args, callback){ + var source = args["source"]; + var host = args._[4]; + + if(subcommand === "get_all"){ + get_all(source, callback); + } else if(subcommand === "get"){ + var options = {}; + if(source){ + options.source = source; + } + if(args["by-source"]){ + options.by_source = true; + } + get(host, options, callback); + } else if(subcommand === "create"){ + var tags = args._[5].split(","); + create(host, tags, source, callback); + } else if(subcommand === "update"){ + var tags = args._[5].split(","); + update(host, tags, source, callback); + } else if(subcommand === "delete"){ + delete_tags(host, source, callback); + } + } } From 4051602e9278c73bdf7e5a845e9238640c0f6a08 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 21 Mar 2015 15:01:37 -0400 Subject: [PATCH 28/65] rename dogapi.tag.delete to dogapi.tag.remove --- lib/api/tag.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/api/tag.js b/lib/api/tag.js index 5d0e634..bede22e 100644 --- a/lib/api/tag.js +++ b/lib/api/tag.js @@ -184,12 +184,12 @@ function update(hostname, tags, source, callback){ * app_key: "app_key" * }; * dogapi.initialize(options); - * dogapi.tag.delete("host.name", function(err, results){ + * dogapi.tag.remove("host.name", function(err, results){ * console.dir(results); * }); * ``` */ -function delete_tags(hostname, source, callback){ +function remove(hostname, source, callback){ if(arguments.length < 3 && typeof arguments[1] === "function"){ callback = source; source = undefined; @@ -209,12 +209,12 @@ module.exports = { get: get, create: create, update: update, - "delete": delete_tags, + remove: remove, getUsage: function(){ return [ "${command} tag get_all [--source ]", "${command} tag get [--source ] [--by-source]", - "${command} tag delete [--source ]", + "${command} tag remove [--source ]", "${command} tag create [--source ]", "${command} tag update [--source ]" ]; @@ -224,7 +224,7 @@ module.exports = { "Tag Commands:", " get_all get all tags", " get get all tags for a given host", - " delete delete tags for a given host", + " remove delete tags for a given host", " create add the comma separates \"tag:value\"'s from to ", " update update the comma separates \"tag:value\"'s from to ", "", @@ -255,7 +255,7 @@ module.exports = { var tags = args._[5].split(","); update(host, tags, source, callback); } else if(subcommand === "delete"){ - delete_tags(host, source, callback); + remove(host, source, callback); } } } From 2e85bb5a1d25dc9145b3baedeef34fbe5cff0ce9 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 21 Mar 2015 15:01:50 -0400 Subject: [PATCH 29/65] rebuild docs --- index.html | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/index.html b/index.html index 33c52e7..55d4309 100644 --- a/index.html +++ b/index.html @@ -181,7 +181,7 @@ document.addEventListener("DOMContentLoaded", function(){hljs.initHighlightingOn

    Parameters:

    query
    -

    the query to use for search see search docs +

    the query to use for search see datadog docs for examples of the query (e.g. "hosts:database", "metrics:system" or "test")

    callback
    @@ -396,7 +396,7 @@ dogapi.metric.query(then, now, query, function(err, res){
  • get
  • create
  • update
  • -
  • delete_tags
  • +
  • remove
  • get_all(source, callback)

    @@ -525,8 +525,8 @@ dogapi.tag.update("host.name", function(err, results){
    -
    -

    delete_tags(hostname, source, callback)

    +
    +

    remove(hostname, source, callback)

    delete the host tags for the provided host name or host id

    Parameters:

    @@ -549,7 +549,7 @@ var options = { app_key: "app_key" }; dogapi.initialize(options); -dogapi.tag.delete("host.name", function(err, results){ +dogapi.tag.remove("host.name", function(err, results){ console.dir(results); });
    From 67290f61d8c78ed1779fa35a4bb49ecdbaae4809 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 21 Mar 2015 15:04:27 -0400 Subject: [PATCH 30/65] update help text for metric command --- lib/api/metric.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/api/metric.js b/lib/api/metric.js index ee9f663..c5e53a2 100644 --- a/lib/api/metric.js +++ b/lib/api/metric.js @@ -151,10 +151,15 @@ module.exports = { }, getHelp: function(){ return [ - "Metric Options:", - " --tags a comma separated list of \"tag:value\"'s", - " --host the hostname that should be associated with this metric", - " --type the type of metric \"gauge\" or \"counter\"" + "Metric:", + " Commands:", + " send add a new datapoint for for right now", + " query query for between and POSIX timestamps", + "", + " Options:", + " --tags a comma separated list of \"tag:value\"'s", + " --host the hostname that should be associated with this metric", + " --type the type of metric \"gauge\" or \"counter\"" ] }, handleCli: function(subcommand, args, callback){ From 63a6394f9795b0059b1d649b11e39c6c2f7eeece Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 21 Mar 2015 15:05:15 -0400 Subject: [PATCH 31/65] reformat tag cli help text --- lib/api/tag.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/api/tag.js b/lib/api/tag.js index bede22e..cf38d9a 100644 --- a/lib/api/tag.js +++ b/lib/api/tag.js @@ -221,16 +221,17 @@ module.exports = { }, getHelp: function(){ return [ - "Tag Commands:", - " get_all get all tags", - " get get all tags for a given host", - " remove delete tags for a given host", - " create add the comma separates \"tag:value\"'s from to ", - " update update the comma separates \"tag:value\"'s from to ", + "Tag:", + " Commands:", + " get_all get all tags", + " get get all tags for a given host", + " remove delete tags for a given host", + " create add the comma separates \"tag:value\"'s from to ", + " update update the comma separates \"tag:value\"'s from to ", "", - "Tag Options:", - " --source the source of the tags (e.g. \"chef\", \"user\", \"jenkins\", etc)", - " --by-source whether the results should be grouped by source" + " Options:", + " --source the source of the tags (e.g. \"chef\", \"user\", \"jenkins\", etc)", + " --by-source whether the results should be grouped by source" ]; }, handleCli: function(subcommand, args, callback){ From 3e152db847b9b2ba9d62a5b3142dfa6a492e53c2 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 21 Mar 2015 15:07:43 -0400 Subject: [PATCH 32/65] add infrastructure help text --- lib/api/infrastructure.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/api/infrastructure.js b/lib/api/infrastructure.js index 73e1deb..5166f46 100644 --- a/lib/api/infrastructure.js +++ b/lib/api/infrastructure.js @@ -38,6 +38,13 @@ module.exports = { "${command} infrastructure search " ] }, + getHelp: function(){ + return [ + "Infrastructure:", + " Commands:", + " search query for hosts or metrics with (see http://docs.datadoghq.com/api/#search)", + ]; + }, handleCli: function(subcommand, args, callback){ var query = args._[4]; search(query, callback); From 83c47d69fe58172b84555180283aa2811ecc95bc Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 21 Mar 2015 15:09:02 -0400 Subject: [PATCH 33/65] add user cli help text --- lib/api/user.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/api/user.js b/lib/api/user.js index 3436ca6..728c419 100644 --- a/lib/api/user.js +++ b/lib/api/user.js @@ -35,6 +35,13 @@ module.exports = { "${command} user invite
    ..." ]; }, + getHelp: function(){ + return [ + "User:", + " Commands:", + " invite
    ... invite the given list of e-mail addresses to your datadog org" + ]; + }, handleCli: function(subcommand, args, callback){ invite(args._.slice(4), callback) } From 967abd7feb3aed7d7575969d49fe70d67efd438a Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 21 Mar 2015 15:10:02 -0400 Subject: [PATCH 34/65] add additional help text to cli --- bin/dogapi | 1 + 1 file changed, 1 insertion(+) diff --git a/bin/dogapi b/bin/dogapi index fbae857..10a6acf 100755 --- a/bin/dogapi +++ b/bin/dogapi @@ -12,6 +12,7 @@ dogapi.initialize(config); var usage = [ "Usage:", "${command} --help", + "${command} --help", "${command} --version", "${command} now", "${command} then " From 8c0cc5920e2216f7e142be98bddea9a1c1216b82 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 21 Mar 2015 15:20:45 -0400 Subject: [PATCH 35/65] add servicecheck check command to cli --- bin/dogapi | 5 +++++ lib/api/serviceCheck.js | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/bin/dogapi b/bin/dogapi index 10a6acf..882ac85 100755 --- a/bin/dogapi +++ b/bin/dogapi @@ -40,6 +40,11 @@ var args = minimist(process.argv); var command = args._[2]; var subcommand = args._[3]; +// this is the one unusual case +if(command === "servicecheck"){ + command = "serviceCheck"; +} + if(command === "now"){ console.log(dogapi.now()); } else if(command === "then" && args._.length > 3){ diff --git a/lib/api/serviceCheck.js b/lib/api/serviceCheck.js index 5db6ea0..39edf5f 100644 --- a/lib/api/serviceCheck.js +++ b/lib/api/serviceCheck.js @@ -51,5 +51,40 @@ function check(check, hostName, status, parameters, callback){ module.exports = { - check: check + check: check, + getUsage: function(){ + return [ + "${command} servicecheck check [--time ] [--message ] [--tags ]" + ]; + }, + getHelp: function(){ + return [ + "Service Check:", + " Commands:", + " check add a new service check for and at level ", + "", + " Options:", + " 0=OK, 1=WARNING, 2=CRITICAL, 3=UNKNOWN", + " --time the POSIX timestamp to use for the check", + " --message an optional message to accompany the check", + " --tags a comma separated list of \"tag:value\"'s for the check" + ]; + }, + handleCli: function(subcommand, args, callback){ + if(args._.length > 6){ + var parameters = {}; + if(args["time"]){ + parameters.time = parseInt(args["time"]); + } + if(args["message"]){ + paramaters.message = args["message"]; + } + if(args["tags"]){ + parameters.tags = args["tags"].split(","); + } + check(args._[4], args._[5], parseInt(args._[6]), parameters, callback); + } else { + callback("not enough arguments try `dogapi servicecheck --help` for help", false); + } + } }; From 21ca627c43ea06193e47115c3089ed148483df29 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 21 Mar 2015 16:33:22 -0400 Subject: [PATCH 36/65] fix variable typo --- lib/api/event.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/api/event.js b/lib/api/event.js index 8801cf9..1e64bdb 100644 --- a/lib/api/event.js +++ b/lib/api/event.js @@ -51,7 +51,7 @@ function create(title, text, properties, callback){ properties = {}; } - properties.title = tile; + properties.title = title; properties.text = text; var params = { From f3d1606d6b9ab8064a97476ed7cc1e24dbdeab75 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 21 Mar 2015 16:33:28 -0400 Subject: [PATCH 37/65] add event cli commands --- lib/api/event.js | 68 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/lib/api/event.js b/lib/api/event.js index 1e64bdb..167eb92 100644 --- a/lib/api/event.js +++ b/lib/api/event.js @@ -139,5 +139,71 @@ function query(start, end, parameters, callback){ module.exports = { create: create, get: get, - query: query + query: query, + getUsage: function(){ + return [ + "${command} event get ", + "${command} event query [--priority ] [--sources ] [--tags ]", + "${command} event create <text> [--time <timestamp>] [--priority <priority>] [--host <host>] [--tags <tags>] [--type <type>] [--agg-key <agg-key>] [--source <source>]" + ]; + }, + getHelp: function(){ + return [ + "Event:", + " Commands:", + " get <event-id> get the event with the provided <event-id>", + " query <from> <to> query the event stream between <from> and <to> POSIX timestamps", + " create <title> <text> create a new event with <title> and <text>", + " Options:", + " --priority <priority> the priority of the event \"normal\" or \"low\"", + " --sources <sources> a comma separated list of sources (e.g. \"users,jenkins,chef\")", + " --tags <tags> a comma separated list of \"tag:value\"'s", + " --time <time> a POSIX timestamp for when the event happened", + " --host <host> the host to associate the event to", + " --type <type> the event type \"error\", \"warning\", \"info\" or \"success\"", + " --agg-key <agg-key> an aggregation key to use to associate like events", + " --source <source> the source to associate with this event (e.g. \"users\", \"jenkins\", etc)" + ]; + }, + handleCli: function(subcommand, args, callback){ + if(subcommand === "get" && args._.length > 4){ + get(parseInt(args._[4]), callback); + } else if(subcommand === "query" && args._.length > 5){ + var from = parseInt(args._[4]); + var to = parseInt(args._[5]); + var parameters = {}; + if(args["sources"]){ + parameters.sources = args["sources"]; + } + if(args["tags"]){ + parameters.tags = args["tags"]; + } + query(from, to, parameters, callback); + } else if(subcommand === "create" && args._.length > 5){ + var title = args._[4]; + var text = args._[5]; + var properties = {}; + if(args["priority"]){ + properties.priority = args["priority"]; + } + if(args["host"]){ + properties.host = args["host"]; + } + if(args["time"]){ + properties.date_happened = parseInt(args["time"]); + } + if(args["tags"]){ + properties.tags = args["tags"].split(","); + } + if(args["agg-key"]){ + properties.aggregation_key = args["agg-key"]; + } + if(args["source"]){ + properties.source_type_name = args["source"]; + } + create(title, text, properties, callback); + } else { + callback("unknown subcommand or arguments try `dogapi event --help` for help", false); + } + } }; From 375a744ccb6d654647adcdb89ce22127f163fccc Mon Sep 17 00:00:00 2001 From: brettlangdon <brett@blangdon.com> Date: Sat, 21 Mar 2015 16:35:47 -0400 Subject: [PATCH 38/65] s/Commands/Subcommands/ --- lib/api/event.js | 2 +- lib/api/infrastructure.js | 2 +- lib/api/metric.js | 2 +- lib/api/serviceCheck.js | 5 ++--- lib/api/tag.js | 2 +- lib/api/user.js | 2 +- 6 files changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/api/event.js b/lib/api/event.js index 167eb92..8641aca 100644 --- a/lib/api/event.js +++ b/lib/api/event.js @@ -150,7 +150,7 @@ module.exports = { getHelp: function(){ return [ "Event:", - " Commands:", + " Subcommands:", " get <event-id> get the event with the provided <event-id>", " query <from> <to> query the event stream between <from> and <to> POSIX timestamps", " create <title> <text> create a new event with <title> and <text>", diff --git a/lib/api/infrastructure.js b/lib/api/infrastructure.js index 5166f46..ee858c4 100644 --- a/lib/api/infrastructure.js +++ b/lib/api/infrastructure.js @@ -41,7 +41,7 @@ module.exports = { getHelp: function(){ return [ "Infrastructure:", - " Commands:", + " Subcommands:", " search <query> query for hosts or metrics with <query> (see http://docs.datadoghq.com/api/#search)", ]; }, diff --git a/lib/api/metric.js b/lib/api/metric.js index c5e53a2..c47e30d 100644 --- a/lib/api/metric.js +++ b/lib/api/metric.js @@ -152,7 +152,7 @@ module.exports = { getHelp: function(){ return [ "Metric:", - " Commands:", + " Subcommands:", " send <metric> <point> add a new datapoint for <metric> for right now", " query <from> <to> <query> query for <query> between <from> and <to> POSIX timestamps", "", diff --git a/lib/api/serviceCheck.js b/lib/api/serviceCheck.js index 39edf5f..3118c03 100644 --- a/lib/api/serviceCheck.js +++ b/lib/api/serviceCheck.js @@ -60,11 +60,10 @@ module.exports = { getHelp: function(){ return [ "Service Check:", - " Commands:", - " check <check> <host> <status> add a new service check for <check> and <host> at level <status>", + " Subcommands:", + " check <check> <host> <status> add a new service check for <check> and <host> at level <status> (0=OK, 1=WARNING, 2=CRITICAL, 3=UNKNOWN)", "", " Options:", - " <status> 0=OK, 1=WARNING, 2=CRITICAL, 3=UNKNOWN", " --time <timestamp> the POSIX timestamp to use for the check", " --message <message> an optional message to accompany the check", " --tags <tags> a comma separated list of \"tag:value\"'s for the check" diff --git a/lib/api/tag.js b/lib/api/tag.js index cf38d9a..29d302e 100644 --- a/lib/api/tag.js +++ b/lib/api/tag.js @@ -222,7 +222,7 @@ module.exports = { getHelp: function(){ return [ "Tag:", - " Commands:", + " Subcommands:", " get_all get all tags", " get <host> get all tags for a given host", " remove <host> delete tags for a given host", diff --git a/lib/api/user.js b/lib/api/user.js index 728c419..76b6815 100644 --- a/lib/api/user.js +++ b/lib/api/user.js @@ -38,7 +38,7 @@ module.exports = { getHelp: function(){ return [ "User:", - " Commands:", + " Subcommands:", " invite <address>... invite the given list of e-mail addresses to your datadog org" ]; }, From 9d7e1b83fa418b56dc4d1d4a43b11257c17355a5 Mon Sep 17 00:00:00 2001 From: brettlangdon <brett@blangdon.com> Date: Sat, 21 Mar 2015 16:45:29 -0400 Subject: [PATCH 39/65] s/get_all/getAll/ use camel case no snake case --- lib/api/tag.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/api/tag.js b/lib/api/tag.js index 29d302e..7ec5cda 100644 --- a/lib/api/tag.js +++ b/lib/api/tag.js @@ -18,12 +18,12 @@ var util = require('util'); * app_key: "app_key" * }; * dogapi.initialize(options); - * dogapi.tag.get_all(function(err, results){ + * dogapi.tag.getAll(function(err, results){ * console.dir(results); * }); * ``` */ -function get_all(source, callback){ +function getAll(source, callback){ if(arguments.length < 2 && typeof arguments[0] === "function"){ callback = source; source = undefined; @@ -205,14 +205,14 @@ function remove(hostname, source, callback){ module.exports = { _client: client, - get_all: get_all, + getAll: getAll, get: get, create: create, update: update, remove: remove, getUsage: function(){ return [ - "${command} tag get_all [--source <source>]", + "${command} tag getall [--source <source>]", "${command} tag get <host> [--source <source>] [--by-source]", "${command} tag remove <host> [--source <source>]", "${command} tag create <host> <tags> [--source <source>]", @@ -223,7 +223,7 @@ module.exports = { return [ "Tag:", " Subcommands:", - " get_all get all tags", + " getall get all tags", " get <host> get all tags for a given host", " remove <host> delete tags for a given host", " create <host> <tags> add the comma separates \"tag:value\"'s from <tag> to <host>", @@ -238,8 +238,8 @@ module.exports = { var source = args["source"]; var host = args._[4]; - if(subcommand === "get_all"){ - get_all(source, callback); + if(subcommand === "getall"){ + getAll(source, callback); } else if(subcommand === "get"){ var options = {}; if(source){ From 81923ef3da4a3c0ef3202e6381ed5bb3544136e3 Mon Sep 17 00:00:00 2001 From: brettlangdon <brett@blangdon.com> Date: Sat, 21 Mar 2015 16:45:58 -0400 Subject: [PATCH 40/65] let each api manage it's owns subcommands --- bin/dogapi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/dogapi b/bin/dogapi index 882ac85..3a8bdb8 100755 --- a/bin/dogapi +++ b/bin/dogapi @@ -50,7 +50,7 @@ if(command === "now"){ } else if(command === "then" && args._.length > 3){ console.log(dogapi.now() - parseInt(args._[args._.length - 1])); } else if(dogapi.hasOwnProperty(command)){ - if(subcommand && dogapi[command].hasOwnProperty(subcommand)){ + if(subcommand){ dogapi[command].handleCli(subcommand, args, function(err, res){ if(err){ console.error(err); From c3f044c6adc35581b5aee08cdb5665665bb28e79 Mon Sep 17 00:00:00 2001 From: brettlangdon <brett@blangdon.com> Date: Sat, 21 Mar 2015 16:46:10 -0400 Subject: [PATCH 41/65] show error on unknown command --- lib/api/tag.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/api/tag.js b/lib/api/tag.js index 7ec5cda..db49461 100644 --- a/lib/api/tag.js +++ b/lib/api/tag.js @@ -257,6 +257,8 @@ module.exports = { update(host, tags, source, callback); } else if(subcommand === "delete"){ remove(host, source, callback); + } else { + callback("unknown subcommand or arguments try `dogapi tag --help` for help", false); } } } From e796eb7eeb63e5673d0848f8e171b5aa00e48822 Mon Sep 17 00:00:00 2001 From: brettlangdon <brett@blangdon.com> Date: Sat, 21 Mar 2015 17:53:33 -0400 Subject: [PATCH 42/65] add monitor api --- lib/api/monitor.js | 438 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 438 insertions(+) create mode 100644 lib/api/monitor.js diff --git a/lib/api/monitor.js b/lib/api/monitor.js new file mode 100644 index 0000000..636a400 --- /dev/null +++ b/lib/api/monitor.js @@ -0,0 +1,438 @@ +var client = require("../client"); +var util = require("util"); + +/*section: monitor + *comment: create a new monitor + *params: + * type: one of "metric alert" or "service check" + * query: the monitor query to use, you probably want to read datadog's [monitor create](http://docs.datadoghq.com/api/#monitor-create) docs + * properties: | + * optional, an object containing any of the following + * * name: the name of the monitor + * * message: the message for the monitor + * * options: an object, to see available options please see the [monitor create](http://docs.datadoghq.com/api/#monitor-create) docs + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * var metricType = "metric alert"; + * var query = "avg(last_1h):sum:system.net.bytes_rcvd{host:host0} > 100"; + * dogapi.monitor.create(metricType, query, function(err, res){ + * console.dir(res); + * }); + * ``` + */ +function create(type, query, properties, callback){ + if(arguments.length < 4 && typeof arguments[2] === "function"){ + callback = properties; + properties = {}; + } + + var params = { + body: { + type: type, + query: query + } + }; + + if(typeof properties === "object"){ + if(properties.name){ + params.body.name = properties.name; + } + if(properties.messsage){ + params.body.message = properties.message; + } + if(typeof properties.options === "object"){ + params.body.options = properties.options; + } + } + client.request("POST", "/monitor", params, callback); +} + +/*section: monitor + *comment: get an existing monitor's details + *params: + * monitorId: the id of the monitor + * groupStates: an array containing any of the following "all", "alert", "warn", or "no data" + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.monitor.get(1234, function(err, res){ + * console.dir(res); + * }); + * ``` + */ +function get(monitorId, groupStates, callback){ + if(arguments.length < 3 && typeof arguments[1] === "function"){ + callback = groupStates; + groupStates = undefined; + } + + var params = {}; + if(groupStates){ + params.query = { + group_states: groupStates.join(",") + }; + } + + client.request("GET", util.format("/monitor/%s", monitorId), params, callback); +} + +/*section: monitor + *comment: get all monitors + *params: + * options: | + * optional, an object containing any of the following + * * group_states: an array containing any of the following "all", "alert", "warn", or "no data" + * * tags: an array of "tag:value"'s to filter on + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.monitor.getAll(function(err, res){ + * console.dir(res); + * }); + * ``` + */ +function getAll(options, callback){ + if(arguments.length < 2 && typeof arguments[0] === "function"){ + callback = options; + options = {}; + } + var params = {}; + if(typeof options === "object"){ + params.query = {}; + if(options.group_states){ + params.query.group_states = options.group_states.join(","); + } + if(options.tags){ + params.query.tags = options.tags.join(","); + } + } + client.request("GET", "/monitor", params, callback); +} + +/*section: monitor + *comment: update a monitor's details + *params: + * monitorId: the id of the monitor to edit + * query: the query that the monitor should have, see the [monitor create](http://docs.datadoghq.com/api/#monitor-create) docs for more info + * properties: | + * optional, an object containing any of the following + * * name: the name of the monitor + * * message: the message for the monitor + * * options: an object, to see available options please see the [monitor create](http://docs.datadoghq.com/api/#monitor-create) docs + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * var query = "avg(last_1h):sum:system.net.bytes_rcvd{host:host0} > 100"; + * dogapi.monitor.update(1234, query, function(err, res){ + * console.dir(res); + * }); + * ``` + */ +function update(monitorId, query, properties, callback){ + if(arguments.length < 4 && typeof arguments[2] === "function"){ + callback = properties; + properties = {}; + } + + var params = { + body: { + query: query + } + }; + + if(typeof properties === "object"){ + if(properties.name){ + params.body.name = properties.name; + } + if(properties.messsage){ + params.body.message = properties.message; + } + if(typeof properties.options === "object"){ + params.body.options = properties.options; + } + } + client.request("PUT", util.format("/monitor/%s", monitorId), params, callback); +} + +/*section: monitor + *comment: delete an existing monitor + *params: + * monitorId: the id of the monitor to remove + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.monitor.remove(1234, function(err, res){ + * console.dir(res); + * }); + * ``` + */ +function remove(monitorId, callback){ + client.request("DELETE", util.format("/monitor/%s", monitorId), callback); +} + +/*section: monitor + *comment: mute an existing monitor + *params: + * monitorId: the id of the monitor to mute + * options: | + * optional, an object containing any of the following + * * scope: the scope to mute (e.g. "role:db") + * * end: POSIX timestamp indicating when the mute should end + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.monitor.mute(1234, function(err, res){ + * console.dir(res); + * }); + * ``` + */ +function mute(monitorId, options, callback){ + if(arguments.length < 3 && typeof arguments[1] === "function"){ + callback = options; + options = {}; + } + var params = {}; + if(typeof options === "object"){ + params.body = {}; + if(options.scope){ + params.body.scope = options.scope; + } + if(options.end){ + params.body.end = parseInt(options.end); + } + } + client.request("POST", util.format("/monitor/%s/mute"), params, callback); +} + +/*section: monitor + *comment: mute all monitors + *params: + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.monitor.muteAll(function(err, res){ + * console.dir(res); + * }); + * ``` + */ +function muteAll(callback){ + client.request("POST", "/monitor/mute_all", callback); +} + +/*section: monitor + *comment: unmute an existing monitor + *params: + * monitorId: the id of the monitor to unmute + * scope: optional, a scope to apply the unmute to (e.g. "role:db") + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.monitor.unmute(1234, function(err, res){ + * console.dir(res); + * }); + * ``` + */ +function unmute(monitorId, scope, callback){ + if(arguments.length < 3 && typeof arguments[1] === "function"){ + callback = scope; + scope = undefined; + } + var params = {}; + if(scope){ + params.body = { + scope: scope + }; + } + client.request("POST", util.format("/monitor/%s/unmute", monitorId), params, callback); +} + +/*section: monitor + *comment: unmute all monitors + *params: + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.monitor.unmuteAll(function(err, res){ + * console.dir(res); + * }); + * ``` + */ +function unmuteAll(callback){ + client.request("POST", "/monitor/unmute_all", callback); +} + +module.exports = { + create: create, + get: get, + update: update, + remove: remove, + getAll: getAll, + mute: mute, + muteAll: muteAll, + unmute: unmute, + unmuteAll: unmuteAll, + getUsage: function(){ + return [ + "${command} monitor create <type> <query> [--name <name>] [--message <message>]", + "${command} monitor get <monitor-id> [--states <states>]", + "${command} monitor getall [--states <states>] [--tags <tags>]", + "${command} monitor mute <monitor-id> [--scope <scope>] [--end <end>]", + "${command} monitor muteall", + "${command} monitor remove <monitor-id>", + "${command} monitor unmute <monitor-id> [--scope <scope>]", + "${command} monitor unmuteall", + "${command} monitor update <monitor-id> <query> [--name <name>] [--message <message>]" + ]; + }, + getHelp: function(){ + return [ + "Monitor:", + " Subcommands:", + " create <type> <query> create a new monitor", + " get <monitor-id> get a monitors details", + " getall get a list of all monitors", + " mute <monitor-id> mute the monitor with the id <monitor-id>", + " muteall mute all monitors", + " remove <monitor-id> delete the monitor with the id <monitor-id>", + " unmute <monitor-id> unmute the monitor with the id <monitor-id>", + " unmuteall unmute all monitors", + " update <monitor-id> <query> update an existing monitor", + "", + " Options:", + " --states <states> a comma separated list containing any of \"all\", \"alert\", \"warn\", or \"no data\"", + " --tags <tags> a comma separated list of \"tag:value\"'s", + " --scope <scope> the scope of the monitor to mute (e.g. \"role:db\")", + " --end <end> POSIX timestamp for when the mute should end", + " --name <name> the name for the monitor", + " --message <message> the message for the monitor" + ]; + }, + handleCli: function(subcommand, args, callback){ + var states = []; + if(args["states"]){ + states = args["states"].split(","); + } + + var tags = []; + if(args["tags"]){ + tags = args["tags"].split(","); + } + + var name = args["name"]; + var message = args["message"]; + + if(subcommand === "get"){ + var monitorId = args._[4]; + get(monitorId, states, callback); + } else if(subcommand === "getall"){ + var options = {}; + if(states.length){ + options.group_states = states; + } + if(tags.length){ + options.tags = tags; + } + getAll(options, callback); + } else if(subcommand === "mute"){ + var monitorId = args._[4]; + var options = {}; + if(args["scope"]){ + options.scope = args["scope"]; + } + if(args["end"]){ + options.end = args["end"]; + } + mute(monitorId, options, callback); + } else if(subcommand === "unmute"){ + var monitorId = args._[4]; + var scope = args["scope"]; + unmute(monitorId, scope, callback); + } else if(subcommand === "unmuteall"){ + unmuteAll(callback); + } else if(subcommand === "muteall"){ + muteAll(callback); + } else if(subcommand === "remove"){ + var monitorId = args._[4]; + remove(monitorId, callback); + } else if(subcommand === "create" && args._.length > 5){ + var type = args._[4]; + var query = args._[5]; + var properties = {}; + if(name){ + properties.name = name; + } + if(message){ + properties.message = message; + } + create(type, query, properties, callback); + } else if(subcommand === "update" && args._.length > 5){ + var monitorId = args._[4]; + var query = args._[5]; + var properties = {}; + if(name){ + properties.name = name; + } + if(message){ + properties.message = message; + } + update(monitorId, query, properties, callback); + } else { + callback("unknown subcommand or arguments try `dogapi monitor --help` for help", false); + } + } +}; From 9ef2edb939a136f327e2fcb23562114953cc315c Mon Sep 17 00:00:00 2001 From: brettlangdon <brett@blangdon.com> Date: Sat, 21 Mar 2015 17:53:42 -0400 Subject: [PATCH 43/65] add monitor api and reorder --- lib/api/index.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/api/index.js b/lib/api/index.js index 1125d63..4ca8d83 100644 --- a/lib/api/index.js +++ b/lib/api/index.js @@ -1,10 +1,11 @@ var api = { - tag: require("./tag"), - metric: require("./metric"), event: require("./event"), + infrastructure: require("./infrastructure"), + metric: require("./metric"), + monitor: require("./monitor"), serviceCheck: require("./serviceCheck"), + tag: require("./tag"), user: require("./user"), - infrastructure: require("./infrastructure") }; module.exports = function(obj){ From ccc91a946b8ed574f28cac792bdcd673b2c31e62 Mon Sep 17 00:00:00 2001 From: brettlangdon <brett@blangdon.com> Date: Sat, 21 Mar 2015 17:53:53 -0400 Subject: [PATCH 44/65] rebuild docs --- index.html | 304 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 300 insertions(+), 4 deletions(-) diff --git a/index.html b/index.html index 55d4309..48666e4 100644 --- a/index.html +++ b/index.html @@ -22,6 +22,8 @@ document.addEventListener("DOMContentLoaded", function(){hljs.initHighlightingOn </li> <li role=""><a href="#metric">metric</a> </li> +<li role=""><a href="#monitor">monitor</a> +</li> <li role=""><a href="#serviceCheck">serviceCheck</a> </li> <li role=""><a href="#tag">tag</a> @@ -337,6 +339,300 @@ dogapi.metric.query(then, now, query, function(err, res){ </div> </div> </section> +<section id="monitor" class="col-sm-12"> +<div class="row"> +<h2 class="bg-primary" style="text-indent:1rem">monitor</h2></div> +<ul class="nav nav-pills"> +<li role"presentation"><a href="#monitor-create">create</a></li> +<li role"presentation"><a href="#monitor-get">get</a></li> +<li role"presentation"><a href="#monitor-getAll">getAll</a></li> +<li role"presentation"><a href="#monitor-update">update</a></li> +<li role"presentation"><a href="#monitor-remove">remove</a></li> +<li role"presentation"><a href="#monitor-mute">mute</a></li> +<li role"presentation"><a href="#monitor-muteAll">muteAll</a></li> +<li role"presentation"><a href="#monitor-unmute">unmute</a></li> +<li role"presentation"><a href="#monitor-unmuteAll">unmuteAll</a></li> +</ul> +<div class="function row" id="monitor-create"> +<h3 class="bg-info" style="text-indent:.5rem;padding:.5rem;margin-top:.5rem">create(type, query, properties, callback)</h3> +<div class="col-md-6"> +<p>create a new monitor</p> +<h4>Parameters:</h4> +<dl> +<dt>type</dt> +<dd><p>one of "metric alert" or "service check"</p> +</dd> +<dt>query</dt> +<dd><p>the monitor query to use, you probably want to read datadog's <a href="http://docs.datadoghq.com/api/#monitor-create">monitor create</a> docs</p> +</dd> +<dt>properties</dt> +<dd><p><em>optional</em>, an object containing any of the following</p> +<ul> +<li>name: the name of the monitor</li> +<li>message: the message for the monitor</li> +<li>options: an object, to see available options please see the <a href="http://docs.datadoghq.com/api/#monitor-create">monitor create</a> docs</li> +</ul> +</dd> +<dt>callback</dt> +<dd><p>function(err, res)</p> +</dd> +</dl> +</div> +<div class="col-md-6"> +<pre><code class="lang-javascript">var dogapi = require("dogapi"); +var options = { + api_key: "api_key", + app_key: "app_key" +}; +dogapi.initialize(options); +var metricType = "metric alert"; +var query = "avg(last_1h):sum:system.net.bytes_rcvd{host:host0} > 100"; +dogapi.monitor.create(metricType, query, function(err, res){ + console.dir(res); +}); +</code></pre> +</div> +</div> +<div class="function row" id="monitor-get"> +<h3 class="bg-info" style="text-indent:.5rem;padding:.5rem;margin-top:.5rem">get(monitorId, groupStates, callback)</h3> +<div class="col-md-6"> +<p>get an existing monitor's details</p> +<h4>Parameters:</h4> +<dl> +<dt>monitorId</dt> +<dd><p>the id of the monitor</p> +</dd> +<dt>groupStates</dt> +<dd><p>an array containing any of the following "all", "alert", "warn", or "no data"</p> +</dd> +<dt>callback</dt> +<dd><p>function(err, res)</p> +</dd> +</dl> +</div> +<div class="col-md-6"> +<pre><code class="lang-javascript">var dogapi = require("dogapi"); +var options = { + api_key: "api_key", + app_key: "app_key" +}; +dogapi.initialize(options); +dogapi.monitor.get(1234, function(err, res){ + console.dir(res); +}); +</code></pre> +</div> +</div> +<div class="function row" id="monitor-getAll"> +<h3 class="bg-info" style="text-indent:.5rem;padding:.5rem;margin-top:.5rem">getAll(options, callback)</h3> +<div class="col-md-6"> +<p>get all monitors</p> +<h4>Parameters:</h4> +<dl> +<dt>options</dt> +<dd><p><em>optional</em>, an object containing any of the following</p> +<ul> +<li>group_states: an array containing any of the following "all", "alert", "warn", or "no data"</li> +<li>tags: an array of "tag:value"'s to filter on</li> +</ul> +</dd> +<dt>callback</dt> +<dd><p>function(err, res)</p> +</dd> +</dl> +</div> +<div class="col-md-6"> +<pre><code class="lang-javascript">var dogapi = require("dogapi"); +var options = { + api_key: "api_key", + app_key: "app_key" +}; +dogapi.initialize(options); +dogapi.monitor.getAll(function(err, res){ + console.dir(res); +}); +</code></pre> +</div> +</div> +<div class="function row" id="monitor-update"> +<h3 class="bg-info" style="text-indent:.5rem;padding:.5rem;margin-top:.5rem">update(monitorId, query, properties, callback)</h3> +<div class="col-md-6"> +<p>update a monitor's details</p> +<h4>Parameters:</h4> +<dl> +<dt>monitorId</dt> +<dd><p>the id of the monitor to edit</p> +</dd> +<dt>query</dt> +<dd><p>the query that the monitor should have, see the <a href="http://docs.datadoghq.com/api/#monitor-create">monitor create</a> docs for more info</p> +</dd> +<dt>properties</dt> +<dd><p><em>optional</em>, an object containing any of the following</p> +<ul> +<li>name: the name of the monitor</li> +<li>message: the message for the monitor</li> +<li>options: an object, to see available options please see the <a href="http://docs.datadoghq.com/api/#monitor-create">monitor create</a> docs</li> +</ul> +</dd> +<dt>callback</dt> +<dd><p>function(err, res)</p> +</dd> +</dl> +</div> +<div class="col-md-6"> +<pre><code class="lang-javascript">var dogapi = require("dogapi"); +var options = { + api_key: "api_key", + app_key: "app_key" +}; +dogapi.initialize(options); +var query = "avg(last_1h):sum:system.net.bytes_rcvd{host:host0} > 100"; +dogapi.monitor.update(1234, query, function(err, res){ + console.dir(res); +}); +</code></pre> +</div> +</div> +<div class="function row" id="monitor-remove"> +<h3 class="bg-info" style="text-indent:.5rem;padding:.5rem;margin-top:.5rem">remove(monitorId, callback)</h3> +<div class="col-md-6"> +<p>delete an existing monitor</p> +<h4>Parameters:</h4> +<dl> +<dt>monitorId</dt> +<dd><p>the id of the monitor to remove</p> +</dd> +<dt>callback</dt> +<dd><p>function(err, res)</p> +</dd> +</dl> +</div> +<div class="col-md-6"> +<pre><code class="lang-javascript">var dogapi = require("dogapi"); +var options = { + api_key: "api_key", + app_key: "app_key" +}; +dogapi.initialize(options); +dogapi.monitor.remove(1234, function(err, res){ + console.dir(res); +}); +</code></pre> +</div> +</div> +<div class="function row" id="monitor-mute"> +<h3 class="bg-info" style="text-indent:.5rem;padding:.5rem;margin-top:.5rem">mute(monitorId, options, callback)</h3> +<div class="col-md-6"> +<p>mute an existing monitor</p> +<h4>Parameters:</h4> +<dl> +<dt>monitorId</dt> +<dd><p>the id of the monitor to mute</p> +</dd> +<dt>options</dt> +<dd><p><em>optional</em>, an object containing any of the following</p> +<ul> +<li>scope: the scope to mute (e.g. "role:db")</li> +<li>end: POSIX timestamp indicating when the mute should end</li> +</ul> +</dd> +<dt>callback</dt> +<dd><p>function(err, res)</p> +</dd> +</dl> +</div> +<div class="col-md-6"> +<pre><code class="lang-javascript">var dogapi = require("dogapi"); +var options = { + api_key: "api_key", + app_key: "app_key" +}; +dogapi.initialize(options); +dogapi.monitor.mute(1234, function(err, res){ + console.dir(res); +}); +</code></pre> +</div> +</div> +<div class="function row" id="monitor-muteAll"> +<h3 class="bg-info" style="text-indent:.5rem;padding:.5rem;margin-top:.5rem">muteAll(callback)</h3> +<div class="col-md-6"> +<p>mute all monitors</p> +<h4>Parameters:</h4> +<dl> +<dt>callback</dt> +<dd><p>function(err, res)</p> +</dd> +</dl> +</div> +<div class="col-md-6"> +<pre><code class="lang-javascript">var dogapi = require("dogapi"); +var options = { + api_key: "api_key", + app_key: "app_key" +}; +dogapi.initialize(options); +dogapi.monitor.muteAll(function(err, res){ + console.dir(res); +}); +</code></pre> +</div> +</div> +<div class="function row" id="monitor-unmute"> +<h3 class="bg-info" style="text-indent:.5rem;padding:.5rem;margin-top:.5rem">unmute(monitorId, scope, callback)</h3> +<div class="col-md-6"> +<p>unmute an existing monitor</p> +<h4>Parameters:</h4> +<dl> +<dt>monitorId</dt> +<dd><p>the id of the monitor to unmute</p> +</dd> +<dt>scope</dt> +<dd><p><em>optional</em>, a scope to apply the unmute to (e.g. "role:db")</p> +</dd> +<dt>callback</dt> +<dd><p>function(err, res)</p> +</dd> +</dl> +</div> +<div class="col-md-6"> +<pre><code class="lang-javascript">var dogapi = require("dogapi"); +var options = { + api_key: "api_key", + app_key: "app_key" +}; +dogapi.initialize(options); +dogapi.monitor.unmute(1234, function(err, res){ + console.dir(res); +}); +</code></pre> +</div> +</div> +<div class="function row" id="monitor-unmuteAll"> +<h3 class="bg-info" style="text-indent:.5rem;padding:.5rem;margin-top:.5rem">unmuteAll(callback)</h3> +<div class="col-md-6"> +<p>unmute all monitors</p> +<h4>Parameters:</h4> +<dl> +<dt>callback</dt> +<dd><p>function(err, res)</p> +</dd> +</dl> +</div> +<div class="col-md-6"> +<pre><code class="lang-javascript">var dogapi = require("dogapi"); +var options = { + api_key: "api_key", + app_key: "app_key" +}; +dogapi.initialize(options); +dogapi.monitor.unmuteAll(function(err, res){ + console.dir(res); +}); +</code></pre> +</div> +</div> +</section> <section id="serviceCheck" class="col-sm-12"> <div class="row"> <h2 class="bg-primary" style="text-indent:1rem">serviceCheck</h2></div> @@ -392,14 +688,14 @@ dogapi.metric.query(then, now, query, function(err, res){ <div class="row"> <h2 class="bg-primary" style="text-indent:1rem">tag</h2></div> <ul class="nav nav-pills"> -<li role"presentation"><a href="#tag-get_all">get_all</a></li> +<li role"presentation"><a href="#tag-getAll">getAll</a></li> <li role"presentation"><a href="#tag-get">get</a></li> <li role"presentation"><a href="#tag-create">create</a></li> <li role"presentation"><a href="#tag-update">update</a></li> <li role"presentation"><a href="#tag-remove">remove</a></li> </ul> -<div class="function row" id="tag-get_all"> -<h3 class="bg-info" style="text-indent:.5rem;padding:.5rem;margin-top:.5rem">get_all(source, callback)</h3> +<div class="function row" id="tag-getAll"> +<h3 class="bg-info" style="text-indent:.5rem;padding:.5rem;margin-top:.5rem">getAll(source, callback)</h3> <div class="col-md-6"> <p>get all host tags</p> <h4>Parameters:</h4> @@ -419,7 +715,7 @@ var options = { app_key: "app_key" }; dogapi.initialize(options); -dogapi.tag.get_all(function(err, results){ +dogapi.tag.getAll(function(err, results){ console.dir(results); }); </code></pre> From 1cfe0c2a5570268e91ae815c59137bc15226fa57 Mon Sep 17 00:00:00 2001 From: brettlangdon <brett@blangdon.com> Date: Sat, 21 Mar 2015 18:17:22 -0400 Subject: [PATCH 45/65] add host api --- lib/api/host.js | 112 +++++++++++++++++++++++++++++++++++++++++++++++ lib/api/index.js | 1 + 2 files changed, 113 insertions(+) create mode 100644 lib/api/host.js diff --git a/lib/api/host.js b/lib/api/host.js new file mode 100644 index 0000000..90772bb --- /dev/null +++ b/lib/api/host.js @@ -0,0 +1,112 @@ +var client = require("../client"); + +/*section: host + *comment: mute the given host, if it is not already muted + *params: + * hostname: the hostname of the host to mute + * options: | + * optional, an object containing any of the following + * * end: POSIX timestamp for when the mute should end + * * override: whether or not to override the end for an existing mute + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.host.mute("my.host.name", function(err, res){ + * console.dir(res); + * }); + * ``` + */ +function mute(hostname, options, callback){ + if(arguments.length < 3 && typeof arguments[1] === "function"){ + callback = options; + options = {}; + } + var params = { + body: { + hostname: hostname + } + }; + if(typeof options === "object"){ + if(options.end){ + params.body.end = parseInt(options.end); + } + if(options.override){ + params.body.override = options.override; + } + } + client.request("POST", "/host", params, callback); +} + +/*section: host + *comment: unmute the given host, if it is not already unmuted + *params: + * hostname: the hostname of the host to unmute + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.host.unmute("my.host.name", function(err, res){ + * console.dir(res); + * }); + * ``` + */ +function unmute(hostname, callback){ + var params = { + body: { + hostname: hostname + } + }; + client.request("POST", "/host", params, callback); +} + +module.exports = { + mute: mute, + unmute: unmute, + getUsage: function(){ + return [ + "${command} host mute <host> [--end <end>] [--override]", + "${command} host unmute <host>" + ]; + }, + getHelp: function(){ + return [ + "Host:", + " Subcommands:", + " mute <host> mute the host with the provided hostname", + " unmute <host> unmute the host with the provided hostname", + "", + " Options:", + " --end <end> POSIX timestamp for when the mute should end", + " --override override an existing \"end\" for a mute on a host" + ]; + }, + handleCli: function(subcommand, args, callback){ + if(subcommand === "mute"){ + var hostname = args._[4]; + var options = {}; + if(args["end"]){ + options.end = parseInt(args["end"]); + } + if(args["override"]){ + options.override = args["override"]; + } + mute(hostname, options, callback); + } else if(subcommand === "unmute"){ + var hostname = args._[4]; + unmute(hostname, callback); + } else { + callback("unknown subcommand or arguments try `dogapi host --help` for help", false); + } + } +}; diff --git a/lib/api/index.js b/lib/api/index.js index 4ca8d83..90a1b3f 100644 --- a/lib/api/index.js +++ b/lib/api/index.js @@ -1,5 +1,6 @@ var api = { event: require("./event"), + host: require("./host"), infrastructure: require("./infrastructure"), metric: require("./metric"), monitor: require("./monitor"), From ab2488135c71f3e07841d3e5e758ff72e6686393 Mon Sep 17 00:00:00 2001 From: brettlangdon <brett@blangdon.com> Date: Sat, 21 Mar 2015 18:17:34 -0400 Subject: [PATCH 46/65] rebuild docs --- index.html | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/index.html b/index.html index 48666e4..a6d8ae3 100644 --- a/index.html +++ b/index.html @@ -18,6 +18,8 @@ document.addEventListener("DOMContentLoaded", function(){hljs.initHighlightingOn <ul class="nav nav-pills"> <li role=""><a href="#event">event</a> </li> +<li role=""><a href="#host">host</a> +</li> <li role=""><a href="#infrastructure">infrastructure</a> </li> <li role=""><a href="#metric">metric</a> @@ -170,6 +172,75 @@ document.addEventListener("DOMContentLoaded", function(){hljs.initHighlightingOn </div> </div> </section> +<section id="host" class="col-sm-12"> +<div class="row"> +<h2 class="bg-primary" style="text-indent:1rem">host</h2></div> +<ul class="nav nav-pills"> +<li role"presentation"><a href="#host-mute">mute</a></li> +<li role"presentation"><a href="#host-unmute">unmute</a></li> +</ul> +<div class="function row" id="host-mute"> +<h3 class="bg-info" style="text-indent:.5rem;padding:.5rem;margin-top:.5rem">mute(hostname, options, callback)</h3> +<div class="col-md-6"> +<p>mute the given host, if it is not already muted</p> +<h4>Parameters:</h4> +<dl> +<dt>hostname</dt> +<dd><p>the hostname of the host to mute</p> +</dd> +<dt>options</dt> +<dd><p><em>optional</em>, an object containing any of the following</p> +<ul> +<li>end: POSIX timestamp for when the mute should end</li> +<li>override: whether or not to override the end for an existing mute</li> +</ul> +</dd> +<dt>callback</dt> +<dd><p>function(err, res)</p> +</dd> +</dl> +</div> +<div class="col-md-6"> +<pre><code class="lang-javascript">var dogapi = require("dogapi"); +var options = { + api_key: "api_key", + app_key: "app_key" +}; +dogapi.initialize(options); +dogapi.host.mute("my.host.name", function(err, res){ + console.dir(res); +}); +</code></pre> +</div> +</div> +<div class="function row" id="host-unmute"> +<h3 class="bg-info" style="text-indent:.5rem;padding:.5rem;margin-top:.5rem">unmute(hostname, callback)</h3> +<div class="col-md-6"> +<p>unmute the given host, if it is not already unmuted</p> +<h4>Parameters:</h4> +<dl> +<dt>hostname</dt> +<dd><p>the hostname of the host to unmute</p> +</dd> +<dt>callback</dt> +<dd><p>function(err, res)</p> +</dd> +</dl> +</div> +<div class="col-md-6"> +<pre><code class="lang-javascript">var dogapi = require("dogapi"); +var options = { + api_key: "api_key", + app_key: "app_key" +}; +dogapi.initialize(options); +dogapi.host.unmute("my.host.name", function(err, res){ + console.dir(res); +}); +</code></pre> +</div> +</div> +</section> <section id="infrastructure" class="col-sm-12"> <div class="row"> <h2 class="bg-primary" style="text-indent:1rem">infrastructure</h2></div> From 4b4183622cec7622a05341e787ad577a012355d9 Mon Sep 17 00:00:00 2001 From: brettlangdon <brett@blangdon.com> Date: Sat, 21 Mar 2015 18:23:34 -0400 Subject: [PATCH 47/65] s/${command}/ dogapi/ --- bin/dogapi | 12 ++++++------ lib/api/event.js | 6 +++--- lib/api/host.js | 4 ++-- lib/api/infrastructure.js | 2 +- lib/api/metric.js | 4 ++-- lib/api/monitor.js | 18 +++++++++--------- lib/api/serviceCheck.js | 2 +- lib/api/tag.js | 10 +++++----- lib/api/user.js | 2 +- 9 files changed, 30 insertions(+), 30 deletions(-) diff --git a/bin/dogapi b/bin/dogapi index 3a8bdb8..064b42e 100755 --- a/bin/dogapi +++ b/bin/dogapi @@ -11,11 +11,11 @@ dogapi.initialize(config); var usage = [ "Usage:", - "${command} --help", - "${command} <command> --help", - "${command} --version", - "${command} now", - "${command} then <seconds-ago>" + "dogapi --help", + "dogapi <command> --help", + "dogapi --version", + "dogapi now", + "dogapi then <seconds-ago>" ]; var help = []; for(var key in dogapi){ @@ -34,7 +34,7 @@ for(var key in dogapi){ } usage = usage.concat(help); -usage = usage.join("\r\n").replace(/\$\{command\}/g, " dogapi"); +usage = usage.join("\r\n"); var args = minimist(process.argv); var command = args._[2]; diff --git a/lib/api/event.js b/lib/api/event.js index 8641aca..4c1be38 100644 --- a/lib/api/event.js +++ b/lib/api/event.js @@ -142,9 +142,9 @@ module.exports = { query: query, getUsage: function(){ return [ - "${command} event get <event-id>", - "${command} event query <from> <to> [--priority <priority>] [--sources <sources>] [--tags <tags>]", - "${command} event create <title> <text> [--time <timestamp>] [--priority <priority>] [--host <host>] [--tags <tags>] [--type <type>] [--agg-key <agg-key>] [--source <source>]" + " dogapi event get <event-id>", + " dogapi event query <from> <to> [--priority <priority>] [--sources <sources>] [--tags <tags>]", + " dogapi event create <title> <text> [--time <timestamp>] [--priority <priority>] [--host <host>] [--tags <tags>] [--type <type>] [--agg-key <agg-key>] [--source <source>]" ]; }, getHelp: function(){ diff --git a/lib/api/host.js b/lib/api/host.js index 90772bb..e2e2bb2 100644 --- a/lib/api/host.js +++ b/lib/api/host.js @@ -75,8 +75,8 @@ module.exports = { unmute: unmute, getUsage: function(){ return [ - "${command} host mute <host> [--end <end>] [--override]", - "${command} host unmute <host>" + " dogapi host mute <host> [--end <end>] [--override]", + " dogapi host unmute <host>" ]; }, getHelp: function(){ diff --git a/lib/api/infrastructure.js b/lib/api/infrastructure.js index ee858c4..b96f245 100644 --- a/lib/api/infrastructure.js +++ b/lib/api/infrastructure.js @@ -35,7 +35,7 @@ module.exports = { search: search, getUsage: function(){ return [ - "${command} infrastructure search <query>" + " dogapi infrastructure search <query>" ] }, getHelp: function(){ diff --git a/lib/api/metric.js b/lib/api/metric.js index c47e30d..5a9ba6e 100644 --- a/lib/api/metric.js +++ b/lib/api/metric.js @@ -145,8 +145,8 @@ module.exports = { query: query, getUsage: function(){ return [ - "${command} metric send <metric> <point> [--tags <tags>] [--host <host>] [--type <type>]", - "${command} metric query <from> <to> <query>" + " dogapi metric send <metric> <point> [--tags <tags>] [--host <host>] [--type <type>]", + " dogapi metric query <from> <to> <query>" ] }, getHelp: function(){ diff --git a/lib/api/monitor.js b/lib/api/monitor.js index 636a400..ac03d5b 100644 --- a/lib/api/monitor.js +++ b/lib/api/monitor.js @@ -328,15 +328,15 @@ module.exports = { unmuteAll: unmuteAll, getUsage: function(){ return [ - "${command} monitor create <type> <query> [--name <name>] [--message <message>]", - "${command} monitor get <monitor-id> [--states <states>]", - "${command} monitor getall [--states <states>] [--tags <tags>]", - "${command} monitor mute <monitor-id> [--scope <scope>] [--end <end>]", - "${command} monitor muteall", - "${command} monitor remove <monitor-id>", - "${command} monitor unmute <monitor-id> [--scope <scope>]", - "${command} monitor unmuteall", - "${command} monitor update <monitor-id> <query> [--name <name>] [--message <message>]" + " dogapi monitor create <type> <query> [--name <name>] [--message <message>]", + " dogapi monitor get <monitor-id> [--states <states>]", + " dogapi monitor getall [--states <states>] [--tags <tags>]", + " dogapi monitor mute <monitor-id> [--scope <scope>] [--end <end>]", + " dogapi monitor muteall", + " dogapi monitor remove <monitor-id>", + " dogapi monitor unmute <monitor-id> [--scope <scope>]", + " dogapi monitor unmuteall", + " dogapi monitor update <monitor-id> <query> [--name <name>] [--message <message>]" ]; }, getHelp: function(){ diff --git a/lib/api/serviceCheck.js b/lib/api/serviceCheck.js index 3118c03..6a6e7fe 100644 --- a/lib/api/serviceCheck.js +++ b/lib/api/serviceCheck.js @@ -54,7 +54,7 @@ module.exports = { check: check, getUsage: function(){ return [ - "${command} servicecheck check <check> <host> <status> [--time <timestamp>] [--message <message>] [--tags <tags>]" + " dogapi servicecheck check <check> <host> <status> [--time <timestamp>] [--message <message>] [--tags <tags>]" ]; }, getHelp: function(){ diff --git a/lib/api/tag.js b/lib/api/tag.js index db49461..3094a36 100644 --- a/lib/api/tag.js +++ b/lib/api/tag.js @@ -212,11 +212,11 @@ module.exports = { remove: remove, getUsage: function(){ return [ - "${command} tag getall [--source <source>]", - "${command} tag get <host> [--source <source>] [--by-source]", - "${command} tag remove <host> [--source <source>]", - "${command} tag create <host> <tags> [--source <source>]", - "${command} tag update <host> <tags> [--source <source>]" + " dogapi tag getall [--source <source>]", + " dogapi tag get <host> [--source <source>] [--by-source]", + " dogapi tag remove <host> [--source <source>]", + " dogapi tag create <host> <tags> [--source <source>]", + " dogapi tag update <host> <tags> [--source <source>]" ]; }, getHelp: function(){ diff --git a/lib/api/user.js b/lib/api/user.js index 76b6815..e0f21f3 100644 --- a/lib/api/user.js +++ b/lib/api/user.js @@ -32,7 +32,7 @@ module.exports = { invite: invite, getUsage: function(){ return [ - "${command} user invite <address>..." + " dogapi user invite <address>..." ]; }, getHelp: function(){ From 461f7529e651d8fdb82b112632f9746bd1f6250a Mon Sep 17 00:00:00 2001 From: brettlangdon <brett@blangdon.com> Date: Sat, 21 Mar 2015 18:29:51 -0400 Subject: [PATCH 48/65] fix some formatting --- bin/dogapi | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/bin/dogapi b/bin/dogapi index 064b42e..7020108 100755 --- a/bin/dogapi +++ b/bin/dogapi @@ -11,11 +11,11 @@ dogapi.initialize(config); var usage = [ "Usage:", - "dogapi --help", - "dogapi <command> --help", - "dogapi --version", - "dogapi now", - "dogapi then <seconds-ago>" + " dogapi --help", + " dogapi <command> --help", + " dogapi --version", + " dogapi now", + " dogapi then <seconds-ago>" ]; var help = []; for(var key in dogapi){ @@ -29,7 +29,7 @@ for(var key in dogapi){ usage = usage.concat(dogapi[key].getUsage()); if(dogapi[key].hasOwnProperty("getHelp") && typeof dogapi[key].getHelp === "function"){ - help = help.concat(["\r\n"], dogapi[key].getHelp()); + help = help.concat([""], dogapi[key].getHelp()); } } From a97398fa01bcfb9736015462c43bf77dcceca981 Mon Sep 17 00:00:00 2001 From: brettlangdon <brett@blangdon.com> Date: Sat, 21 Mar 2015 18:30:07 -0400 Subject: [PATCH 49/65] always output valid json --- bin/dogapi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/dogapi b/bin/dogapi index 7020108..3489d1e 100755 --- a/bin/dogapi +++ b/bin/dogapi @@ -53,13 +53,13 @@ if(command === "now"){ if(subcommand){ dogapi[command].handleCli(subcommand, args, function(err, res){ if(err){ - console.error(err); + console.error(JSON.stringify(err, null, ' ')); process.exit(1); } else { if(res === ""){ res = "success"; } - console.log(res); + console.log(JSON.stringify(res, null, ' ')); } }); } else { From 7b321cc8ee0025ae1ee631b174882bbcfc074d93 Mon Sep 17 00:00:00 2001 From: brettlangdon <brett@blangdon.com> Date: Sat, 21 Mar 2015 18:46:50 -0400 Subject: [PATCH 50/65] add graph api --- lib/api/graph.js | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/api/index.js | 1 + 2 files changed, 74 insertions(+) create mode 100644 lib/api/graph.js diff --git a/lib/api/graph.js b/lib/api/graph.js new file mode 100644 index 0000000..84fdbff --- /dev/null +++ b/lib/api/graph.js @@ -0,0 +1,73 @@ +var client = require("../client"); + +/*section: graph + *comment: take a snapshot of a metric query + *params: + * query: the metric query to use for the snapshot + * from: POSIX timestamp for the beginning of the query + * to: POSIX timestamp for the end of the query + * eventQuery: optional, an event query to overlay event bands on the snapshot + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * var query = "system.cpu.idle{*}"; + * var to = dogapi.now(); + * var from = from - 3600; // an hour ago + * dogapi.graph.snapshot(query, from, to, function(err, res){ + * console.dir(res); + * }); + * ``` + */ +function snapshot(query, from, to, eventQuery, callback){ + if(arguments.length < 5 && typeof arguments[3] === "function"){ + callback = eventQuery; + eventQuery = undefined; + } + var params = { + query: { + metric_query: query, + start: parseInt(from), + end: parseInt(to) + } + }; + if(eventQuery){ + params.query.event_query = eventQuery; + } + + client.request("GET", "/graph/snapshot", params, callback); +} + +module.exports = { + snapshot: snapshot, + getUsage: function(){ + return [ + " dogapi graph snapshot <query> <from> <to> [--events <event-query>]" + ]; + }, + getHelp: function(){ + return [ + "Graph:", + " Subcommands:", + " snapshot <query> <from> <to> take a snapshot of a graph", + " Options:", + " --events <event-query> a query for event bands to add to the snapshot" + ]; + }, + handleCli: function(subcommand, args, callback){ + if(args._.length > 5){ + var query = args._[4]; + var from = parseInt(args._[5]); + var to = parseInt(args._[6]); + var eventQuery = args["events"]; + snapshot(query, from, to, eventQuery, callback); + } else { + callback("unknown subcommand or arguments try `dogapi graph --help` for help", false); + } + } +}; diff --git a/lib/api/index.js b/lib/api/index.js index 90a1b3f..e8edf67 100644 --- a/lib/api/index.js +++ b/lib/api/index.js @@ -1,5 +1,6 @@ var api = { event: require("./event"), + graph: require("./graph"), host: require("./host"), infrastructure: require("./infrastructure"), metric: require("./metric"), From 7c6d7baaf254eaed150a7b0604f997d74e8abcf7 Mon Sep 17 00:00:00 2001 From: brettlangdon <brett@blangdon.com> Date: Sat, 21 Mar 2015 18:46:54 -0400 Subject: [PATCH 51/65] rebuild docs --- index.html | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/index.html b/index.html index a6d8ae3..75a0a2a 100644 --- a/index.html +++ b/index.html @@ -18,6 +18,8 @@ document.addEventListener("DOMContentLoaded", function(){hljs.initHighlightingOn <ul class="nav nav-pills"> <li role=""><a href="#event">event</a> </li> +<li role=""><a href="#graph">graph</a> +</li> <li role=""><a href="#host">host</a> </li> <li role=""><a href="#infrastructure">infrastructure</a> @@ -172,6 +174,52 @@ document.addEventListener("DOMContentLoaded", function(){hljs.initHighlightingOn </div> </div> </section> +<section id="graph" class="col-sm-12"> +<div class="row"> +<h2 class="bg-primary" style="text-indent:1rem">graph</h2></div> +<ul class="nav nav-pills"> +<li role"presentation"><a href="#graph-snapshot">snapshot</a></li> +</ul> +<div class="function row" id="graph-snapshot"> +<h3 class="bg-info" style="text-indent:.5rem;padding:.5rem;margin-top:.5rem">snapshot(query, from, to, eventQuery, callback)</h3> +<div class="col-md-6"> +<p>take a snapshot of a metric query</p> +<h4>Parameters:</h4> +<dl> +<dt>query</dt> +<dd><p>the metric query to use for the snapshot</p> +</dd> +<dt>from</dt> +<dd><p>POSIX timestamp for the beginning of the query</p> +</dd> +<dt>to</dt> +<dd><p>POSIX timestamp for the end of the query</p> +</dd> +<dt>eventQuery</dt> +<dd><p><em>optional</em>, an event query to overlay event bands on the snapshot</p> +</dd> +<dt>callback</dt> +<dd><p>function(err, res)</p> +</dd> +</dl> +</div> +<div class="col-md-6"> +<pre><code class="lang-javascript">var dogapi = require("dogapi"); +var options = { + api_key: "api_key", + app_key: "app_key" +}; +dogapi.initialize(options); +var query = "system.cpu.idle{*}"; +var to = dogapi.now(); +var from = from - 3600; // an hour ago +dogapi.graph.snapshot(query, from, to, function(err, res){ + console.dir(res); +}); +</code></pre> +</div> +</div> +</section> <section id="host" class="col-sm-12"> <div class="row"> <h2 class="bg-primary" style="text-indent:1rem">host</h2></div> From 971a7ccb95f50ed1183e277ce4f4b684d453757e Mon Sep 17 00:00:00 2001 From: brettlangdon <brett@blangdon.com> Date: Sun, 22 Mar 2015 11:35:08 -0400 Subject: [PATCH 52/65] change 'then' command to 'past' and 'future' --- bin/dogapi | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/bin/dogapi b/bin/dogapi index 3489d1e..ad28513 100755 --- a/bin/dogapi +++ b/bin/dogapi @@ -15,7 +15,8 @@ var usage = [ " dogapi <command> --help", " dogapi --version", " dogapi now", - " dogapi then <seconds-ago>" + " dogapi past <seconds-ago>", + " dogapi future <seconds-ahead>" ]; var help = []; for(var key in dogapi){ @@ -47,8 +48,10 @@ if(command === "servicecheck"){ if(command === "now"){ console.log(dogapi.now()); -} else if(command === "then" && args._.length > 3){ +} else if(command === "past" && args._.length > 3){ console.log(dogapi.now() - parseInt(args._[args._.length - 1])); +} else if(command === "future" && args._.length > 3){ + console.log(dogapi.now() + parseInt(args._[args._.length - 1])); } else if(dogapi.hasOwnProperty(command)){ if(subcommand){ dogapi[command].handleCli(subcommand, args, function(err, res){ From bd3e2bb8f7b88290d38b0cabd2d13cedca0bc739 Mon Sep 17 00:00:00 2001 From: brettlangdon <brett@blangdon.com> Date: Sun, 22 Mar 2015 11:35:23 -0400 Subject: [PATCH 53/65] update unknown help text for metric command --- lib/api/metric.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/api/metric.js b/lib/api/metric.js index 5a9ba6e..71c9ec3 100644 --- a/lib/api/metric.js +++ b/lib/api/metric.js @@ -181,7 +181,7 @@ module.exports = { var q = args._[6]; query(from, to, q, callback); } else { - callback("unknown subcommand or arguments try `dogapi --help` for help", false); + callback("unknown subcommand or arguments try `dogapi metric --help` for help", false); } } }; From 0a18a915c539c28db3e6fdb7f0865b4e24898d13 Mon Sep 17 00:00:00 2001 From: brettlangdon <brett@blangdon.com> Date: Sun, 22 Mar 2015 11:43:08 -0400 Subject: [PATCH 54/65] add downtime api --- lib/api/downtime.js | 242 ++++++++++++++++++++++++++++++++++++++++++++ lib/api/index.js | 1 + 2 files changed, 243 insertions(+) create mode 100644 lib/api/downtime.js diff --git a/lib/api/downtime.js b/lib/api/downtime.js new file mode 100644 index 0000000..5f35a96 --- /dev/null +++ b/lib/api/downtime.js @@ -0,0 +1,242 @@ +var client = require("../client"); +var util = require("util"); + +/*section: downtime + *comment: schedule a new downtime + *params: + * scope: string scope that the downtime should apply to (e.g. "env:staging") + * properties: | + * optional, an object containing any of the following + * * start: POSIX timestamp for when the downtime should start + * * end: POSIX timestamp for when the downtime should end + * * message: a string message to accompany the downtime + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.downtime.create("env:staging", function(err, res){ + * console.dir(res); + * }); + * ``` + */ +function create(scope, properties, callback){ + if(arguments.length < 3 && typeof arguments[1] === "function"){ + callback = properties; + properties = {}; + } + + var params = { + body: { + scope: scope + } + }; + if(typeof properties === "object"){ + if(properties.start){ + params.body.start = parseInt(properties.start); + } + if(properties.end){ + params.body.end = parseInt(properties.end); + } + if(properties.message){ + params.body.message = properties.message; + } + } + client.request("POST", "/downtime", params, callback); +} + +/*section: downtime + *comment: update an existing downtime + *params: + * downtimeId: the id the downtie to update + * properties: | + * optional, an object containing any of the following + * * scope: the scope the downtime should be changed to (e.g. "env:staging") + * * start: POSIX timestamp for when the downtime should start + * * end: POSIX timestamp for when the downtime should end + * * message: a string message to accompany the downtime + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * var properties = { + * scope: "env:staging" + * }; + * dogapi.downtime.update(1234, properties, function(err, res){ + * console.dir(res); + * }); + * ``` + */ +function update(downtimeId, properties, callback){ + if(arguments.length < 3 && typeof arguments[1] === "function"){ + callback = properties; + properties = {}; + } + var params = { + body: {} + }; + if(typeof properties === "object"){ + if(properties.scope){ + params.body.scope = properties.scope; + } + if(properties.start){ + params.body.start = parseInt(properties.start); + } + if(properties.end){ + params.body.end = parseInt(properties.end); + } + if(properties.message){ + params.body.message = properties.message; + } + } + client.request("PUT", util.format("/downtime/%s", downtimeId), params, callback); +} + +/*section: downtime + *comment: delete a scheduled downtime + *params: + * downtimeId: the id of the downtime + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.downtime.remove(1234, function(err, res){ + * console.dir(res); + * }); + * ``` + */ +function remove(downtimeId, callback){ + client.request("DELETE", util.format("/downtime/%s", downtimeId), callback); +} + +/*section: downtime + *comment: get a scheduled downtimes details + *params: + * downtimeId: the id of the downtime + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.downtime.get(1234, function(err, res){ + * console.dir(res); + * }); + * ``` + */ +function get(downtimeId, callback){ + client.request("GET", util.format("/downtime/%s", downtimeId), callback); +} + +/*section: downtime + *comment: get all downtimes details + *params: + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.downtime.getAll(function(err, res){ + * console.dir(res); + * }); + * ``` + */ +function getAll(callback){ + client.request("GET", "/downtime", callback); +} + + +module.exports = { + create: create, + update: update, + remove: remove, + get: get, + getAll: getAll, + getUsage: function(){ + return [ + " dogapi downtime create <scope> [--start <start>] [--end <end>] [--message <message>]", + " dogapi downtime update <downtime-id> [--scope <scope>] [--start <start>] [--end <end>] [--message <message>]", + " dogapi downtime remove <downtime-id>", + " dogapi downtime get <downtime-id>", + " dogapi downtime getall", + ]; + }, + getHelp: function(){ + return [ + "Downtime:", + " Subcommands:", + " create <scope> create a new downtime with the provided scope (e.g. \"env:staging\")", + " update <downtime-id> update an existing downtime with the provided id", + " remove <downtime-id> remove the downtime with the provided id", + " get <downtime-id> get the details of the downtime with the provided id", + " getall get the details of all downtimes", + "", + " Options:", + " --start <start> POSIX timestamp for when the downtime should start", + " --end <end> POSIX timestamp for when the downtime should end", + " --message <message> a string message to accompany the downtime", + " --scope <scope> the scope of the downtime (e.g. \"env:staging\")" + ]; + }, + handleCli: function(subcommand, args, callback){ + if(subcommand === "get"){ + get(args._[4], callback); + } else if(subcommand === "getall"){ + getAll(callback); + } else if(subcommand === "remove"){ + remove(args._[4], callback); + } else if(subcommand === "create"){ + var scope = args._[4]; + var properties = {}; + if(args["start"]){ + properties.start = parseInt(args["start"]); + } + if(args["end"]){ + properties.end = parseInt(args["end"]); + } + if(args["message"]){ + properties.message = args["message"]; + } + create(scope, properties, callback); + } else if(subcommand === "update"){ + var downtimeId = args._[4]; + var properties = {}; + if(args["scope"]){ + properties.scope = args["scope"]; + } + if(args["start"]){ + properties.start = parseInt(args["start"]); + } + if(args["end"]){ + properties.end = parseInt(args["end"]); + } + if(args["message"]){ + properties.message = args["message"]; + } + update(downtimeId, properties, callback); + } else { + callback("unknown subcommand or arguments try `dogapi downtime --help` for help", false); + } + } +}; diff --git a/lib/api/index.js b/lib/api/index.js index e8edf67..964da18 100644 --- a/lib/api/index.js +++ b/lib/api/index.js @@ -1,4 +1,5 @@ var api = { + downtime: require("./downtime"), event: require("./event"), graph: require("./graph"), host: require("./host"), From 81cfd8d1642ed47564271f2445288e85424c90f3 Mon Sep 17 00:00:00 2001 From: brettlangdon <brett@blangdon.com> Date: Sun, 22 Mar 2015 16:24:42 -0400 Subject: [PATCH 55/65] add comment api --- lib/api/comment.js | 157 +++++++++++++++++++++++++++++++++++++++++++++ lib/api/index.js | 1 + 2 files changed, 158 insertions(+) create mode 100644 lib/api/comment.js diff --git a/lib/api/comment.js b/lib/api/comment.js new file mode 100644 index 0000000..309c857 --- /dev/null +++ b/lib/api/comment.js @@ -0,0 +1,157 @@ +var client = require("../client"); +var util = require("util"); + +/*section: comment + *comment: create a new comment + *params: + * message: the message of the comment + * properties: | + * optional, an object containing any of the following + * * handle: the handle to associate the comment with (e.g. "user@domain.com") + * * related_event_id: the event to associate the comment with + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.comment.create("a comment message", function(err, res){ + * console.dir(res); + * }); + * ``` + */ +function create(message, properties, callback){ + if(arguments.length < 3 && typeof arguments[1] === "function"){ + callback = properties; + properties = {}; + } + + var params = { + body: { + message: message + } + }; + + if(typeof properties === "object"){ + if(properties.handle){ + params.body.handle = properties.handle; + } + if(properties.related_event_id){ + params.body.related_event_id = properties.related_event_id; + } + } + + client.request("POST", "/comments", params, callback); +} + +/*section: comment + *comment: update an existing comment + *params: + * commentId: the id of the comment to update + * message: the message of the comment + * handle: optional, the handle to associate the comment with (e.g. "user@domain.com") + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.comment.update(1234, "new message", function(err, res){ + * console.dir(res); + * }); + * ``` + */ +function update(commentId, message, handle, callback){ + if(arguments.length < 4 && typeof arguments[2] === "function"){ + callback = handle; + handle = undefined; + } + + var params = { + body: { + message: message + } + }; + if(handle){ + params.body.handle = properties.handle; + } + + client.request("PUT", util.format("/comments/%s", commentId), params, callback); +} + +/*section: comment + *comment: remove a comment + *params: + * commentId: the id of the comment to remove + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.comment.remove(1234, function(err, res){ + * console.dir(res); + * }); + * ``` + */ +function remove(commentId, callback){ + client.request("DELETE" ,util.format("/comments/%s", commentId), callback); +} + + +module.exports = { + create: create, + update: update, + remove: remove, + getUsage: function(){ + return [ + " dogapi comment create <message> [--handle <handle>] [--event <event-id>]", + " dogapi comment update <comment-id> <message> [--handle <handle>]", + " dogapi comment remove <comment-id>" + ]; + }, + getHelp: function(){ + return [ + "Comment:", + " Subcommands:", + " create <message> add a new comment", + " update <comment-id> <message> update an existing comment", + " remove <comment-id> delete a comment", + "", + " Options:", + " --handle <handle> the handle to associate with the comment (e.g. \"user@domain.com\")", + " --event <event-id> related event id to associate the comment with" + ]; + }, + handleCli: function(subcommand, args, callback){ + if(subcommand === "create"){ + var message = args._[4]; + var properties = {}; + if(args["handle"]){ + properties.handle = args["handle"]; + } + if(args["event"]){ + properties.related_event_id = parseInt(args["event"]); + } + create(message, properties, callback); + } else if(subcommand === "update"){ + var commentId = args._[4]; + var message = args._[5]; + update(commentId, message, args["handle"], callback); + } else if(subcommand === "remove"){ + var commentId = args._[4]; + remove(commentId, callback); + } else { + callback("unknown subcommand or arguments try `dogapi comment --help` for help", false); + } + } +}; diff --git a/lib/api/index.js b/lib/api/index.js index 964da18..4aa6891 100644 --- a/lib/api/index.js +++ b/lib/api/index.js @@ -1,4 +1,5 @@ var api = { + comment: require("./comment"), downtime: require("./downtime"), event: require("./event"), graph: require("./graph"), From 336642f1f173e982eddb68bcbac6c923c2cf9d62 Mon Sep 17 00:00:00 2001 From: brettlangdon <brett@blangdon.com> Date: Sun, 22 Mar 2015 16:24:57 -0400 Subject: [PATCH 56/65] rebuild docs --- index.html | 270 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 270 insertions(+) diff --git a/index.html b/index.html index 75a0a2a..c7bdd33 100644 --- a/index.html +++ b/index.html @@ -16,6 +16,10 @@ document.addEventListener("DOMContentLoaded", function(){hljs.initHighlightingOn <div class="col-sm-12"> <h1>Node Dogapi</h1> <ul class="nav nav-pills"> +<li role=""><a href="#comment">comment</a> +</li> +<li role=""><a href="#downtime">downtime</a> +</li> <li role=""><a href="#event">event</a> </li> <li role=""><a href="#graph">graph</a> @@ -41,6 +45,272 @@ document.addEventListener("DOMContentLoaded", function(){hljs.initHighlightingOn </ul> </div> </div> +<section id="comment" class="col-sm-12"> +<div class="row"> +<h2 class="bg-primary" style="text-indent:1rem">comment</h2></div> +<ul class="nav nav-pills"> +<li role"presentation"><a href="#comment-create">create</a></li> +<li role"presentation"><a href="#comment-update">update</a></li> +<li role"presentation"><a href="#comment-remove">remove</a></li> +</ul> +<div class="function row" id="comment-create"> +<h3 class="bg-info" style="text-indent:.5rem;padding:.5rem;margin-top:.5rem">create(message, properties, callback)</h3> +<div class="col-md-6"> +<p>create a new comment</p> +<h4>Parameters:</h4> +<dl> +<dt>message</dt> +<dd><p>the message of the comment</p> +</dd> +<dt>properties</dt> +<dd><p><em>optional</em>, an object containing any of the following</p> +<ul> +<li>handle: the handle to associate the comment with (e.g. "user@domain.com")</li> +<li>related_event_id: the event to associate the comment with</li> +</ul> +</dd> +<dt>callback</dt> +<dd><p>function(err, res)</p> +</dd> +</dl> +</div> +<div class="col-md-6"> +<pre><code class="lang-javascript">var dogapi = require("dogapi"); +var options = { + api_key: "api_key", + app_key: "app_key" +}; +dogapi.initialize(options); +dogapi.comment.create("a comment message", function(err, res){ + console.dir(res); +}); +</code></pre> +</div> +</div> +<div class="function row" id="comment-update"> +<h3 class="bg-info" style="text-indent:.5rem;padding:.5rem;margin-top:.5rem">update(commentId, message, handle, callback)</h3> +<div class="col-md-6"> +<p>update an existing comment</p> +<h4>Parameters:</h4> +<dl> +<dt>commentId</dt> +<dd><p>the id of the comment to update</p> +</dd> +<dt>message</dt> +<dd><p>the message of the comment</p> +</dd> +<dt>handle</dt> +<dd><p><em>optional</em>, the handle to associate the comment with (e.g. "user@domain.com")</p> +</dd> +<dt>callback</dt> +<dd><p>function(err, res)</p> +</dd> +</dl> +</div> +<div class="col-md-6"> +<pre><code class="lang-javascript">var dogapi = require("dogapi"); +var options = { + api_key: "api_key", + app_key: "app_key" +}; +dogapi.initialize(options); +dogapi.comment.update(1234, "new message", function(err, res){ + console.dir(res); +}); +</code></pre> +</div> +</div> +<div class="function row" id="comment-remove"> +<h3 class="bg-info" style="text-indent:.5rem;padding:.5rem;margin-top:.5rem">remove(commentId, callback)</h3> +<div class="col-md-6"> +<p>remove a comment</p> +<h4>Parameters:</h4> +<dl> +<dt>commentId</dt> +<dd><p>the id of the comment to remove</p> +</dd> +<dt>callback</dt> +<dd><p>function(err, res)</p> +</dd> +</dl> +</div> +<div class="col-md-6"> +<pre><code class="lang-javascript">var dogapi = require("dogapi"); +var options = { + api_key: "api_key", + app_key: "app_key" +}; +dogapi.initialize(options); +dogapi.comment.remove(1234, function(err, res){ + console.dir(res); +}); +</code></pre> +</div> +</div> +</section> +<section id="downtime" class="col-sm-12"> +<div class="row"> +<h2 class="bg-primary" style="text-indent:1rem">downtime</h2></div> +<ul class="nav nav-pills"> +<li role"presentation"><a href="#downtime-create">create</a></li> +<li role"presentation"><a href="#downtime-update">update</a></li> +<li role"presentation"><a href="#downtime-remove">remove</a></li> +<li role"presentation"><a href="#downtime-get">get</a></li> +<li role"presentation"><a href="#downtime-getAll">getAll</a></li> +</ul> +<div class="function row" id="downtime-create"> +<h3 class="bg-info" style="text-indent:.5rem;padding:.5rem;margin-top:.5rem">create(scope, properties, callback)</h3> +<div class="col-md-6"> +<p>schedule a new downtime</p> +<h4>Parameters:</h4> +<dl> +<dt>scope</dt> +<dd><p>string scope that the downtime should apply to (e.g. "env:staging")</p> +</dd> +<dt>properties</dt> +<dd><p><em>optional</em>, an object containing any of the following</p> +<ul> +<li>start: POSIX timestamp for when the downtime should start</li> +<li>end: POSIX timestamp for when the downtime should end</li> +<li>message: a string message to accompany the downtime</li> +</ul> +</dd> +<dt>callback</dt> +<dd><p>function(err, res)</p> +</dd> +</dl> +</div> +<div class="col-md-6"> +<pre><code class="lang-javascript">var dogapi = require("dogapi"); +var options = { + api_key: "api_key", + app_key: "app_key" +}; +dogapi.initialize(options); +dogapi.downtime.create("env:staging", function(err, res){ + console.dir(res); +}); +</code></pre> +</div> +</div> +<div class="function row" id="downtime-update"> +<h3 class="bg-info" style="text-indent:.5rem;padding:.5rem;margin-top:.5rem">update(downtimeId, properties, callback)</h3> +<div class="col-md-6"> +<p>update an existing downtime</p> +<h4>Parameters:</h4> +<dl> +<dt>downtimeId</dt> +<dd><p>the id the downtie to update</p> +</dd> +<dt>properties</dt> +<dd><p><em>optional</em>, an object containing any of the following</p> +<ul> +<li>scope: the scope the downtime should be changed to (e.g. "env:staging")</li> +<li>start: POSIX timestamp for when the downtime should start</li> +<li>end: POSIX timestamp for when the downtime should end</li> +<li>message: a string message to accompany the downtime</li> +</ul> +</dd> +<dt>callback</dt> +<dd><p>function(err, res)</p> +</dd> +</dl> +</div> +<div class="col-md-6"> +<pre><code class="lang-javascript">var dogapi = require("dogapi"); +var options = { + api_key: "api_key", + app_key: "app_key" +}; +dogapi.initialize(options); +var properties = { + scope: "env:staging" +}; +dogapi.downtime.update(1234, properties, function(err, res){ + console.dir(res); +}); +</code></pre> +</div> +</div> +<div class="function row" id="downtime-remove"> +<h3 class="bg-info" style="text-indent:.5rem;padding:.5rem;margin-top:.5rem">remove(downtimeId, callback)</h3> +<div class="col-md-6"> +<p>delete a scheduled downtime</p> +<h4>Parameters:</h4> +<dl> +<dt>downtimeId</dt> +<dd><p>the id of the downtime</p> +</dd> +<dt>callback</dt> +<dd><p>function(err, res)</p> +</dd> +</dl> +</div> +<div class="col-md-6"> +<pre><code class="lang-javascript">var dogapi = require("dogapi"); +var options = { + api_key: "api_key", + app_key: "app_key" +}; +dogapi.initialize(options); +dogapi.downtime.remove(1234, function(err, res){ + console.dir(res); +}); +</code></pre> +</div> +</div> +<div class="function row" id="downtime-get"> +<h3 class="bg-info" style="text-indent:.5rem;padding:.5rem;margin-top:.5rem">get(downtimeId, callback)</h3> +<div class="col-md-6"> +<p>get a scheduled downtimes details</p> +<h4>Parameters:</h4> +<dl> +<dt>downtimeId</dt> +<dd><p>the id of the downtime</p> +</dd> +<dt>callback</dt> +<dd><p>function(err, res)</p> +</dd> +</dl> +</div> +<div class="col-md-6"> +<pre><code class="lang-javascript">var dogapi = require("dogapi"); +var options = { + api_key: "api_key", + app_key: "app_key" +}; +dogapi.initialize(options); +dogapi.downtime.get(1234, function(err, res){ + console.dir(res); +}); +</code></pre> +</div> +</div> +<div class="function row" id="downtime-getAll"> +<h3 class="bg-info" style="text-indent:.5rem;padding:.5rem;margin-top:.5rem">getAll(callback)</h3> +<div class="col-md-6"> +<p>get all downtimes details</p> +<h4>Parameters:</h4> +<dl> +<dt>callback</dt> +<dd><p>function(err, res)</p> +</dd> +</dl> +</div> +<div class="col-md-6"> +<pre><code class="lang-javascript">var dogapi = require("dogapi"); +var options = { + api_key: "api_key", + app_key: "app_key" +}; +dogapi.initialize(options); +dogapi.downtime.getAll(function(err, res){ + console.dir(res); +}); +</code></pre> +</div> +</div> +</section> <section id="event" class="col-sm-12"> <div class="row"> <h2 class="bg-primary" style="text-indent:1rem">event</h2></div> From f8541ecc18c579ebd4352f8ef937e18473594b81 Mon Sep 17 00:00:00 2001 From: brettlangdon <brett@blangdon.com> Date: Thu, 26 Mar 2015 07:09:09 -0400 Subject: [PATCH 57/65] replace all instances of 'optional' with '_optional_' --- docs/create.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/create.js b/docs/create.js index 43eb893..e633180 100644 --- a/docs/create.js +++ b/docs/create.js @@ -31,7 +31,7 @@ glob(match, function(er, files){ for(var key in comment.doc.params){ if(comment.doc.params.hasOwnProperty(key)){ - comment.doc.params[key] = comment.doc.params[key].replace("optional", "_optional_"); + comment.doc.params[key] = comment.doc.params[key].replace(/optional/g, "_optional_"); comment.doc.params[key] = marked(comment.doc.params[key]); } } From 28e5d00bd703b30ada9fb7586c03fae8f695f224 Mon Sep 17 00:00:00 2001 From: brettlangdon <brett@blangdon.com> Date: Thu, 26 Mar 2015 07:26:53 -0400 Subject: [PATCH 58/65] update readme --- README.md | 38 ++++++-------------------------------- 1 file changed, 6 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index b3cc743..f836f80 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,6 @@ Keys can be found at: https://app.datadoghq.com/account/settings#api The keys can be provided either as constructor parameters when creating an instance of `dogapi` as `api_key` and `app_key` or as the environment variables `DD_API_KEY` and `DD_APP_KEY`. -**Constructor parameters:** ```javascript var dogapi = require('dogapi'); @@ -37,43 +36,18 @@ var options = { app_key: 'YOUR_KEY_HERE', }; -var app = new dogapi(options); +dogapi.initialize(options); ``` -**Environment Variables:** -```bash -DD_API_KEY=YOUR_KEY_HERE DD_APP_KEY=YOUR_KEY_HERE node app.js -``` +## API Documentation -## Sample Usage: +https://brettlangdon.github.io/dogapi -**Example:** get all events since this time yesterday: -```javascript -var dogapi = require('dogapi'); -var options = { - api_key: 'YOUR_KEY_HERE', - app_key: 'YOUR KEY_HERE', -}; - -var api = new dogapi(options); - -var end = parseInt(new Date().getTime() / 1000); -var start = end - 86400; - -api.stream(start, end, function(error, result, status_code){ - if(error){ - console.log('Error: ', error); - console.log('Status Code: ', status_code); - return; - } - - result['events'].forEach(function(event){ - console.log(event['id'] + ': ' + event['title']); - }); -}); -``` +## TODO +- [ ] Add tests +- [ ] Add parameter validation (especially for dashboards) ## License From 706c72f11e81647f68a5fe2518bea648db0c746f Mon Sep 17 00:00:00 2001 From: brettlangdon <brett@blangdon.com> Date: Thu, 26 Mar 2015 07:27:08 -0400 Subject: [PATCH 59/65] add start to timeboard api --- lib/api/index.js | 1 + lib/api/timeboard.js | 245 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 246 insertions(+) create mode 100644 lib/api/timeboard.js diff --git a/lib/api/index.js b/lib/api/index.js index 4aa6891..41dec64 100644 --- a/lib/api/index.js +++ b/lib/api/index.js @@ -9,6 +9,7 @@ var api = { monitor: require("./monitor"), serviceCheck: require("./serviceCheck"), tag: require("./tag"), + timeboard: require("./timeboard"), user: require("./user"), }; diff --git a/lib/api/timeboard.js b/lib/api/timeboard.js new file mode 100644 index 0000000..1a28fe7 --- /dev/null +++ b/lib/api/timeboard.js @@ -0,0 +1,245 @@ +var client = require("../client"); +var util = require("util"); + +/*section: timeboard + *comment: add a new timeboard + *params: + * title: the title of the timeboard + * description: the description of the timeboard + * graphs: | + * an array of objects with the following keys + * * title: the name of the graph + * * definition: an object containing the graph definition, e.g. `{"requests": [{"q": "system.cpu.idle{*} by {host}"}` + * templateVariables: | + * optional, an array of objects with the following keys + * * name: the name of the variable + * * prefix: optional, the tag prefix for this variable + * * default: optional, the default value for this tag + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * var title = "Time Keeps on Slipping"; + * var description = "Into the Future"; + * var graphs = [ + * { + * definition: { + * events: [], + * requests: [ + * {q: "avg:system.mem.free{*}"} + * ], + * viz: "timeseries" + * }, + * title: "Average Memory Free" + * } + * ]; + * var templateVariables = [ + * { + * name: "host1", + * prefix: "host", + * default: "host:my-host" + * } + * ]; + * dogapi.timeboard.create( + * title, description, graphs, templateVariables, + * function(err, res){ + * console.dir(res); + * } + * ); + * ``` + */ +function create(title, description, graphs, templateVariables, callback){ + if(arguments.length < 5 && typeof arguments[3] === "function"){ + callback = templateVariables; + templateVariables = []; + } + + var params = { + body: { + title: title, + description: description, + graphs: graphs + } + }; + if(Array.isArray(templateVariables) && templateVariables.length){ + params.body.template_variables = templateVariables; + } + + client.request("POST", "/dash", params, callback); +} + +/*section: timeboard + *comment: update an existing timeboard + *params: + * dashId: the id of the timeboard to update + * title: the title of the timeboard + * description: the description of the timeboard + * graphs: | + * an array of objects with the following keys + * * title: the name of the graph + * * definition: an object containing the graph definition, e.g. `{"requests": [{"q": "system.cpu.idle{*} by {host}"}` + * templateVariables: | + * optional, an array of objects with the following keys + * * name: the name of the variable + * * prefix: optional, the tag prefix for this variable + * * default: optional, the default value for this tag + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * var title = "Time Keeps on Slipping"; + * var description = "Into the Future"; + * var graphs = [ + * { + * definition: { + * events: [], + * requests: [ + * {q: "avg:system.mem.free{*}"} + * ], + * viz: "timeseries" + * }, + * title: "Average Memory Free" + * } + * ]; + * var templateVariables = [ + * { + * name: "host1", + * prefix: "host", + * default: "host:my-host" + * } + * ]; + * dogapi.timeboard.update( + * 1234, title, description, graphs, templateVariables, + * function(err, res){ + * console.dir(res); + * } + * ); + * ``` + */ +function update(dashId, title, description, graphs, templateVariables, callback){ + if(arguments.length < 6 && typeof arguments[4] === "function"){ + callback = templateVariables; + templateVariables = []; + } + + var params = { + body: { + title: title, + description: description, + graphs: graphs + } + }; + if(Array.isArray(templateVariables) && templateVariables.length){ + params.body.template_variables = templateVariables; + } + + client.request("POST", util.format("/dash/%s", dashId), params, callback); +} + +/*section: timeboard + *comment: remove an existing timeboard + *params: + * dashId: the id of the timeboard to remove + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.timeboard.remove(1234, function(err, res){ + * console.dir(res); + * }); + * ``` + */ +function remove(dashId, callback){ + client.request("DELETE", util.format("/dash/%s", dashId), params, callback); +} + +/*section: timeboard + *comment: get all existing timeboards + *params: + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.timeboard.getAll(1234, function(err, res){ + * console.dir(res); + * }); + * ``` + */ +function getAll(callback){ + client.request("GET", "/dash", params, callback); +} + +/*section: timeboard + *comment: get an existing timeboard + *params: + * dashId: the id of the timeboard to get + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.timeboard.get(1234, function(err, res){ + * console.dir(res); + * }); + * ``` + */ +function get(dashId, callback){ + client.request("GET", util.format("/dash/%s", dashId), params, callback); +} + +module.exports = { + create: create, + update: update, + remove: remove, + getAll: getAll, + get: get, + getUsage: function(){ + return [ + " dogapi timeboard get <dash-id>", + " dogapi timeboard getall", + " dogapi timeboard remove <dash-id>", + " dogapi timeboard create <title> <description> <graphs> [--tmpvars <templateVariables>]", + " dogapi timeboard update <dash-id> <title> <description> <graphs> [--tmpvars <templateVariables>]", + ]; + }, + getHelp: function(){ + return [ + "Timeboard:", + " Subcommands:", + " get <dash-id>", + " getall", + " remove <dash-id>", + " create <title> <description> <graphs>", + " update <dash-id> <title> <description> <graphs>", + " Options:", + " --tmpvars <templateVariables>" + ]; + }, + handleCli: function(subcommand, args, callback){ + + } +}; From fb8802e57aad35deed45d9858cfafefd8b08bb8e Mon Sep 17 00:00:00 2001 From: brettlangdon <brett@blangdon.com> Date: Thu, 26 Mar 2015 07:27:19 -0400 Subject: [PATCH 60/65] update documentation --- index.html | 234 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 233 insertions(+), 1 deletion(-) diff --git a/index.html b/index.html index c7bdd33..2b0a6a5 100644 --- a/index.html +++ b/index.html @@ -36,6 +36,8 @@ document.addEventListener("DOMContentLoaded", function(){hljs.initHighlightingOn </li> <li role=""><a href="#tag">tag</a> </li> +<li role=""><a href="#timeboard">timeboard</a> +</li> <li role=""><a href="#user">user</a> </li> <li role=""><a href="#client">client</a> @@ -332,7 +334,7 @@ dogapi.downtime.getAll(function(err, res){ <dd><p>the body of the event</p> </dd> <dt>properties</dt> -<dd><p>an <em>optional</em> object continaing any of the following additional optional properties</p> +<dd><p>an <em>optional</em> object continaing any of the following additional <em>optional</em> properties</p> <ul> <li>date_happened: POSIX timestamp of when it happened</li> <li>priority: "normal" or "low" [defualt: "normal"]</li> @@ -1241,6 +1243,236 @@ dogapi.tag.remove("host.name", function(err, results){ </div> </div> </section> +<section id="timeboard" class="col-sm-12"> +<div class="row"> +<h2 class="bg-primary" style="text-indent:1rem">timeboard</h2></div> +<ul class="nav nav-pills"> +<li role"presentation"><a href="#timeboard-create">create</a></li> +<li role"presentation"><a href="#timeboard-update">update</a></li> +<li role"presentation"><a href="#timeboard-remove">remove</a></li> +<li role"presentation"><a href="#timeboard-getAll">getAll</a></li> +<li role"presentation"><a href="#timeboard-get">get</a></li> +</ul> +<div class="function row" id="timeboard-create"> +<h3 class="bg-info" style="text-indent:.5rem;padding:.5rem;margin-top:.5rem">create(title, description, graphs, templateVariables, callback)</h3> +<div class="col-md-6"> +<p>add a new timeboard</p> +<h4>Parameters:</h4> +<dl> +<dt>title</dt> +<dd><p>the title of the timeboard</p> +</dd> +<dt>description</dt> +<dd><p>the description of the timeboard</p> +</dd> +<dt>graphs</dt> +<dd><p>an array of objects with the following keys</p> +<ul> +<li>title: the name of the graph</li> +<li>definition: an object containing the graph definition, e.g. <code>{"requests": [{"q": "system.cpu.idle{*} by {host}"}</code></li> +</ul> +</dd> +<dt>templateVariables</dt> +<dd><p><em>optional</em>, an array of objects with the following keys</p> +<ul> +<li>name: the name of the variable</li> +<li>prefix: <em>optional</em>, the tag prefix for this variable</li> +<li>default: <em>optional</em>, the default value for this tag</li> +</ul> +</dd> +<dt>callback</dt> +<dd><p>function(err, res)</p> +</dd> +</dl> +</div> +<div class="col-md-6"> +<pre><code class="lang-javascript">var dogapi = require("dogapi"); +var options = { + api_key: "api_key", + app_key: "app_key" +}; +dogapi.initialize(options); +var title = "Time Keeps on Slipping"; +var description = "Into the Future"; +var graphs = [ + { + definition: { + events: [], + requests: [ + {q: "avg:system.mem.free{*}"} + ], + viz: "timeseries" + }, + title: "Average Memory Free" + } +]; +var templateVariables = [ + { + name: "host1", + prefix: "host", + default: "host:my-host" + } +]; +dogapi.timeboard.create( + title, description, graphs, templateVariables, + function(err, res){ + console.dir(res); + } +); +</code></pre> +</div> +</div> +<div class="function row" id="timeboard-update"> +<h3 class="bg-info" style="text-indent:.5rem;padding:.5rem;margin-top:.5rem">update(dashId, title, description, graphs, templateVariables, callback)</h3> +<div class="col-md-6"> +<p>update an existing timeboard</p> +<h4>Parameters:</h4> +<dl> +<dt>dashId</dt> +<dd><p>the id of the timeboard to update</p> +</dd> +<dt>title</dt> +<dd><p>the title of the timeboard</p> +</dd> +<dt>description</dt> +<dd><p>the description of the timeboard</p> +</dd> +<dt>graphs</dt> +<dd><p>an array of objects with the following keys</p> +<ul> +<li>title: the name of the graph</li> +<li>definition: an object containing the graph definition, e.g. <code>{"requests": [{"q": "system.cpu.idle{*} by {host}"}</code></li> +</ul> +</dd> +<dt>templateVariables</dt> +<dd><p><em>optional</em>, an array of objects with the following keys</p> +<ul> +<li>name: the name of the variable</li> +<li>prefix: <em>optional</em>, the tag prefix for this variable</li> +<li>default: <em>optional</em>, the default value for this tag</li> +</ul> +</dd> +<dt>callback</dt> +<dd><p>function(err, res)</p> +</dd> +</dl> +</div> +<div class="col-md-6"> +<pre><code class="lang-javascript">var dogapi = require("dogapi"); +var options = { + api_key: "api_key", + app_key: "app_key" +}; +dogapi.initialize(options); +var title = "Time Keeps on Slipping"; +var description = "Into the Future"; +var graphs = [ + { + definition: { + events: [], + requests: [ + {q: "avg:system.mem.free{*}"} + ], + viz: "timeseries" + }, + title: "Average Memory Free" + } +]; +var templateVariables = [ + { + name: "host1", + prefix: "host", + default: "host:my-host" + } +]; +dogapi.timeboard.update( + 1234, title, description, graphs, templateVariables, + function(err, res){ + console.dir(res); + } +); +</code></pre> +</div> +</div> +<div class="function row" id="timeboard-remove"> +<h3 class="bg-info" style="text-indent:.5rem;padding:.5rem;margin-top:.5rem">remove(dashId, callback)</h3> +<div class="col-md-6"> +<p>remove an existing timeboard</p> +<h4>Parameters:</h4> +<dl> +<dt>dashId</dt> +<dd><p>the id of the timeboard to remove</p> +</dd> +<dt>callback</dt> +<dd><p>function(err, res)</p> +</dd> +</dl> +</div> +<div class="col-md-6"> +<pre><code class="lang-javascript">var dogapi = require("dogapi"); +var options = { + api_key: "api_key", + app_key: "app_key" +}; +dogapi.initialize(options); +dogapi.timeboard.remove(1234, function(err, res){ + console.dir(res); +}); +</code></pre> +</div> +</div> +<div class="function row" id="timeboard-getAll"> +<h3 class="bg-info" style="text-indent:.5rem;padding:.5rem;margin-top:.5rem">getAll(callback)</h3> +<div class="col-md-6"> +<p>get all existing timeboards</p> +<h4>Parameters:</h4> +<dl> +<dt>callback</dt> +<dd><p>function(err, res)</p> +</dd> +</dl> +</div> +<div class="col-md-6"> +<pre><code class="lang-javascript">var dogapi = require("dogapi"); +var options = { + api_key: "api_key", + app_key: "app_key" +}; +dogapi.initialize(options); +dogapi.timeboard.getAll(1234, function(err, res){ + console.dir(res); +}); +</code></pre> +</div> +</div> +<div class="function row" id="timeboard-get"> +<h3 class="bg-info" style="text-indent:.5rem;padding:.5rem;margin-top:.5rem">get(dashId, callback)</h3> +<div class="col-md-6"> +<p>get an existing timeboard</p> +<h4>Parameters:</h4> +<dl> +<dt>dashId</dt> +<dd><p>the id of the timeboard to get</p> +</dd> +<dt>callback</dt> +<dd><p>function(err, res)</p> +</dd> +</dl> +</div> +<div class="col-md-6"> +<pre><code class="lang-javascript">var dogapi = require("dogapi"); +var options = { + api_key: "api_key", + app_key: "app_key" +}; +dogapi.initialize(options); +dogapi.timeboard.get(1234, function(err, res){ + console.dir(res); +}); +</code></pre> +</div> +</div> +</section> <section id="user" class="col-sm-12"> <div class="row"> <h2 class="bg-primary" style="text-indent:1rem">user</h2></div> From 62b7ae40dc78e9fe0a13425fb7ab7fedfefe6f43 Mon Sep 17 00:00:00 2001 From: brettlangdon <brett@blangdon.com> Date: Thu, 26 Mar 2015 07:28:43 -0400 Subject: [PATCH 61/65] fix documentation reference in readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f836f80..86ac385 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ dogapi.initialize(options); ## API Documentation -https://brettlangdon.github.io/dogapi +https://brettlangdon.github.io/node-dogapi ## TODO From ff505eb6dffdcba8567ed0d5f93ed2b05325928c Mon Sep 17 00:00:00 2001 From: brettlangdon <brett@blangdon.com> Date: Wed, 1 Apr 2015 09:09:01 -0400 Subject: [PATCH 62/65] add screenboard api --- lib/api/index.js | 1 + lib/api/screenboard.js | 239 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 240 insertions(+) create mode 100644 lib/api/screenboard.js diff --git a/lib/api/index.js b/lib/api/index.js index 41dec64..21bb552 100644 --- a/lib/api/index.js +++ b/lib/api/index.js @@ -7,6 +7,7 @@ var api = { infrastructure: require("./infrastructure"), metric: require("./metric"), monitor: require("./monitor"), + screenboard: require("./screenboard"), serviceCheck: require("./serviceCheck"), tag: require("./tag"), timeboard: require("./timeboard"), diff --git a/lib/api/screenboard.js b/lib/api/screenboard.js new file mode 100644 index 0000000..86797d7 --- /dev/null +++ b/lib/api/screenboard.js @@ -0,0 +1,239 @@ +var client = require("../client"); +var util = require("util"); + + +/*section: screenboard + *comment: create a new screenboard + *params: + * title: the name of the screenboard + * description: description of the screenboard + * graphs: | + * an array of objects which required the following keys + * * title: the name of the graph + * * widgets: an array of widgets, see http://docs.datadoghq.com/api/screenboards/ for more info + * options: | + * optional, a object which can contain any of the following keys + * * templateVariables: | + * an array of objects with the following keys + * * name: the name of the variable + * * prefix: optional, the tag prefix for this variable + * * default: optional, the default value for this tag + * * width: the width of the screenboard in pixels + * * height: the height of the screenboard in pixels + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * var title = "my screenboard"; + * var description = "it is super awesome"; + * var graphs = [ + * { + * type: "image", + * height: 20, + * width: 32, + * y: 7, + * x: 32, + * url: "https://path/to/image.jpg" + * } + * ]; + * var options = { + * templateVariables: [ + * { + * name: "host1", + * prefix: "host", + * "default": "host:my-host" + * } + * ] + * }; + * dogapi.screenboard.create( + * title, description, graphs, options, + * function(err, res){ + * console.dir(res); + * } + * ); + * ``` + */ +function create(title, description, graphs, options, callback){ + if(arguments.length < 5 && typeof arguments[3] === "function"){ + callback = options; + options = {}; + } + if(typeof options !== "object"){ + options = {}; + } + + var params = { + body: { + title: title, + description: description, + graphs: graphs + } + }; + + if(options.templateVariables){ + params.body.template_variables = options.templateVariables; + } + if(options.width){ + params.body.width = options.width; + } + if(options.height){ + params.body.height = options.height; + } + + client.request("POST", "/screen", params, callback); +} + +/*section: screenboard + *comment: delete an existing screenboard + *params: + * boardId: the id of the screenboard to delete + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.screenboard.remove(1234, function(err, res){ + * console.dir(res); + * }); + * ``` + */ +function remove(boardId, callback){ + client.request("DELETE", util.format("/screen/%s", boardId), callback); +} + +/*section: screenboard + *comment: get the info of a single existing screenboard + *params: + * boardId: the id of the screenboard to fetch + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.screenboard.get(1234, function(err, res){ + * console.dir(res); + * }); + * ``` + */ +function get(boardId, callback){ + client.request("GET", util.format("/screen/%s", boardId), callback); +} + +/*section: screenboard + *comment: get all existing screenboards + *params: + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.screenboard.getAll(function(err, res){ + * console.dir(res); + * }); + * ``` + */ +function getAll(callback){ + client.request("GET", "/screen", callback); +} + +/*section: screenboard + *comment: share an existing screenboard + *params: + * boardId: the id of the screenboard to share + * callback: function(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.screenboard.share(1234, function(err, res){ + * console.dir(res); + * }); + * ``` + */ +function share(boardId, callback){ + client.request("GET", util.format("/screen/share/%s", boardId), callback); +} + + +module.exports = { + create: create, + remove: remove, + get: get, + getAll: getAll, + share: share, + getUsage: function(){ + return [ + "dogapi screenboard create <title> <description> <graphs> [--tmpvars <templateVariables>] [--width <width>] [--height <height>]", + "dogapi screenboard remove <boardId>", + "dogapi screenboard get <boardId>", + "dogapi screenboard getall", + "dogapi screenboard share <boardId>" + ]; + }, + getHelp: function(){ + return [ + "Screenboard:", + " Subcommands:", + " create <title> <description> <graphs> create a new screenboard, <graphs> is a json of the graph definition", + " remove <boardId> remove an existing screenboard", + " get <boardId> get an existing screenboard", + " getall get all screenboards", + " share <boardId> get share info for an existing screenboard", + " Options:", + " --tmpvars <templateVariables> json representation of the template variable definition", + " --width <width> width of the screenboard in pixels", + " --height <height> height of the screenboard in pixels", + ]; + }, + handleCli: function(subcommand, args, callback){ + if(subcommand === "get"){ + get(args._[4], callback); + } else if(subcommand === "getall"){ + getAll(callback); + } else if(subcommand === "remove"){ + remove(args._[4], callback); + } else if(subcommand === "share"){ + share(args._[4], callback); + } else if(subcommand === "create"){ + var title = args._[4]; + var description = args._[5]; + var graphs = JSON.parse(args._[6]); + + var options = {}; + if(args["tmpvars"]){ + options.templateVariables = JSON.parse(args["tmpvars"]); + } + if(args["width"]){ + options.width = parseInt(args["width"]); + } + if(args["height"]){ + options.height = parseInt(args["height"]); + } + + create(title, description, graphs, options, callback); + } else { + callback("unknown subcommand or arguments try `dogapi screenboard --help` for help", false); + } + } +}; From 863875e85b1d25657c20e57f2a865eb5cbd805d3 Mon Sep 17 00:00:00 2001 From: brettlangdon <brett@blangdon.com> Date: Wed, 1 Apr 2015 09:09:21 -0400 Subject: [PATCH 63/65] quote 'default' since it is a 'reserved word' --- lib/api/timeboard.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/api/timeboard.js b/lib/api/timeboard.js index 1a28fe7..1ec50db 100644 --- a/lib/api/timeboard.js +++ b/lib/api/timeboard.js @@ -42,7 +42,7 @@ var util = require("util"); * { * name: "host1", * prefix: "host", - * default: "host:my-host" + * "default": "host:my-host" * } * ]; * dogapi.timeboard.create( From 8ce1fb2e95bf4154db9109069127b8179254c304 Mon Sep 17 00:00:00 2001 From: brettlangdon <brett@blangdon.com> Date: Wed, 1 Apr 2015 09:15:09 -0400 Subject: [PATCH 64/65] actually implement cli for timeboards --- lib/api/timeboard.js | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/lib/api/timeboard.js b/lib/api/timeboard.js index 1ec50db..c2c4e18 100644 --- a/lib/api/timeboard.js +++ b/lib/api/timeboard.js @@ -230,16 +230,45 @@ module.exports = { return [ "Timeboard:", " Subcommands:", - " get <dash-id>", - " getall", - " remove <dash-id>", - " create <title> <description> <graphs>", - " update <dash-id> <title> <description> <graphs>", + " get <dash-id> get an existing timeboard", + " getall get all existing timeboards", + " remove <dash-id> remove an existing timeboard", + " create <title> <description> <graphs> create a new timeboard, <graphs> is a json of the graphs definition", + " update <dash-id> <title> <description> <graphs> update an existing timeboard, <graphs> is a json of the graphs definition", " Options:", - " --tmpvars <templateVariables>" + " --tmpvars <templateVariables> a json representation of the template variables definition" ]; }, handleCli: function(subcommand, args, callback){ + if(subcommand === "get"){ + get(args._[4], callback); + } else if(subcommand === "getall"){ + getAll(callback); + } else if(subcommand === "remove"){ + remove(args._[4], callback); + } else if(subcommand === "create"){ + var title = args._[4]; + var description = args._[5]; + var graphs = JSON.parse(args._[6]); + var templateVariables = []; + if(args["tmpvars"]){ + templateVariables = JSON.parse(args["tmpvars"]); + } + create(title, description, graphs, templateVariables, callback); + } else if(subcommand === "update"){ + var dashId = parseInt(args._[4]); + var title = args._[5]; + var description = args._[6]; + var graphs = JSON.parse(args._[7]); + var templateVariables = []; + if(args["tmpvars"]){ + templateVariables = JSON.parse(args["tmpvars"]); + } + + update(dashId, title, description, graphs, templateVariables, callback); + } else { + callback("unknown subcommand or arguments try `dogapi timeboard --help` for help", false); + } } }; From a1787a40d1e7438f9e5e97a46ebf19cc9d6a5f7b Mon Sep 17 00:00:00 2001 From: brettlangdon <brett@blangdon.com> Date: Wed, 1 Apr 2015 09:15:19 -0400 Subject: [PATCH 65/65] update docs --- index.html | 195 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 194 insertions(+), 1 deletion(-) diff --git a/index.html b/index.html index 2b0a6a5..1846c11 100644 --- a/index.html +++ b/index.html @@ -32,6 +32,8 @@ document.addEventListener("DOMContentLoaded", function(){hljs.initHighlightingOn </li> <li role=""><a href="#monitor">monitor</a> </li> +<li role=""><a href="#screenboard">screenboard</a> +</li> <li role=""><a href="#serviceCheck">serviceCheck</a> </li> <li role=""><a href="#tag">tag</a> @@ -1024,6 +1026,197 @@ dogapi.monitor.unmuteAll(function(err, res){ </div> </div> </section> +<section id="screenboard" class="col-sm-12"> +<div class="row"> +<h2 class="bg-primary" style="text-indent:1rem">screenboard</h2></div> +<ul class="nav nav-pills"> +<li role"presentation"><a href="#screenboard-create">create</a></li> +<li role"presentation"><a href="#screenboard-remove">remove</a></li> +<li role"presentation"><a href="#screenboard-get">get</a></li> +<li role"presentation"><a href="#screenboard-getAll">getAll</a></li> +<li role"presentation"><a href="#screenboard-share">share</a></li> +</ul> +<div class="function row" id="screenboard-create"> +<h3 class="bg-info" style="text-indent:.5rem;padding:.5rem;margin-top:.5rem">create(title, description, graphs, options, callback)</h3> +<div class="col-md-6"> +<p>create a new screenboard</p> +<h4>Parameters:</h4> +<dl> +<dt>title</dt> +<dd><p>the name of the screenboard</p> +</dd> +<dt>description</dt> +<dd><p>description of the screenboard</p> +</dd> +<dt>graphs</dt> +<dd><p>an array of objects which required the following keys</p> +<ul> +<li>title: the name of the graph</li> +<li>widgets: an array of widgets, see <a href="http://docs.datadoghq.com/api/screenboards/">http://docs.datadoghq.com/api/screenboards/</a> for more info</li> +</ul> +</dd> +<dt>options</dt> +<dd><p><em>optional</em>, a object which can contain any of the following keys</p> +<ul> +<li>templateVariables: | +an array of objects with the following keys<ul> +<li>name: the name of the variable</li> +<li>prefix: <em>optional</em>, the tag prefix for this variable</li> +<li>default: <em>optional</em>, the default value for this tag</li> +</ul> +</li> +<li>width: the width of the screenboard in pixels</li> +<li>height: the height of the screenboard in pixels</li> +</ul> +</dd> +<dt>callback</dt> +<dd><p>function(err, res)</p> +</dd> +</dl> +</div> +<div class="col-md-6"> +<pre><code class="lang-javascript">var dogapi = require("dogapi"); +var options = { + api_key: "api_key", + app_key: "app_key" +}; +dogapi.initialize(options); +var title = "my screenboard"; +var description = "it is super awesome"; +var graphs = [ + { + type: "image", + height: 20, + width: 32, + y: 7, + x: 32, + url: "https://path/to/image.jpg" + } +]; +var options = { + templateVariables: [ + { + name: "host1", + prefix: "host", + "default": "host:my-host" + } + ] +}; +dogapi.screenboard.create( + title, description, graphs, options, + function(err, res){ + console.dir(res); + } +); +</code></pre> +</div> +</div> +<div class="function row" id="screenboard-remove"> +<h3 class="bg-info" style="text-indent:.5rem;padding:.5rem;margin-top:.5rem">remove(boardId, callback)</h3> +<div class="col-md-6"> +<p>delete an existing screenboard</p> +<h4>Parameters:</h4> +<dl> +<dt>boardId</dt> +<dd><p>the id of the screenboard to delete</p> +</dd> +<dt>callback</dt> +<dd><p>function(err, res)</p> +</dd> +</dl> +</div> +<div class="col-md-6"> +<pre><code class="lang-javascript">var dogapi = require("dogapi"); +var options = { + api_key: "api_key", + app_key: "app_key" +}; +dogapi.initialize(options); +dogapi.screenboard.remove(1234, function(err, res){ + console.dir(res); +}); +</code></pre> +</div> +</div> +<div class="function row" id="screenboard-get"> +<h3 class="bg-info" style="text-indent:.5rem;padding:.5rem;margin-top:.5rem">get(boardId, callback)</h3> +<div class="col-md-6"> +<p>get the info of a single existing screenboard</p> +<h4>Parameters:</h4> +<dl> +<dt>boardId</dt> +<dd><p>the id of the screenboard to fetch</p> +</dd> +<dt>callback</dt> +<dd><p>function(err, res)</p> +</dd> +</dl> +</div> +<div class="col-md-6"> +<pre><code class="lang-javascript">var dogapi = require("dogapi"); +var options = { + api_key: "api_key", + app_key: "app_key" +}; +dogapi.initialize(options); +dogapi.screenboard.get(1234, function(err, res){ + console.dir(res); +}); +</code></pre> +</div> +</div> +<div class="function row" id="screenboard-getAll"> +<h3 class="bg-info" style="text-indent:.5rem;padding:.5rem;margin-top:.5rem">getAll(callback)</h3> +<div class="col-md-6"> +<p>get all existing screenboards</p> +<h4>Parameters:</h4> +<dl> +<dt>callback</dt> +<dd><p>function(err, res)</p> +</dd> +</dl> +</div> +<div class="col-md-6"> +<pre><code class="lang-javascript">var dogapi = require("dogapi"); +var options = { + api_key: "api_key", + app_key: "app_key" +}; +dogapi.initialize(options); +dogapi.screenboard.getAll(function(err, res){ + console.dir(res); +}); +</code></pre> +</div> +</div> +<div class="function row" id="screenboard-share"> +<h3 class="bg-info" style="text-indent:.5rem;padding:.5rem;margin-top:.5rem">share(boardId, callback)</h3> +<div class="col-md-6"> +<p>share an existing screenboard</p> +<h4>Parameters:</h4> +<dl> +<dt>boardId</dt> +<dd><p>the id of the screenboard to share</p> +</dd> +<dt>callback</dt> +<dd><p>function(err, res)</p> +</dd> +</dl> +</div> +<div class="col-md-6"> +<pre><code class="lang-javascript">var dogapi = require("dogapi"); +var options = { + api_key: "api_key", + app_key: "app_key" +}; +dogapi.initialize(options); +dogapi.screenboard.share(1234, function(err, res){ + console.dir(res); +}); +</code></pre> +</div> +</div> +</section> <section id="serviceCheck" class="col-sm-12"> <div class="row"> <h2 class="bg-primary" style="text-indent:1rem">serviceCheck</h2></div> @@ -1310,7 +1503,7 @@ var templateVariables = [ { name: "host1", prefix: "host", - default: "host:my-host" + "default": "host:my-host" } ]; dogapi.timeboard.create(