First working version of the configurable graphlist stuff.
[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 "action_list_graphs.h"
10 #include "graph_list.h"
11 #include "utils_params.h"
12
13 static int print_graph_json (const graph_list_t *gl, void *user_data) /* {{{ */
14 {
15   _Bool *first;
16
17   if ((gl == NULL) || (user_data == NULL))
18     return (EINVAL);
19
20   first = (_Bool *) user_data;
21   if (!*first)
22     printf (",\n");
23   *first = 0;
24
25   printf (" {");
26
27   printf ("\"host\":\"%s\"", gl->host);
28       
29   printf (",\"plugin\":\"%s\"", gl->plugin);
30   if (gl->plugin_instance != NULL)
31     printf (",\"plugin_instance\":\"%s\"", gl->plugin_instance);
32   else
33     printf (",\"plugin_instance\":null");
34
35   printf (",\"type\":\"%s\"", gl->type);
36   if (gl->type_instance != NULL)
37     printf (",\"type_instance\":\"%s\"", gl->type_instance);
38   else
39     printf (",\"type_instance\":null");
40
41   printf ("}");
42
43   return (0);
44 } /* }}} int print_graph_json */
45
46 static int print_graph_inst_html (graph_config_t *cfg, /* {{{ */
47     graph_instance_t *inst,
48     __attribute__((unused)) void *user_data)
49 {
50   char buffer[1024];
51
52   memset (buffer, 0, sizeof (buffer));
53   gl_instance_get_params (cfg, inst, buffer, sizeof (buffer));
54
55   printf ("<li><a href=\"test.fcgi?action=graph;%s\">%s</a></li>\n", buffer, buffer);
56
57   return (0);
58 } /* }}} int print_graph_inst_html */
59
60 static int print_graph_html (graph_config_t *cfg, /* {{{ */
61     __attribute__((unused)) void *user_data)
62 {
63   char buffer[1024];
64
65   memset (buffer, 0, sizeof (buffer));
66   gl_graph_get_title (cfg, buffer, sizeof (buffer));
67
68   printf ("<li>%s\n<ul>\n", buffer);
69   gl_graph_instance_get_all (cfg, print_graph_inst_html, /* user_data = */ NULL);
70   printf ("</ul>\n");
71
72   return (0);
73 } /* }}} int print_graph_html */
74
75 static int list_graphs_json (void) /* {{{ */
76 {
77   _Bool first = 1;
78
79   printf ("Content-Type: application/json\n\n");
80
81   printf ("[\n");
82   gl_foreach (print_graph_json, /* user_data = */ &first);
83   printf ("\n]");
84
85   return (0);
86 } /* }}} int list_graphs_json */
87
88 static int list_graphs_html (void) /* {{{ */
89 {
90   printf ("Content-Type: text/html\n\n");
91
92   printf ("<ul>\n");
93   gl_graph_get_all (print_graph_html, /* user_data = */ NULL);
94   printf ("</ul>\n");
95
96   return (0);
97 } /* }}} int list_graphs_html */
98
99 int action_list_graphs (void) /* {{{ */
100 {
101   const char *format;
102
103   gl_update ();
104
105   format = param ("format");
106   if (format == NULL)
107     format = "html";
108
109   if (strcmp ("json", format) == 0)
110     return (list_graphs_json ());
111   else
112     return (list_graphs_html ());
113 } /* }}} int action_list_graphs */
114
115 /* vim: set sw=2 sts=2 et fdm=marker : */