From f3fc23c1262970dab2779d2a6e5dbc1cc5e89def Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Wed, 8 Sep 2010 10:56:33 +0200 Subject: [PATCH] graph_def_json action: Add action to request graph defs as JSON. --- src/Makefile.am | 1 + src/action_graph_def_json.c | 104 ++++++++++++++++++++++++++++++++++++++++++++ src/action_graph_def_json.h | 30 +++++++++++++ src/main.c | 2 + 4 files changed, 137 insertions(+) create mode 100644 src/action_graph_def_json.c create mode 100644 src/action_graph_def_json.h diff --git a/src/Makefile.am b/src/Makefile.am index 741f7e5..2923d04 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -16,6 +16,7 @@ bin_PROGRAMS = collection.fcgi collection_fcgi_SOURCES = main.c \ oconfig.c oconfig.h aux_types.h scanner.l parser.y \ action_graph.c action_graph.h \ + action_graph_def_json.c action_graph_def_json.h \ action_list_graphs.c action_list_graphs.h \ action_list_hosts.c action_list_hosts.h \ action_search.c action_search.h \ diff --git a/src/action_graph_def_json.c b/src/action_graph_def_json.c new file mode 100644 index 0000000..ea30ad9 --- /dev/null +++ b/src/action_graph_def_json.c @@ -0,0 +1,104 @@ +/** + * collection4 - action_graph_def_json.c + * 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 + **/ + +#include +#include +#include +#include +#include +#include +#include + +#include "action_graph_def_json.h" +#include "common.h" +#include "graph.h" +#include "graph_def.h" +#include "graph_list.h" +#include "utils_cgi.h" + +#include +#include + +/* Expire data after one day. */ +#define EXPIRES_SECS 86400 + +static void write_callback (__attribute__((unused)) void *ctx, /* {{{ */ + const char *str, unsigned int len) +{ + fwrite ((void *) str, /* size = */ len, /* nmemb = */ 1, stdout); +} /* }}} void write_callback */ + +int action_graph_def_json (void) /* {{{ */ +{ + graph_config_t *cfg; + graph_def_t *def; + + yajl_gen_config handler_config; + yajl_gen handler; + + time_t now; + char time_buffer[128]; + int status; + + cfg = gl_graph_get_selected (); + if (cfg == NULL) + return (ENOMEM); + + def = graph_get_defs (cfg); + if (def == NULL) + return (EINVAL); + + memset (&handler_config, 0, sizeof (handler_config)); + handler_config.beautify = 1; + handler_config.indentString = " "; + + handler = yajl_gen_alloc2 (write_callback, + &handler_config, + /* alloc functions = */ NULL, + /* context = */ NULL); + if (handler == NULL) + { + graph_destroy (cfg); + return (-1); + } + + printf ("Content-Type: application/json\n"); + + now = time (NULL); + status = time_to_rfc1123 (now + EXPIRES_SECS, time_buffer, sizeof (time_buffer)); + if (status == 0) + printf ("Expires: %s\n" + "Cache-Control: public\n", + time_buffer); + printf ("\n"); + + status = def_to_json (def, handler); + + graph_destroy (cfg); + def = NULL; + yajl_gen_free (handler); + + return (status); +} /* }}} int action_graph_def_json */ + +/* vim: set sw=2 sts=2 et fdm=marker : */ diff --git a/src/action_graph_def_json.h b/src/action_graph_def_json.h new file mode 100644 index 0000000..b56ee90 --- /dev/null +++ b/src/action_graph_def_json.h @@ -0,0 +1,30 @@ +/** + * collection4 - action_graph_def_json.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 ACTION_GRAPH_DEF_JSON_H +#define ACTION_GRAPH_DEF_JSON_H 1 + +int action_graph_def_json (void); + +#endif /* ACTION_GRAPH_DEF_JSON_H */ +/* vim: set sw=2 sts=2 et fdm=marker : */ diff --git a/src/main.c b/src/main.c index 6d8a025..b370e4c 100644 --- a/src/main.c +++ b/src/main.c @@ -37,6 +37,7 @@ #include "utils_cgi.h" #include "action_graph.h" +#include "action_graph_def_json.h" #include "action_list_graphs.h" #include "action_list_hosts.h" #include "action_search.h" @@ -62,6 +63,7 @@ static int action_usage (void); static const action_t actions[] = { { "graph", action_graph }, + { "graph_def_json", action_graph_def_json }, { "list_graphs", action_list_graphs }, { "list_hosts", action_list_hosts }, { "search", action_search }, -- 2.11.0