Implement the "list_graphs_json" action.
[collection4.git] / src / main.c
1 /**
2  * collection4 - main.c
3  * Copyright (C) 2010  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 <strings.h>
28 #include <errno.h>
29
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33 #include <dirent.h>
34
35 #include "common.h"
36 #include "graph_list.h"
37 #include "utils_cgi.h"
38
39 #include "action_graph.h"
40 #include "action_instance_data_json.h"
41 #include "action_graph_def_json.h"
42 #include "action_list_graphs.h"
43 #include "action_list_graphs_json.h"
44 #include "action_list_hosts.h"
45 #include "action_search.h"
46 #include "action_search_json.h"
47 #include "action_show_graph.h"
48 #include "action_show_graph_json.h"
49 #include "action_show_instance.h"
50
51 /* Include this last, so the macro magic of <fcgi_stdio.h> doesn't interfere
52  * with our own header files. */
53 #include <fcgiapp.h>
54 #include <fcgi_stdio.h>
55
56 struct action_s
57 {
58   const char *name;
59   int (*callback) (void);
60 };
61 typedef struct action_s action_t;
62
63 static int action_usage (void);
64
65 static const action_t actions[] =
66 {
67   { "graph",       action_graph },
68   { "instance_data_json", action_instance_data_json },
69   { "graph_def_json", action_graph_def_json },
70   { "list_graphs", action_list_graphs },
71   { "list_graphs_json", action_list_graphs_json },
72   { "list_hosts",  action_list_hosts },
73   { "search",      action_search },
74   { "search_json", action_search_json },
75   { "show_graph",  action_show_graph },
76   { "show_graph_json",  action_show_graph_json },
77   { "show_instance", action_show_instance },
78   { "usage",       action_usage }
79 };
80 static const size_t actions_num = sizeof (actions) / sizeof (actions[0]);
81
82
83 static int action_usage (void) /* {{{ */
84 {
85   size_t i;
86
87   printf ("Content-Type: text/plain\n\n");
88
89   printf ("Usage:\n"
90       "\n"
91       "  Available actions:\n"
92       "\n");
93
94   for (i = 0; i < actions_num; i++)
95     printf ("  * %s\n", actions[i].name);
96
97   printf ("\n");
98
99   return (0);
100 } /* }}} int action_usage */
101
102 static int handle_request (void) /* {{{ */
103 {
104   const char *action;
105
106   param_init ();
107
108   action = param ("action");
109   if (action == NULL)
110   {
111     return (action_list_graphs ());
112   }
113   else
114   {
115     size_t i;
116     int status = ENOENT;
117
118     gl_update (/* request_served = */ 0);
119
120     for (i = 0; i < actions_num; i++)
121     {
122       if (strcmp (action, actions[i].name) == 0)
123       {
124         status = (*actions[i].callback) ();
125         break;
126       }
127     }
128
129     if (i >= actions_num)
130       status = action_usage ();
131
132     /* Call finish before updating the graph list, so clients don't wait for
133      * the update to finish. */
134     FCGI_Finish ();
135
136     gl_update (/* request_served = */ 1);
137
138     return (status);
139   }
140 } /* }}} int handle_request */
141
142 static int run (void) /* {{{ */
143 {
144   while (FCGI_Accept() >= 0)
145   {
146     handle_request ();
147     param_finish ();
148   }
149
150   return (0);
151 } /* }}} int run */
152
153 int main (int argc, char **argv) /* {{{ */
154 {
155   int status;
156
157   argc = 0;
158   argv = NULL;
159
160   if (FCGX_IsCGI ())
161     status = handle_request ();
162   else
163     status = run ();
164
165   exit ((status == 0) ? EXIT_SUCCESS : EXIT_FAILURE);
166 } /* }}} int main */
167
168 /* vim: set sw=2 sts=2 et fdm=marker : */