From 97a0180ffe26c193c835e7906ea75c3b47fe3944 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Tue, 7 Sep 2010 10:10:46 +0200 Subject: [PATCH] src/dp_rrdtool.c: Implement "get_ident_data" callback. --- src/data_provider.h | 67 +++++++++++++++++++++++++++++++++++++++ src/dp_rrdtool.c | 76 +++++++++++++++++++++++++++++++++++++++------ 2 files changed, 134 insertions(+), 9 deletions(-) create mode 100644 src/data_provider.h diff --git a/src/data_provider.h b/src/data_provider.h new file mode 100644 index 0000000..a5e9c76 --- /dev/null +++ b/src/data_provider.h @@ -0,0 +1,67 @@ +/** + * collection4 - data_provider.h + * Copyright (C) 2010 Florian octo Forster + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA + * + * Authors: + * Florian octo Forster + **/ + +#ifndef DATA_PROVIDER_H +#define DATA_PROVIDER_H 1 + +#include "graph_types.h" + +#include + +typedef struct timespec dp_time_t; + +struct dp_data_point_s +{ + dp_time_t time; + double value; +}; +typedef struct dp_data_point_s dp_data_point_t; + +/* Callback passed to the "get_idents" function. */ +typedef int (*dp_get_idents_callback) (const graph_ident_t *, void *); + +/* Callback passed to the "get_ident_ds_names" function. */ +typedef int (*dp_list_get_ident_ds_names_callback) (const graph_ident_t *, + const char *ds_name, void *); + +/* Callback passed to the "get_ident_data" function. */ +typedef int (*dp_get_ident_data_callback) (graph_ident_t *, const char *ds_name, + const dp_data_point_t *, void *); + +struct data_provider_s +{ + int (*get_idents) (void *priv, dp_get_idents_callback, void *); + int (*get_ident_ds_names) (void *priv, graph_ident_t *, + dp_list_get_ident_ds_names_callback, void *); + int (*get_ident_data) (void *priv, + graph_ident_t *, const char *ds_name, + dp_time_t begin, dp_time_t end, + dp_get_ident_data_callback, void *); + /* Optional method: Prints graph to STDOUT, including HTTP header. */ + int (*print_graph) (void *priv, graph_config_t *cfg, graph_instance_t *inst); + void *private_data; +}; +typedef struct data_provider_s data_provider_t; + +#endif /* DATA_PROVIDER_H */ +/* vim: set sw=2 sts=2 et fdm=marker : */ diff --git a/src/dp_rrdtool.c b/src/dp_rrdtool.c index bbfe59e..214603d 100644 --- a/src/dp_rrdtool.c +++ b/src/dp_rrdtool.c @@ -134,7 +134,7 @@ static int scan_host_cb (const char *base_dir, return (fs_foreach_dir (abs_dir, scan_plugin_cb, data)); } /* }}} int scan_host_cb */ -static int ident_to_rrdfile (const graph_ident_t *ident, +static int ident_to_rrdfile (const graph_ident_t *ident, /* {{{ */ dp_rrdtool_t *config, char *buffer, size_t buffer_size) { @@ -271,16 +271,74 @@ static int get_ident_data (void *priv, { /* {{{ */ dp_rrdtool_t *config = priv; - ident = NULL; - ds_name = NULL; - begin.tv_sec = 0; - end.tv_sec = 0; - cb = NULL; - ud = NULL; + char filename[PATH_MAX + 1]; + const char *cf = "AVERAGE"; /* FIXME */ + time_t rrd_start; + time_t rrd_end; + unsigned long step; + unsigned long ds_count; + char **ds_namv; + rrd_value_t *data; + int status; + + unsigned long ds_index; + unsigned long data_index; + unsigned long data_length; + + status = ident_to_rrdfile (ident, config, filename, sizeof (filename)); + if (status != 0) + return (status); - config = NULL; + rrd_start = (time_t) begin.tv_sec; + rrd_end = (time_t) end.tv_sec; + step = 0; + ds_count = 0; + ds_namv = NULL; + data = NULL; + + status = rrd_fetch_r (filename, cf, + &rrd_start, &rrd_end, + &step, &ds_count, &ds_namv, + &data); + if (status != 0) + return (status); + +#define BAIL_OUT(ret_status) do { \ + unsigned long i; \ + for (i = 0; i < ds_count; i++) \ + free (ds_namv[i]); \ + free (ds_namv); \ + free (data); \ + return (ret_status); \ +} while (0) + + for (ds_index = 0; ds_index < ds_count; ds_index++) + if (strcmp (ds_name, ds_namv[ds_index]) == 0) + break; + + if (ds_index >= ds_count) + BAIL_OUT (ENOENT); + + /* Number of data points returned. */ + data_length = (rrd_end - rrd_start) / step; + + for (data_index = 0; data_index < data_length; data_index++) + { + dp_data_point_t dp; + unsigned long index = (ds_count * data_index) + ds_index; + + memset (&dp, 0, sizeof (dp)); + dp.time.tv_sec = rrd_start + (data_index * step); + dp.time.tv_nsec = 0; + dp.value = (double) data[index]; + + status = (*cb) (ident, ds_name, &dp, ud); + if (status != 0) + BAIL_OUT (status); + } - return (EINVAL); + BAIL_OUT (0); +#undef BAIL_OUT } /* }}} int get_ident_data */ static int print_graph (void *priv, -- 2.30.2