Skip to content

Commit

Permalink
Merge branch 'rrd'
Browse files Browse the repository at this point in the history
  • Loading branch information
adh committed Oct 14, 2012
2 parents 4e0aef9 + 66f05c8 commit 7a61f5f
Show file tree
Hide file tree
Showing 3 changed files with 250 additions and 2 deletions.
15 changes: 13 additions & 2 deletions Makefile.am
Expand Up @@ -112,6 +112,12 @@ curl_libs=
curl_modules=
endif

if RRD
rrd_modules=rrd.la
else
rrd_modules=
endif


if WIN32
nonwin32_libs=
Expand Down Expand Up @@ -214,7 +220,8 @@ dfschlibexec_LTLIBRARIES = threads.la \
$(tt_modules)\
$(gd_modules)\
$(ffi_modules)\
$(curl_modules)
$(curl_modules)\
$(rrd_modules)


dfschlibscm_DATA = lib-scm/stream-functions.scm \
Expand Down Expand Up @@ -656,6 +663,10 @@ curl_la_SOURCES = lib/curl_mod.c
curl_la_LIBADD = libdfsch-curl.la libdfsch.la
curl_la_LDFLAGS = -module -shrext .dsl -avoid-version -no-undefined

rrd_la_SOURCES = lib/rrd_mod.c
rrd_la_LIBADD = libdfsch.la
rrd_la_LDFLAGS = -module -shrext .dsl -avoid-version -no-undefined -lrrd


if WIN32
gui_flags = -mwindows
Expand All @@ -680,7 +691,7 @@ version.h: $(vm_SOURCES) $(stdlib_sources) $(syslib_sources)

.PHONY: documentation

MODULES = cmdopts crypto curl csv extref fastlz ffi gcollect gd http inet \
MODULES = cmdopts crypto curl rrd csv extref fastlz ffi gcollect gd http inet \
ini-file \
json markdown minizip os pcre posix-regex process shtml socket-port \
sxml threads tokyo-cabinet tokyo-tyrant unix xml zlib \
Expand Down
23 changes: 23 additions & 0 deletions configure.ac
Expand Up @@ -364,6 +364,28 @@ fi
AM_CONDITIONAL(CURL, test x"$enable_curl" == x"yes")


enable_rrd=yes

AC_ARG_ENABLE([rrd],
AC_HELP_STRING([--disable-rrd], [Disable rrd module]))
AC_ARG_WITH([rrd],
AC_HELP_STRING([--with-librrd=PATH], [Path to librrd installation]),
CPPFLAGS="$CPPFLAGS -I${withval}/include -I${withval}"
LDFLAGS="$LDFLAGS -L${withval}/lib -L${withval}")

if test x$"$enable_rrd" == x"yes"; then
AC_CHECK_HEADERS(rrd.h, [have_rrd="1"], [have_rrd="0"])
AC_CHECK_LIB(rrd, rrd_create,
[dummy_rrd=], [have_rrd="0"])

if test x"$have_rrd" == x"0" ; then
enable_rrd=no
fi
fi

AM_CONDITIONAL(RRD, test x"$enable_rrd" == x"yes")




AC_SYS_LARGEFILE
Expand Down Expand Up @@ -415,6 +437,7 @@ tokyo-tyrant ....... : $enable_tokyo_tyrant
gd ................. : $enable_gd
FFI ................ : $enable_ffi
CURL ............... : $enable_curl
RRD ................ : $enable_rrd


EOF
214 changes: 214 additions & 0 deletions lib/rrd_mod.c
@@ -0,0 +1,214 @@
#include <pthread.h>
#include <rrd.h>
#include <dfsch/dfsch.h>
#include <dfsch/load.h>
#include <dfsch/hash.h>
#include <dfsch/number.h>
#include <dfsch/strings.h>
#include <dfsch/util.h>
#include <dfsch/conditions.h>

static char* convert_arg(dfsch_object_t* obj){
if (dfsch_keyword_p(obj)){
char* str = dfsch_symbol(obj);
if (strlen(str) == 1){
return dfsch_saprintf("-%s", str);
} else {
return dfsch_saprintf("--%s", str);
}
} else if (dfsch_string_p(obj)) {
return dfsch_string_to_cstr(obj);
} else {
return dfsch_object_2_string(obj, 10, 1);
}
}

static void build_args(dfsch_object_t* list, int* pargc, char*** pargv){
int alloc = 16;
char** argv = GC_MALLOC(sizeof(char*) * alloc);
int argc = 0;

while (DFSCH_PAIR_P(list)){
if (alloc <= argc){
alloc *= 2;
argv = GC_REALLOC(argv, sizeof(char*) * alloc);
}

argv[argc] = convert_arg(DFSCH_FAST_CAR(list));

argc++;
list = DFSCH_FAST_CDR(list);
}
*pargc = argc;
*pargv = argv;
}

