Code

core: Changed internal API to allow for per-plugin intervals.
[collectd.git] / src / network.c
index 43193cfca250093849a918b547904fc7f999981e..1d6708d5f54d65ca583a4181ed3bc74c8dfb60b9 100644 (file)
@@ -1,6 +1,6 @@
 /**
  * collectd - src/network.c
- * Copyright (C) 2005-2009  Florian octo Forster
+ * Copyright (C) 2005-2010  Florian octo Forster
  * Copyright (C) 2009       Aman Gupta
  *
  * This program is free software; you can redistribute it and/or modify it
@@ -31,6 +31,7 @@
 #include "utils_fbhash.h"
 #include "utils_avltree.h"
 #include "utils_cache.h"
+#include "utils_complain.h"
 
 #include "network.h"
 
@@ -295,14 +296,14 @@ static pthread_mutex_t  send_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
  * example). Only if neither is true, the stats_lock is acquired. The counters
  * are always read without holding a lock in the hope that writing 8 bytes to
  * memory is an atomic operation. */
-static uint64_t stats_octets_rx  = 0;
-static uint64_t stats_octets_tx  = 0;
-static uint64_t stats_packets_rx = 0;
-static uint64_t stats_packets_tx = 0;
-static uint64_t stats_values_dispatched = 0;
-static uint64_t stats_values_not_dispatched = 0;
-static uint64_t stats_values_sent = 0;
-static uint64_t stats_values_not_sent = 0;
+static derive_t stats_octets_rx  = 0;
+static derive_t stats_octets_tx  = 0;
+static derive_t stats_packets_rx = 0;
+static derive_t stats_packets_tx = 0;
+static derive_t stats_values_dispatched = 0;
+static derive_t stats_values_not_dispatched = 0;
+static derive_t stats_values_sent = 0;
+static derive_t stats_values_not_sent = 0;
 static pthread_mutex_t stats_lock = PTHREAD_MUTEX_INITIALIZER;
 
 /*
@@ -787,7 +788,6 @@ static int parse_part_number (void **ret_buffer, size_t *ret_buffer_len,
        size_t exp_size = 2 * sizeof (uint16_t) + sizeof (uint64_t);
 
        uint16_t pkg_length;
-       uint16_t pkg_type;
 
        if ((buffer_len < 0) || ((size_t) buffer_len < exp_size))
        {
@@ -801,7 +801,7 @@ static int parse_part_number (void **ret_buffer, size_t *ret_buffer_len,
 
        memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
        buffer += sizeof (tmp16);
-       pkg_type = ntohs (tmp16);
+       /* pkg_type = ntohs (tmp16); */
 
        memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
        buffer += sizeof (tmp16);
