Esempio n. 1
0
static int kafka_write(const data_set_t *ds, /* {{{ */
	      const value_list_t *vl,
	      user_data_t *ud)
{
	int			 status = 0;
    u_int32_t    key;
    char         buffer[8192];
    size_t bfree = sizeof(buffer);
    size_t bfill = 0;
    size_t blen = 0;
	struct kafka_topic_context	*ctx = ud->data;

    if ((ds == NULL) || (vl == NULL) || (ctx == NULL))
        return EINVAL;

    bzero(buffer, sizeof(buffer));

    switch (ctx->format) {
    case KAFKA_FORMAT_COMMAND:
        status = create_putval(buffer, sizeof(buffer), ds, vl);
        if (status != 0) {
            ERROR("write_kafka plugin: create_putval failed with status %i.",
                  status);
            return status;
        }
        blen = strlen(buffer);
        break;
    case KAFKA_FORMAT_JSON:

        format_json_initialize(buffer, &bfill, &bfree);
        format_json_value_list(buffer, &bfill, &bfree, ds, vl,
                               ctx->store_rates);
        format_json_finalize(buffer, &bfill, &bfree);
        blen = strlen(buffer);
        break;
    case KAFKA_FORMAT_GRAPHITE:
        status = format_graphite(buffer, sizeof(buffer), ds, vl,
                                 ctx->prefix, ctx->postfix, ctx->escape_char,
                                 ctx->graphite_flags);
        if (status != 0) {
            ERROR("write_kafka plugin: format_graphite failed with status %i.",
                  status);
            return status;
        }
        blen = strlen(buffer);
        break;
    default:
        ERROR("write_kafka plugin: invalid format %i.", ctx->format);
        return -1;
    }

    /*
     * We partition our stream by metric name
     */
    if (ctx->has_key)
        key = ctx->key;
    else
        key = rand();

    rd_kafka_produce(ctx->topic, RD_KAFKA_PARTITION_UA,
                     RD_KAFKA_MSG_F_COPY, buffer, blen,
                     &key, sizeof(key), NULL);

	return status;
} /* }}} int kafka_write */
Esempio n. 2
0
static int camqp_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
        user_data_t *user_data)
{
    camqp_config_t *conf = user_data->data;
    char routing_key[6 * DATA_MAX_NAME_LEN];
    char buffer[4096];
    int status;

    if ((ds == NULL) || (vl == NULL) || (conf == NULL))
        return (EINVAL);

    memset (buffer, 0, sizeof (buffer));

    if (conf->routing_key != NULL)
    {
        sstrncpy (routing_key, conf->routing_key, sizeof (routing_key));
    }
    else
    {
        size_t i;
        ssnprintf (routing_key, sizeof (routing_key), "collectd/%s/%s/%s/%s/%s",
                vl->host,
                vl->plugin, vl->plugin_instance,
                vl->type, vl->type_instance);

        /* Switch slashes (the only character forbidden by collectd) and dots
         * (the separation character used by AMQP). */
        for (i = 0; routing_key[i] != 0; i++)
        {
            if (routing_key[i] == '.')
                routing_key[i] = '/';
            else if (routing_key[i] == '/')
                routing_key[i] = '.';
        }
    }

    if (conf->format == CAMQP_FORMAT_COMMAND)
    {
        status = create_putval (buffer, sizeof (buffer), ds, vl);
        if (status != 0)
        {
            ERROR ("amqp plugin: create_putval failed with status %i.",
                    status);
            return (status);
        }
    }
    else if (conf->format == CAMQP_FORMAT_JSON)
    {
        size_t bfree = sizeof (buffer);
        size_t bfill = 0;

        format_json_initialize (buffer, &bfill, &bfree);
        format_json_value_list (buffer, &bfill, &bfree, ds, vl, conf->store_rates);
        format_json_finalize (buffer, &bfill, &bfree);
    }
    else if (conf->format == CAMQP_FORMAT_GRAPHITE)
    {
        status = format_graphite (buffer, sizeof (buffer), ds, vl,
                    conf->prefix, conf->postfix, conf->escape_char);
        if (status != 0)
        {
            ERROR ("amqp plugin: format_graphite failed with status %i.",
                    status);
            return (status);
        }
    }
    else
    {
        ERROR ("amqp plugin: Invalid format (%i).", conf->format);
        return (-1);
    }

    pthread_mutex_lock (&conf->lock);
    status = camqp_write_locked (conf, buffer, routing_key);
    pthread_mutex_unlock (&conf->lock);

    return (status);
} /* }}} int camqp_write */
Esempio n. 3
0
File: amqp.c Progetto: brd/collectd
/* XXX: You must hold "conf->lock" when calling this function! */
static int camqp_write_locked (camqp_config_t *conf, /* {{{ */
        const char *buffer, const char *routing_key)
{
    int status;

    status = camqp_connect (conf);
    if (status != 0)
        return (status);

    amqp_basic_properties_t props = {
        ._flags = AMQP_BASIC_CONTENT_TYPE_FLAG
            | AMQP_BASIC_DELIVERY_MODE_FLAG
            | AMQP_BASIC_APP_ID_FLAG,
        .delivery_mode = conf->delivery_mode,
        .app_id = amqp_cstring_bytes("collectd")
    };

    if (conf->format == CAMQP_FORMAT_COMMAND)
        props.content_type = amqp_cstring_bytes("text/collectd");
    else if (conf->format == CAMQP_FORMAT_JSON)
        props.content_type = amqp_cstring_bytes("application/json");
    else if (conf->format == CAMQP_FORMAT_GRAPHITE)
        props.content_type = amqp_cstring_bytes("text/graphite");
    else
        assert (23 == 42);

    status = amqp_basic_publish(conf->connection,
                /* channel = */ 1,
                amqp_cstring_bytes(CONF(conf, exchange)),
                amqp_cstring_bytes (routing_key),
                /* mandatory = */ 0,
                /* immediate = */ 0,
                &props,
                amqp_cstring_bytes(buffer));
    if (status != 0)
    {
        ERROR ("amqp plugin: amqp_basic_publish failed with status %i.",
                status);
        camqp_close_connection (conf);
    }

    return (status);
} /* }}} int camqp_write_locked */