static dfsch_object_t* convert_info(rrd_info_t * data){
dfsch_object_t* res = dfsch_make_idhash();
while (data) {
dfsch_object_t* val = NULL;

switch (data->type) {
case RD_I_VAL:
val = isnan(data->value.u_val)
? NULL
: dfsch_make_number_from_double(data->value.u_val);
break;
case RD_I_CNT:
val = dfsch_make_number_from_uint64(data->value.u_cnt);
break;
case RD_I_INT:
val = dfsch_make_number_from_long(data->value.u_int);
break;
case RD_I_STR:
val = dfsch_make_string_cstr(data->value.u_str);
break;
case RD_I_BLO:
val = dfsch_make_byte_vector((char *) data->value.u_blo.ptr,
data->value.u_blo.size);
break;
}

dfsch_idhash_set((dfsch_hash_t*)res, dfsch_make_keyword(data->key), val);

data = data->next;
}
return res;
}

static pthread_mutex_t rrd_lock = PTHREAD_MUTEX_INITIALIZER;
#define RRD_ERROR_TYPE (&rrd_error_type)
static dfsch_type_t rrd_error_type =
DFSCH_CONDITION_TYPE_INIT(DFSCH_RUNTIME_ERROR_TYPE, "rrd-error");

static void rrd_error(char* fun){
dfsch_object_t* c = dfsch_make_condition(RRD_ERROR_TYPE);
dfsch_condition_put_field_cstr(c, "message",
dfsch_make_string_cstr(rrd_get_error()));
dfsch_condition_put_field_cstr(c, "function",
dfsch_make_string_cstr(fun));
pthread_mutex_unlock(&rrd_lock);
dfsch_signal(c);
}

DFSCH_DEFINE_PRIMITIVE(create,
"Create new RRD file"){
int argc;
char**argv;
build_args(args, &argc, &argv);

pthread_mutex_lock(&rrd_lock);
if (rrd_create(argc, argv) == -1){
rrd_error("create");
}
pthread_mutex_unlock(&rrd_lock);

return NULL;
}

DFSCH_DEFINE_PRIMITIVE(update,
"Write new values into RRD file"){
int argc;
char**argv;
build_args(args, &argc, &argv);

pthread_mutex_lock(&rrd_lock);
if (rrd_update(argc, argv) == -1){
rrd_error("update");
}
pthread_mutex_unlock(&rrd_lock);

return NULL;
}

DFSCH_DEFINE_PRIMITIVE(tune,
"Modify parameters of RRD file"){
int argc;
char**argv;
build_args(args, &argc, &argv);

pthread_mutex_lock(&rrd_lock);
if (rrd_tune(argc, argv) == -1){
rrd_error("tune");
}
pthread_mutex_unlock(&rrd_lock);

return NULL;
}

DFSCH_DEFINE_PRIMITIVE(resize,
"Resize an RRD file"){
int argc;
char**argv;
build_args(args, &argc, &argv);

pthread_mutex_lock(&rrd_lock);
if (rrd_resize(argc, argv) == -1){
rrd_error("resize");
}
pthread_mutex_unlock(&rrd_lock);

return NULL;
}

DFSCH_DEFINE_PRIMITIVE(graph,
"Draw graph from data in RRD file(s)"){
int argc;
char**argv;
rrd_info_t* info;
dfsch_object_t* res;
build_args(args, &argc, &argv);

pthread_mutex_lock(&rrd_lock);
if ((info = rrd_graph_v(argc, argv)) == NULL){
rrd_error("graph");
}
res = convert_info(info);
rrd_info_free(info);
pthread_mutex_unlock(&rrd_lock);

return res;
}

DFSCH_DEFINE_PRIMITIVE(info,
"Draw graph from data in RRD file(s)"){
int argc;
char**argv;
rrd_info_t* info;
dfsch_object_t* res;
build_args(args, &argc, &argv);

pthread_mutex_lock(&rrd_lock);
if ((info = rrd_info(argc, argv)) == NULL){
rrd_error("info");
}
res = convert_info(info);
rrd_info_free(info);
pthread_mutex_unlock(&rrd_lock);

return res;
}

void dfsch_module_rrd_register(dfsch_object_t* env){
dfsch_object_t* rrd = dfsch_make_package("rrd",
"Native library based interface to RRDtool, "
"see original RRDtool documentation for details");
dfsch_provide(env, "rrd");

dfsch_defcanon_pkgcstr(env, rrd, "<error>", RRD_ERROR_TYPE);

dfsch_defcanon_pkgcstr(env, rrd, "create",
DFSCH_PRIMITIVE_REF(create));
dfsch_defcanon_pkgcstr(env, rrd, "update",
DFSCH_PRIMITIVE_REF(update));
dfsch_defcanon_pkgcstr(env, rrd, "tune",
DFSCH_PRIMITIVE_REF(tune));
dfsch_defcanon_pkgcstr(env, rrd, "resize",
DFSCH_PRIMITIVE_REF(resize));
dfsch_defcanon_pkgcstr(env, rrd, "graph",
DFSCH_PRIMITIVE_REF(graph));
dfsch_defcanon_pkgcstr(env, rrd, "info",
DFSCH_PRIMITIVE_REF(info));


}

0 comments on commit 7a61f5f

Please sign in to comment.