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