Several fixes to RRD API.
TODO: fix test building
TODO: improve update of rrd files
TODO: rework shared libraries again
return g_quark_from_static_string ("rrd-error");
}
+/**
+ * Convert rrd dst type from string to numeric value
+ */
+enum rrd_dst_type
+rrd_dst_from_string (const gchar *str)
+{
+ if (g_ascii_strcasecmp (str, "counter") == 0) {
+ return RRD_DST_COUNTER;
+ }
+ else if (g_ascii_strcasecmp (str, "absolute") == 0) {
+ return RRD_DST_ABSOLUTE;
+ }
+ else if (g_ascii_strcasecmp (str, "gauge") == 0) {
+ return RRD_DST_GAUGE;
+ }
+ else if (g_ascii_strcasecmp (str, "cdef") == 0) {
+ return RRD_DST_CDEF;
+ }
+ else if (g_ascii_strcasecmp (str, "derive") == 0) {
+ return RRD_DST_DERIVE;
+ }
+ return -1;
+}
+
+/**
+ * Convert numeric presentation of dst to string
+ */
+const gchar*
+rrd_dst_to_string (enum rrd_dst_type type)
+{
+ switch (type) {
+ case RRD_DST_COUNTER:
+ return "COUNTER";
+ case RRD_DST_ABSOLUTE:
+ return "ABSOLUTE";
+ case RRD_DST_GAUGE:
+ return "GAUGE";
+ case RRD_DST_CDEF:
+ return "CDEF";
+ case RRD_DST_DERIVE:
+ return "DERIVE";
+ default:
+ return "U";
+ }
+
+ return "U";
+}
+
+/**
+ * Convert rrd consolidation function type from string to numeric value
+ */
+enum rrd_cf_type
+rrd_cf_from_string (const gchar *str)
+{
+ if (g_ascii_strcasecmp (str, "average") == 0) {
+ return RRD_CF_AVERAGE;
+ }
+ else if (g_ascii_strcasecmp (str, "minimum") == 0) {
+ return RRD_CF_MINIMUM;
+ }
+ else if (g_ascii_strcasecmp (str, "maximum") == 0) {
+ return RRD_CF_MAXIMUM;
+ }
+ else if (g_ascii_strcasecmp (str, "last") == 0) {
+ return RRD_CF_LAST;
+ }
+ /* XXX: add other CF functions supported by rrd */
+ return -1;
+}
+
+/**
+ * Convert numeric presentation of cf to string
+ */
+const gchar*
+rrd_cf_to_string (enum rrd_cf_type type)
+{
+ switch (type) {
+ case RRD_CF_AVERAGE:
+ return "AVERAGE";
+ case RRD_CF_MINIMUM:
+ return "MINIMUM";
+ case RRD_CF_MAXIMUM:
+ return "MAXIMUM";
+ case RRD_CF_LAST:
+ return "LAST";
+ default:
+ return "U";
+ }
+
+ /* XXX: add other CF functions supported by rrd */
+
+ return "U";
+}
+
/**
* Check rrd file for correctness (size, cookies, etc)
*/
close (fd);
return FALSE;
}
- head_size += rra.row_cnt * head.ds_cnt;
+ head_size += rra.row_cnt * head.ds_cnt * sizeof (gdouble);
}
if (st.st_size != head_size) {
rspamd_rrd_add_ds (struct rspamd_rrd_file *file, GArray *ds, GError **err)
{
- if (file == NULL || file->stat_head->ds_cnt != ds->len * sizeof (struct rrd_ds_def)) {
+ if (file == NULL || file->stat_head->ds_cnt * sizeof (struct rrd_ds_def) != ds->len) {
g_set_error (err, rrd_error_quark (), EINVAL, "rrd add ds failed: wrong arguments");
return FALSE;
}
gboolean
rspamd_rrd_add_rra (struct rspamd_rrd_file *file, GArray *rra, GError **err)
{
- if (file == NULL || file->stat_head->rra_cnt != rra->len * sizeof (struct rrd_rra_def)) {
+ if (file == NULL || file->stat_head->rra_cnt * sizeof (struct rrd_rra_def) != rra->len) {
g_set_error (err, rrd_error_quark (), EINVAL, "rrd add rra failed: wrong arguments");
return FALSE;
}
rspamd_rrd_finalize (struct rspamd_rrd_file *file, GError **err)
{
gint fd;
- guint i, count = 0;
+ guint i;
+ gint count = 0;
gdouble vbuf[1024];
struct stat st;
while (count > 0) {
/* Write values in buffered matter */
- if (write (fd, vbuf, MIN (G_N_ELEMENTS (vbuf), count) * sizeof (gdouble)) == -1) {
+ if (write (fd, vbuf, MIN ((gint)G_N_ELEMENTS (vbuf), count) * sizeof (gdouble)) == -1) {
g_set_error (err, rrd_error_quark (), errno, "rrd write error: %s", strerror (errno));
close (fd);
return FALSE;
/* Adjust pointers */
rspamd_rrd_adjust_pointers (file, TRUE);
+ file->finalized = TRUE;
+
return TRUE;
}
gdouble *row;
guint i;
- if (file == NULL || file->stat_head->ds_cnt != points->len * sizeof (gdouble) || rra_idx >= file->stat_head->rra_cnt) {
+ if (file == NULL || file->stat_head->ds_cnt * sizeof (gdouble) != points->len || rra_idx >= file->stat_head->rra_cnt) {
g_set_error (err, rrd_error_quark (), EINVAL, "rrd add points failed: wrong arguments");
return FALSE;
}
}
/* Write data */
- memcpy (row, points, points->len);
+ memcpy (row, points->data, points->len);
return TRUE;
}
*/
gint rspamd_rrd_close (struct rspamd_rrd_file* file);
+/*
+ * Conversion functions
+ */
+
+/**
+ * Convert rrd dst type from string to numeric value
+ */
+enum rrd_dst_type rrd_dst_from_string (const gchar *str);
+/**
+ * Convert numeric presentation of dst to string
+ */
+const gchar* rrd_dst_to_string (enum rrd_dst_type type);
+/**
+ * Convert rrd consolidation function type from string to numeric value
+ */
+enum rrd_cf_type rrd_cf_from_string (const gchar *str);
+/**
+ * Convert numeric presentation of cf to string
+ */
+const gchar* rrd_cf_to_string (enum rrd_cf_type type);
+
#endif /* RRD_H_ */
rspamd_url_test.c
rspamd_dns_test.c
rspamd_async_test.c
- rspamd_dkim_test.c)
+ rspamd_dkim_test.c
+ rspamd_rrd_test.c)
ADD_EXECUTABLE(rspamd-test EXCLUDE_FROM_ALL ${TESTSRC})
SET_TARGET_PROPERTIES(rspamd-test PROPERTIES LINKER_LANGUAGE C)
SET_TARGET_PROPERTIES(rspamd-test PROPERTIES COMPILE_FLAGS "-DRSPAMD_TEST")
-TARGET_LINK_LIBRARIES(rspamd-test rspamd_lua)
-TARGET_LINK_LIBRARIES(rspamd-test rspamdserver)
+
TARGET_LINK_LIBRARIES(rspamd-test event)
TARGET_LINK_LIBRARIES(rspamd-test m)
TARGET_LINK_LIBRARIES(rspamd-test ${GLIB2_LIBRARIES})
TARGET_LINK_LIBRARIES(rspamd-test ${CMAKE_REQUIRED_LIBRARIES})
+TARGET_LINK_LIBRARIES(rspamd-test rspamd-mime)
+TARGET_LINK_LIBRARIES(rspamd-test rspamd-server)
+TARGET_LINK_LIBRARIES(rspamd-test rspamd-util)
+TARGET_LINK_LIBRARIES(rspamd-test hiredis)
IF(HAVE_LIBEVENT2)
TARGET_LINK_LIBRARIES(rspamd-test event_pthreads)
ENDIF(HAVE_LIBEVENT2)
--- /dev/null
+/* Copyright (c) 2012, Vsevolod Stakhov
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "tests.h"
+#include "rrd.h"
+#include "main.h"
+
+void
+rspamd_rrd_test_func ()
+{
+ gchar tmpfile[PATH_MAX];
+ struct rrd_rra_def rra;
+ struct rrd_ds_def ds;
+ GArray ar;
+ struct rspamd_rrd_file *rrd;
+ gint fd, i;
+ gdouble t;
+
+ rspamd_snprintf (tmpfile, sizeof (tmpfile), "/tmp/rspamd_rrd.rrd");
+
+ /* Create sample rrd */
+ g_assert ((rrd = rspamd_rrd_create (tmpfile, 1, 1, 5, NULL)) != NULL);
+ /* Add RRA */
+ rspamd_strlcpy (rra.cf_nam, rrd_cf_to_string (RRD_CF_AVERAGE), sizeof (rra.cf_nam));
+ rra.pdp_cnt = 1;
+ rra.row_cnt = 100;
+ ar.data = &rra;
+ ar.len = sizeof (rra);
+ g_assert (rspamd_rrd_add_rra (rrd, &ar, NULL));
+ /* Add DS */
+ rspamd_strlcpy (ds.dst, rrd_dst_to_string (RRD_DST_ABSOLUTE), sizeof (ds.dst));
+ rspamd_strlcpy (ds.ds_nam, "test", sizeof (ds.ds_nam));
+ ar.data = &ds;
+ ar.len = sizeof (ds);
+ g_assert (rspamd_rrd_add_ds (rrd, &ar, NULL));
+ /* Finalize */
+ g_assert (rspamd_rrd_finalize (rrd, NULL));
+ /* Close */
+ rspamd_rrd_close (rrd);
+
+ /* Reopen */
+ g_assert ((rrd = rspamd_rrd_open (tmpfile, NULL)) != NULL);
+
+ /* Add some points */
+ for (i = 0; i < 200; i ++) {
+ t = i;
+ ar.data = &t;
+ ar.len = sizeof (gdouble);
+ g_assert (rspamd_rrd_add_record (rrd, 0, &ar, NULL));
+ }
+
+ /* Finish */
+ rspamd_rrd_close (rrd);
+ /* unlink (tmpfile); */
+}
p = memory_pool_new (memory_pool_get_size ());
umask (S_IWGRP | S_IWOTH);
- pool = statfile_pool_new (p, 10 * 1024 * 1024);
+ pool = statfile_pool_new (p, 10 * 1024 * 1024, TRUE);
now = time (NULL);
/* Fill random array */
g_test_add_func ("/rspamd/dns", rspamd_dns_test_func);
g_test_add_func ("/rspamd/aio", rspamd_async_test_func);
g_test_add_func ("/rspamd/dkim", rspamd_dkim_test_func);
+ g_test_add_func ("/rspamd/rrd", rspamd_rrd_test_func);
g_test_run ();
*/
/* URL parser test */
-void rspamd_url_test_func ();
+void rspamd_url_test_func (void);
/* Memcached library test */
-void rspamd_memcached_test_func ();
+void rspamd_memcached_test_func (void);
/* Memory pools */
-void rspamd_mem_pool_test_func ();
+void rspamd_mem_pool_test_func (void);
/* Expressions */
-void rspamd_expression_test_func ();
+void rspamd_expression_test_func (void);
/* Fuzzy hashes */
-void rspamd_fuzzy_test_func ();
+void rspamd_fuzzy_test_func (void);
/* Stat file */
-void rspamd_statfile_test_func ();
+void rspamd_statfile_test_func (void);
/* DNS resolving */
-void rspamd_dns_test_func ();
+void rspamd_dns_test_func (void);
/* Async IO */
-void rspamd_async_test_func ();
+void rspamd_async_test_func (void);
/* DKIM test */
-void rspamd_dkim_test_func ();
+void rspamd_dkim_test_func (void);
+
+/* RRD test */
+void rspamd_rrd_test_func (void);
#endif