list_hosts_json action: Create an object for each host.
[collection4.git] / src / action_list_hosts_json.c
1 /**
2  * collection4 - action_list_hosts_json.c
3  * Copyright (C) 2010,2011  Florian octo Forster
4  * 
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.
9  * 
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.
14  * 
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
19  *
20  * Authors:
21  *   Florian octo Forster <ff at octo.it>
22  **/
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <errno.h>
28
29 #include "action_list_hosts_json.h"
30 #include "common.h"
31 #include "graph.h"
32 #include "graph_list.h"
33 #include "utils_cgi.h"
34
35 #include <fcgiapp.h>
36 #include <fcgi_stdio.h>
37
38 static void write_callback (__attribute__((unused)) void *ctx, /* {{{ */
39     const char *str, unsigned int len)
40 {
41   fwrite ((void *) str, /* size = */ len, /* nmemb = */ 1, stdout);
42 } /* }}} void write_callback */
43
44 static int print_one_host (const char *host, /* {{{ */
45     void *user_data)
46 {
47   yajl_gen handler = user_data;
48
49   yajl_gen_map_open (handler);
50
51   yajl_gen_string (handler,
52       (unsigned char *) "host",
53       (unsigned int) strlen ("host"));
54   yajl_gen_string (handler,
55       (unsigned char *) host,
56       (unsigned int) strlen (host));
57
58   yajl_gen_map_close (handler);
59
60   return (0);
61 } /* }}} int print_one_host */
62
63 static int print_all_hosts (yajl_gen handler) /* {{{ */
64 {
65   yajl_gen_array_open (handler);
66   gl_foreach_host (print_one_host, /* user_data = */ handler);
67   yajl_gen_array_close (handler);
68
69   return (0);
70 } /* }}} int print_all_hosts */
71
72 int action_list_hosts_json (void) /* {{{ */
73 {
74   yajl_gen_config handler_config;
75   yajl_gen handler;
76
77   time_t now;
78   char time_buffer[128];
79   int status;
80
81   memset (&handler_config, 0, sizeof (handler_config));
82   handler_config.beautify = 1;
83   handler_config.indentString = "  ";
84
85   handler = yajl_gen_alloc2 (write_callback,
86       &handler_config,
87       /* alloc functions = */ NULL,
88       /* context = */ NULL);
89   if (handler == NULL)
90     return (-1);
91
92   printf ("Content-Type: application/json\n");
93
94   now = time (NULL);
95   status = time_to_rfc1123 (now + 300, time_buffer, sizeof (time_buffer));
96   if (status == 0)
97     printf ("Expires: %s\n"
98         "Cache-Control: public\n",
99         time_buffer);
100   printf ("\n");
101
102   print_all_hosts (handler);
103
104   yajl_gen_free (handler);
105
106   return (status);
107 } /* }}} int action_list_hosts_json */
108
109 /* vim: set sw=2 sts=2 et fdm=marker : */