2 * Copyright (C) 2010-2014 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
19 #define _LARGEFILE_SOURCE 1
24 #define _(str) gettext(str)
31 #include <sys/types.h>
32 #include <sys/select.h>
33 #include <sys/socket.h>
38 #include <microhttpd.h>
46 #include "psensor_json.h"
51 static const char *DEFAULT_LOG_FILE = "/var/log/psensor-server.log";
53 #define HTML_STOP_REQUESTED \
54 (_("<html><body><p>Server stop requested</p></body></html>"))
56 static const char *program_name;
58 static const int DEFAULT_PORT = 3131;
60 #define PAGE_NOT_FOUND (_("<html><body><p>"\
61 "Page not found - Go to <a href='/'>Main page</a></p></body>"))
63 static struct option long_options[] = {
64 {"version", no_argument, 0, 'v'},
65 {"help", no_argument, 0, 'h'},
66 {"port", required_argument, 0, 'p'},
67 {"wdir", required_argument, 0, 'w'},
68 {"debug", required_argument, 0, 'd'},
69 {"log-file", required_argument, 0, 'l'},
70 {"sensor-log-file", required_argument, 0, 0},
71 {"sensor-log-interval", required_argument, 0, 0},
75 static struct server_data server_data;
77 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
79 static int server_stop_requested;
81 static void print_version()
83 printf("psensor-server %s\n", VERSION);
84 printf(_("Copyright (C) %s jeanfi@gmail.com\n"
85 "License GPLv2: GNU GPL version 2 or later "
86 "<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n"
87 "This is free software: you are free to change and redistribute it.\n"
88 "There is NO WARRANTY, to the extent permitted by law.\n"),
92 static void print_help()
94 printf(_("Usage: %s [OPTION]...\n"), program_name);
96 puts(_("psensor-server is an HTTP server for monitoring hardware "
97 "sensors remotely."));
101 puts(_(" -h, --help display this help and exit\n"
102 " -v, --version display version information and exit"));
105 puts(_(" -p,--port=PORT webserver port\n"
106 " -w,--wdir=DIR directory containing webserver pages"));
109 puts(_(" -d, --debug=LEVEL "
110 "set the debug level, integer between 0 and 3"));
111 puts(_(" -l, --log-file=PATH set the log file to PATH"));
112 puts(_(" --sensor-log-file=PATH set the sensor log file to PATH"));
113 puts(_(" --sensor-log-interval=S "
114 "set the sensor log interval to S (seconds)"));
117 printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
119 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
123 * Returns the file path corresponding to a given URL
125 static char *get_path(const char *url, const char *www_dir)
130 if (!strlen(url) || !strcmp(url, ".") || !strcmp(url, "/"))
135 res = malloc(strlen(www_dir)+strlen(p)+1);
137 strcpy(res, www_dir);
143 #if MHD_VERSION >= 0x00090200
145 file_reader(void *cls, uint64_t pos, char *buf, size_t max)
148 file_reader(void *cls, uint64_t pos, char *buf, int max)
153 fseeko(file, pos, SEEK_SET);
154 return fread(buf, 1, max, file);
157 static struct MHD_Response *
158 create_response_api(const char *nurl, const char *method, unsigned int *rp_code)
160 struct MHD_Response *resp;
164 if (!strcmp(nurl, URL_BASE_API_1_1_SENSORS)) {
165 page = sensors_to_json_string(server_data.sensors);
167 } else if (!strcmp(nurl, URL_API_1_1_SYSINFO)) {
168 page = sysinfo_to_json_string(&server_data.psysinfo);
169 } else if (!strcmp(nurl, URL_API_1_1_CPU_USAGE)) {
170 page = sensor_to_json_string(server_data.cpu_usage);
172 } else if (!strncmp(nurl, URL_BASE_API_1_1_SENSORS,
173 strlen(URL_BASE_API_1_1_SENSORS))
174 && nurl[strlen(URL_BASE_API_1_1_SENSORS)] == '/') {
176 const char *sid = nurl + strlen(URL_BASE_API_1_1_SENSORS) + 1;
178 s = psensor_list_get_by_id(server_data.sensors, sid);
181 page = sensor_to_json_string(s);
183 } else if (!strcmp(nurl, URL_API_1_1_SERVER_STOP)) {
185 server_stop_requested = 1;
186 page = strdup(HTML_STOP_REQUESTED);
190 *rp_code = MHD_HTTP_OK;
192 resp = MHD_create_response_from_data(strlen(page), page,
195 MHD_add_response_header(resp, MHD_HTTP_HEADER_CONTENT_TYPE,
204 static struct MHD_Response *create_response_file(const char *nurl,
206 unsigned int *rp_code,
213 ret = stat(fpath, &st);
215 if (!ret && (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))) {
216 file = fopen(fpath, "rb");
219 *rp_code = MHD_HTTP_OK;
223 return MHD_create_response_from_data
224 (0, NULL, MHD_NO, MHD_NO);
227 return MHD_create_response_from_callback
232 (MHD_ContentReaderFreeCallback)&fclose);
235 log_err("Failed to open: %s.", fpath);
242 static struct MHD_Response *
243 create_response(const char *nurl, const char *method, unsigned int *rp_code)
245 struct MHD_Response *resp = NULL;
247 if (!strncmp(nurl, URL_BASE_API_1_1, strlen(URL_BASE_API_1_1))) {
248 resp = create_response_api(nurl, method, rp_code);
250 char *fpath = get_path(nurl, server_data.www_dir);
252 resp = create_response_file(nurl, method, rp_code, fpath);
260 char *page = strdup(PAGE_NOT_FOUND);
261 *rp_code = MHD_HTTP_NOT_FOUND;
263 return MHD_create_response_from_data
264 (strlen(page), page, MHD_YES, MHD_NO);
268 static int cbk_http_request(void *cls,
269 struct MHD_Connection *connection,
273 const char *upload_data,
274 size_t *upload_data_size, void **ptr)
277 struct MHD_Response *response;
280 unsigned int resp_code;
282 if (strcmp(method, "GET"))
285 if (&dummy != *ptr) {
286 /* The first time only the headers are valid, do not
287 respond in the first round... */
292 if (*upload_data_size)
295 *ptr = NULL; /* clear context pointer */
297 log_debug(_("HTTP Request: %s"), url);
299 nurl = url_normalize(url);
301 pthread_mutex_lock(&mutex);
302 response = create_response(nurl, method, &resp_code);
303 pthread_mutex_unlock(&mutex);
305 ret = MHD_queue_response(connection, resp_code, response);
306 MHD_destroy_response(response);
313 int main(int argc, char *argv[])
315 struct MHD_Daemon *d;
316 int port, opti, optc, cmdok, ret, slog_interval;
317 char *log_file, *slog_file;
319 program_name = argv[0];
321 setlocale(LC_ALL, "");
324 bindtextdomain(PACKAGE, LOCALEDIR);
328 server_data.www_dir = NULL;
330 server_data.psysinfo.interfaces = NULL;
338 while ((optc = getopt_long(argc,
346 server_data.www_dir = strdup(optarg);
359 log_level = atoi(optarg);
360 log_info(_("Enables debug mode: %d"), log_level);
364 log_file = strdup(optarg);
367 if (!strcmp(long_options[opti].name, "sensor-log-file"))
368 slog_file = strdup(optarg);
369 else if (!strcmp(long_options[opti].name,
370 "sensor-log-interval"))
371 slog_interval = atoi(optarg);
379 if (!cmdok || optind != argc) {
380 fprintf(stderr, _("Try `%s --help' for more information.\n"),
385 if (!server_data.www_dir)
386 server_data.www_dir = strdup(DEFAULT_WWW_DIR);
389 log_file = strdup(DEFAULT_LOG_FILE);
395 server_data.sensors = get_all_sensors(0, 600);
398 server_data.cpu_usage = create_cpu_usage_sensor(600);
401 if (!*server_data.sensors)
402 log_err(_("No sensors detected."));
404 d = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION,
406 NULL, NULL, &cbk_http_request, server_data.sensors,
409 log_err(_("Failed to create Web server."));
413 log_info(_("Web server started on port: %d"), port);
414 log_info(_("WWW directory: %s"), server_data.www_dir);
415 log_info(_("URL: http://localhost:%d"), port);
418 if (slog_interval <= 0)
420 ret = slog_activate(slog_file,
425 log_err(_("Failed to activate logging of sensors."));
428 while (!server_stop_requested) {
429 pthread_mutex_lock(&mutex);
432 sysinfo_update(&server_data.psysinfo);
433 cpu_usage_sensor_update(server_data.cpu_usage);
435 psensor_list_update_measures(server_data.sensors);
437 psensor_log_measures(server_data.sensors);
439 pthread_mutex_unlock(&mutex);
447 /* sanity cleanup for valgrind */
448 psensor_list_free(server_data.sensors);
450 psensor_free(server_data.cpu_usage);
452 free(server_data.www_dir);
460 if (log_file != DEFAULT_LOG_FILE)