Пример #1
0
int cb_td_flush(void *data, size_t bytes, void *out_context,
                struct flb_config *config)
{
    int n;
    char buf[1024];
    ssize_t w_bytes;
    size_t out_len;
    char *request;
    struct flb_out_td_config *ctx = out_context;

    request = td_http_request(data, bytes, &out_len, ctx, config);
    w_bytes = write(ctx->fd, request, out_len);
    if (w_bytes < 0) {
        perror("write");
        /* FIXME: handle connection timeout */
        if (errno == EBADF) {
            close(ctx->fd);
            ctx->fd = flb_net_tcp_connect(out_td_plugin.host,
                                          out_td_plugin.port);
        }
    }
    free(request);

    n = read(ctx->fd, buf, 4096);
    buf[n] = '\0';

    flb_debug("[TD] API server response:\n%s", buf);
    return w_bytes;
}
Пример #2
0
/* Creates a new upstream context */
struct flb_io_upstream *flb_io_upstream_new(char *host, int port, int flags)
{
    int fd;
    struct flb_io_upstream *u;

    u = malloc(sizeof(struct flb_io_upstream));
    if (!u) {
        perror("malloc");
        return NULL;
    }


    /* Upon upstream creation, we always try to perform a connection */
    flb_debug("[upstream] connecting to %s:%i", host, port);
    fd = flb_net_tcp_connect(host, port);
    if (fd == -1) {
        flb_warn("[upstream] could not connect to %s:%i", host, port);
    }
    else {
        flb_debug("[upstream] connected!");
    }

    u->fd   = fd;
    u->tcp_host = strdup(host);
    u->tcp_port = port;
    u->flags    = flags;

    return u;
}
Пример #3
0
int cb_td_pre_run(void *out_context, struct flb_config *config)
{
    int fd;
    struct flb_out_td_config *ctx = out_context;

    fd = flb_net_tcp_connect(out_td_plugin.host,
                             out_td_plugin.port);
    if (fd <= 0) {
        return -1;
    }

    ctx->fd = fd;
    return 0;
}
Пример #4
0
int cb_fluentd_flush(void *data, size_t bytes, void *out_context,
                     struct flb_config *config)
{
    int fd;
    int ret = -1;
    int maps = 0;
    size_t total;
    char *buf = NULL;
    msgpack_packer   mp_pck;
    msgpack_sbuffer  mp_sbuf;
    msgpack_unpacked mp_umsg;
    size_t mp_upos = 0;
    (void) out_context;
    (void) config;

    /*
     * The incoming data comes in Fluent Bit format an array of objects, as we
     * aim to send this information to Fluentd through it in_forward plugin, we
     * need to transform the data. The Fluentd in_forward plugin allows one
     * of the following formats:
     *
     *   1. [tag, time, record]
     *
     *    or
     *
     *   2. [tag, [[time,record], [time,record], ...]]
     *
     *   we use the format #2
     */

    /* Initialize packager */
    msgpack_sbuffer_init(&mp_sbuf);
    msgpack_packer_init(&mp_pck, &mp_sbuf, msgpack_sbuffer_write);

    /*
     * Count the number of map entries
     *
     * FIXME: Fluent Bit should expose the number of maps into the
     * data, so we avoid this silly counting.
     */
    msgpack_unpacked_init(&mp_umsg);
    while (msgpack_unpack_next(&mp_umsg, data, bytes, &mp_upos)) {
        maps++;
    }
    msgpack_unpacked_destroy(&mp_umsg);

    /* Output: root array */
    msgpack_pack_array(&mp_pck, 2);
    msgpack_pack_bin(&mp_pck, sizeof(FLB_CONFIG_DEFAULT_TAG) - 1);
    msgpack_pack_bin_body(&mp_pck,
                          FLB_CONFIG_DEFAULT_TAG,
                          sizeof(FLB_CONFIG_DEFAULT_TAG) - 1);
    msgpack_pack_array(&mp_pck, maps);

    /* Allocate a new buffer to merge data */
    total = bytes + mp_sbuf.size;
    buf = malloc(total);
    if (!buf) {
        perror("malloc");
        return -1;
    }

    memcpy(buf, mp_sbuf.data, mp_sbuf.size);
    memcpy(buf + mp_sbuf.size, data, bytes);
    msgpack_sbuffer_destroy(&mp_sbuf);

    fd = flb_net_tcp_connect(out_fluentd_plugin.host,
                             out_fluentd_plugin.port);
    if (fd <= 0) {
        free(buf);
        return -1;
    }


    /* FIXME: plain TCP write */
    ret = write(fd, buf, total);
    close(fd);
    free(buf);

    return ret;
}