-
Notifications
You must be signed in to change notification settings - Fork 0
/
houselog.c
594 lines (483 loc) · 17.4 KB
/
houselog.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/* houseportal - A simple web portal for home servers
*
* Copyright 2019, Pascal Martin
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*
* houselog.c - A generic application log recording modules.
*
* SYNOPSYS:
*
* void houselog_initialize (const char *application,
* int argc, const char **argv);
*
* Initialize the environment required to record logs. This must be
* the first function that the application calls.
*
* void houselog_trace (const char *file, int line, const char *level,
* const char *object,
* const char *format, ...);
*
* Record a new trace. The first 3 parameters are handled by macros:
* HOUSE_INFO, HOUSE_WARNING, HOUSE_FAILURE.
*
* void houselog_event (const char *category,
* const char *object,
* const char *action,
* const char *format, ...);
*
* Record a new event.
*
* void houselog_background (time_t now);
*
* This function must be called a regular intervals for background
* processing, e.g. cleanup of expired resources, file backup, etc.
*
* const char *houselog_host (void);
*
* Return the name of the local machine, as used in the logs.
*
* In order to avoid writing frequently to SD cards, the active logs are
* written to /dev/shm, moved to permanent storage at the end of the day.
* To limit loss of data on power outage, the logs are also saved to
* permanent storage every hour.
*
* When the application starts, the module tries to backup any existing
* log file in /dev/shm and then restore any existing file from permanent
* storage to /dev/shm.
*
* Any of these file operations is performed only if the source is more
* recent than the destination.
*/
#include <unistd.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <time.h>
#include "echttp.h"
#include "echttp_static.h"
#include "houselog.h"
static const char *LogTypes = "et";
static const char *LogFolder = "/var/lib/house/log";
static const char *LogName = "portal";
static const char *PortalHost = 0;
static char LocalHost[256] = {0};
struct EventHistory {
struct timeval timestamp;
char category[32];
char object[32];
char action[16];
char description[128];
};
#define HISTORY_DEPTH 256
static struct EventHistory History[HISTORY_DEPTH];
static int HistoryCursor = 0;
static long HistoryLatestId = 0;
FILE *EventFile = 0;
FILE *TraceFile = 0;
static void houselog_backup (char id, const char *method);
static const char *houselog_temp (char id) {
static char buffer[512];
snprintf (buffer, sizeof(buffer), "/dev/shm/house%s_%c.csv", LogName, id);
return buffer;
}
static const char *houselog_name (const struct tm *local, char id, int write) {
static char buffer[512];
char path[256];
snprintf (path, sizeof(path), "%s/%04d/%02d/%02d",
LogFolder,
local->tm_year + 1900, local->tm_mon + 1, local->tm_mday);
if (write) {
snprintf (buffer, sizeof(buffer), "/bin/mkdir -p %s", path);
system (buffer);
}
snprintf (buffer, sizeof(buffer), "%s/%s_%c_%04d%02d%02d.csv",
path, LogName, id,
local->tm_year + 1900, local->tm_mon + 1, local->tm_mday);
return buffer;
}
static FILE *houselog_update (const struct tm *local, char id) {
const char *name = houselog_temp (id);
FILE *fd = fopen (name, "a");
if (fd == 0) {
fprintf (stderr, "%s: cannot open, %s\n", name, strerror (errno));
return 0;
}
if (ftell(fd) <= 0) {
const char *head;
switch (id) {
case 't': head = "TIMESTAMP,LEVEL,FILE,LINE,OBJECT,DESCRIPTION"; break;
case 'e': head = "TIMESTAMP,CATEGORY,OBJECT,ACTION,DESCRIPTION"; break;
default: return 0;
}
fprintf (fd, "%s\n", head);
}
return fd;
}
static FILE *houselog_access (const struct tm *local, char id) {
const char *name = houselog_name (local, id, 0);
FILE *fd = fopen (name, "r");
if (fd == 0) {
fprintf (stderr, "%s: cannot open, %s\n", name, strerror (errno));
return 0;
}
return fd;
}
static void safecpy (char *t, const char *s, int size) {
strncpy (t, s, size);
t[size-1] = 0;
}
static void houselog_updated (void) {
if (HistoryLatestId == 0) {
// Seed the latest event ID based on the first event's time.
// This makes it random enough to make its value change after
// a restart.
HistoryLatestId = (long) (time(0) & 0xffff);
}
HistoryLatestId += 1;
}
void houselog_trace (const char *file, int line, const char *level,
const char *object,
const char *format, ...) {
static struct tm Last = {0};
va_list ap;
char text[1024];
struct timeval timestamp;
gettimeofday (×tamp, 0);
va_start (ap, format);
vsnprintf (text, sizeof(text), format, ap);
va_end (ap);
struct tm local = *localtime (&(timestamp.tv_sec));
if (local.tm_year != Last.tm_year ||
local.tm_mon != Last.tm_mon ||
local.tm_mday != Last.tm_mday) {
if (TraceFile) {
fclose (TraceFile);
TraceFile = 0;
}
Last = local;
}
if (!TraceFile)
TraceFile = houselog_update (&local, 't');
if (echttp_isdebug())
printf ("%s %s, %d: %s %s\n", level, file, line, object, text);
if (TraceFile) {
fprintf (TraceFile, "%ld.%03d,\"%s\",\"%s\",%d,\"%s\",\"%s\"\n",
(long) (timestamp.tv_sec), (int)(timestamp.tv_usec/1000),
level, file, line, object, text);
fflush (TraceFile);
}
houselog_updated();
}
void houselog_event (const char *category,
const char *object,
const char *action,
const char *format, ...) {
static struct tm Last = {0};
va_list ap;
char *text;
struct EventHistory *cursor = History + HistoryCursor;
va_start (ap, format);
vsnprintf (cursor->description, sizeof(cursor->description), format, ap);
va_end (ap);
gettimeofday (&(cursor->timestamp), 0);
safecpy (cursor->category, category, sizeof(cursor->category));
safecpy (cursor->object, object, sizeof(cursor->object));
safecpy (cursor->action, action, sizeof(cursor->action));
text = cursor->description;
HistoryCursor += 1;
if (HistoryCursor >= HISTORY_DEPTH) HistoryCursor = 0;
History[HistoryCursor].timestamp.tv_sec = 0;
struct tm local = *localtime (&(cursor->timestamp.tv_sec));
if (local.tm_year != Last.tm_year ||
local.tm_mon != Last.tm_mon ||
local.tm_mday != Last.tm_mday) {
if (EventFile) {
fclose (EventFile);
EventFile = 0;
}
Last = local;
}
if (!EventFile)
EventFile = houselog_update (&local, 'e');
if (EventFile) {
fprintf (EventFile, "%ld.%03d,\"%s\",\"%s\",\"%s\",\"%s\"\n",
(long) (cursor->timestamp.tv_sec),
(int)(cursor->timestamp.tv_usec/1000),
category, object, action, text);
}
houselog_updated();
}
static int houselog_getheader (time_t now, char *buffer, int size) {
echttp_content_type_json ();
if (PortalHost) {
return snprintf (buffer, size,
"{\"host\":\"%s\",\"proxy\":\"%s\",\"apps\":[\"%s\"],"
"\"timestamp\":%ld,\"%s\":{\"latest\":%ld",
LocalHost, PortalHost, LogName,
(long)now, LogName, HistoryLatestId);
}
return snprintf (buffer, size,
"{\"host\":\"%s\",\"apps\":[\"%s\"],"
"\"timestamp\":%ld,\"%s\":{\"latest\":%ld",
LocalHost, LogName,
(long)now, LogName, HistoryLatestId);
}
static const char *houselog_get (struct tm *local, const char *id) {
static char buffer[65537];
time_t start = mktime(local);
int length;
length = houselog_getheader (time(0), buffer, sizeof(buffer));
length += snprintf (buffer+length, sizeof(buffer)-length, ",\"%s\":[", id);
houselog_backup (id[0], "cp"); // Make sure the file is up-to-date.
FILE *fd = houselog_access (local, id[0]);
if (fd) {
char line[1024];
const char *prefix = "";
fgets (line, sizeof(line), fd); // Consume the header.
while (!feof(fd)) {
line[0] = 0;
fgets (line, sizeof(line), fd);
if (line[0] == 0) continue;
time_t ts = atol(line);
if (ts < start) continue;
char *eol = strchr(line, '\n');
if (eol) *eol = 0;
int wrote = snprintf (buffer+length, sizeof(buffer)-length,
"%s[%s]", prefix, line);
if (wrote >= sizeof(buffer) - length) {
buffer[length] = 0;
break;
}
length += strlen(buffer+length);
prefix = ",";
}
fclose(fd);
}
snprintf (buffer+length, sizeof(buffer)-length, "]}}");
return buffer;
}
static const char *houselog_live (time_t now) {
static char buffer[128+HISTORY_DEPTH*(sizeof(struct EventHistory)+24)] = {0};
const char * prefix = "";
int length;
int i;
length = houselog_getheader (now, buffer, sizeof(buffer));
length += snprintf (buffer+length, sizeof(buffer)-length, ",\"events\":[");
for (i = HistoryCursor + 1; i != HistoryCursor; ++i) {
if (i >= HISTORY_DEPTH) {
i = 0;
if (!HistoryCursor) break;
}
struct EventHistory *cursor = History + i;
if (!(cursor->timestamp.tv_sec)) continue;
int wrote = snprintf (buffer+length, sizeof(buffer)-length,
"%s[%ld%03d,\"%s\",\"%s\",\"%s\",\"%s\"]",
prefix,
(long)(cursor->timestamp.tv_sec),
(int)(cursor->timestamp.tv_usec/1000),
cursor->category,
cursor->object,
cursor->action,
cursor->description);
if (wrote >= sizeof(buffer)-length) {
buffer[length] = 0;
break;
}
length += wrote;
prefix = ",";
}
snprintf (buffer+length, sizeof(buffer)-length, "]}}");
return buffer;
}
static const char *houselog_weblatest (const char *method, const char *uri,
const char *data, int length) {
static char buffer[256];
int written = houselog_getheader (time(0), buffer, sizeof(buffer));
snprintf (buffer+written, sizeof(buffer)-written, "}}");
return buffer;
}
static const char *houselog_webget (const char *method, const char *uri,
const char *data, int length) {
// The default is to show the most recent data.
// To force a specific start time, use the "time" parameter.
// For getting data from another day, set the "date" parameter.
// Both are local time relative to the server.
//
time_t now = time(0);
const char *date = echttp_parameter_get ("date"); // YYYY-MM-DD
const char *hhmm = echttp_parameter_get ("time"); // HH:MM
time_t start = now - 600; // Default: show the last 10 minutes.
struct tm local = *localtime (&start);
const char *id;
if (strstr (uri, "/events")) id = "events";
else if (strstr (uri, "/traces")) id = "traces";
else {
echttp_error (404, "unsupported data type");
return "";
}
echttp_content_type_json ();
if (id[0] == 'e' && !date && !hhmm) {
return houselog_live (now);
}
if (date) {
int year = atoi(date);
if (year >= 2000) {
const char *cursor = strchr (date, '-');
if (cursor) {
if (cursor[1] == '0') ++cursor;
int month = atoi(++cursor);
if (month >= 1 && month <= 12) {
cursor = strchr (cursor, '-');
if (cursor) {
if (cursor[1] == '0') ++cursor;
int day = atoi(++cursor);
if (day >= 1 && day <= 31) {
local.tm_year = year - 1900;
local.tm_mon = month - 1;
local.tm_mday = day;
}
}
}
}
}
}
if (hhmm) {
int hour = atoi(hhmm);
if (hour >= 0 && hour <= 23) {
const char *cursor = strchr (hhmm, ':');
if (cursor) {
if (cursor[1] == '0') ++cursor;
int minute = atoi(++cursor);
if (minute >= 0 && minute <= 59) {
local.tm_sec = 0;
local.tm_min = minute;
local.tm_hour = hour;
}
}
}
}
return houselog_get (&local, id);
}
static void houselog_backup (char id, const char *method) {
struct stat buffer;
const char *tempname = houselog_temp (id);
if (stat (tempname, &buffer) == 0) {
char command[1024];
struct tm oldfile = *localtime (&(buffer.st_ctim.tv_sec));
const char *archivename = houselog_name (&oldfile, id, 1);
snprintf (command, sizeof(command),
"/bin/%s -f -u %s %s", method, tempname, archivename);
system (command);
}
}
static void houselog_restore (const struct tm *local, char id) {
struct stat buffer;
const char *tempname = houselog_temp (id);
houselog_backup (id, "mv"); // Backup any existing shm file first.
const char *archivename = houselog_name (local, id, 0);
if (stat (archivename, &buffer) == 0) {
char command[1024];
snprintf (command, sizeof(command),
"/bin/cp -u %s %s", archivename, tempname);
system (command);
}
}
void houselog_initialize (const char *name, int argc, const char **argv) {
int i;
char uri[256];
const char *folder;
const char *portal = 0;
for (i = 1; i < argc; ++i) {
if (echttp_option_match ("-log=", argv[i], &folder)) {
LogFolder = folder;
continue;
}
if (echttp_option_match("-portal-server=", argv[i], &portal)) continue;
}
if (name) LogName = strdup(name);
gethostname (LocalHost, sizeof(LocalHost));
PortalHost = portal ? portal : LocalHost;
snprintf (uri, sizeof(uri), "/%s/log/traces", LogName);
echttp_route_uri (strdup(uri), houselog_webget);
snprintf (uri, sizeof(uri), "/%s/log/events", LogName);
echttp_route_uri (strdup(uri), houselog_webget);
snprintf (uri, sizeof(uri), "/%s/log/latest", LogName);
echttp_route_uri (strdup(uri), houselog_weblatest);
snprintf (uri, sizeof(uri), "/%s/log/files", LogName);
echttp_static_route (strdup(uri), LogFolder);
// Alternate paths for application-independent web pages.
// (The log files are stored at the same place for all applications.)
//
echttp_route_uri ("/log/traces", houselog_webget);
echttp_route_uri ("/log/events", houselog_webget);
echttp_route_uri ("/log/latest", houselog_weblatest);
echttp_static_route ("/log/files", LogFolder);
time_t now = time(0);
struct tm local = *localtime (&now);
for (i = 0; LogTypes[i] > 0; ++i) {
houselog_restore (&local, LogTypes[i]);
}
houselog_background (now); // Initial state.
houselog_event ("SERVICE", LogName, "STARTING", "ON %s", LocalHost);
}
void houselog_background (time_t now) {
static int LastDay = 0;
static int LastHour = 0;
static time_t LastCleanup = 0;
struct tm local = *localtime (&now);
if (LastCleanup == 0) {
LastDay = local.tm_mday;
LastHour = local.tm_hour;
LastCleanup = now;
return;
}
if (now > LastCleanup + 10) {
int i;
if (TraceFile) {
fclose (TraceFile);
TraceFile = 0;
}
if (EventFile) {
fclose (EventFile);
EventFile = 0;
}
LastCleanup = now;
if (local.tm_mday != LastDay) {
for (i = 0; LogTypes[i] > 0; ++i) {
houselog_backup (LogTypes[i], "mv");
}
LastDay = local.tm_mday;
LastHour = local.tm_hour;
} else if (local.tm_hour != LastHour) {
for (i = 0; LogTypes[i] > 0; ++i) {
houselog_backup (LogTypes[i], "cp");
}
LastHour = local.tm_hour;
}
}
}
const char *houselog_host (void) {
return LocalHost;
}