/* Initialize plugin */ int in_lib_init(struct flb_input_instance *in, struct flb_config *config, void *data) { int ret; struct flb_in_lib_config *ctx; (void) data; /* Allocate space for the configuration */ ctx = flb_malloc(sizeof(struct flb_in_lib_config)); if (!ctx) { return -1; } ctx->buf_size = LIB_BUF_CHUNK; ctx->buf_data = flb_calloc(1, LIB_BUF_CHUNK); ctx->buf_len = 0; if (!ctx->buf_data) { flb_utils_error_c("Could not allocate initial buf memory buffer"); } ctx->msgp_size = LIB_BUF_CHUNK; ctx->msgp_data = flb_malloc(LIB_BUF_CHUNK); ctx->msgp_len = 0; /* Init communication channel */ flb_input_channel_init(in); ctx->fd = in->channel[0]; if (!ctx->msgp_data) { flb_utils_error_c("Could not allocate initial msgp memory buffer"); } /* Set the context */ flb_input_set_context(in, ctx); /* Collect upon data available on the standard input */ ret = flb_input_set_collector_event(in, in_lib_collect, ctx->fd, config); if (ret == -1) { flb_utils_error_c("Could not set collector for LIB input plugin"); } flb_pack_state_init(&ctx->state); return 0; }
/* Initialize plugin */ int in_mqtt_init(struct flb_input_instance *in, struct flb_config *config, void *data) { int ret; struct flb_in_mqtt_config *ctx; (void) data; /* Allocate space for the configuration */ ctx = mqtt_config_init(in); if (!ctx) { return -1; } ctx->msgp_len = 0; /* Set the context */ flb_input_set_context(in, ctx); /* Create TCP server */ ctx->server_fd = flb_net_server(ctx->tcp_port, ctx->listen); if (ctx->server_fd > 0) { flb_debug("[in_mqtt] binding %s:%s", ctx->listen, ctx->tcp_port); } else { flb_error("[in_mqtt] could not bind address %s:%s", ctx->listen, ctx->tcp_port); mqtt_config_free(ctx); return -1; } ctx->evl = config->evl; /* Collect upon data available on the standard input */ ret = flb_input_set_collector_event(in, in_mqtt_collect, ctx->server_fd, config); if (ret == -1) { flb_error("[in_mqtt] Could not set collector for MQTT input plugin"); mqtt_config_free(ctx); return -1; } return 0; }
/* Init kmsg input */ int in_kmsg_init(struct flb_config *config) { int fd; int ret; struct flb_in_kmsg_config *ctx; ctx = calloc(1, sizeof(struct flb_in_kmsg_config)); if (!ctx) { perror("calloc"); return -1; } /* open device */ fd = open(FLB_KMSG_DEV, O_RDONLY); if (fd == -1) { perror("open"); flb_utils_error_c("Could not open kernel log buffer on kmsg plugin"); } ctx->fd = fd; /* get the system boot time */ ret = boot_time(&ctx->boot_time); if (ret == -1) { flb_utils_error_c("Could not get system boot time for kmsg input plugin"); } /* set context */ ret = flb_input_set_context("kmsg", ctx, config); if (ret == -1) { flb_utils_error_c("Could not set configuration for kmsg input plugin"); } /* Set our collector based on a file descriptor event */ ret = flb_input_set_collector_event("kmsg", in_kmsg_collect, ctx->fd, config); if (ret == -1) { flb_utils_error_c("Could not set collector for kmsg input plugin"); } return 0; }
/* Init serial input */ int in_serial_init(struct flb_config *config) { int fd; int ret; struct flb_in_serial_config *ctx; ctx = calloc(1, sizeof(struct flb_in_serial_config)); if (!ctx) { perror("calloc"); return -1; } if (!config->file) { flb_utils_error_c("serial input plugin needs configuration file"); return -1; } serial_config_read(ctx, config->file); /* set context */ ret = flb_input_set_context("serial", ctx, config); if (ret == -1) { flb_utils_error_c("Could not set configuration for" "serial input plugin"); } if (ret == -1) { flb_utils_error_c("Could not set collector for serial input plugin"); } /* initialize MessagePack buffers */ msgpack_sbuffer_init(&ctx->mp_sbuf); msgpack_packer_init(&ctx->mp_pck, &ctx->mp_sbuf, msgpack_sbuffer_write); tcgetattr(fd, &ctx->tio_orig); memset(&ctx->tio, 0, sizeof(ctx->tio)); ctx->tio.c_cflag = ctx->tio.c_ispeed = ctx->tio.c_ospeed = atoi(ctx->bitrate); ctx->tio.c_cflag |= CRTSCTS | CS8 | CLOCAL | CREAD; ctx->tio.c_iflag = IGNPAR | IGNCR; ctx->tio.c_oflag = 0; ctx->tio.c_lflag = ICANON; /* open device */ fd = open(ctx->file, O_RDWR | O_NOCTTY | O_NONBLOCK); if (fd == -1) { perror("open"); flb_utils_error_c("Could not open serial port device"); } ctx->fd = fd; tcflush(fd, TCIFLUSH); tcsetattr(fd, TCSANOW, &ctx->tio); #if __linux__ /* Set our collector based on a file descriptor event */ ret = flb_input_set_collector_event("serial", in_serial_collect, ctx->fd, config); #else /* Set our collector based on a timer event */ ret = flb_input_set_collector_time("serial", in_serial_collect, IN_SERIAL_COLLECT_SEC, IN_SERIAL_COLLECT_NSEC, config); #endif return 0; }
/* Init serial input */ int in_serial_init(struct flb_config *config) { int fd; int ret; struct flb_in_serial_config *ctx; ctx = calloc(1, sizeof(struct flb_in_serial_config)); if (!ctx) { perror("calloc"); return -1; } if (!config->file) { flb_utils_error_c("serial input plugin needs configuration file"); return -1; } serial_config_read(ctx, config->file); /* set context */ ret = flb_input_set_context("serial", ctx, config); if (ret == -1) { flb_utils_error_c("Could not set configuration for" "serial input plugin"); } if (ret == -1) { flb_utils_error_c("Could not set collector for serial input plugin"); } /* initialize MessagePack buffers */ msgpack_sbuffer_init(&ctx->mp_sbuf); msgpack_packer_init(&ctx->mp_pck, &ctx->mp_sbuf, msgpack_sbuffer_write); tcgetattr(fd, &ctx->tio_orig); memset(&ctx->tio, 0, sizeof(ctx->tio)); switch (atoi(ctx->bitrate)) { case 1200: ctx->tio.c_cflag = B1200; break; case 2400: ctx->tio.c_cflag = B2400; break; case 4800: ctx->tio.c_cflag = B4800; break; case 9600: ctx->tio.c_cflag = B9600; break; case 19200: ctx->tio.c_cflag = B19200; break; case 38400: ctx->tio.c_cflag = B38400; break; #ifdef __LINUX__ case 576000: ctx->tio.c_cflag = B576000; break; case 115200: ctx->tio.c_cflag = B115200; break; #endif default: flb_utils_error_c("Invalid bitrate for serial plugin"); } ctx->tio.c_cflag |= CRTSCTS | CS8 | CLOCAL | CREAD; ctx->tio.c_iflag = IGNPAR | IGNCR; ctx->tio.c_oflag = 0; ctx->tio.c_lflag = ICANON; /* open device */ fd = open(ctx->file, O_RDWR | O_NOCTTY); if (fd == -1) { perror("open"); flb_utils_error_c("Could not open serial port device"); } ctx->fd = fd; tcflush(fd, TCIFLUSH); tcsetattr(fd, TCSANOW, &ctx->tio); /* Set our collector based on a file descriptor event */ ret = flb_input_set_collector_event("serial", in_serial_collect, ctx->fd, config); return 0; }
/* Init serial input */ int in_serial_init(struct flb_input_instance *in, struct flb_config *config, void *data) { int fd; int ret; int br; struct flb_in_serial_config *ctx; (void) data; ctx = calloc(1, sizeof(struct flb_in_serial_config)); if (!ctx) { perror("calloc"); return -1; } if (!serial_config_read(ctx, in)) { return -1; } /* set context */ flb_input_set_context(in, ctx); /* initialize MessagePack buffers */ msgpack_sbuffer_init(&ctx->mp_sbuf); msgpack_packer_init(&ctx->mp_pck, &ctx->mp_sbuf, msgpack_sbuffer_write); /* open device */ fd = open(ctx->file, O_RDWR | O_NOCTTY | O_NONBLOCK); if (fd == -1) { perror("open"); flb_utils_error_c("Could not open serial port device"); } ctx->fd = fd; /* Store original settings */ tcgetattr(fd, &ctx->tio_orig); /* Reset for new... */ memset(&ctx->tio, 0, sizeof(ctx->tio)); tcgetattr(fd, &ctx->tio); br = atoi(ctx->bitrate); cfsetospeed(&ctx->tio, (speed_t) flb_serial_speed(br)); cfsetispeed(&ctx->tio, (speed_t) flb_serial_speed(br)); /* Settings */ ctx->tio.c_cflag &= ~PARENB; /* 8N1 */ ctx->tio.c_cflag &= ~CSTOPB; ctx->tio.c_cflag &= ~CSIZE; ctx->tio.c_cflag |= CS8; ctx->tio.c_cflag &= ~CRTSCTS; /* No flow control */ ctx->tio.c_cc[VMIN] = ctx->min_bytes; /* Min number of bytes to read */ ctx->tio.c_cflag |= CREAD | CLOCAL; /* Enable READ & ign ctrl lines */ tcflush(fd, TCIFLUSH); tcsetattr(fd, TCSANOW, &ctx->tio); #if __linux__ /* Set our collector based on a file descriptor event */ ret = flb_input_set_collector_event(in, in_serial_collect, ctx->fd, config); #else /* Set our collector based on a timer event */ ret = flb_input_set_collector_time(in, in_serial_collect, IN_SERIAL_COLLECT_SEC, IN_SERIAL_COLLECT_NSEC, config); #endif return ret; }