static int camqp_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
        user_data_t *user_data)
{
    camqp_config_t *conf = user_data->data;
    char routing_key[6 * DATA_MAX_NAME_LEN];
    char buffer[8192];
    int status;

    if ((ds == NULL) || (vl == NULL) || (conf == NULL))
        return (EINVAL);

    if (conf->routing_key != NULL)
    {
        sstrncpy (routing_key, conf->routing_key, sizeof (routing_key));
    }
    else
    {
        ssnprintf (routing_key, sizeof (routing_key), "collectd/%s/%s/%s/%s/%s",
                vl->host,
                vl->plugin, vl->plugin_instance,
                vl->type, vl->type_instance);

        /* Switch slashes (the only character forbidden by collectd) and dots
         * (the separation character used by AMQP). */
        for (size_t i = 0; routing_key[i] != 0; i++)
        {
            if (routing_key[i] == '.')
                routing_key[i] = '/';
            else if (routing_key[i] == '/')
                routing_key[i] = '.';
        }
    }

    if (conf->format == CAMQP_FORMAT_COMMAND)
    {
        status = create_putval (buffer, sizeof (buffer), ds, vl);
        if (status != 0)
        {
            ERROR ("amqp plugin: create_putval failed with status %i.",
                    status);
            return (status);
        }
    }
    else if (conf->format == CAMQP_FORMAT_JSON)
    {
        size_t bfree = sizeof (buffer);
        size_t bfill = 0;

        format_json_initialize (buffer, &bfill, &bfree);
        format_json_value_list (buffer, &bfill, &bfree, ds, vl, conf->store_rates);
        format_json_finalize (buffer, &bfill, &bfree);
    }
    else if (conf->format == CAMQP_FORMAT_GRAPHITE)
    {
        status = format_graphite (buffer, sizeof (buffer), ds, vl,
                    conf->prefix, conf->postfix, conf->escape_char,
                    conf->graphite_flags);
        if (status != 0)
        {
            ERROR ("amqp plugin: format_graphite failed with status %i.",
                    status);
            return (status);
        }
    }
    else
    {
        ERROR ("amqp plugin: Invalid format (%i).", conf->format);
        return (-1);
    }

    pthread_mutex_lock (&conf->lock);
    status = camqp_write_locked (conf, buffer, routing_key);
    pthread_mutex_unlock (&conf->lock);

    return (status);
} /* }}} int camqp_write */