2 * Copyright (C) 2010-2012 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},
73 static struct server_data server_data;
75 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
77 static int server_stop_requested;
79 static void print_version()
81 printf("psensor-server %s\n", VERSION);
82 printf(_("Copyright (C) %s jeanfi@gmail.com\n"
83 "License GPLv2: GNU GPL version 2 or later "
84 "<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n"
85 "This is free software: you are free to change and redistribute it.\n"
86 "There is NO WARRANTY, to the extent permitted by law.\n"),
90 static void print_help()
92 printf(_("Usage: %s [OPTION]...\n"), program_name);
94 puts(_("psensor-server is an HTTP server for monitoring hardware "
95 "sensors remotely."));
99 puts(_(" -h, --help display this help and exit\n"
100 " -v, --version display version information and exit"));
103 puts(_(" -p,--port=PORT webserver port\n"
104 " -w,--wdir=DIR directory containing webserver pages"));
107 puts(_(" -d, --debug=LEVEL "
108 "set the debug level, integer between 0 and 3"));
109 puts(_(" -l, --log-file=PATH set the log file to PATH"));
110 puts(_(" --sensor-log-file=PATH set the sensor log file to PATH"));
113 printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
115 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
119 * Returns the file path corresponding to a given URL
121 static char *get_path(const char *url, const char *www_dir)
126 if (!strlen(url) || !strcmp(url, ".") || !strcmp(url, "/"))
131 res = malloc(strlen(www_dir)+strlen(p)+1);
133 strcpy(res, www_dir);
139 #if MHD_VERSION >= 0x00090200
141 file_reader(void *cls, uint64_t pos, char *buf, size_t max)
144 file_reader(void *cls, uint64_t pos, char *buf, int max)
149 fseek(file, pos, SEEK_SET);
150 return fread(buf, 1, max, file);
153 static struct MHD_Response *
154 create_response_api(const char *nurl, const char *method, unsigned int *rp_code)
156 struct MHD_Response *resp;
160 if (!strcmp(nurl, URL_BASE_API_1_0_SENSORS)) {
161 page = sensors_to_json_string(server_data.sensors);
163 } else if (!strcmp(nurl, URL_API_1_0_SYSINFO)) {
164 page = sysinfo_to_json_string(&server_data.psysinfo);
165 } else if (!strcmp(nurl, URL_API_1_0_CPU_USAGE)) {
166 page = sensor_to_json_string(server_data.cpu_usage);
168 } else if (!strncmp(nurl, URL_BASE_API_1_0_SENSORS,
169 strlen(URL_BASE_API_1_0_SENSORS))
170 && nurl[strlen(URL_BASE_API_1_0_SENSORS)] == '/') {
172 const char *sid = nurl + strlen(URL_BASE_API_1_0_SENSORS) + 1;
174 s = psensor_list_get_by_id(server_data.sensors, sid);
177 page = sensor_to_json_string(s);
179 } else if (!strcmp(nurl, URL_API_1_0_SERVER_STOP)) {
181 server_stop_requested = 1;
182 page = strdup(HTML_STOP_REQUESTED);
186 *rp_code = MHD_HTTP_OK;
188 resp = MHD_create_response_from_data(strlen(page), page,
191 MHD_add_response_header(resp, MHD_HTTP_HEADER_CONTENT_TYPE,
200 static struct MHD_Response *create_response_file(const char *nurl,
202 unsigned int *rp_code,
209 ret = stat(fpath, &st);
211 if (!ret && (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))) {
212 file = fopen(fpath, "rb");
215 *rp_code = MHD_HTTP_OK;
219 return MHD_create_response_from_data
220 (0, NULL, MHD_NO, MHD_NO);
223 return MHD_create_response_from_callback
228 (MHD_ContentReaderFreeCallback)&fclose);
231 log_err("Failed to open: %s.", fpath);
238 static struct MHD_Response *
239 create_response(const char *nurl, const char *method, unsigned int *rp_code)
241 struct MHD_Response *resp = NULL;
243 if (!strncmp(nurl, URL_BASE_API_1_0, strlen(URL_BASE_API_1_0))) {
244 resp = create_response_api(nurl, method, rp_code);
246 char *fpath = get_path(nurl, server_data.www_dir);
248 resp = create_response_file(nurl, method, rp_code, fpath);
256 char *page = strdup(PAGE_NOT_FOUND);
257 *rp_code = MHD_HTTP_NOT_FOUND;
259 return MHD_create_response_from_data
260 (strlen(page), page, MHD_YES, MHD_NO);
264 static int cbk_http_request(void *cls,
265 struct MHD_Connection *connection,
269 const char *upload_data,
270 size_t *upload_data_size, void **ptr)
273 struct MHD_Response *response;
276 unsigned int resp_code;
278 if (strcmp(method, "GET"))
281 if (&dummy != *ptr) {
282 /* The first time only the headers are valid, do not
283 respond in the first round... */
288 if (*upload_data_size)
291 *ptr = NULL; /* clear context pointer */
293 log_debug(_("HTTP Request: %s"), url);
295 nurl = url_normalize(url);
297 pthread_mutex_lock(&mutex);
298 response = create_response(nurl, method, &resp_code);
299 pthread_mutex_unlock(&mutex);
301 ret = MHD_queue_response(connection, resp_code, response);
302 MHD_destroy_response(response);
309 static void *slog_routine(void *data)
315 log_debug("slog_routine");
317 slog_init(path, server_data.sensors);
320 pthread_mutex_lock(&mutex);
321 slog_write_sensors(server_data.sensors);
322 pthread_mutex_unlock(&mutex);
330 int main(int argc, char *argv[])
332 struct MHD_Daemon *d;
333 int port, opti, optc, cmdok, ret;
334 char *log_file, *slog_file;
335 pthread_t slog_thread;
337 program_name = argv[0];
339 setlocale(LC_ALL, "");
342 bindtextdomain(PACKAGE, LOCALEDIR);
346 server_data.www_dir = NULL;
347 server_data.psysinfo.interfaces = NULL;
353 while ((optc = getopt_long(argc,
361 server_data.www_dir = strdup(optarg);
374 log_level = atoi(optarg);
375 log_info(_("Enables debug mode: %d"), log_level);
379 log_file = strdup(optarg);
382 if (!strcmp(long_options[opti].name, "sensor-log-file"))
383 slog_file = strdup(optarg);
391 if (!cmdok || optind != argc) {
392 fprintf(stderr, _("Try `%s --help' for more information.\n"),
397 if (!server_data.www_dir)
398 server_data.www_dir = strdup(DEFAULT_WWW_DIR);
401 log_file = strdup(DEFAULT_LOG_FILE);
407 server_data.sensors = get_all_sensors(0, 600);
410 server_data.cpu_usage = create_cpu_usage_sensor(600);
413 if (!*server_data.sensors)
414 log_err(_("No sensors detected."));
416 d = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION,
418 NULL, NULL, &cbk_http_request, server_data.sensors,
421 log_err(_("Failed to create Web server."));
425 log_info(_("Web server started on port: %d"), port);
426 log_info(_("WWW directory: %s"), server_data.www_dir);
427 log_info(_("URL: http://localhost:%d"), port);
430 ret = pthread_create(&slog_thread,
435 log_err(_("Failed to create sensor log thread."));
438 while (!server_stop_requested) {
439 pthread_mutex_lock(&mutex);
442 sysinfo_update(&server_data.psysinfo);
443 cpu_usage_sensor_update(server_data.cpu_usage);
445 psensor_list_update_measures(server_data.sensors);
447 psensor_log_measures(server_data.sensors);
449 pthread_mutex_unlock(&mutex);
457 /* sanity cleanup for valgrind */
458 psensor_list_free(server_data.sensors);
460 psensor_free(server_data.cpu_usage);
462 free(server_data.www_dir);
470 if (log_file != DEFAULT_LOG_FILE)