Exemplo n.º 1
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;
}
Exemplo n.º 2
0
int in_lib_collect(struct flb_config *config, void *in_context)
{
    int n;
    int ret;
    int bytes;
    int out_size;
    int capacity;
    int size;
    char *ptr;
    char *pack;
    struct flb_in_lib_config *ctx = in_context;

    capacity = (ctx->buf_size - ctx->buf_len);

    /* Allocate memory as required (FIXME: this will be limited in later) */
    if (capacity == 0) {
        size = ctx->buf_size + LIB_BUF_CHUNK;
        ptr = flb_realloc(ctx->buf_data, size);
        if (!ptr) {
            perror("realloc");
            return -1;
        }
        ctx->buf_data = ptr;
        ctx->buf_size = size;
        capacity = LIB_BUF_CHUNK;
    }

    bytes = read(ctx->fd,
                 ctx->buf_data + ctx->buf_len,
                 capacity);
    flb_trace("in_lib read() = %i", bytes);
    if (bytes == -1) {
        perror("read");
        if (errno == -EPIPE) {
            return -1;
        }
        return 0;
    }
    ctx->buf_len += bytes;

    /* initially we should support json input */
    ret = flb_pack_json_state(ctx->buf_data, ctx->buf_len,
                              &pack, &out_size, &ctx->state);
    if (ret == FLB_ERR_JSON_PART) {
        flb_warn("lib data incomplete, waiting for more data...");
        return 0;
    }
    else if (ret == FLB_ERR_JSON_INVAL) {
        flb_warn("lib data invalid");
        flb_pack_state_reset(&ctx->state);
        flb_pack_state_init(&ctx->state);
        return -1;
    }
    ctx->buf_len = 0;

    capacity = (ctx->msgp_size - ctx->msgp_len);
    if (capacity < out_size) {
        n = ((out_size - capacity) / LIB_BUF_CHUNK) + 1;
        size = ctx->msgp_size + (LIB_BUF_CHUNK * n);
        ptr = flb_realloc(ctx->msgp_data, size);
        if (!ptr) {
            perror("realloc");
            flb_free(pack);
            flb_pack_state_reset(&ctx->state);
            flb_pack_state_init(&ctx->state);
            return -1;
        }
        ctx->msgp_data = ptr;
        ctx->msgp_size = size;
    }

    memcpy(ctx->msgp_data + ctx->msgp_len, pack, out_size);
    ctx->msgp_len += out_size;
    flb_free(pack);

    flb_pack_state_reset(&ctx->state);
    flb_pack_state_init(&ctx->state);

    return 0;
}