From a5177bd853eedbe69d87869316093a0e9526e931 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Sun, 31 Oct 2010 12:50:35 +0100 Subject: [PATCH] src/plugin.[ch]: Change the "time" member to "cdtime_t". --- src/common.c | 7 ++-- src/csv.c | 3 +- src/ipmi.c | 8 ++-- src/logfile.c | 10 +++-- src/match_timediff.c | 28 +++----------- src/mysql.c | 2 +- src/netapp.c | 100 +++++++++++++++++++++++------------------------- src/network.c | 11 ++++-- src/notify_email.c | 4 +- src/perl.c | 30 +++++++++++---- src/plugin.c | 10 ++--- src/plugin.h | 5 ++- src/rrdtool.c | 92 +++++++++++++++++++++++++++----------------- src/utils_cache.c | 44 +++++++++++---------- src/utils_cache.h | 2 +- src/utils_cmd_listval.c | 5 ++- 16 files changed, 195 insertions(+), 166 deletions(-) diff --git a/src/common.c b/src/common.c index 08653dcd..1cef5e17 100644 --- a/src/common.c +++ b/src/common.c @@ -833,7 +833,7 @@ int format_values (char *ret, size_t ret_len, /* {{{ */ offset += ((size_t) status); \ } while (0) - BUFFER_ADD ("%lu", (unsigned long) vl->time); + BUFFER_ADD ("%.3f", CDTIME_T_TO_DOUBLE (vl->time)); for (i = 0; i < ds->ds_num; i++) { @@ -979,9 +979,10 @@ int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds) if (i == -1) { if (strcmp ("N", ptr) == 0) - vl->time = time (NULL); + vl->time = cdtime (); else - vl->time = (time_t) atoi (ptr); + /* FIXME: Add error checking here. */ + vl->time = DOUBLE_TO_CDTIME_T (atof (ptr)); } else { diff --git a/src/csv.c b/src/csv.c index 96c1e3e7..889ad62c 100644 --- a/src/csv.c +++ b/src/csv.c @@ -53,7 +53,8 @@ static int value_list_to_string (char *buffer, int buffer_len, memset (buffer, '\0', buffer_len); - status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) vl->time); + status = ssnprintf (buffer, buffer_len, "%.3f", + CDTIME_T_TO_DOUBLE (vl->time)); if ((status < 1) || (status >= buffer_len)) return (-1); offset = status; diff --git a/src/ipmi.c b/src/ipmi.c index 95b3dbf5..c631574d 100644 --- a/src/ipmi.c +++ b/src/ipmi.c @@ -136,7 +136,7 @@ static void sensor_read_handler (ipmi_sensor_t *sensor, if (c_ipmi_nofiy_notpresent) { - notification_t n = { NOTIF_WARNING, time(NULL), "", "", "ipmi", + notification_t n = { NOTIF_WARNING, cdtime (), "", "", "ipmi", "", "", "", NULL }; sstrncpy (n.host, hostname_g, sizeof (n.host)); @@ -190,7 +190,7 @@ static void sensor_read_handler (ipmi_sensor_t *sensor, if (c_ipmi_nofiy_notpresent) { - notification_t n = { NOTIF_OKAY, time(NULL), "", "", "ipmi", + notification_t n = { NOTIF_OKAY, cdtime (), "", "", "ipmi", "", "", "", NULL }; sstrncpy (n.host, hostname_g, sizeof (n.host)); @@ -363,7 +363,7 @@ static int sensor_list_add (ipmi_sensor_t *sensor) if (c_ipmi_nofiy_add && (c_ipmi_init_in_progress == 0)) { - notification_t n = { NOTIF_OKAY, time(NULL), "", "", "ipmi", + notification_t n = { NOTIF_OKAY, cdtime (), "", "", "ipmi", "", "", "", NULL }; sstrncpy (n.host, hostname_g, sizeof (n.host)); @@ -417,7 +417,7 @@ static int sensor_list_remove (ipmi_sensor_t *sensor) if (c_ipmi_nofiy_remove && c_ipmi_active) { - notification_t n = { NOTIF_WARNING, time(NULL), "", "", + notification_t n = { NOTIF_WARNING, cdtime (), "", "", "ipmi", "", "", "", NULL }; sstrncpy (n.host, hostname_g, sizeof (n.host)); diff --git a/src/logfile.c b/src/logfile.c index 6d0f6e07..60fb5d92 100644 --- a/src/logfile.c +++ b/src/logfile.c @@ -92,7 +92,8 @@ static int logfile_config (const char *key, const char *value) return 0; } /* int logfile_config (const char *, const char *) */ -static void logfile_print (const char *msg, int severity, time_t timestamp_time) +static void logfile_print (const char *msg, int severity, + cdtime_t timestamp_time) { FILE *fh; int do_close = 0; @@ -126,7 +127,8 @@ static void logfile_print (const char *msg, int severity, time_t timestamp_time) if (print_timestamp) { - localtime_r (×tamp_time, ×tamp_tm); + time_t tt = CDTIME_T_TO_TIME_T (timestamp_time); + localtime_r (&tt, ×tamp_tm); strftime (timestamp_str, sizeof (timestamp_str), "%Y-%m-%d %H:%M:%S", ×tamp_tm); @@ -179,7 +181,7 @@ static void logfile_log (int severity, const char *msg, if (severity > log_level) return; - logfile_print (msg, severity, time (NULL)); + logfile_print (msg, severity, cdtime ()); } /* void logfile_log (int, const char *) */ static int logfile_notification (const notification_t *n, @@ -218,7 +220,7 @@ static int logfile_notification (const notification_t *n, buf[sizeof (buf) - 1] = '\0'; logfile_print (buf, LOG_INFO, - (n->time > 0) ? n->time : time (NULL)); + (n->time != 0) ? n->time : cdtime ()); return (0); } /* int logfile_notification */ diff --git a/src/match_timediff.c b/src/match_timediff.c index 4ac944af..2e274155 100644 --- a/src/match_timediff.c +++ b/src/match_timediff.c @@ -34,29 +34,13 @@ struct mt_match_s; typedef struct mt_match_s mt_match_t; struct mt_match_s { - time_t future; - time_t past; + cdtime_t future; + cdtime_t past; }; /* * internal helper functions */ -static int mt_config_add_time_t (time_t *ret_value, /* {{{ */ - oconfig_item_t *ci) -{ - - if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) - { - ERROR ("timediff match: `%s' needs exactly one numeric argument.", - ci->key); - return (-1); - } - - *ret_value = (time_t) ci->values[0].value.number; - - return (0); -} /* }}} int mt_config_add_time_t */ - static int mt_create (const oconfig_item_t *ci, void **user_data) /* {{{ */ { mt_match_t *m; @@ -80,9 +64,9 @@ static int mt_create (const oconfig_item_t *ci, void **user_data) /* {{{ */ oconfig_item_t *child = ci->children + i; if (strcasecmp ("Future", child->key) == 0) - status = mt_config_add_time_t (&m->future, child); + status = cf_util_get_cdtime (child, &m->future); else if (strcasecmp ("Past", child->key) == 0) - status = mt_config_add_time_t (&m->past, child); + status = cf_util_get_cdtime (child, &m->past); else { ERROR ("timediff match: The `%s' configuration option is not " @@ -132,13 +116,13 @@ static int mt_match (const data_set_t __attribute__((unused)) *ds, /* {{{ */ notification_meta_t __attribute__((unused)) **meta, void **user_data) { mt_match_t *m; - time_t now; + cdtime_t now; if ((user_data == NULL) || (*user_data == NULL)) return (-1); m = *user_data; - now = time (NULL); + now = cdtime (); if (m->future != 0) { diff --git a/src/mysql.c b/src/mysql.c index a01bbe40..1ca3b484 100644 --- a/src/mysql.c +++ b/src/mysql.c @@ -485,7 +485,7 @@ static int mysql_read_slave_stats (mysql_database_t *db, MYSQL *con) if (db->slave_notif) { - notification_t n = { 0, time (NULL), "", "", + notification_t n = { 0, cdtime (), "", "", "mysql", "", "time_offset", "", NULL }; char *io, *sql; diff --git a/src/netapp.c b/src/netapp.c index 317b0fe4..2336d467 100644 --- a/src/netapp.c +++ b/src/netapp.c @@ -38,8 +38,8 @@ typedef void service_handler_t(host_config_t *host, na_elem_t *result, void *dat struct cna_interval_s { - time_t interval; - time_t last_read; + cdtime_t interval; + cdtime_t last_read; }; typedef struct cna_interval_s cna_interval_t; @@ -79,7 +79,7 @@ typedef struct { cna_interval_t interval; na_elem_t *query; - time_t timestamp; + cdtime_t timestamp; uint64_t name_cache_hit; uint64_t name_cache_miss; uint64_t find_dir_hit; @@ -104,7 +104,7 @@ typedef struct { typedef struct disk_s { char *name; uint32_t flags; - time_t timestamp; + cdtime_t timestamp; uint64_t disk_busy; uint64_t base_for_disk_busy; double disk_busy_percent; @@ -153,7 +153,7 @@ typedef struct data_volume_perf_s data_volume_perf_t; struct data_volume_perf_s { char *name; uint32_t flags; - time_t timestamp; + cdtime_t timestamp; uint64_t read_bytes; uint64_t write_bytes; @@ -242,7 +242,7 @@ struct host_config_s { int port; char *username; char *password; - int interval; + cdtime_t interval; na_server_t *srv; cfg_wafl_t *cfg_wafl; @@ -566,7 +566,7 @@ static int submit_values (const char *host, /* {{{ */ const char *plugin_inst, const char *type, const char *type_inst, value_t *values, int values_len, - time_t timestamp) + cdtime_t timestamp) { value_list_t vl = VALUE_LIST_INIT; @@ -592,7 +592,7 @@ static int submit_values (const char *host, /* {{{ */ static int submit_two_counters (const char *host, const char *plugin_inst, /* {{{ */ const char *type, const char *type_inst, counter_t val0, counter_t val1, - time_t timestamp) + cdtime_t timestamp) { value_t values[2]; @@ -604,7 +604,7 @@ static int submit_two_counters (const char *host, const char *plugin_inst, /* {{ } /* }}} int submit_two_counters */ static int submit_counter (const char *host, const char *plugin_inst, /* {{{ */ - const char *type, const char *type_inst, counter_t counter, time_t timestamp) + const char *type, const char *type_inst, counter_t counter, cdtime_t timestamp) { value_t v; @@ -616,7 +616,7 @@ static int submit_counter (const char *host, const char *plugin_inst, /* {{{ */ static int submit_two_gauge (const char *host, const char *plugin_inst, /* {{{ */ const char *type, const char *type_inst, gauge_t val0, gauge_t val1, - time_t timestamp) + cdtime_t timestamp) { value_t values[2]; @@ -628,7 +628,7 @@ static int submit_two_gauge (const char *host, const char *plugin_inst, /* {{{ * } /* }}} int submit_two_gauge */ static int submit_double (const char *host, const char *plugin_inst, /* {{{ */ - const char *type, const char *type_inst, double d, time_t timestamp) + const char *type, const char *type_inst, double d, cdtime_t timestamp) { value_t v; @@ -647,7 +647,7 @@ static int submit_cache_ratio (const char *host, /* {{{ */ uint64_t new_misses, uint64_t old_hits, uint64_t old_misses, - time_t timestamp) + cdtime_t timestamp) { value_t v; @@ -812,6 +812,16 @@ static int submit_volume_perf_data (const char *hostname, /* {{{ */ return (0); } /* }}} int submit_volume_perf_data */ +static cdtime_t cna_child_get_cdtime (na_elem_t *data) /* {{{ */ +{ + time_t t; + + t = (time_t) na_child_get_uint64 (data, "timestamp", /* default = */ 0); + + return (TIME_T_TO_CDTIME_T (t)); +} /* }}} cdtime_t cna_child_get_cdtime */ + + /* * Query functions * @@ -831,7 +841,7 @@ static int cna_handle_wafl_data (const char *hostname, cfg_wafl_t *cfg_wafl, /* memset (&perf_data, 0, sizeof (perf_data)); - perf_data.timestamp = (time_t) na_child_get_uint64 (data, "timestamp", 0); + perf_data.timestamp = cna_child_get_cdtime (data); instances = na_elem_child(na_elem_child (data, "instances"), "instance-data"); if (instances == NULL) @@ -946,7 +956,7 @@ static int cna_query_wafl (host_config_t *host) /* {{{ */ { na_elem_t *data; int status; - time_t now; + cdtime_t now; if (host == NULL) return (EINVAL); @@ -955,7 +965,7 @@ static int cna_query_wafl (host_config_t *host) /* {{{ */ if (host->cfg_wafl == NULL) return (0); - now = time (NULL); + now = cdtime (); if ((host->cfg_wafl->interval.interval + host->cfg_wafl->interval.last_read) > now) return (0); @@ -986,7 +996,7 @@ static int cna_query_wafl (host_config_t *host) /* {{{ */ static int cna_handle_disk_data (const char *hostname, /* {{{ */ cfg_disk_t *cfg_disk, na_elem_t *data) { - time_t timestamp; + cdtime_t timestamp; na_elem_t *instances; na_elem_t *instance; na_elem_iter_t instance_iter; @@ -995,7 +1005,7 @@ static int cna_handle_disk_data (const char *hostname, /* {{{ */ if ((cfg_disk == NULL) || (data == NULL)) return (EINVAL); - timestamp = (time_t) na_child_get_uint64(data, "timestamp", 0); + timestamp = cna_child_get_cdtime (data); instances = na_elem_child (data, "instances"); if (instances == NULL) @@ -1140,7 +1150,7 @@ static int cna_query_disk (host_config_t *host) /* {{{ */ { na_elem_t *data; int status; - time_t now; + cdtime_t now; if (host == NULL) return (EINVAL); @@ -1150,7 +1160,7 @@ static int cna_query_disk (host_config_t *host) /* {{{ */ if (host->cfg_disk == NULL) return (0); - now = time (NULL); + now = cdtime (); if ((host->cfg_disk->interval.interval + host->cfg_disk->interval.last_read) > now) return (0); @@ -1181,12 +1191,12 @@ static int cna_query_disk (host_config_t *host) /* {{{ */ static int cna_handle_volume_perf_data (const char *hostname, /* {{{ */ cfg_volume_perf_t *cvp, na_elem_t *data) { - time_t timestamp; + cdtime_t timestamp; na_elem_t *elem_instances; na_elem_iter_t iter_instances; na_elem_t *elem_instance; - timestamp = (time_t) na_child_get_uint64(data, "timestamp", 0); + timestamp = cna_child_get_cdtime (data); elem_instances = na_elem_child(data, "instances"); if (elem_instances == NULL) @@ -1311,7 +1321,7 @@ static int cna_query_volume_perf (host_config_t *host) /* {{{ */ { na_elem_t *data; int status; - time_t now; + cdtime_t now; if (host == NULL) return (EINVAL); @@ -1321,7 +1331,7 @@ static int cna_query_volume_perf (host_config_t *host) /* {{{ */ if (host->cfg_volume_perf == NULL) return (0); - now = time (NULL); + now = cdtime (); if ((host->cfg_volume_perf->interval.interval + host->cfg_volume_perf->interval.last_read) > now) return (0); @@ -1440,7 +1450,7 @@ static int cna_change_volume_status (const char *hostname, /* {{{ */ notification_t n; memset (&n, 0, sizeof (&n)); - n.time = time (NULL); + n.time = cdtime (); sstrncpy (n.host, hostname, sizeof (n.host)); sstrncpy (n.plugin, "netapp", sizeof (n.plugin)); sstrncpy (n.plugin_instance, v->name, sizeof (n.plugin_instance)); @@ -1675,7 +1685,7 @@ static int cna_query_volume_usage (host_config_t *host) /* {{{ */ { na_elem_t *data; int status; - time_t now; + cdtime_t now; if (host == NULL) return (EINVAL); @@ -1685,7 +1695,7 @@ static int cna_query_volume_usage (host_config_t *host) /* {{{ */ if (host->cfg_volume_usage == NULL) return (0); - now = time (NULL); + now = cdtime (); if ((host->cfg_volume_usage->interval.interval + host->cfg_volume_usage->interval.last_read) > now) return (0); @@ -1726,9 +1736,9 @@ static int cna_handle_system_data (const char *hostname, /* {{{ */ uint32_t counter_flags = 0; const char *instance; - time_t timestamp; + cdtime_t timestamp; - timestamp = (time_t) na_child_get_uint64 (data, "timestamp", 0); + timestamp = cna_child_get_cdtime (data); instances = na_elem_child(na_elem_child (data, "instances"), "instance-data"); if (instances == NULL) @@ -1835,7 +1845,7 @@ static int cna_query_system (host_config_t *host) /* {{{ */ { na_elem_t *data; int status; - time_t now; + cdtime_t now; if (host == NULL) return (EINVAL); @@ -1844,7 +1854,7 @@ static int cna_query_system (host_config_t *host) /* {{{ */ if (host->cfg_system == NULL) return (0); - now = time (NULL); + now = cdtime (); if ((host->cfg_system->interval.interval + host->cfg_system->interval.last_read) > now) return (0); @@ -1901,23 +1911,12 @@ static int cna_config_bool_to_flag (const oconfig_item_t *ci, /* {{{ */ static int cna_config_get_interval (const oconfig_item_t *ci, /* {{{ */ cna_interval_t *out_interval) { - time_t tmp; - - if ((ci == NULL) || (out_interval == NULL)) - return (EINVAL); - - if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) - { - WARNING ("netapp plugin: The `Interval' option needs exactly one numeric argument."); - return (-1); - } + cdtime_t tmp = 0; + int status; - tmp = (time_t) (ci->values[0].value.number + .5); - if (tmp < 1) - { - WARNING ("netapp plugin: The `Interval' option needs a positive integer argument."); - return (-1); - } + status = cf_util_get_cdtime (ci, &tmp); + if (status != 0) + return (status); out_interval->interval = tmp; out_interval->last_read = 0; @@ -2417,11 +2416,7 @@ static host_config_t *cna_config_host (const oconfig_item_t *ci) /* {{{ */ } else if (!strcasecmp(item->key, "Password")) { status = cf_util_get_string (item, &host->password); } else if (!strcasecmp(item->key, "Interval")) { - if (item->values_num != 1 || item->values[0].type != OCONFIG_TYPE_NUMBER || item->values[0].value.number != (int) item->values[0].value.number || item->values[0].value.number < 2) { - WARNING("netapp plugin: \"Interval\" of host %s needs exactly one integer argument.", ci->values[0].value.string); - continue; - } - host->interval = item->values[0].value.number; + status = cf_util_get_cdtime (item, &host->interval); } else if (!strcasecmp(item->key, "WAFL")) { cna_config_wafl(host, item); } else if (!strcasecmp(item->key, "Disks")) { @@ -2552,8 +2547,7 @@ static int cna_config (oconfig_item_t *ci) { /* {{{ */ ssnprintf (cb_name, sizeof (cb_name), "netapp-%s", host->name); - memset (&interval, 0, sizeof (interval)); - interval.tv_sec = host->interval; + interval = CDTIME_T_TO_TIMESPEC (host->interval); memset (&ud, 0, sizeof (ud)); ud.data = host; diff --git a/src/network.c b/src/network.c index 9ca2eeea..93d403f9 100644 --- a/src/network.c +++ b/src/network.c @@ -1377,8 +1377,8 @@ static int parse_packet (sockent_t *se, /* {{{ */ &tmp); if (status == 0) { - vl.time = (time_t) tmp; - n.time = (time_t) tmp; + vl.time = TIME_T_TO_CDTIME_T (tmp); + n.time = TIME_T_TO_CDTIME_T (tmp); } } else if (pkg_type == TYPE_INTERVAL) @@ -2583,8 +2583,9 @@ 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) vl->time)) + (uint64_t) tmp)) return (-1); vl_def->time = vl->time; } @@ -3070,12 +3071,14 @@ 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) n->time); + (uint64_t) tmp); if (status != 0) return (-1); diff --git a/src/notify_email.c b/src/notify_email.c index 62e1c486..a13b1f9e 100644 --- a/src/notify_email.c +++ b/src/notify_email.c @@ -208,6 +208,7 @@ static int notify_email_notification (const notification_t *n, { smtp_recipient_t recipient; + time_t tt; struct tm timestamp_tm; char timestamp_str[64]; @@ -227,7 +228,8 @@ static int notify_email_notification (const notification_t *n, (email_subject == NULL) ? DEFAULT_SMTP_SUBJECT : email_subject, severity, n->host); - localtime_r (&n->time, ×tamp_tm); + tt = CDTIME_T_TO_TIME_T (n->time); + localtime_r (&tt, ×tamp_tm); strftime (timestamp_str, sizeof (timestamp_str), "%Y-%m-%d %H:%M:%S", ×tamp_tm); timestamp_str[sizeof (timestamp_str) - 1] = '\0'; diff --git a/src/perl.c b/src/perl.c index afb3ba7e..c82d16b6 100644 --- a/src/perl.c +++ b/src/perl.c @@ -393,7 +393,10 @@ static int hv2value_list (pTHX_ HV *hash, value_list_t *vl) } if (NULL != (tmp = hv_fetch (hash, "time", 4, 0))) - vl->time = (time_t)SvIV (*tmp); + { + double t = SvNV (*tmp); + vl->time = DOUBLE_TO_CDTIME_T (t); + } if (NULL != (tmp = hv_fetch (hash, "interval", 8, 0))) vl->interval = SvIV (*tmp); @@ -548,9 +551,12 @@ static int hv2notification (pTHX_ HV *hash, notification_t *n) n->severity = NOTIF_FAILURE; if (NULL != (tmp = hv_fetch (hash, "time", 4, 0))) - n->time = (time_t)SvIV (*tmp); + { + double t = SvNV (*tmp); + n->time = DOUBLE_TO_CDTIME_T (t); + } else - n->time = time (NULL); + n->time = cdtime (); if (NULL != (tmp = hv_fetch (hash, "message", 7, 0))) sstrncpy (n->message, SvPV_nolen (*tmp), sizeof (n->message)); @@ -668,8 +674,11 @@ static int value_list2hv (pTHX_ value_list_t *vl, data_set_t *ds, HV *hash) return -1; if (0 != vl->time) - if (NULL == hv_store (hash, "time", 4, newSViv (vl->time), 0)) + { + double t = CDTIME_T_TO_DOUBLE (vl->time); + if (NULL == hv_store (hash, "time", 4, newSVnv (t), 0)) return -1; + } if (NULL == hv_store (hash, "interval", 8, newSViv (vl->interval), 0)) return -1; @@ -750,8 +759,11 @@ static int notification2hv (pTHX_ notification_t *n, HV *hash) return -1; if (0 != n->time) - if (NULL == hv_store (hash, "time", 4, newSViv (n->time), 0)) + { + double t = CDTIME_T_TO_DOUBLE (n->time); + if (NULL == hv_store (hash, "time", 4, newSVnv (t), 0)) return -1; + } if ('\0' != *n->message) if (NULL == hv_store (hash, "message", 7, newSVpv (n->message, 0), 0)) @@ -1102,11 +1114,15 @@ static int pplugin_call_all (pTHX_ int type, ...) XPUSHs (sv_2mortal (newRV_noinc ((SV *)notif))); } else if (PLUGIN_FLUSH == type) { + cdtime_t timeout; + /* * $_[0] = $timeout; * $_[1] = $identifier; */ - XPUSHs (sv_2mortal (newSViv (va_arg (ap, int)))); + timeout = va_arg (ap, cdtime_t); + + XPUSHs (sv_2mortal (newSVnv (CDTIME_T_TO_DOUBLE (timeout)))); XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0))); } @@ -1982,7 +1998,7 @@ static int perl_notify (const notification_t *notif, return pplugin_call_all (aTHX_ PLUGIN_NOTIF, notif); } /* static int perl_notify (const notification_t *) */ -static int perl_flush (int timeout, const char *identifier, +static int perl_flush (cdtime_t timeout, const char *identifier, user_data_t __attribute__((unused)) *user_data) { dTHX; diff --git a/src/plugin.c b/src/plugin.c index af894d54..061b78bb 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -1384,16 +1384,16 @@ int plugin_dispatch_values (value_list_t *vl) } if (vl->time == 0) - vl->time = time (NULL); + vl->time = cdtime (); if (vl->interval <= 0) vl->interval = interval_g; - DEBUG ("plugin_dispatch_values: time = %u; interval = %i; " + DEBUG ("plugin_dispatch_values: time = %.3f; interval = %i; " "host = %s; " "plugin = %s; plugin_instance = %s; " "type = %s; type_instance = %s;", - (unsigned int) vl->time, vl->interval, + CDTIME_T_TO_DOUBLE (vl->time), vl->interval, vl->host, vl->plugin, vl->plugin_instance, vl->type, vl->type_instance); @@ -1518,9 +1518,9 @@ int plugin_dispatch_notification (const notification_t *notif) /* Possible TODO: Add flap detection here */ DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; " - "time = %u; host = %s;", + "time = %.3f; host = %s;", notif->severity, notif->message, - (unsigned int) notif->time, notif->host); + CDTIME_T_TO_DOUBLE (notif->time), notif->host); /* Nobody cares for notifications */ if (list_notification == NULL) diff --git a/src/plugin.h b/src/plugin.h index 8b9449ee..8fe7fc4a 100644 --- a/src/plugin.h +++ b/src/plugin.h @@ -25,6 +25,7 @@ #include "collectd.h" #include "configfile.h" #include "meta_data.h" +#include "utils_time.h" #define PLUGIN_FLAGS_GLOBAL 0x0001 @@ -85,7 +86,7 @@ struct value_list_s { value_t *values; int values_len; - time_t time; + cdtime_t time; int interval; char host[DATA_MAX_NAME_LEN]; char plugin[DATA_MAX_NAME_LEN]; @@ -143,7 +144,7 @@ typedef struct notification_meta_s typedef struct notification_s { int severity; - time_t time; + cdtime_t time; char message[NOTIF_MAX_MSG_LEN]; char host[DATA_MAX_NAME_LEN]; char plugin[DATA_MAX_NAME_LEN]; diff --git a/src/rrdtool.c b/src/rrdtool.c index 4655b96e..9ef0fa1f 100644 --- a/src/rrdtool.c +++ b/src/rrdtool.c @@ -40,11 +40,11 @@ */ struct rrd_cache_s { - int values_num; - char **values; - time_t first_value; - time_t last_value; - int random_variation; + int values_num; + char **values; + cdtime_t first_value; + cdtime_t last_value; + int64_t random_variation; enum { FLAG_NONE = 0x00, @@ -107,10 +107,10 @@ static rrdcreate_config_t rrdcreate_config = /* XXX: If you need to lock both, cache_lock and queue_lock, at the same time, * ALWAYS lock `cache_lock' first! */ -static int cache_timeout = 0; -static int cache_flush_timeout = 0; -static int random_timeout = 1; -static time_t cache_flush_last; +static cdtime_t cache_timeout = 0; +static cdtime_t cache_flush_timeout = 0; +static cdtime_t random_timeout = 1; +static cdtime_t cache_flush_last; static c_avl_tree_t *cache = NULL; static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER; @@ -199,11 +199,13 @@ static int value_list_to_string (char *buffer, int buffer_len, { int offset; int status; + time_t tt; int i; memset (buffer, '\0', buffer_len); - status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) vl->time); + tt = CDTIME_T_TO_TIME_T (vl->time); + status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) tt); if ((status < 1) || (status >= buffer_len)) return (-1); offset = status; @@ -642,8 +644,38 @@ static int rrd_cache_flush_identifier (int timeout, const char *identifier) return (status); } /* int rrd_cache_flush_identifier */ +static int64_t rrd_get_random_variation (void) +{ + double dbl_timeout; + cdtime_t ctm_timeout; + double rand_fact; + _Bool negative; + int64_t ret; + + if (random_timeout <= 0) + return (0); + + /* This seems a bit complicated, but "random_timeout" is likely larger than + * RAND_MAX, so we can't simply use modulo here. */ + dbl_timeout = CDTIME_T_TO_DOUBLE (random_timeout); + rand_fact = ((double) random ()) + / ((double) RAND_MAX); + negative = (_Bool) (random () % 2); + + ctm_timeout = DOUBLE_TO_CDTIME_T (dbl_timeout * rand_fact); + + ret = (int64_t) ctm_timeout; + if (negative) + ret *= -1; + + DEBUG ("rrdtool plugin: random_variation = %.3f s", + (negative ? -1.0 : 1.0) * dbl_timeout * rand_fact); + + return (ret); +} /* int64_t rrd_get_random_variation */ + static int rrd_cache_insert (const char *filename, - const char *value, time_t value_time) + const char *value, cdtime_t value_time) { rrd_cache_t *rc = NULL; int new_rc = 0; @@ -664,7 +696,7 @@ static int rrd_cache_insert (const char *filename, if (rc == NULL) { - rc = (rrd_cache_t *) malloc (sizeof (rrd_cache_t)); + rc = malloc (sizeof (*rc)); if (rc == NULL) return (-1); rc->values_num = 0; @@ -679,9 +711,9 @@ static int rrd_cache_insert (const char *filename, if (rc->last_value >= value_time) { pthread_mutex_unlock (&cache_lock); - DEBUG ("rrdtool plugin: (rc->last_value = %u) >= (value_time = %u)", - (unsigned int) rc->last_value, - (unsigned int) value_time); + DEBUG ("rrdtool plugin: (rc->last_value = %"PRIu64") " + ">= (value_time = %"PRIu64")", + rc->last_value, value_time); return (-1); } @@ -738,9 +770,9 @@ static int rrd_cache_insert (const char *filename, } DEBUG ("rrdtool plugin: rrd_cache_insert: file = %s; " - "values_num = %i; age = %lu;", + "values_num = %i; age = %.3f;", filename, rc->values_num, - (unsigned long)(rc->last_value - rc->first_value)); + CDTIME_T_TO_DOUBLE (rc->last_value - rc->first_value)); if ((rc->last_value + rc->random_variation - rc->first_value) >= cache_timeout) { @@ -754,17 +786,7 @@ static int rrd_cache_insert (const char *filename, if (status == 0) rc->flags = FLAG_QUEUED; - /* Update the jitter value. Negative values are - * slightly preferred. */ - if (random_timeout > 0) - { - rc->random_variation = (rand () % (2 * random_timeout)) - - random_timeout; - } - else - { - rc->random_variation = 0; - } + rc->random_variation = rrd_get_random_variation (); } else { @@ -773,7 +795,7 @@ static int rrd_cache_insert (const char *filename, } if ((cache_timeout > 0) && - ((time (NULL) - cache_flush_last) > cache_flush_timeout)) + ((cdtime () - cache_flush_last) > cache_flush_timeout)) rrd_cache_flush (cache_flush_timeout); pthread_mutex_unlock (&cache_lock); @@ -919,7 +941,7 @@ static int rrd_config (const char *key, const char *value) { if (strcasecmp ("CacheTimeout", key) == 0) { - int tmp = atoi (value); + double tmp = atof (value); if (tmp < 0) { fprintf (stderr, "rrdtool: `CacheTimeout' must " @@ -928,7 +950,7 @@ static int rrd_config (const char *key, const char *value) "be greater than 0.\n"); return (1); } - cache_timeout = tmp; + cache_timeout = DOUBLE_TO_CDTIME_T (tmp); } else if (strcasecmp ("CacheFlush", key) == 0) { @@ -1061,10 +1083,10 @@ static int rrd_config (const char *key, const char *value) } else if (strcasecmp ("RandomTimeout", key) == 0) { - int tmp; + double tmp; - tmp = atoi (value); - if (tmp < 0) + tmp = atof (value); + if (tmp < 0.0) { fprintf (stderr, "rrdtool: `RandomTimeout' must " "be greater than or equal to zero.\n"); @@ -1073,7 +1095,7 @@ static int rrd_config (const char *key, const char *value) } else { - random_timeout = tmp; + cache_timeout = DOUBLE_TO_CDTIME_T (tmp); } } else diff --git a/src/utils_cache.c b/src/utils_cache.c index 69ea864b..c9a5f7a3 100644 --- a/src/utils_cache.c +++ b/src/utils_cache.c @@ -38,10 +38,10 @@ typedef struct cache_entry_s value_t *values_raw; /* Time contained in the package * (for calculating rates) */ - time_t last_time; + cdtime_t last_time; /* Time according to the local clock * (for purging old entries) */ - time_t last_update; + cdtime_t last_update; /* Interval in which the data is collected * (for purding old entries) */ int interval; @@ -164,7 +164,7 @@ static int uc_send_notification (const char *name) * acquiring the lock takes and we will use this time later to decide * whether or not the state is OKAY. */ - n.time = time (NULL); + n.time = cdtime (); status = c_avl_get (cache_tree, name, (void *) &ce); if (status != 0) @@ -184,8 +184,8 @@ static int uc_send_notification (const char *name) } ssnprintf (n.message, sizeof (n.message), - "%s has not been updated for %i seconds.", name, - (int) (n.time - ce->last_update)); + "%s has not been updated for %.3f seconds.", name, + CDTIME_T_TO_DOUBLE (n.time - ce->last_update)); pthread_mutex_unlock (&cache_lock); @@ -274,7 +274,7 @@ static int uc_insert (const data_set_t *ds, const value_list_t *vl, uc_check_range (ds, ce); ce->last_time = vl->time; - ce->last_update = time (NULL); + ce->last_update = cdtime (); ce->interval = vl->interval; ce->state = STATE_OKAY; @@ -300,7 +300,7 @@ int uc_init (void) int uc_check_timeout (void) { - time_t now; + cdtime_t now; cache_entry_t *ce; char **keys = NULL; @@ -312,7 +312,7 @@ int uc_check_timeout (void) pthread_mutex_lock (&cache_lock); - now = time (NULL); + now = cdtime (); /* Build a list of entries to be flushed */ iter = c_avl_get_iterator (cache_tree); @@ -448,7 +448,7 @@ int uc_update (const data_set_t *ds, const value_list_t *vl) char name[6 * DATA_MAX_NAME_LEN]; cache_entry_t *ce = NULL; int send_okay_notification = 0; - time_t update_delay = 0; + cdtime_t update_delay = 0; notification_t n; int status; int i; @@ -475,9 +475,11 @@ int uc_update (const data_set_t *ds, const value_list_t *vl) if (ce->last_time >= vl->time) { pthread_mutex_unlock (&cache_lock); - NOTICE ("uc_update: Value too old: name = %s; value time = %u; " - "last cache update = %u;", - name, (unsigned int) vl->time, (unsigned int) ce->last_time); + NOTICE ("uc_update: Value too old: name = %s; value time = %.3f; " + "last cache update = %.3f;", + name, + CDTIME_T_TO_DOUBLE (vl->time), + CDTIME_T_TO_DOUBLE (ce->last_time)); return (-1); } @@ -487,7 +489,7 @@ int uc_update (const data_set_t *ds, const value_list_t *vl) { send_okay_notification = 1; ce->state = STATE_OKAY; - update_delay = time (NULL) - ce->last_update; + update_delay = cdtime () - ce->last_update; } for (i = 0; i < ds->ds_num; i++) @@ -514,7 +516,7 @@ int uc_update (const data_set_t *ds, const value_list_t *vl) } ce->values_gauge[i] = ((double) diff) - / ((double) (vl->time - ce->last_time)); + / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time)); ce->values_raw[i].counter = vl->values[i].counter; } break; @@ -531,14 +533,14 @@ int uc_update (const data_set_t *ds, const value_list_t *vl) diff = vl->values[i].derive - ce->values_raw[i].derive; ce->values_gauge[i] = ((double) diff) - / ((double) (vl->time - ce->last_time)); + / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time)); ce->values_raw[i].derive = vl->values[i].derive; } break; case DS_TYPE_ABSOLUTE: ce->values_gauge[i] = ((double) vl->values[i].absolute) - / ((double) (vl->time - ce->last_time)); + / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time)); ce->values_raw[i].absolute = vl->values[i].absolute; break; @@ -571,7 +573,7 @@ int uc_update (const data_set_t *ds, const value_list_t *vl) uc_check_range (ds, ce); ce->last_time = vl->time; - ce->last_update = time (NULL); + ce->last_update = cdtime (); ce->interval = vl->interval; pthread_mutex_unlock (&cache_lock); @@ -682,14 +684,14 @@ gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl) return (ret); } /* gauge_t *uc_get_rate */ -int uc_get_names (char ***ret_names, time_t **ret_times, size_t *ret_number) +int uc_get_names (char ***ret_names, cdtime_t **ret_times, size_t *ret_number) { c_avl_iterator_t *iter; char *key; cache_entry_t *value; char **names = NULL; - time_t *times = NULL; + cdtime_t *times = NULL; size_t number = 0; int status = 0; @@ -710,9 +712,9 @@ int uc_get_names (char ***ret_names, time_t **ret_times, size_t *ret_number) if (ret_times != NULL) { - time_t *tmp_times; + cdtime_t *tmp_times; - tmp_times = (time_t *) realloc (times, sizeof (time_t) * (number + 1)); + tmp_times = (cdtime_t *) realloc (times, sizeof (cdtime_t) * (number + 1)); if (tmp_times == NULL) { status = -1; diff --git a/src/utils_cache.h b/src/utils_cache.h index f8059eca..87f93c0e 100644 --- a/src/utils_cache.h +++ b/src/utils_cache.h @@ -35,7 +35,7 @@ int uc_update (const data_set_t *ds, const value_list_t *vl); int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num); gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl); -int uc_get_names (char ***ret_names, time_t **ret_times, size_t *ret_number); +int uc_get_names (char ***ret_names, cdtime_t **ret_times, size_t *ret_number); int uc_get_state (const data_set_t *ds, const value_list_t *vl); int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state); diff --git a/src/utils_cmd_listval.c b/src/utils_cmd_listval.c index 4ca9646d..ef66af56 100644 --- a/src/utils_cmd_listval.c +++ b/src/utils_cmd_listval.c @@ -50,7 +50,7 @@ int handle_listval (FILE *fh, char *buffer) { char *command; char **names = NULL; - time_t *times = NULL; + cdtime_t *times = NULL; size_t number = 0; size_t i; int status; @@ -90,7 +90,8 @@ int handle_listval (FILE *fh, char *buffer) print_to_socket (fh, "%i Value%s found\n", (int) number, (number == 1) ? "" : "s"); for (i = 0; i < number; i++) - print_to_socket (fh, "%u %s\n", (unsigned int) times[i], names[i]); + print_to_socket (fh, "%.3f %s\n", CDTIME_T_TO_DOUBLE (times[i]), + names[i]); free_everything_and_return (0); } /* int handle_listval */ -- 2.11.0