comparison src/wflogs-config.cpp @ 4:37eace15ef87

allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
author Carl Byington <carl@five-ten-sg.com>
date Fri, 17 May 2013 12:03:21 -0700
parents 400b1de6e1c6
children a043b7d7269c
comparison
equal deleted inserted replaced
3:2ea606326f5b 4:37eace15ef87
11 #include <sys/socket.h> 11 #include <sys/socket.h>
12 #include <netinet/in.h> 12 #include <netinet/in.h>
13 #include <arpa/inet.h> 13 #include <arpa/inet.h>
14 #include <netdb.h> 14 #include <netdb.h>
15 #include <limits.h> 15 #include <limits.h>
16 #include <time.h>
17
18
19 string_set all_strings;// owns all the strings, only modified by the config loader thread
20 const int maxlen = 1000; // used for snprintf buffers
16 21
17 const char *token_context; 22 const char *token_context;
23 const char *token_daily;
18 const char *token_file; 24 const char *token_file;
25 const char *token_hourly;
19 const char *token_include; 26 const char *token_include;
20 const char *token_lbrace; 27 const char *token_lbrace;
21 const char *token_output; 28 const char *token_output;
22 const char *token_pattern; 29 const char *token_pattern;
23 const char *token_period; 30 const char *token_period;
24 const char *token_rbrace; 31 const char *token_rbrace;
25 const char *token_semi; 32 const char *token_semi;
26 const char *token_tempin; 33 const char *token_tempin;
34 const char *token_trigger;
27 const char *token_versions; 35 const char *token_versions;
36 const char *token_weekly;
28 const char *token_wflogs; 37 const char *token_wflogs;
29
30 string_set all_strings;// owns all the strings, only modified by the config loader thread
31 const int maxlen = 1000; // used for snprintf buffers
32
33 38
34 39
35 //////////////////////////////////////////////// 40 ////////////////////////////////////////////////
36 // 41 //
37 CONTEXT::CONTEXT(const char *nam) { 42 CONTEXT::CONTEXT(const char *nam) {
39 fd = -1; 44 fd = -1;
40 len = 0; 45 len = 0;
41 fdo = -1; 46 fdo = -1;
42 period = 120; 47 period = 120;
43 versions = 3; 48 versions = 3;
49 trigger = NULL;
44 output = NULL; 50 output = NULL;
45 tempin = NULL; 51 tempin = NULL;
46 wflogs = NULL; 52 wflogs = NULL;
47 fn = NULL; 53 fn = NULL;
48 pattern = NULL; 54 pattern = NULL;
56 62
57 void CONTEXT::dump() { 63 void CONTEXT::dump() {
58 printf("context %s {\n", name); 64 printf("context %s {\n", name);
59 printf(" period %d; \n", period); 65 printf(" period %d; \n", period);
60 printf(" versions %d; \n", versions); 66 printf(" versions %d; \n", versions);
67 if (trigger) printf(" trigger \"%s\";\n", trigger);
61 printf(" output \"%s\";\n", output); 68 printf(" output \"%s\";\n", output);
62 printf(" tempin \"%s\";\n", tempin); 69 printf(" tempin \"%s\";\n", tempin);
63 printf(" wflogs \"%s\";\n", wflogs); 70 printf(" wflogs \"%s\";\n", wflogs);
64 printf(" file \"%s\";\n", fn); 71 printf(" file \"%s\";\n", fn);
65 printf(" pattern \"%s\";\n", pattern); 72 printf(" pattern \"%s\";\n", pattern);
67 } 74 }
68 75
69 76
70 void CONTEXT::openo(bool msg) { 77 void CONTEXT::openo(bool msg) {
71 open_time = time(NULL); 78 open_time = time(NULL);
72 fdo = ::creat(tempin, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); 79 localtime_r(&open_time, &open_tm);
80 fdo = ::open(tempin, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
73 if (fdo == -1) { 81 if (fdo == -1) {
74 if (msg) { 82 if (msg) {
75 char buf[maxlen]; 83 char buf[maxlen];
76 snprintf(buf, sizeof(buf), "wflogs tempin file %s not writeable", tempin); 84 snprintf(buf, sizeof(buf), "wflogs tempin file %s not writeable", tempin);
77 tokp->token_error(buf); 85 tokp->token_error(buf);
78 } 86 }
87 }
88 else {
89 lseek(fdo, 0, SEEK_END);
79 } 90 }
80 } 91 }
81 92
82 93
83 void CONTEXT::open(bool msg) { 94 void CONTEXT::open(bool msg) {
189 } 200 }
190 } 201 }
191 } 202 }
192 203
193 204
205 bool CONTEXT::check_wflog_time() {
206 time_t now_time = time(NULL);
207 tm now_tm;
208 localtime_r(&now_time, &now_tm);
209 return (open_time + period < now_time) || \
210 ((trigger == token_hourly) && (now_tm.tm_hour != open_tm.tm_hour)) || \
211 ((trigger == token_daily) && (now_tm.tm_wday != open_tm.tm_wday)) || \
212 ((trigger == token_weekly) && (now_tm.tm_wday != open_tm.tm_wday) && (now_tm.tm_wday == 0));
213 }
214
215
194 void CONTEXT::check_wflog() { 216 void CONTEXT::check_wflog() {
195 time_t now = time(NULL); 217 if ((fdo != -1) && check_wflog_time()) {
196 if ((fdo != -1) && (open_time + period < now)) {
197 closeo(); 218 closeo();
198 // rename previous wflog html output files 219 // rename previous wflog html output files
199 char buf[maxlen]; 220 char buf[maxlen];
200 char fn1[maxlen]; 221 char fn1[maxlen];
201 char fn2[maxlen]; 222 char fn2[maxlen];
366 char buf[maxlen]; 387 char buf[maxlen];
367 snprintf(buf, sizeof(buf), "pattern %s not valid - %s", con->pattern, bu); 388 snprintf(buf, sizeof(buf), "pattern %s not valid - %s", con->pattern, bu);
368 tok.token_error(buf); 389 tok.token_error(buf);
369 con->pattern = NULL; 390 con->pattern = NULL;
370 } 391 }
371 392 if (!tsa(tok, token_semi)) return false;
393 }
394 else if (have == token_trigger) {
395 have = tok.next();
396 if ((have == token_hourly) || (have == token_daily) || (have == token_weekly)) {
397 con->trigger = have;
398 }
399 else {
400 tok.token_error("hourly/daily/weekly", have);
401 }
372 if (!tsa(tok, token_semi)) return false; 402 if (!tsa(tok, token_semi)) return false;
373 } 403 }
374 else { 404 else {
375 tok.token_error("period/versions/output/tempin/wflogs/file/pattern", have); 405 tok.token_error("period/versions/output/tempin/wflogs/file/pattern", have);
376 return false; 406 return false;
414 //////////////////////////////////////////////// 444 ////////////////////////////////////////////////
415 // init the tokens 445 // init the tokens
416 // 446 //
417 void token_init() { 447 void token_init() {
418 token_context = register_string("context"); 448 token_context = register_string("context");
449 token_daily = register_string("daily");
419 token_file = register_string("file"); 450 token_file = register_string("file");
451 token_hourly = register_string("hourly");
420 token_include = register_string("include"); 452 token_include = register_string("include");
421 token_lbrace = register_string("{"); 453 token_lbrace = register_string("{");
422 token_output = register_string("output"); 454 token_output = register_string("output");
423 token_pattern = register_string("pattern"); 455 token_pattern = register_string("pattern");
424 token_period = register_string("period"); 456 token_period = register_string("period");
425 token_rbrace = register_string("}"); 457 token_rbrace = register_string("}");
426 token_semi = register_string(";"); 458 token_semi = register_string(";");
427 token_tempin = register_string("tempin"); 459 token_tempin = register_string("tempin");
460 token_trigger = register_string("trigger");
428 token_versions = register_string("versions"); 461 token_versions = register_string("versions");
462 token_weekly = register_string("weekly");
429 token_wflogs = register_string("wflogs"); 463 token_wflogs = register_string("wflogs");
430 } 464 }