14a13bbc6211c9ba6338a7388c7d6b0d2f9a6dca
[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_html (const graph_list_t *gl,
47     void __attribute__((unused)) *user_data)
48 {
49   if (gl == NULL)
50     return (EINVAL);
51
52   printf ("<li>");
53
54   printf ("<a href=\"%s?action=graph;", getenv ("SCRIPT_NAME"));
55   printf ("host=%s;plugin=%s;", gl->host, gl->plugin);
56   if (gl->plugin_instance != NULL)
57     printf ("plugin_instance=%s;", gl->plugin_instance);
58   printf ("type=%s;", gl->type);
59   if (gl->type_instance != NULL)
60     printf ("type_instance=%s;", gl->type_instance);
61   printf ("\">");
62
63   printf ("%s/%s", gl->host, gl->plugin);
64   if (gl->plugin_instance != NULL)
65     printf ("-%s", gl->plugin_instance);
66   printf ("/%s", gl->type);
67   if (gl->type_instance != NULL)
68     printf ("-%s", gl->type_instance);
69   printf ("</a></li>\n");
70
71   return (0);
72 }
73
74 static int list_graphs_json (void) /* {{{ */
75 {
76   _Bool first = 1;
77
78   printf ("Content-Type: application/json\n\n");
79
80   printf ("[\n");
81   gl_foreach (print_graph_json, /* user_data = */ &first);
82   printf ("\n]");
83
84   return (0);
85 } /* }}} int list_graphs_json */
86
87 static int list_graphs_html (void) /* {{{ */
88 {
89   printf ("Content-Type: text/html\n\n");
90
91   printf ("<ul>\n");
92   gl_foreach (print_graph_html, /* user_data = */ NULL);
93   printf ("</ul>\n");
94
95   return (0);
96 } /* }}} int list_graphs_html */
97
98 int action_list_graphs (void) /* {{{ */
99 {
100   const char *format;
101
102   gl_update ();
103
104   format = param ("format");
105   if (format == NULL)
106     format = "html";
107
108   if (strcmp ("json", format) == 0)
109     return (list_graphs_json ());
110   else
111     return (list_graphs_html ());
112 } /* }}} int action_list_graphs */
113
114 /* vim: set sw=2 sts=2 et fdm=marker : */