summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: dd51aaf)
raw | patch | inline | side by side (parent: dd51aaf)
author | Florian Forster <octo@verplant.org> | |
Tue, 20 Dec 2011 14:55:02 +0000 (15:55 +0100) | ||
committer | Florian Forster <octo@verplant.org> | |
Tue, 20 Dec 2011 14:55:02 +0000 (15:55 +0100) |
src/Makefile.am | patch | blob | history | |
src/action_list_graphs_json.c | [new file with mode: 0644] | patch | blob |
src/action_list_graphs_json.h | [new file with mode: 0644] | patch | blob |
src/main.c | patch | blob | history |
diff --git a/src/Makefile.am b/src/Makefile.am
index 9595ebd988e7342c5d2b997e0856cf6f3e526554..d76e589fcc8ec7befc7b98275d3d4b55dc08c41a 100644 (file)
--- a/src/Makefile.am
+++ b/src/Makefile.am
action_instance_data_json.c action_instance_data_json.h \
action_graph_def_json.c action_graph_def_json.h \
action_list_graphs.c action_list_graphs.h \
+ action_list_graphs_json.c action_list_graphs_json.h \
action_list_hosts.c action_list_hosts.h \
action_search.c action_search.h \
action_search_json.c action_search_json.h \
diff --git a/src/action_list_graphs_json.c b/src/action_list_graphs_json.c
--- /dev/null
@@ -0,0 +1,148 @@
+/**
+ * collection4 - action_list_graphs_json.c
+ * Copyright (C) 2010,2011 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 <ff at octo.it>
+ **/
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+
+#include "action_list_graphs_json.h"
+#include "common.h"
+#include "graph.h"
+#include "graph_list.h"
+#include "utils_cgi.h"
+
+#include <fcgiapp.h>
+#include <fcgi_stdio.h>
+
+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 */
+
+static int print_one_graph (graph_config_t *cfg, /* {{{ */
+ void *user_data)
+{
+ char params[1024];
+ char title[1024];
+ size_t num_instances;
+
+ yajl_gen handler = user_data;
+
+ num_instances = graph_num_instances (cfg);
+ if (num_instances < 1)
+ return (0);
+
+ yajl_gen_map_open (handler);
+
+ memset (title, 0, sizeof (title));
+ graph_get_title (cfg, title, sizeof (title));
+
+ memset (params, 0, sizeof (params));
+ graph_get_params (cfg, params, sizeof (params));
+
+ yajl_gen_string (handler,
+ (unsigned char *) "title",
+ (unsigned int) strlen ("title"));
+ yajl_gen_string (handler,
+ (unsigned char *) title,
+ (unsigned int) strlen (title));
+
+ yajl_gen_string (handler,
+ (unsigned char *) "params",
+ (unsigned int) strlen ("params"));
+ yajl_gen_string (handler,
+ (unsigned char *) params,
+ (unsigned int) strlen (params));
+
+ yajl_gen_string (handler,
+ (unsigned char *) "num_instances",
+ (unsigned int) strlen ("num_instances"));
+ yajl_gen_integer (handler, (long int) num_instances);
+
+ yajl_gen_map_close (handler);
+
+ return (0);
+} /* }}} int print_one_graph */
+
+static int print_all_graphs (yajl_gen handler) /* {{{ */
+{
+ const char *dynamic;
+ _Bool include_dynamic = 0;
+
+ dynamic = param ("dynamic");
+ if ((dynamic != NULL)
+ && (strcmp ("true", dynamic) == 0))
+ include_dynamic = 1;
+
+ yajl_gen_array_open (handler);
+
+ gl_graph_get_all (include_dynamic, print_one_graph,
+ /* user_data = */ handler);
+
+ yajl_gen_array_close (handler);
+
+ return (0);
+} /* }}} int print_all_graphs */
+
+int action_list_graphs_json (void) /* {{{ */
+{
+ graph_config_t *cfg;
+
+ yajl_gen_config handler_config;
+ yajl_gen handler;
+
+ time_t now;
+ char time_buffer[128];
+ int status;
+
+ 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)
+ return (-1);
+
+ printf ("Content-Type: application/json\n");
+
+ now = time (NULL);
+ status = time_to_rfc1123 (now + 300, time_buffer, sizeof (time_buffer));
+ if (status == 0)
+ printf ("Expires: %s\n"
+ "Cache-Control: public\n",
+ time_buffer);
+ printf ("\n");
+
+ print_all_graphs (handler);
+
+ yajl_gen_free (handler);
+
+ return (status);
+} /* }}} int action_list_graphs_json */
+
+/* vim: set sw=2 sts=2 et fdm=marker : */
diff --git a/src/action_list_graphs_json.h b/src/action_list_graphs_json.h
--- /dev/null
@@ -0,0 +1,30 @@
+/**
+ * collection4 - action_list_graphs_json.h
+ * Copyright (C) 2010,2011 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 <ff at octo.it>
+ **/
+
+#ifndef ACTION_LIST_GRAPHS_JSON_H
+#define ACTION_LIST_GRAPHS_JSON_H 1
+
+int action_list_graphs_json (void);
+
+#endif /* ACTION_LIST_GRAPHS_JSON_H */
+/* vim: set sw=2 sts=2 et fdm=marker : */
diff --git a/src/main.c b/src/main.c
index 877f61bab7e9bdc4cb1544b1daf643cde661662a..d0e87c1321a50c7cf6d38da1dc440feadc05ec66 100644 (file)
--- a/src/main.c
+++ b/src/main.c
#include "action_instance_data_json.h"
#include "action_graph_def_json.h"
#include "action_list_graphs.h"
+#include "action_list_graphs_json.h"
#include "action_list_hosts.h"
#include "action_search.h"
#include "action_search_json.h"
{ "instance_data_json", action_instance_data_json },
{ "graph_def_json", action_graph_def_json },
{ "list_graphs", action_list_graphs },
+ { "list_graphs_json", action_list_graphs_json },
{ "list_hosts", action_list_hosts },
{ "search", action_search },
{ "search_json", action_search_json },