UNPKG

nsolid-graphite

Version:

a daemon that sends N|Solid metrics to graphite

106 lines (97 loc) 2.93 kB
'use strict' module.exports = getParams const stringify = require('querystring').stringify /* id: agent id app: app name; eg, NSOLID_APPNAME tag: a comma-separated tag from NSOLID_TAGS, may use multiple times hostname: hostname duration: numeric value specified in seconds name: custom command name; a string data: custom command data; a string field: metrics / info field to return; may use multiple times asset: the id of the asset to return; eg, a snapshot start: absolute or relative date (see below) end: absolute or relative date (see below) g(ranularity): time unit of granularity; number with suffix s for seconds, m for minutes, h for hours, d for days; defaults to seconds agg: function to aggregate data - raw, min, max, mean, median */ /* const allParams = new Set([ 'id', 'app', 'tag', 'hostname', 'duration', 'name', 'data', 'field', 'asset', 'start', 'end', 'g', 'agg' ]) */ const idOnly = new Set(['id']) const appOnly = new Set(['app']) const filters = new Set(['id', 'app', 'tag', 'hostname']) const range = new Set(['start', 'end']) const aggregation = new Set(['agg', 'g']) const custom = new Set(['name', 'data']) const profile = new Set(['duration']) const asset = new Set(['asset']) const fields = new Set(['field']) const interval = new Set(['interval']) const argument = new Set(['data']) const assetType = new Set(['type']) const license = new Set(['license']) const licenseInfo = new Set(['firstName', 'lastName', 'email', 'company', 'optOut']) // this would be easier with the spread operator // return new Set([...a, ...b]) function union (sets) { const joined = new Set() for (let subset of sets) { for (let element of subset) { joined.add(element) } } return joined } const endpoints = { 'ping': filters, 'metrics': union([filters, fields, range, aggregation]), 'info': union([filters, fields, range]), 'packages': union([filters]), 'startup-times': union([filters]), 'async-activity': union([filters]), 'list': new Set(), 'custom': union([filters, custom]), 'snapshot': union([idOnly, argument]), 'profile': union([idOnly, profile]), 'asset': union([asset]), 'assets': union([filters, assetType, range]), 'applications': interval, 'application': union([appOnly, interval]), 'thresholds': interval, 'vulnerabilities': interval, 'settings': new Set(), 'license-info': union([license, licenseInfo]), 'check-license': license, 'generate-keypair': new Set() } function getParams (command, options) { if (command == null || options == null) { return '' } const paramObj = {} Object.keys(options).forEach((key) => { if (endpoints[command] && endpoints[command].has(key)) { // TODO: worth validating/restricting: // * field name length? (for safety) // * how many repeats? (can there only be one id?) paramObj[key] = options[key] } }) return stringify(paramObj) }