コード例 #1
0
ファイル: index.js プロジェクト: circonus-labs/nad
/**
 * sets the optional reverse connection config
 * @arg {Object} options from command line/environment/default
 * @returns {Undefined} nothing
 */
function setReverseOptions(options) {
    let reverse_enabled = typeof options.reverse === 'boolean' ? options.reverse : false;

    if (!reverse_enabled && process.env.NAD_REVERSE) {
        reverse_enabled = process.env.NAD_REVERSE.toLowerCase() === 'yes';
    }

    if (!reverse_enabled) {
        return;
    }

    instance.reverse.enabled = true;

    let cid = null;

    if (options.cid && options.cid !== '') {
        cid = options.cid;
    } else if (process.env.NAD_REVERSE_CID && process.env.NAD_REVERSE_CID !== '') {
        cid = process.env.NAD_REVERSE_CID;
    }

    if (cid !== null) {
        if ((/^[0-9]+$/).test(cid)) {
            instance.reverse.check_bundle_id = cid;
        } else {
            const msg = 'reverse - invalid check bundle id';

            log.fatal({ cid }, msg);
            configError(`${msg} '${cid}'`);
        }
    }

    if (instance.reverse.check_bundle_id === null) {
        try {
            const cosiCfg = path.resolve(path.join(COSI_DIR, 'registration', 'registration-check-system.json'));
            const cosi = require(cosiCfg);

            instance.reverse.check_bundle_id = cosi._cid.replace('/check_bundle/', '');
        } catch (err) {
            if (err.code !== 'MODULE_NOT_FOUND') {
                const msg = 'reverse - check bundle id';

                log.error({ err }, msg);
                configError(`${msg} ${err}`);
            }
            // otherwise ignore it and let nad try to find a check
        }
    }

    // NOTE: for target, environment takes higher precedence than command line as it is explicitly for reverse
    //       and there is no reverse-specific command line parameter (until self-configure is fully deprecated).
    //
    // use target specified via --target or default, override with env setting if available
    instance.reverse.target = instance.target;
    if (process.env.NAD_REVERSE_TARGET && process.env.NAD_REVERSE_TARGET !== '') {
        instance.reverse.target = process.env.NAD_REVERSE_TARGET;
    }
}
コード例 #2
0
ファイル: index.js プロジェクト: circonus-labs/nad
/**
 * sets the plugin directory
 * @arg {Object} options from command line/environment/default
 * @returns {Undefined} nothing
 */
function setStatsdOptions(options) {
    if (!options.statsd) {
        return;
    }

    if (process.env.NAD_STATSD && process.env.NAD_STATSD.toLowerCase() === 'no') {
        return;
    }

    const default_flush_interval = 10 * 1000; // 10 seconds

    instance.statsd = {
        app_name           : `${instance.app_name}-statsd`,
        app_version        : `${instance.app_version}`,
        enabled            : true,
        flush_interval     : 10000,
        group_check_id     : null,
        group_counter_op   : 'sum',
        group_gauge_op     : 'average',
        group_key          : null,
        group_set_op       : 'sum',
        host_category      : 'statsd',
        host_check_id      : null,
        host_key           : null,
        send_process_stats : true,
        servers            : [],
        start_time         : Date.now()
    };

    instance.statsd.logger = instance.logger.child({ module: `${instance.statsd.app_name}` });

    if (instance.reverse.enabled) {
        // add the reverse check bundle id, if provided
        // if it wasn't provided, the reverse module will add it if it finds a check
        if (instance.reverse.check_bundle_id) {
            instance.statsd.host_check_id = `${instance.reverse.check_bundle_id}`;
        }
    }

    loadStatsdConfigFile(options, default_flush_interval);

    if (typeof instance.statsd.group_key === 'undefined') {
        instance.statsd.group_key = null;
    }
    if (instance.statsd.group_key && instance.statsd.group_key === '') {
        instance.statsd.group_key = null;
    }

    if (typeof instance.statsd.host_key === 'undefined') {
        instance.statsd.host_key = null;
    }
    if (instance.statsd.host_key && instance.statsd.host_key === '') {
        instance.statsd.host_key = null;
    }

    if (instance.statsd.group_check_id !== null) {
        if (!(/^[0-9]+$/).test(`${instance.statsd.group_check_id}`)) {
            log.fatal({ cid: instance.statsd.group_check_id }, 'invalid group check bundle id');
            configError('invalid group check bundle in statsd config', instance.statsd.group_check_id);
        }
    }

    if (instance.statsd.group_check_id === null) {
        try {
            // add cosi group check information, if it is available
            const cfgFile = fs.realpathSync(path.join(COSI_DIR, 'registration', 'registration-check-group.json'));
            const checkCfg = require(cfgFile);

            instance.statsd.group_check_id = checkCfg._cid.replace('/check_bundle/', '');
        } catch (err) {
            log.debug({
                err   : err.message,
                group : 'disabled',
                host  : 'metrics to NAD.push_receiver'
            }, 'no cosi group check found');
        }
    }

    if (instance.statsd.host_check_id !== null) {
        if (!(/^[0-9]+$/).test(`${instance.statsd.host_check_id}`)) {
            log.fatal({ cid: instance.statsd.host_check_id }, 'invalid host check bundle id');
            configError('invalid host check bundle in statsd config', instance.statsd.host_check_id);
        }
    }

    if (instance.statsd.host_check_id === null) {
        try {
            // add cosi host check information, if it is available
            const cfgFile = fs.realpathSync(path.join(COSI_DIR, 'registration', 'registration-check-system.json'));
            const checkCfg = require(cfgFile);

            instance.statsd.host_check_id = checkCfg._cid.replace('/check_bundle/', '');
        } catch (err) {
            log.debug({ err: err.message }, 'disabling check management');
            // ignore, just send metrics to nad and ensure check management is OFF
            instance.statsd.manage_check_metrics = false;
        }
    }

    if (instance.statsd.group_check_id !== null || instance.statsd.host_check_id !== null) {
        if (instance.api.key === null) {
            const msg = 'A valid API Token key is required to use use explicit host or group checks with nad-statsd';

            log.fatal(msg);
            configError(msg);
        }
    }

    // the local push_receiver will be used for:
    //      all metrics prefixed with hostKey
    //      all metrics if no statsd-config supplied
    // push_receiver metric category name
    const pr_mc = instance.statsd.host_category;

    instance.statsd.push_receiver_url = `http://${instance.listen[0].address || '127.0.0.1'}:${instance.listen[0].port}/write/${pr_mc}`;

    if (instance.statsd.flush_interval < default_flush_interval) {
        log.warn({ flush_interval: instance.statsd.flush_interval }, `invalid flush interval, using ${default_flush_interval}`);
        instance.statsd.flush_interval = default_flush_interval;
    }
}