struct flb_in_serial_config *serial_config_read(struct flb_in_serial_config *config, struct mk_rconf *conf) { char *file; char *bitrate; struct mk_rconf_section *section; section = mk_rconf_section_get(conf, "serial"); if (!section) { return NULL; } /* Validate serial section keys */ file = mk_rconf_section_get_key(section, "file", MK_RCONF_STR); bitrate = mk_rconf_section_get_key(section, "bitrate", MK_RCONF_STR); if (!file) { flb_utils_error_c("[serial] error reading filename from " "configuration"); } if (!bitrate) { flb_utils_error_c("[serial] error reading bitrate from " "configuration"); } config->fd = -1; config->file = file; config->bitrate = bitrate; flb_debug("Serial / file='%s' bitrate='%s'", config->file, config->bitrate); return config; }
int in_mem_init(struct flb_config *config) { int ret; struct flb_in_mem_config *ctx; ctx = malloc(sizeof(struct flb_in_mem_config)); if (!ctx) { return -1; } ctx->idx = 0; msgpack_sbuffer_init(&ctx->sbuf); msgpack_packer_init(&ctx->pckr, &ctx->sbuf, msgpack_sbuffer_write); ret = flb_input_set_context("mem", ctx, config); if (ret == -1) { flb_utils_error_c("Could not set configuration for " "memory input plugin"); } ret = flb_input_set_collector_time("mem", in_mem_collect, IN_MEM_COLLECT_SEC, IN_MEM_COLLECT_NSEC, config); if (ret == -1) { flb_utils_error_c("Could not set collector for memory input plugin"); } return 0; }
/* 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; }
int in_mem_pre_run(void *in_context, struct flb_config *config) { struct flb_in_mem_config *ctx = in_context; ctx->tag_len = snprintf(ctx->tag, sizeof(ctx->tag), "%s.mem", config->tag); if (ctx->tag_len == -1) { flb_utils_error_c("Could not set custom tag on memory input plugin"); } 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; }
struct flb_out_td_config *td_config_init(struct mk_rconf *conf) { char *api; char *db_name; char *db_table; struct mk_rconf_section *section; struct flb_out_td_config *config; section = mk_rconf_section_get(conf, "TD"); if (!section) { return NULL; } /* Validate TD section keys */ api = mk_rconf_section_get_key(section, "API", MK_RCONF_STR); db_name = mk_rconf_section_get_key(section, "Database", MK_RCONF_STR); db_table = mk_rconf_section_get_key(section, "Table", MK_RCONF_STR); if (!api) { flb_utils_error_c("[TD] error reading API key value"); } if (!db_name) { flb_utils_error_c("[TD] error reading Database name"); } if (!db_table) { flb_utils_error_c("[TD] error reading Table name"); } config = malloc(sizeof(struct flb_out_td_config)); config->fd = -1; config->api = api; config->db_name = db_name; config->db_table = db_table; flb_debug("TreasureData / database='%s' table='%s'", config->db_name, config->db_table); return config; }
int cb_td_init(struct flb_config *config) { int ret; struct flb_out_td_config *ctx; if (!config->file) { flb_utils_error_c("TD output requires a configuration file"); } ctx = td_config_init(config->file); if (!ctx) { return -1; } ret = flb_output_set_context("td", ctx, config); if (ret == -1) { flb_utils_error_c("Could not set configuration for td output plugin"); } return 0; }
/* Callback invoked after setup but before to join the main loop */ int in_kmsg_pre_run(void *in_context, struct flb_config *config) { struct flb_in_kmsg_config *ctx = in_context; /* Tag */ ctx->tag_len = snprintf(ctx->tag, sizeof(ctx->tag) - 1, "%s.kmsg", config->tag); if (ctx->tag_len == -1) { flb_utils_error_c("Could not set custom tag on kmsg input plugin"); } return 0; }
int cb_td_init(struct flb_output_plugin *plugin, struct flb_config *config) { int ret; struct flb_out_td_config *ctx; struct flb_io_upstream *upstream; if (!config->file) { flb_utils_error_c("TD output requires a configuration file"); } ctx = td_config_init(config->file); if (!ctx) { return -1; } /* Default server */ plugin->net_host = strdup("api.treasuredata.com"); plugin->net_port = 443; upstream = flb_io_upstream_new(config, plugin->net_host, plugin->net_port, FLB_IO_TLS, (void *) &plugin->tls); if (!upstream) { free(ctx); return -1; } ctx->u = upstream; ret = flb_output_set_context("td", ctx, config); if (ret == -1) { flb_utils_error_c("Could not set configuration for td output plugin"); } return 0; }
int cb_fluentd_init(struct flb_config *config) { int ret; struct flb_out_fluentd_config *ctx; ctx = calloc(1, sizeof(struct flb_out_fluentd_config)); if (!ctx) { perror("calloc"); return -1; } ret = flb_output_set_context("fluentd", ctx, config); if (ret == -1) { flb_utils_error_c("Could not set configuration for fluentd output 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 CPU input */ int in_cpu_init(struct flb_config *config) { int i; int ret; int len; double total; struct flb_in_cpu_config *ctx; struct cpu_stats *cstats; struct cpu_snapshot *snap; /* Allocate space for the configuration */ ctx = malloc(sizeof(struct flb_in_cpu_config)); if (!ctx) { perror("malloc"); return -1; } /* Gather number of processors and CPU ticks */ ctx->n_processors = sysconf(_SC_NPROCESSORS_ONLN); ctx->cpu_ticks = sysconf(_SC_CLK_TCK); /* Initialize buffers for CPU stats */ cstats = &ctx->cstats; cstats->info = malloc(sizeof(struct cpu_snapshot) * (ctx->n_processors + 1)); if (!cstats->info) { perror("malloc"); return -1; } for (i = 1; i <= ctx->n_processors; i++) { snap = &cstats->info[i]; CPU_KEY_FORMAT(snap, user, i); CPU_KEY_FORMAT(snap, nice, i); CPU_KEY_FORMAT(snap, system, i); CPU_KEY_FORMAT(snap, idle, i); CPU_KEY_FORMAT(snap, iowait, i); CPU_KEY_FORMAT(snap, irq, i); CPU_KEY_FORMAT(snap, softirq, i); } /* initialize MessagePack buffers */ msgpack_sbuffer_init(&ctx->mp_sbuf); msgpack_packer_init(&ctx->mp_pck, &ctx->mp_sbuf, msgpack_sbuffer_write); /* Get CPU load, ready to be updated once fired the calc callback */ total = proc_cpu_load(ctx->n_processors, &ctx->cstats); if (total == -1) { flb_utils_error_c("Could not obtain CPU data"); } ctx->cstats.load_pre = total; /* Set the context */ ret = flb_input_set_context("cpu", ctx, config); if (ret == -1) { flb_utils_error_c("Could not set configuration for CPU input plugin"); } /* Set our collector based on time, CPU usage every 1 second */ ret = flb_input_set_collector_time("cpu", in_cpu_collect, IN_CPU_COLLECT_SEC, IN_CPU_COLLECT_NSEC, config); if (ret == -1) { flb_utils_error_c("Could not set collector for CPU input plugin"); } return 0; }
/* Init kmsg input */ int in_xbee_init(struct flb_config *config) { int ret; int opt_baudrate = 9600; char *tmp; char *opt_device = FLB_XBEE_DEFAULT_DEVICE; struct stat dev_st; struct xbee *xbee; struct xbee_con *con; struct xbee_conAddress address; struct flb_in_xbee_config *ctx; /* Check an optional baudrate */ tmp = getenv("FLB_XBEE_BAUDRATE"); if (tmp) { opt_baudrate = atoi(tmp); } /* Get the target device entry */ tmp = getenv("FLB_XBEE_DEVICE"); if (tmp) { opt_device = strdup(tmp); } flb_info("XBee device=%s, baudrate=%i", opt_device, opt_baudrate); ret = stat(opt_device, &dev_st); if (ret < 0) { printf("Error: could not open %s device\n", opt_device); exit(EXIT_FAILURE); } if (!S_ISCHR(dev_st.st_mode)) { printf("Error: invalid device %s \n", opt_device); exit(EXIT_FAILURE); } if (access(opt_device, R_OK | W_OK) == -1) { printf("Error: cannot open the device %s (permission denied ?)\n", opt_device); exit(EXIT_FAILURE); } /* Init library */ xbee_init(); ret = xbee_setup(&xbee, "xbeeZB", opt_device, opt_baudrate); if (ret != XBEE_ENONE) { flb_utils_error_c("xbee_setup"); return ret; } /* FIXME: just a built-in example */ memset(&address, 0, sizeof(address)); address.addr64_enabled = 1; address.addr64[0] = 0x00; address.addr64[1] = 0x13; address.addr64[2] = 0xA2; address.addr64[3] = 0x00; address.addr64[4] = 0x40; address.addr64[5] = 0xB7; address.addr64[6] = 0xB1; address.addr64[7] = 0xEB; /* Prepare a connection with the peer XBee */ if ((ret = xbee_conNew(xbee, &con, "Data", &address)) != XBEE_ENONE) { xbee_log(xbee, -1, "xbee_conNew() returned: %d (%s)", ret, xbee_errorToStr(ret)); return ret; } /* Prepare the configuration context */ ctx = calloc(1, sizeof(struct flb_in_xbee_config)); if (!ctx) { perror("calloc"); return -1; } ctx->device = opt_device; ctx->baudrate = opt_baudrate; ctx->con = con; ctx->buffer_len = 0; if ((ret = xbee_conDataSet(con, ctx, NULL)) != XBEE_ENONE) { xbee_log(xbee, -1, "xbee_conDataSet() returned: %d", ret); return ret; } if ((ret = xbee_conCallbackSet(con, in_xbee_cb, NULL)) != XBEE_ENONE) { xbee_log(xbee, -1, "xbee_conCallbackSet() returned: %d", ret); return ret; } /* Set the context */ ret = flb_input_set_context("xbee", ctx, config); if (ret == -1) { flb_utils_error_c("Could not set configuration for xbee input plugin"); } /* * Set our collector based on time. We will trigger a collection at certain * intervals. For now it works but it's not the ideal implementation. I am * talking with libxbee maintainer to check possible workarounds and use * proper events mechanism. */ ret = flb_input_set_collector_time("xbee", in_xbee_collect, IN_XBEE_COLLECT_SEC, IN_XBEE_COLLECT_NSEC, config); if (ret == -1) { flb_utils_error_c("Could not set collector for xbee 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)); 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; }
/* Init xbee input */ int in_xbee_init(struct flb_config *config, void *data) { int ret; struct stat dev_st; struct xbee *xbee; struct xbee_conAddress address; struct flb_in_xbee_config *ctx; struct xbee_conSettings settings; (void) data; /* Prepare the configuration context */ ctx = calloc(1, sizeof(struct flb_in_xbee_config)); if (!ctx) { perror("calloc"); return -1; } if (!config->file) { flb_utils_error_c("XBee input plugin needs configuration file"); return -1; } xbee_config_read(ctx, config->file); /* initialize MessagePack buffers */ msgpack_sbuffer_init(&ctx->mp_sbuf); msgpack_packer_init(&ctx->mp_pck, &ctx->mp_sbuf, msgpack_sbuffer_write); flb_info("XBee device=%s, baudrate=%i", ctx->file, ctx->baudrate); ret = stat(ctx->file, &dev_st); if (ret < 0) { printf("Error: could not open %s device\n", ctx->file); free(ctx->file); exit(EXIT_FAILURE); } if (!S_ISCHR(dev_st.st_mode)) { printf("Error: invalid device %s \n", ctx->file); free(ctx->file); exit(EXIT_FAILURE); } if (access(ctx->file, R_OK | W_OK) == -1) { printf("Error: cannot open the device %s (permission denied ?)\n", ctx->file); free(ctx->file); exit(EXIT_FAILURE); } ctx->config = config; pthread_mutex_init(&ctx->mtx_mp, NULL); ctx->buffer_len = 0; /* Init library */ xbee_init(); ret = xbee_setup(&xbee, ctx->xbeeMode, ctx->file, ctx->baudrate); if (ret != XBEE_ENONE) { flb_utils_error_c("xbee_setup"); return ret; } /* 000000000000FFFF: broadcast address */ memset(&address, 0, sizeof(address)); address.addr64_enabled = 1; address.addr64[0] = 0x00; address.addr64[1] = 0x00; address.addr64[2] = 0x00; address.addr64[3] = 0x00; address.addr64[4] = 0x00; address.addr64[5] = 0x00; address.addr64[6] = 0xFF; address.addr64[7] = 0xFF; if (ctx->xbeeLogLevel >= 0) xbee_logLevelSet(xbee, ctx->xbeeLogLevel); /* Prepare a connection with the peer XBee */ if ((ret = xbee_conNew(xbee, &ctx->con_data, "Data", &address)) != XBEE_ENONE) { xbee_log(xbee, -1, "xbee_conNew() returned: %d (%s)", ret, xbee_errorToStr(ret)); return ret; } xbee_conSettings(ctx->con_data, NULL, &settings); settings.disableAck = ctx->xbeeDisableAck ? 1 : 0; settings.catchAll = ctx->xbeeCatchAll ? 1 : 0; xbee_conSettings(ctx->con_data, &settings, NULL); if ((ret = xbee_conDataSet(ctx->con_data, ctx, NULL)) != XBEE_ENONE) { xbee_log(xbee, -1, "xbee_conDataSet() returned: %d", ret); return ret; } if ((ret = xbee_conCallbackSet(ctx->con_data, in_xbee_cb, NULL)) != XBEE_ENONE) { xbee_log(xbee, -1, "xbee_conCallbackSet() returned: %d", ret); return ret; } if ((ret = xbee_conNew(xbee, &ctx->con_io, "I/O", &address)) != XBEE_ENONE) { xbee_log(xbee, -1, "xbee_conNew() returned: %d (%s)", ret, xbee_errorToStr(ret)); return ret; } xbee_conSettings(ctx->con_io, NULL, &settings); settings.disableAck = ctx->xbeeDisableAck ? 1 : 0; settings.catchAll = ctx->xbeeCatchAll ? 1 : 0; xbee_conSettings(ctx->con_io, &settings, NULL); if ((ret = xbee_conDataSet(ctx->con_io, ctx, NULL)) != XBEE_ENONE) { xbee_log(xbee, -1, "xbee_conDataSet() returned: %d", ret); return ret; } if ((ret = xbee_conCallbackSet(ctx->con_io, in_xbee_iosampling_cb, NULL)) != XBEE_ENONE) { xbee_log(xbee, -1, "xbee_conCallbackSet() returned: %d", ret); return ret; } /* Set the context */ ret = flb_input_set_context("xbee", ctx, config); if (ret == -1) { flb_utils_error_c("Could not set configuration for xbee input plugin"); } return 0; }