2 * collection4 - action_list_hosts_json.c
3 * Copyright (C) 2010,2011 Florian octo Forster
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA
21 * Florian octo Forster <ff at octo.it>
29 #include "action_list_hosts_json.h"
32 #include "graph_list.h"
33 #include "utils_cgi.h"
36 #include <fcgi_stdio.h>
38 static void write_callback (__attribute__((unused)) void *ctx, /* {{{ */
39 const char *str, unsigned int len)
41 fwrite ((void *) str, /* size = */ len, /* nmemb = */ 1, stdout);
42 } /* }}} void write_callback */
44 static int print_one_host (const char *host, /* {{{ */
47 yajl_gen handler = user_data;
49 yajl_gen_string (handler,
50 (unsigned char *) host,
51 (unsigned int) strlen (host));
54 } /* }}} int print_one_host */
56 static int print_all_hosts (yajl_gen handler) /* {{{ */
58 yajl_gen_array_open (handler);
59 gl_foreach_host (print_one_host, /* user_data = */ handler);
60 yajl_gen_array_close (handler);
63 } /* }}} int print_all_hosts */
65 int action_list_hosts_json (void) /* {{{ */
69 yajl_gen_config handler_config;
73 char time_buffer[128];
76 memset (&handler_config, 0, sizeof (handler_config));
77 handler_config.beautify = 1;
78 handler_config.indentString = " ";
80 handler = yajl_gen_alloc2 (write_callback,
82 /* alloc functions = */ NULL,
83 /* context = */ NULL);
87 printf ("Content-Type: application/json\n");
90 status = time_to_rfc1123 (now + 300, time_buffer, sizeof (time_buffer));
92 printf ("Expires: %s\n"
93 "Cache-Control: public\n",
97 print_all_hosts (handler);
99 yajl_gen_free (handler);
102 } /* }}} int action_list_hosts_json */
104 /* vim: set sw=2 sts=2 et fdm=marker : */