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, NULL, 'v'},
68 {"help", no_argument, NULL, 'h'},
69 {"port", required_argument, NULL, 'p'},
70 {"wdir", required_argument, NULL, 'w'},
71 {"debug", required_argument, NULL, 'd'},
72 {"log-file", required_argument, NULL, 'l'},
73 {"sensor-log-file", required_argument, NULL, 0},
74 {"sensor-log-interval", required_argument, NULL, 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(void)
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(void)
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)
249 struct MHD_Response *resp = NULL;
251 if (!strncmp(nurl, URL_BASE_API_1_1, strlen(URL_BASE_API_1_1))) {
252 resp = create_response_api(nurl, method, rp_code);
254 fpath = get_path(nurl, server_data.www_dir);
256 resp = create_response_file(nurl, method, rp_code, fpath);
264 page = strdup(PAGE_NOT_FOUND);
265 *rp_code = MHD_HTTP_NOT_FOUND;
267 return MHD_create_response_from_data(strlen(page),
273 static int cbk_http_request(void *cls,
274 struct MHD_Connection *connection,
278 const char *upload_data,
279 size_t *upload_data_size,
283 struct MHD_Response *response;
286 unsigned int resp_code;
288 if (strcmp(method, "GET"))
291 if (&dummy != *ptr) {
292 /* The first time only the headers are valid, do not
293 respond in the first round... */
298 if (*upload_data_size)
301 *ptr = NULL; /* clear context pointer */
303 log_debug(_("HTTP Request: %s"), url);
305 nurl = url_normalize(url);
308 response = create_response(nurl, method, &resp_code);
309 pmutex_unlock(&mutex);
311 ret = MHD_queue_response(connection, resp_code, response);
312 MHD_destroy_response(response);
319 int main(int argc, char *argv[])
321 struct MHD_Daemon *d;
322 int port, opti, optc, cmdok, ret, slog_interval;
323 char *log_file, *slog_file;
325 program_name = argv[0];
327 setlocale(LC_ALL, "");
330 bindtextdomain(PACKAGE, LOCALEDIR);
334 server_data.www_dir = NULL;
336 server_data.psysinfo.interfaces = NULL;
344 while ((optc = getopt_long(argc,
352 server_data.www_dir = strdup(optarg);
365 log_level = atoi(optarg);
366 log_info(_("Enables debug mode: %d"), log_level);
370 log_file = strdup(optarg);
373 if (!strcmp(long_options[opti].name, "sensor-log-file"))
374 slog_file = strdup(optarg);
375 else if (!strcmp(long_options[opti].name,
376 "sensor-log-interval"))
377 slog_interval = atoi(optarg);
385 if (!cmdok || optind != argc) {
386 fprintf(stderr, _("Try `%s --help' for more information.\n"),
391 if (!server_data.www_dir)
392 server_data.www_dir = strdup(DEFAULT_WWW_DIR);
395 log_file = strdup(DEFAULT_LOG_FILE);
401 hddtemp_psensor_list_append(&server_data.sensors, 600);
403 lmsensor_psensor_list_append(&server_data.sensors, 600);
406 server_data.cpu_usage = create_cpu_usage_sensor(600);
409 if (!*server_data.sensors)
410 log_err(_("No sensors detected."));
412 d = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION,
414 NULL, NULL, &cbk_http_request, server_data.sensors,
417 log_err(_("Failed to create Web server."));
421 log_info(_("Web server started on port: %d"), port);
422 log_info(_("WWW directory: %s"), server_data.www_dir);
423 log_info(_("URL: http://localhost:%d"), port);
426 if (slog_interval <= 0)
428 ret = slog_activate(slog_file,
433 log_err(_("Failed to activate logging of sensors."));
436 while (!server_stop_requested) {
440 sysinfo_update(&server_data.psysinfo);
441 cpu_usage_sensor_update(server_data.cpu_usage);
445 atasmart_psensor_list_update(server_data.sensors);
448 hddtemp_psensor_list_update(server_data.sensors);
450 lmsensor_psensor_list_update(server_data.sensors);
452 psensor_log_measures(server_data.sensors);
454 pmutex_unlock(&mutex);
462 /* sanity cleanup for valgrind */
463 psensor_list_free(server_data.sensors);
465 psensor_free(server_data.cpu_usage);
467 free(server_data.www_dir);
474 if (log_file != DEFAULT_LOG_FILE)