From: Florian Forster Date: Wed, 21 Dec 2011 10:52:53 +0000 (+0100) Subject: Implement the "list_hosts_json" action. X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=6ac3d2add70027faa94bc19c06aa1932d4283395;p=collection4.git Implement the "list_hosts_json" action. --- diff --git a/src/Makefile.am b/src/Makefile.am index d76e589..adfa023 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -21,6 +21,7 @@ collection_fcgi_SOURCES = main.c \ 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_list_hosts_json.c action_list_hosts_json.h \ action_search.c action_search.h \ action_search_json.c action_search_json.h \ action_show_graph.c action_show_graph.h \ diff --git a/src/action_list_hosts_json.c b/src/action_list_hosts_json.c new file mode 100644 index 0000000..029b700 --- /dev/null +++ b/src/action_list_hosts_json.c @@ -0,0 +1,104 @@ +/** + * collection4 - action_list_hosts_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 + **/ + +#include +#include +#include +#include + +#include "action_list_hosts_json.h" +#include "common.h" +#include "graph.h" +#include "graph_list.h" +#include "utils_cgi.h" + +#include +#include + +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_host (const char *host, /* {{{ */ + void *user_data) +{ + yajl_gen handler = user_data; + + yajl_gen_string (handler, + (unsigned char *) host, + (unsigned int) strlen (host)); + + return (0); +} /* }}} int print_one_host */ + +static int print_all_hosts (yajl_gen handler) /* {{{ */ +{ + yajl_gen_array_open (handler); + gl_foreach_host (print_one_host, /* user_data = */ handler); + yajl_gen_array_close (handler); + + return (0); +} /* }}} int print_all_hosts */ + +int action_list_hosts_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_hosts (handler); + + yajl_gen_free (handler); + + return (status); +} /* }}} int action_list_hosts_json */ + +/* vim: set sw=2 sts=2 et fdm=marker : */ diff --git a/src/action_list_hosts_json.h b/src/action_list_hosts_json.h new file mode 100644 index 0000000..a96abd5 --- /dev/null +++ b/src/action_list_hosts_json.h @@ -0,0 +1,30 @@ +/** + * collection4 - action_list_hosts_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 + **/ + +#ifndef ACTION_LIST_HOSTS_JSON_H +#define ACTION_LIST_HOSTS_JSON_H 1 + +int action_list_hosts_json (void); + +#endif /* ACTION_LIST_HOSTS_JSON_H */ +/* vim: set sw=2 sts=2 et fdm=marker : */ diff --git a/src/main.c b/src/main.c index d0e87c1..896a612 100644 --- a/src/main.c +++ b/src/main.c @@ -42,6 +42,7 @@ #include "action_list_graphs.h" #include "action_list_graphs_json.h" #include "action_list_hosts.h" +#include "action_list_hosts_json.h" #include "action_search.h" #include "action_search_json.h" #include "action_show_graph.h" @@ -70,6 +71,7 @@ static const action_t actions[] = { "list_graphs", action_list_graphs }, { "list_graphs_json", action_list_graphs_json }, { "list_hosts", action_list_hosts }, + { "list_hosts_json", action_list_hosts_json }, { "search", action_search }, { "search_json", action_search_json }, { "show_graph", action_show_graph },