static int ts_config_add_meta(meta_data_t **dest, /* {{{ */ const oconfig_item_t *ci, int may_be_empty) { char *key = NULL; char *string = NULL; int status; status = ts_util_get_key_and_string_wo_strdup(ci, &key, &string); if (status != 0) return (status); if (strlen(key) == 0) { ERROR("Target `set': The `%s' option does not accept empty string as " "first argument.", ci->key); return (-1); } if (!may_be_empty && (strlen(string) == 0)) { ERROR("Target `set': The `%s' option does not accept empty string as " "second argument.", ci->key); return (-1); } if ((*dest) == NULL) { /* Create a new meta_data_t */ if ((*dest = meta_data_create()) == NULL) { ERROR("Target `set': failed to create a meta data for `%s'.", ci->key); return (-ENOMEM); } } return (meta_data_add_string(*dest, key, string)); } /* }}} int ts_config_add_meta */
void submit_app_value (unsigned long value, const char* type_instance, const char* tags, const char* app_id) { value_t values[1]; value_list_t vl = VALUE_LIST_INIT; init_value_list (&vl); values[0].derive = value; vl.values = values; vl.values_len = 1; meta_data_add_string (vl.meta,"tsdb_tags",tags); sstrncpy (vl.type, "app", sizeof (vl.type)); sstrncpy (vl.plugin_instance, app_id, sizeof (vl.type)); sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance)); plugin_dispatch_values (&vl); }
static void cpus_submit (unsigned long long vcpu_time, int vcpu_nr, container_t* container, const char *type) { value_t values[1]; value_list_t vl = VALUE_LIST_INIT; init_value_list (&vl); values[0].derive = vcpu_time; vl.values = values; vl.values_len = 1; char tags[1024]; ssnprintf(tags,1024,"container_id=%s image_name=%s",container->id,container->image_name); meta_data_add_string (vl.meta,"tsdb_tags",tags); sstrncpy (vl.type, type, sizeof (vl.type)); ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%d", vcpu_nr); plugin_dispatch_values (&vl); }
void memory_submit (unsigned long value, container_t* container, const char* type_instance) { value_t values[1]; value_list_t vl = VALUE_LIST_INIT; init_value_list (&vl); values[0].gauge = value; vl.values = values; vl.values_len = 1; char tags[1024]; ssnprintf(tags,1024,"container_id=%s image_name=%s",container->id,container->image_name); meta_data_add_string (vl.meta,"tsdb_tags",tags); sstrncpy (vl.type, "memory", sizeof (vl.type)); ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s", type_instance); plugin_dispatch_values (&vl); }
static void submit_derive2 (const char *type, derive_t v0, derive_t v1, container_t* container) { value_t values[2]; value_list_t vl = VALUE_LIST_INIT; init_value_list (&vl); values[0].derive = v0; values[1].derive = v1; vl.values = values; vl.values_len = 2; char tags[1024]; ssnprintf (tags,1024,"container_id=%s image_name=%s",container->id,container->image_name); meta_data_add_string (vl.meta,"tsdb_tags",tags); sstrncpy (vl.type, type, sizeof (vl.type)); plugin_dispatch_values (&vl); }
/* * Result private functions */ static int udb_result_submit (udb_result_t *r, /* {{{ */ udb_result_preparation_area_t *r_area, udb_query_t const *q, udb_query_preparation_area_t *q_area) { value_list_t vl = VALUE_LIST_INIT; size_t i; int status; assert (r != NULL); assert (r_area->ds != NULL); assert (((size_t) r_area->ds->ds_num) == r->values_num); assert (r->values_num > 0); vl.values = calloc (r->values_num, sizeof (*vl.values)); if (vl.values == NULL) { ERROR ("db query utils: calloc failed."); return (-1); } vl.values_len = r_area->ds->ds_num; for (i = 0; i < r->values_num; i++) { char *value_str = r_area->values_buffer[i]; if (0 != parse_value (value_str, &vl.values[i], r_area->ds->ds[i].type)) { ERROR ("db query utils: udb_result_submit: Parsing `%s' as %s failed.", value_str, DS_TYPE_TO_STRING (r_area->ds->ds[i].type)); errno = EINVAL; free (vl.values); return (-1); } } if (q_area->interval > 0) vl.interval = q_area->interval; sstrncpy (vl.host, q_area->host, sizeof (vl.host)); sstrncpy (vl.plugin, q_area->plugin, sizeof (vl.plugin)); sstrncpy (vl.type, r->type, sizeof (vl.type)); /* Set vl.plugin_instance */ if (q->plugin_instance_from != NULL) { sstrncpy (vl.plugin_instance, r_area->plugin_instance, sizeof (vl.plugin_instance)); } else { sstrncpy (vl.plugin_instance, q_area->db_name, sizeof (vl.plugin_instance)); } /* Set vl.type_instance {{{ */ if (r->instances_num == 0) { if (r->instance_prefix == NULL) vl.type_instance[0] = 0; else sstrncpy (vl.type_instance, r->instance_prefix, sizeof (vl.type_instance)); } else /* if ((r->instances_num > 0) */ { if (r->instance_prefix == NULL) { strjoin (vl.type_instance, sizeof (vl.type_instance), r_area->instances_buffer, r->instances_num, "-"); } else { char tmp[DATA_MAX_NAME_LEN]; strjoin (tmp, sizeof (tmp), r_area->instances_buffer, r->instances_num, "-"); tmp[sizeof (tmp) - 1] = 0; snprintf (vl.type_instance, sizeof (vl.type_instance), "%s-%s", r->instance_prefix, tmp); } } vl.type_instance[sizeof (vl.type_instance) - 1] = 0; /* }}} */ /* Annotate meta data. {{{ */ if (r->metadata_num > 0) { vl.meta = meta_data_create (); if (vl.meta == NULL) { ERROR ("db query utils:: meta_data_create failed."); return (-ENOMEM); } for (i = 0; i < r->metadata_num; i++) { status = meta_data_add_string (vl.meta, r->metadata[i], r_area->metadata_buffer[i]); if (status != 0) { ERROR ("db query utils:: meta_data_add_string failed."); meta_data_destroy (vl.meta); vl.meta = NULL; return (status); } } } /* }}} */ plugin_dispatch_values (&vl); if (r->metadata_num > 0) { meta_data_destroy (vl.meta); vl.meta = NULL; } sfree (vl.values); return (0); } /* }}} void udb_result_submit */
static int tr_meta_data_action_invoke(/* {{{ */ tr_meta_data_action_t *act_head, meta_data_t **dest) { int status; regmatch_t matches[8] = {[0] = {0}}; if (act_head == NULL) return (-EINVAL); if ((*dest) == NULL) /* nothing to do */ return (0); for (tr_meta_data_action_t *act = act_head; act != NULL; act = act->next) { char temp[DATA_MAX_NAME_LEN]; char *subst_status; int value_type; int meta_data_status; char *value; meta_data_t *result; value_type = meta_data_type(*dest, act->key); if (value_type == 0) /* not found */ continue; if (value_type != MD_TYPE_STRING) { WARNING("Target `replace': Attempting replace on metadata key `%s', " "which isn't a string.", act->key); continue; } meta_data_status = meta_data_get_string(*dest, act->key, &value); if (meta_data_status != 0) { ERROR("Target `replace': Unable to retrieve metadata value for `%s'.", act->key); return (meta_data_status); } DEBUG("target_replace plugin: tr_meta_data_action_invoke: `%s' " "old value = `%s'", act->key, value); status = regexec(&act->re, value, STATIC_ARRAY_SIZE(matches), matches, /* flags = */ 0); if (status == REG_NOMATCH) { sfree(value); continue; } else if (status != 0) { char errbuf[1024] = ""; regerror(status, &act->re, errbuf, sizeof(errbuf)); ERROR("Target `replace': Executing a regular expression failed: %s.", errbuf); sfree(value); continue; } if (act->replacement == NULL) { /* no replacement; delete the key */ DEBUG("target_replace plugin: tr_meta_data_action_invoke: " "deleting `%s'", act->key); meta_data_delete(*dest, act->key); sfree(value); continue; } subst_status = subst(temp, sizeof(temp), value, (size_t)matches[0].rm_so, (size_t)matches[0].rm_eo, act->replacement); if (subst_status == NULL) { ERROR("Target `replace': subst (value = %s, start = %zu, end = %zu, " "replacement = %s) failed.", value, (size_t)matches[0].rm_so, (size_t)matches[0].rm_eo, act->replacement); sfree(value); continue; } DEBUG("target_replace plugin: tr_meta_data_action_invoke: `%s' " "value `%s' -> `%s'", act->key, value, temp); if ((result = meta_data_create()) == NULL) { ERROR("Target `replace': failed to create metadata for `%s'.", act->key); sfree(value); return (-ENOMEM); } meta_data_status = meta_data_add_string(result, act->key, temp); if (meta_data_status != 0) { ERROR("Target `replace': Unable to set metadata value for `%s'.", act->key); meta_data_destroy(result); sfree(value); return (meta_data_status); } meta_data_clone_merge(dest, result); meta_data_destroy(result); sfree(value); } /* for (act = act_head; act != NULL; act = act->next) */ return (0); } /* }}} int tr_meta_data_action_invoke */
static int network_dispatch_values (value_list_t *vl, /* {{{ */ const char *username) { int status; // DEBUG("host: %s", vl->host); // DEBUG("plugin: %s", vl->plugin); // DEBUG("plugin_instance: %s", vl->plugin_instance); // DEBUG("type: %s", vl->type); // DEBUG("type_instance: %s", vl->type_instance); if ((vl->time <= 0) || (strlen (vl->host) <= 0) || (strlen (vl->plugin) <= 0) || (strlen (vl->type) <= 0)) return (-EINVAL); if (!check_receive_okay (vl)) { #if COLLECT_DEBUG char name[6*DATA_MAX_NAME_LEN]; FORMAT_VL (name, sizeof (name), vl); name[sizeof (name) - 1] = 0; DEBUG ("network plugin: network_dispatch_values: " "NOT dispatching %s.", name); #endif stats_values_not_dispatched++; return (0); } assert (vl->meta == NULL); vl->meta = meta_data_create (); if (vl->meta == NULL) { ERROR ("network plugin: meta_data_create failed."); return (-ENOMEM); } status = meta_data_add_boolean (vl->meta, "zeromq:received", 1); if (status != 0) { ERROR ("network plugin: meta_data_add_boolean failed."); meta_data_destroy (vl->meta); vl->meta = NULL; return (status); } if (username != NULL) { status = meta_data_add_string (vl->meta, "zeromq:username", username); if (status != 0) { ERROR ("network plugin: meta_data_add_string failed."); meta_data_destroy (vl->meta); vl->meta = NULL; return (status); } } // DEBUG("dispatching %d values", vl->values_len); plugin_dispatch_values (vl); stats_values_dispatched++; meta_data_destroy (vl->meta); vl->meta = NULL; return (0); } /* }}} int network_dispatch_values */