@@ -827,7 +827,6 @@ static int parse_part_string (void **ret_buffer, size_t *ret_buffer_len,
        size_t header_size = 2 * sizeof (uint16_t);
 
        uint16_t pkg_length;
-       uint16_t pkg_type;
 
        if ((buffer_len < 0) || (buffer_len < header_size))
        {
@@ -841,7 +840,7 @@ static int parse_part_string (void **ret_buffer, size_t *ret_buffer_len,
 
        memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
        buffer += sizeof (tmp16);
-       pkg_type = ntohs (tmp16);
+       /* pkg_type = ntohs (tmp16); */
 
        memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
        buffer += sizeof (tmp16);
@@ -917,6 +916,8 @@ static int parse_packet (sockent_t *se,
 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
     void **ret_buffer, size_t *ret_buffer_len, int flags)
 {
+  static c_complain_t complain_no_users = C_COMPLAIN_INIT_STATIC;
+
   char *buffer;
   size_t buffer_len;
   size_t buffer_offset;
@@ -938,8 +939,9 @@ static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
 
   if (se->data.server.userdb == NULL)
   {
-    NOTICE ("network plugin: Received signed network packet but can't verify "
-        "it because no user DB has been configured. Will accept it.");
+    c_complain (LOG_NOTICE, &complain_no_users,
+        "network plugin: Received signed network packet but can't verify it "
+        "because no user DB has been configured. Will accept it.");
     return (0);
   }
 
@@ -1270,7 +1272,7 @@ static int parse_packet (sockent_t *se, /* {{{ */
 {
        int status;
 
-       value_list_t vl = VALUE_LIST_INIT;
+       value_list_t vl = VALUE_LIST_INIT (/* default interval = */ 0);
        notification_t n;
 
 #if HAVE_LIBGCRYPT
@@ -1378,7 +1380,18 @@ static int parse_packet (sockent_t *se, /* {{{ */
                        if (status == 0)
                        {
                                vl.time = TIME_T_TO_CDTIME_T (tmp);
-                               n.time = TIME_T_TO_CDTIME_T (tmp);
+                               n.time  = TIME_T_TO_CDTIME_T (tmp);
+                       }
+               }
+               else if (pkg_type == TYPE_TIME_HR)
+               {
+                       uint64_t tmp = 0;
+                       status = parse_part_number (&buffer, &buffer_size,
+                                       &tmp);
+                       if (status == 0)
+                       {
+                               vl.time = (cdtime_t) tmp;
+                               n.time  = (cdtime_t) tmp;
                        }
                }
                else if (pkg_type == TYPE_INTERVAL)
@@ -1389,6 +1402,14 @@ static int parse_packet (sockent_t *se, /* {{{ */
                        if (status == 0)
                                vl.interval = TIME_T_TO_CDTIME_T (tmp);
                }
+               else if (pkg_type == TYPE_INTERVAL_HR)
+               {
+                       uint64_t tmp = 0;
+                       status = parse_part_number (&buffer, &buffer_size,
+                                       &tmp);
+                       if (status == 0)
+                               vl.interval = (cdtime_t) tmp;
+               }
                else if (pkg_type == TYPE_HOST)
                {
                        status = parse_part_string (&buffer, &buffer_size,
@@ -2583,18 +2604,16 @@ static int add_to_buffer (char *buffer, int buffer_size, /* {{{ */
 
        if (vl_def->time != vl->time)
        {
-               time_t tmp = CDTIME_T_TO_TIME_T (vl->time);
-               if (write_part_number (&buffer, &buffer_size, TYPE_TIME,
-                                       (uint64_t) tmp))
+               if (write_part_number (&buffer, &buffer_size, TYPE_TIME_HR,
+                                       (uint64_t) vl->time))
                        return (-1);
                vl_def->time = vl->time;
        }
 
        if (vl_def->interval != vl->interval)
        {
-               /* TODO: Create a new type for sub-second intervals. */
-               if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL,
-                                       (uint64_t) CDTIME_T_TO_TIME_T (vl->interval)))
+               if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL_HR,
+                                       (uint64_t) vl->interval))
                        return (-1);
                vl_def->interval = vl->interval;
        }
@@ -3072,14 +3091,11 @@ static int network_notification (const notification_t *n,
   char *buffer_ptr = buffer;
   int   buffer_free = sizeof (buffer);
   int   status;
-  time_t tmp;
 
   memset (buffer, '\0', sizeof (buffer));
 
-
-  tmp = CDTIME_T_TO_TIME_T (n->time);
-  status = write_part_number (&buffer_ptr, &buffer_free, TYPE_TIME,
-      (uint64_t) tmp);
+  status = write_part_number (&buffer_ptr, &buffer_free, TYPE_TIME_HR,
+      (uint64_t) n->time);
   if (status != 0)
     return (-1);
 
@@ -3183,16 +3199,16 @@ static int network_shutdown (void)
 
 static int network_stats_read (void) /* {{{ */
 {
-       uint64_t copy_octets_rx;
-       uint64_t copy_octets_tx;
-       uint64_t copy_packets_rx;
-       uint64_t copy_packets_tx;
-       uint64_t copy_values_dispatched;
-       uint64_t copy_values_not_dispatched;
-       uint64_t copy_values_sent;
-       uint64_t copy_values_not_sent;
-       uint64_t copy_receive_list_length;
-       value_list_t vl = VALUE_LIST_INIT;
+       derive_t copy_octets_rx;
+       derive_t copy_octets_tx;
+       derive_t copy_packets_rx;
+       derive_t copy_packets_tx;
+       derive_t copy_values_dispatched;
+       derive_t copy_values_not_dispatched;
+       derive_t copy_values_sent;
+       derive_t copy_values_not_sent;
+       derive_t copy_receive_list_length;
+       value_list_t vl = VALUE_LIST_INIT (plugin_interval);
        value_t values[2];
 
        copy_octets_rx = stats_octets_rx;
@@ -3214,14 +3230,14 @@ static int network_stats_read (void) /* {{{ */
        sstrncpy (vl.plugin, "network", sizeof (vl.plugin));
 
        /* Octets received / sent */
-       vl.values[0].counter = (counter_t) copy_octets_rx;
-       vl.values[1].counter = (counter_t) copy_octets_tx;
+       vl.values[0].derive = (derive_t) copy_octets_rx;
+       vl.values[1].derive = (derive_t) copy_octets_tx;
        sstrncpy (vl.type, "if_octets", sizeof (vl.type));
        plugin_dispatch_values (&vl);
 
        /* Packets received / send */
-       vl.values[0].counter = (counter_t) copy_packets_rx;
-       vl.values[1].counter = (counter_t) copy_packets_tx;
+       vl.values[0].derive = (derive_t) copy_packets_rx;
+       vl.values[1].derive = (derive_t) copy_packets_tx;
        sstrncpy (vl.type, "if_packets", sizeof (vl.type));
        plugin_dispatch_values (&vl);
 
@@ -3275,7 +3291,7 @@ static int network_init (void)
 #endif
 
        if (network_config_stats != 0)
-               plugin_register_read ("network", network_stats_read);
+               plugin_register_read ("network", network_stats_read, plugin_interval);
 
        plugin_register_shutdown ("network", network_shutdown);
 
@@ -3366,8 +3382,9 @@ static int network_flush (__attribute__((unused)) cdtime_t timeout,
        return (0);
 } /* int network_flush */
 
-void module_register (void)
+void module_register (plugin_loaddata_t *data)
 {
+       PLUGIN_INIT_INTERVAL (data);
        plugin_register_complex_config ("network", network_config);
        plugin_register_init   ("network", network_init);
        plugin_register_flush   ("network", network_flush,