X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fnetwork.c;h=b6778fa0797444901b71364c4f8a559552349f3c;hb=430b0b4d0275f7fea64376fc166e81fc0bcc36bf;hp=b67928c7c2fe6a10aec868ba61c5c7fbe512f6c9;hpb=3469385f04ffcfa2f4ef06d34ed4b24acb378b34;p=collectd.git diff --git a/src/network.c b/src/network.c index b67928c7..b6778fa0 100644 --- a/src/network.c +++ b/src/network.c @@ -183,7 +183,7 @@ static pthread_mutex_t send_buffer_lock = PTHREAD_MUTEX_INITIALIZER; static c_avl_tree_t *cache_tree = NULL; static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER; -static time_t cache_flush_last; +static time_t cache_flush_last = 0; static int cache_flush_interval = 1800; /* @@ -246,7 +246,7 @@ static int cache_flush (void) return (0); } /* int cache_flush */ -static int cache_check (const char *type, const value_list_t *vl) +static int cache_check (const value_list_t *vl) { char key[1024]; time_t *value = NULL; @@ -256,7 +256,7 @@ static int cache_check (const char *type, const value_list_t *vl) return (-1); if (format_name (key, sizeof (key), vl->host, vl->plugin, - vl->plugin_instance, type, vl->type_instance)) + vl->plugin_instance, vl->type, vl->type_instance)) return (-1); pthread_mutex_lock (&cache_lock); @@ -678,14 +678,12 @@ static int parse_packet (void *buffer, int buffer_len) int status; value_list_t vl = VALUE_LIST_INIT; - char type[DATA_MAX_NAME_LEN]; notification_t n; DEBUG ("network plugin: parse_packet: buffer = %p; buffer_len = %i;", buffer, buffer_len); memset (&vl, '\0', sizeof (vl)); - memset (&type, '\0', sizeof (type)); memset (&n, '\0', sizeof (n)); status = 0; @@ -722,10 +720,10 @@ static int parse_packet (void *buffer, int buffer_len) if ((vl.time > 0) && (strlen (vl.host) > 0) && (strlen (vl.plugin) > 0) - && (strlen (type) > 0) - && (cache_check (type, &vl) == 0)) + && (strlen (vl.type) > 0) + && (cache_check (&vl) == 0)) { - plugin_dispatch_values (type, &vl); + plugin_dispatch_values (&vl); } else { @@ -782,9 +780,9 @@ static int parse_packet (void *buffer, int buffer_len) else if (pkg_type == TYPE_TYPE) { status = parse_part_string (&buffer, &buffer_len, - type, sizeof (type)); + vl.type, sizeof (vl.type)); if (status == 0) - sstrncpy (n.type, type, sizeof (n.type)); + sstrncpy (n.type, vl.type, sizeof (n.type)); } else if (pkg_type == TYPE_TYPE_INSTANCE) { @@ -1441,12 +1439,12 @@ static int add_to_buffer (char *buffer, int buffer_size, strcpy (vl_def->plugin_instance, vl->plugin_instance); } - if (strcmp (type_def, ds->type) != 0) + if (strcmp (type_def, vl->type) != 0) { if (write_part_string (&buffer, &buffer_size, TYPE_TYPE, - ds->type, strlen (ds->type)) != 0) + vl->type, strlen (vl->type)) != 0) return (-1); - strcpy (type_def, ds->type); + strcpy (type_def, vl->type); } if (strcmp (vl_def->type_instance, vl->type_instance) != 0) @@ -1483,7 +1481,7 @@ static int network_write (const data_set_t *ds, const value_list_t *vl) /* If the value is already in the cache, we have received it via the * network. We write it again if forwarding is activated. It's then in * the cache and should we receive it again we will ignore it. */ - status = cache_check (ds->type, vl); + status = cache_check (vl); if ((network_config_forward == 0) && (status != 0)) return (0); @@ -1706,11 +1704,19 @@ static int network_shutdown (void) plugin_unregister_write ("network"); plugin_unregister_shutdown ("network"); + /* Let the init function do it's move again ;) */ + cache_flush_last = 0; + return (0); } /* int network_shutdown */ static int network_init (void) { + /* Check if we were already initialized. If so, just return - there's + * nothing more to do (for now, that is). */ + if (cache_flush_last != 0) + return (0); + plugin_register_shutdown ("network", network_shutdown); send_buffer_ptr = send_buffer; @@ -1759,9 +1765,32 @@ static int network_init (void) return (0); } /* int network_init */ +/* + * The flush option of the network plugin cannot flush individual identifiers. + * All the values are added to a buffer and sent when the buffer is full, the + * requested value may or may not be in there, it's not worth finding out. We + * just send the buffer if `flush' is called - if the requested value was in + * there, good. If not, well, then there is nothing to flush.. -octo + */ +static int network_flush (int timeout, const char *identifier) +{ + pthread_mutex_lock (&send_buffer_lock); + + if (((time (NULL) - cache_flush_last) >= timeout) + && (send_buffer_fill > 0)) + { + flush_buffer (); + } + + pthread_mutex_unlock (&send_buffer_lock); + + return (0); +} /* int network_flush */ + void module_register (void) { plugin_register_config ("network", network_config, config_keys, config_keys_num); plugin_register_init ("network", network_init); + plugin_register_flush ("network", network_flush); } /* void module_register */