2 * Copyright (C) 2010-2013 jeanfi@gmail.com
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #define _(str) gettext(str)
30 #include <sys/types.h>
31 #include <sys/select.h>
32 #include <sys/socket.h>
37 #include <microhttpd.h>
45 #include "psensor_json.h"
50 static const char *DEFAULT_LOG_FILE = "/var/log/psensor-server.log";
52 #define HTML_STOP_REQUESTED \
53 (_("<html><body><p>Server stop requested</p></body></html>"))
55 static const char *program_name;
57 static const int DEFAULT_PORT = 3131;
59 #define PAGE_NOT_FOUND (_("<html><body><p>"\
60 "Page not found - Go to <a href='/'>Main page</a></p></body>"))
62 static struct option long_options[] = {
63 {"version", no_argument, 0, 'v'},
64 {"help", no_argument, 0, 'h'},
65 {"port", required_argument, 0, 'p'},
66 {"wdir", required_argument, 0, 'w'},
67 {"debug", required_argument, 0, 'd'},
68 {"log-file", required_argument, 0, 'l'},
69 {"sensor-log-file", required_argument, 0, 0},
70 {"sensor-log-interval", required_argument, 0, 0},
74 static struct server_data server_data;
76 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
78 static int server_stop_requested;
80 static void print_version()
82 printf("psensor-server %s\n", VERSION);
83 printf(_("Copyright (C) %s jeanfi@gmail.com\n"
84 "License GPLv2: GNU GPL version 2 or later "
85 "<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n"
86 "This is free software: you are free to change and redistribute it.\n"
87 "There is NO WARRANTY, to the extent permitted by law.\n"),
91 static void print_help()
93 printf(_("Usage: %s [OPTION]...\n"), program_name);
95 puts(_("psensor-server is an HTTP server for monitoring hardware "
96 "sensors remotely."));
100 puts(_(" -h, --help display this help and exit\n"
101 " -v, --version display version information and exit"));
104 puts(_(" -p,--port=PORT webserver port\n"
105 " -w,--wdir=DIR directory containing webserver pages"));
108 puts(_(" -d, --debug=LEVEL "
109 "set the debug level, integer between 0 and 3"));
110 puts(_(" -l, --log-file=PATH set the log file to PATH"));
111 puts(_(" --sensor-log-file=PATH set the sensor log file to PATH"));
112 puts(_(" --sensor-log-interval=S "
113 "set the sensor log interval to S (seconds)"));
116 printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
118 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
122 * Returns the file path corresponding to a given URL
124 static char *get_path(const char *url, const char *www_dir)
129 if (!strlen(url) || !strcmp(url, ".") || !strcmp(url, "/"))
134 res = malloc(strlen(www_dir)+strlen(p)+1);
136 strcpy(res, www_dir);
142 #if MHD_VERSION >= 0x00090200
144 file_reader(void *cls, uint64_t pos, char *buf, size_t max)
147 file_reader(void *cls, uint64_t pos, char *buf, int max)
152 fseek(file, pos, SEEK_SET);
153 return fread(buf, 1, max, file);
156 static struct MHD_Response *
157 create_response_api(const char *nurl, const char *method, unsigned int *rp_code)
159 struct MHD_Response *resp;
163 if (!strcmp(nurl, URL_BASE_API_1_1_SENSORS)) {
164 page = sensors_to_json_string(server_data.sensors);
166 } else if (!strcmp(nurl, URL_API_1_1_SYSINFO)) {
167 page = sysinfo_to_json_string(&server_data.psysinfo);
168 } else if (!strcmp(nurl, URL_API_1_1_CPU_USAGE)) {
169 page = sensor_to_json_string(server_data.cpu_usage);
171 } else if (!strncmp(nurl, URL_BASE_API_1_1_SENSORS,
172 strlen(URL_BASE_API_1_1_SENSORS))
173 && nurl[strlen(URL_BASE_API_1_1_SENSORS)] == '/') {
175 const char *sid = nurl + strlen(URL_BASE_API_1_1_SENSORS) + 1;
177 s = psensor_list_get_by_id(server_data.sensors, sid);
180 page = sensor_to_json_string(s);
182 } else if (!strcmp(nurl, URL_API_1_1_SERVER_STOP)) {
184 server_stop_requested = 1;
185 page = strdup(HTML_STOP_REQUESTED);
189 *rp_code = MHD_HTTP_OK;
191 resp = MHD_create_response_from_data(strlen(page), page,
194 MHD_add_response_header(resp, MHD_HTTP_HEADER_CONTENT_TYPE,
203 static struct MHD_Response *create_response_file(const char *nurl,
205 unsigned int *rp_code,
212 ret = stat(fpath, &st);
214 if (!ret && (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))) {
215 file = fopen(fpath, "rb");
218 *rp_code = MHD_HTTP_OK;
222 return MHD_create_response_from_data
223 (0, NULL, MHD_NO, MHD_NO);
226 return MHD_create_response_from_callback
231 (MHD_ContentReaderFreeCallback)&fclose);
234 log_err("Failed to open: %s.", fpath);
241 static struct MHD_Response *
242 create_response(const char *nurl, const char *method, unsigned int *rp_code)
244 struct MHD_Response *resp = NULL;
246 if (!strncmp(nurl, URL_BASE_API_1_1, strlen(URL_BASE_API_1_1))) {
247 resp = create_response_api(nurl, method, rp_code);
249 char *fpath = get_path(nurl, server_data.www_dir);
251 resp = create_response_file(nurl, method, rp_code, fpath);
259 char *page = strdup(PAGE_NOT_FOUND);
260 *rp_code = MHD_HTTP_NOT_FOUND;
262 return MHD_create_response_from_data
263 (strlen(page), page, MHD_YES, MHD_NO);
267 static int cbk_http_request(void *cls,
268 struct MHD_Connection *connection,
272 const char *upload_data,
273 size_t *upload_data_size, void **ptr)
276 struct MHD_Response *response;
279 unsigned int resp_code;
281 if (strcmp(method, "GET"))
284 if (&dummy != *ptr) {
285 /* The first time only the headers are valid, do not
286 respond in the first round... */
291 if (*upload_data_size)
294 *ptr = NULL; /* clear context pointer */
296 log_debug(_("HTTP Request: %s"), url);
298 nurl = url_normalize(url);
300 pthread_mutex_lock(&mutex);
301 response = create_response(nurl, method, &resp_code);
302 pthread_mutex_unlock(&mutex);
304 ret = MHD_queue_response(connection, resp_code, response);
305 MHD_destroy_response(response);
312 int main(int argc, char *argv[])
314 struct MHD_Daemon *d;
315 int port, opti, optc, cmdok, ret, slog_interval;
316 char *log_file, *slog_file;
318 program_name = argv[0];
320 setlocale(LC_ALL, "");
323 bindtextdomain(PACKAGE, LOCALEDIR);
327 server_data.www_dir = NULL;
328 server_data.psysinfo.interfaces = NULL;
335 while ((optc = getopt_long(argc,
343 server_data.www_dir = strdup(optarg);
356 log_level = atoi(optarg);
357 log_info(_("Enables debug mode: %d"), log_level);
361 log_file = strdup(optarg);
364 if (!strcmp(long_options[opti].name, "sensor-log-file"))
365 slog_file = strdup(optarg);
366 else if (!strcmp(long_options[opti].name,
367 "sensor-log-interval"))
368 slog_interval = atoi(optarg);
376 if (!cmdok || optind != argc) {
377 fprintf(stderr, _("Try `%s --help' for more information.\n"),
382 if (!server_data.www_dir)
383 server_data.www_dir = strdup(DEFAULT_WWW_DIR);
386 log_file = strdup(DEFAULT_LOG_FILE);
392 server_data.sensors = get_all_sensors(0, 600);
395 server_data.cpu_usage = create_cpu_usage_sensor(600);
398 if (!*server_data.sensors)
399 log_err(_("No sensors detected."));
401 d = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION,
403 NULL, NULL, &cbk_http_request, server_data.sensors,
406 log_err(_("Failed to create Web server."));
410 log_info(_("Web server started on port: %d"), port);
411 log_info(_("WWW directory: %s"), server_data.www_dir);
412 log_info(_("URL: http://localhost:%d"), port);
415 if (slog_interval <= 0)
417 ret = slog_activate(slog_file,
422 log_err(_("Failed to activate logging of sensors."));
425 while (!server_stop_requested) {
426 pthread_mutex_lock(&mutex);
429 sysinfo_update(&server_data.psysinfo);
430 cpu_usage_sensor_update(server_data.cpu_usage);
432 psensor_list_update_measures(server_data.sensors);
434 psensor_log_measures(server_data.sensors);
436 pthread_mutex_unlock(&mutex);
444 /* sanity cleanup for valgrind */
445 psensor_list_free(server_data.sensors);
447 psensor_free(server_data.cpu_usage);
449 free(server_data.www_dir);
457 if (log_file != DEFAULT_LOG_FILE)