Free the graph_def structure.
[collection4.git] / action_list_graphs.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <errno.h>
5
6 #include <fcgiapp.h>
7 #include <fcgi_stdio.h>
8
9 #include "graph_list.h"
10 #include "utils_params.h"
11
12 static int print_graph_json (const graph_list_t *gl, void *user_data) /* {{{ */
13 {
14   _Bool *first;
15
16   if ((gl == NULL) || (user_data == NULL))
17     return (EINVAL);
18
19   first = (_Bool *) user_data;
20   if (!*first)
21     printf (",\n");
22   *first = 0;
23
24   printf (" {");
25
26   printf ("\"host\":\"%s\"", gl->host);
27       
28   printf (",\"plugin\":\"%s\"", gl->plugin);
29   if (gl->plugin_instance != NULL)
30     printf (",\"plugin_instance\":\"%s\"", gl->plugin_instance);
31   else
32     printf (",\"plugin_instance\":null");
33
34   printf (",\"type\":\"%s\"", gl->type);
35   if (gl->type_instance != NULL)
36     printf (",\"type_instance\":\"%s\"", gl->type_instance);
37   else
38     printf (",\"type_instance\":null");
39
40   printf ("}");
41
42   return (0);
43 } /* }}} int print_graph_json */
44
45 static int print_graph_html (const graph_list_t *gl,
46     void __attribute__((unused)) *user_data)
47 {
48   if (gl == NULL)
49     return (EINVAL);
50
51   printf ("<li>%s/%s", gl->host, gl->plugin);
52   if (gl->plugin_instance != NULL)
53     printf ("-%s", gl->plugin_instance);
54   printf ("/%s", gl->type);
55   if (gl->type_instance != NULL)
56     printf ("-%s", gl->type_instance);
57   printf ("</li>\n");
58
59   return (0);
60 }
61
62 static int list_graphs_json (void) /* {{{ */
63 {
64   _Bool first = 1;
65
66   printf ("Content-Type: application/json\n\n");
67
68   printf ("[\n");
69   gl_foreach (print_graph_json, /* user_data = */ &first);
70   printf ("\n]");
71
72   return (0);
73 } /* }}} int list_graphs_json */
74
75 static int list_graphs_html (void) /* {{{ */
76 {
77   printf ("Content-Type: text/html\n\n");
78
79   printf ("<ul>\n");
80   gl_foreach (print_graph_html, /* user_data = */ NULL);
81   printf ("</ul>\n");
82
83   return (0);
84 } /* }}} int list_graphs_html */
85
86 int action_list_graphs (void) /* {{{ */
87 {
88   const char *format;
89
90   gl_update ();
91
92   format = param ("format");
93   if (format == NULL)
94     format = "html";
95
96   if (strcmp ("json", format) == 0)
97     return (list_graphs_json ());
98   else
99     return (list_graphs_html ());
100 } /* }}} int action_list_graphs */
101
102 /* vim: set sw=2 sts=2 et fdm=marker : */