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>
48 #include "psensor_json.h"
54 static const char *DEFAULT_LOG_FILE = "/var/log/psensor-server.log";
56 #define HTML_STOP_REQUESTED \
57 (_("<html><body><p>Server stop requested</p></body></html>"))
59 static const char *program_name;
61 static const int DEFAULT_PORT = 3131;
63 #define PAGE_NOT_FOUND (_("<html><body><p>"\
64 "Page not found - Go to <a href='/'>Main page</a></p></body>"))
66 static struct option long_options[] = {
67 {"version", no_argument, 0, 'v'},
68 {"help", no_argument, 0, 'h'},
69 {"port", required_argument, 0, 'p'},
70 {"wdir", required_argument, 0, 'w'},
71 {"debug", required_argument, 0, 'd'},
72 {"log-file", required_argument, 0, 'l'},
73 {"sensor-log-file", required_argument, 0, 0},
74 {"sensor-log-interval", required_argument, 0, 0},
78 static struct server_data server_data;
80 static pthread_mutex_t mutex;
82 static int server_stop_requested;
84 static void print_version()
86 printf("psensor-server %s\n", VERSION);
87 printf(_("Copyright (C) %s jeanfi@gmail.com\n"
88 "License GPLv2: GNU GPL version 2 or later "
89 "<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n"
90 "This is free software: you are free to change and redistribute it.\n"
91 "There is NO WARRANTY, to the extent permitted by law.\n"),
95 static void print_help()
97 printf(_("Usage: %s [OPTION]...\n"), program_name);
99 puts(_("psensor-server is an HTTP server for monitoring hardware "
100 "sensors remotely."));
104 puts(_(" -h, --help display this help and exit\n"
105 " -v, --version display version information and exit"));
108 puts(_(" -p,--port=PORT webserver port\n"
109 " -w,--wdir=DIR directory containing webserver pages"));
112 puts(_(" -d, --debug=LEVEL "
113 "set the debug level, integer between 0 and 3"));
114 puts(_(" -l, --log-file=PATH set the log file to PATH"));
115 puts(_(" --sensor-log-file=PATH set the sensor log file to PATH"));
116 puts(_(" --sensor-log-interval=S "
117 "set the sensor log interval to S (seconds)"));
120 printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
122 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
126 * Returns the file path corresponding to a given URL
128 static char *get_path(const char *url, const char *www_dir)
133 if (!strlen(url) || !strcmp(url, ".") || !strcmp(url, "/"))
138 res = malloc(strlen(www_dir)+strlen(p)+1);
140 strcpy(res, www_dir);
146 #if MHD_VERSION >= 0x00090200
148 file_reader(void *cls, uint64_t pos, char *buf, size_t max)
151 file_reader(void *cls, uint64_t pos, char *buf, int max)
156 fseeko(file, pos, SEEK_SET);
157 return fread(buf, 1, max, file);
160 static struct MHD_Response *
161 create_response_api(const char *nurl, const char *method, unsigned int *rp_code)
163 struct MHD_Response *resp;
167 if (!strcmp(nurl, URL_BASE_API_1_1_SENSORS)) {
168 page = sensors_to_json_string(server_data.sensors);
170 } else if (!strcmp(nurl, URL_API_1_1_SYSINFO)) {
171 page = sysinfo_to_json_string(&server_data.psysinfo);
172 } else if (!strcmp(nurl, URL_API_1_1_CPU_USAGE)) {
173 page = sensor_to_json_string(server_data.cpu_usage);
175 } else if (!strncmp(nurl, URL_BASE_API_1_1_SENSORS,
176 strlen(URL_BASE_API_1_1_SENSORS))
177 && nurl[strlen(URL_BASE_API_1_1_SENSORS)] == '/') {
179 const char *sid = nurl + strlen(URL_BASE_API_1_1_SENSORS) + 1;
181 s = psensor_list_get_by_id(server_data.sensors, sid);
184 page = sensor_to_json_string(s);
186 } else if (!strcmp(nurl, URL_API_1_1_SERVER_STOP)) {
188 server_stop_requested = 1;
189 page = strdup(HTML_STOP_REQUESTED);
193 *rp_code = MHD_HTTP_OK;
195 resp = MHD_create_response_from_data(strlen(page), page,
198 MHD_add_response_header(resp, MHD_HTTP_HEADER_CONTENT_TYPE,
207 static struct MHD_Response *create_response_file(const char *nurl,
209 unsigned int *rp_code,
216 ret = stat(fpath, &st);
218 if (!ret && (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))) {
219 file = fopen(fpath, "rb");
222 *rp_code = MHD_HTTP_OK;
226 return MHD_create_response_from_data
227 (0, NULL, MHD_NO, MHD_NO);
230 return MHD_create_response_from_callback
235 (MHD_ContentReaderFreeCallback)&fclose);
238 log_err("Failed to open: %s.", fpath);
245 static struct MHD_Response *
246 create_response(const char *nurl, const char *method, unsigned int *rp_code)
248 struct MHD_Response *resp = NULL;
250 if (!strncmp(nurl, URL_BASE_API_1_1, strlen(URL_BASE_API_1_1))) {
251 resp = create_response_api(nurl, method, rp_code);
253 char *fpath = get_path(nurl, server_data.www_dir);
255 resp = create_response_file(nurl, method, rp_code, fpath);
263 char *page = strdup(PAGE_NOT_FOUND);
264 *rp_code = MHD_HTTP_NOT_FOUND;
266 return MHD_create_response_from_data(strlen(page),
272 static int cbk_http_request(void *cls,
273 struct MHD_Connection *connection,
277 const char *upload_data,
278 size_t *upload_data_size, void **ptr)
281 struct MHD_Response *response;
284 unsigned int resp_code;
286 if (strcmp(method, "GET"))
289 if (&dummy != *ptr) {
290 /* The first time only the headers are valid, do not
291 respond in the first round... */
296 if (*upload_data_size)
299 *ptr = NULL; /* clear context pointer */
301 log_debug(_("HTTP Request: %s"), url);
303 nurl = url_normalize(url);
306 response = create_response(nurl, method, &resp_code);
307 pmutex_unlock(&mutex);
309 ret = MHD_queue_response(connection, resp_code, response);
310 MHD_destroy_response(response);
317 int main(int argc, char *argv[])
319 struct MHD_Daemon *d;
320 int port, opti, optc, cmdok, ret, slog_interval;
321 char *log_file, *slog_file;
323 program_name = argv[0];
325 setlocale(LC_ALL, "");
328 bindtextdomain(PACKAGE, LOCALEDIR);
332 server_data.www_dir = NULL;
334 server_data.psysinfo.interfaces = NULL;
342 while ((optc = getopt_long(argc,
350 server_data.www_dir = strdup(optarg);
363 log_level = atoi(optarg);
364 log_info(_("Enables debug mode: %d"), log_level);
368 log_file = strdup(optarg);
371 if (!strcmp(long_options[opti].name, "sensor-log-file"))
372 slog_file = strdup(optarg);
373 else if (!strcmp(long_options[opti].name,
374 "sensor-log-interval"))
375 slog_interval = atoi(optarg);
383 if (!cmdok || optind != argc) {
384 fprintf(stderr, _("Try `%s --help' for more information.\n"),
389 if (!server_data.www_dir)
390 server_data.www_dir = strdup(DEFAULT_WWW_DIR);
393 log_file = strdup(DEFAULT_LOG_FILE);
399 hddtemp_psensor_list_append(&server_data.sensors, 600);
401 lmsensor_psensor_list_append(&server_data.sensors, 600);
404 server_data.cpu_usage = create_cpu_usage_sensor(600);
407 if (!*server_data.sensors)
408 log_err(_("No sensors detected."));
410 d = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION,
412 NULL, NULL, &cbk_http_request, server_data.sensors,
415 log_err(_("Failed to create Web server."));
419 log_info(_("Web server started on port: %d"), port);
420 log_info(_("WWW directory: %s"), server_data.www_dir);
421 log_info(_("URL: http://localhost:%d"), port);
424 if (slog_interval <= 0)
426 ret = slog_activate(slog_file,
431 log_err(_("Failed to activate logging of sensors."));
434 while (!server_stop_requested) {
438 sysinfo_update(&server_data.psysinfo);
439 cpu_usage_sensor_update(server_data.cpu_usage);
443 atasmart_psensor_list_update(server_data.sensors);
446 hddtemp_psensor_list_update(server_data.sensors);
448 lmsensor_psensor_list_update(server_data.sensors);
450 psensor_log_measures(server_data.sensors);
452 pmutex_unlock(&mutex);
460 /* sanity cleanup for valgrind */
461 psensor_list_free(server_data.sensors);
463 psensor_free(server_data.cpu_usage);
465 free(server_data.www_dir);
472 if (log_file != DEFAULT_LOG_FILE)