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