Changeset dcd539d74168658a6f7fae5302239f74553717bb
- Timestamp:
- 07/01/08 20:16:09
(5 years ago)
- Author:
- Theo Schlossnagle <jesus@omniti.com>
- git-committer:
- Theo Schlossnagle <jesus@omniti.com> 1214943369 +0000
- git-parent:
[a19b8157a03891ec782d7a880a640be1c6c98663]
- git-author:
- Theo Schlossnagle <jesus@omniti.com> 1214943369 +0000
- Message:
implements stratcon configuration storage in DB, refs #27
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r70451c5 |
rdcd539d |
|
| 462 | 462 | int len; |
|---|
| 463 | 463 | int allocd; |
|---|
| 464 | | enum { CONFIG_RAW = 0, CONFIG_COMPRESSED, CONFIG_B64 } encoded; |
|---|
| | 464 | enum { CONFIG_RAW = 0, CONFIG_COMPRESSED, CONFIG_B64 } target, encoded; |
|---|
| 465 | 465 | }; |
|---|
| 466 | 466 | static int |
|---|
| … | … | |
| 494 | 494 | |
|---|
| 495 | 495 | if(clv->buff == NULL) { |
|---|
| 496 | | clv->encoded = CONFIG_B64; |
|---|
| | 496 | clv->encoded = clv->target; |
|---|
| 497 | 497 | return 0; |
|---|
| 498 | 498 | } |
|---|
| 499 | 499 | clv->raw_len = clv->len; |
|---|
| 500 | 500 | assert(clv->encoded == CONFIG_RAW); |
|---|
| | 501 | if(clv->encoded == clv->target) return 0; |
|---|
| | 502 | |
|---|
| 501 | 503 | /* Compress */ |
|---|
| 502 | 504 | initial_dlen = dlen = compressBound(clv->len); |
|---|
| … | … | |
| 514 | 516 | clv->len = dlen; |
|---|
| 515 | 517 | clv->encoded = CONFIG_COMPRESSED; |
|---|
| | 518 | if(clv->encoded == clv->target) return 0; |
|---|
| | 519 | |
|---|
| 516 | 520 | /* Encode */ |
|---|
| 517 | 521 | initial_dlen = ((clv->len + 2) / 3) * 4; |
|---|
| … | … | |
| 528 | 532 | clv->len = dlen; |
|---|
| 529 | 533 | clv->encoded = CONFIG_B64; |
|---|
| 530 | | return 0; |
|---|
| | 534 | if(clv->encoded == clv->target) return 0; |
|---|
| | 535 | return -1; |
|---|
| 531 | 536 | } |
|---|
| 532 | 537 | |
|---|
| … | … | |
| 592 | 597 | return 0; |
|---|
| 593 | 598 | } |
|---|
| | 599 | char * |
|---|
| | 600 | noit_conf_xml_in_mem(size_t *len) { |
|---|
| | 601 | struct config_line_vstr *clv; |
|---|
| | 602 | xmlOutputBufferPtr out; |
|---|
| | 603 | xmlCharEncodingHandlerPtr enc; |
|---|
| | 604 | char *rv; |
|---|
| | 605 | |
|---|
| | 606 | clv = calloc(1, sizeof(*clv)); |
|---|
| | 607 | clv->target = CONFIG_RAW; |
|---|
| | 608 | enc = xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF8); |
|---|
| | 609 | out = xmlOutputBufferCreateIO(noit_config_log_write_xml, |
|---|
| | 610 | noit_config_log_close_xml, |
|---|
| | 611 | clv, enc); |
|---|
| | 612 | xmlSaveFormatFileTo(out, master_config, "utf8", 1); |
|---|
| | 613 | if(clv->encoded != CONFIG_RAW) { |
|---|
| | 614 | noitL(noit_error, "Error logging configuration\n"); |
|---|
| | 615 | if(clv->buff) free(clv->buff); |
|---|
| | 616 | free(clv); |
|---|
| | 617 | return NULL; |
|---|
| | 618 | } |
|---|
| | 619 | rv = clv->buff; |
|---|
| | 620 | *len = clv->len; |
|---|
| | 621 | free(clv); |
|---|
| | 622 | return rv; |
|---|
| | 623 | } |
|---|
| | 624 | |
|---|
| 594 | 625 | int |
|---|
| 595 | 626 | noit_conf_write_log() { |
|---|
| … | … | |
| 607 | 638 | gettimeofday(&__now, NULL); |
|---|
| 608 | 639 | clv = calloc(1, sizeof(*clv)); |
|---|
| | 640 | clv->target = CONFIG_B64; |
|---|
| 609 | 641 | enc = xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF8); |
|---|
| 610 | 642 | out = xmlOutputBufferCreateIO(noit_config_log_write_xml, |
|---|
| r70451c5 |
rdcd539d |
|
| 84 | 84 | noit_console_state_t *state, void *closure); |
|---|
| 85 | 85 | |
|---|
| | 86 | API_EXPORT(char *) |
|---|
| | 87 | noit_conf_xml_in_mem(size_t *len); |
|---|
| | 88 | |
|---|
| 86 | 89 | API_EXPORT(int) |
|---|
| 87 | 90 | noit_conf_write_log(); |
|---|
| r70451c5 |
rdcd539d |
|
| 111 | 111 | |
|---|
| 112 | 112 | /* Write our log out, and setup a watchdog to write it out on change. */ |
|---|
| 113 | | noit_conf_write_log(); |
|---|
| | 113 | noit_conf_write_log(NULL); |
|---|
| 114 | 114 | noit_conf_coalesce_changes(10); /* 10 seconds of no changes before we write */ |
|---|
| 115 | 115 | noit_conf_watch_and_journal_watchdog(noit_conf_write_log, NULL); |
|---|
| r70451c5 |
rdcd539d |
|
| 36 | 36 | |
|---|
| 37 | 37 | #define MAX_PARAMS 8 |
|---|
| | 38 | #define POSTGRES_PARTS \ |
|---|
| | 39 | int nparams; \ |
|---|
| | 40 | int metric_type; \ |
|---|
| | 41 | char *paramValues[MAX_PARAMS]; \ |
|---|
| | 42 | int paramLengths[MAX_PARAMS]; \ |
|---|
| | 43 | int paramFormats[MAX_PARAMS]; \ |
|---|
| | 44 | int paramAllocd[MAX_PARAMS]; |
|---|
| | 45 | |
|---|
| | 46 | typedef struct ds_single_detail { |
|---|
| | 47 | POSTGRES_PARTS |
|---|
| | 48 | } ds_single_detail; |
|---|
| 38 | 49 | typedef struct ds_job_detail { |
|---|
| | 50 | /* Postgres specific stuff */ |
|---|
| | 51 | POSTGRES_PARTS |
|---|
| | 52 | |
|---|
| 39 | 53 | char *data; /* The raw string, NULL means the stream is done -- commit. */ |
|---|
| 40 | 54 | int problematic; |
|---|
| 41 | 55 | eventer_t completion_event; /* This event should be registered if non NULL */ |
|---|
| 42 | 56 | struct ds_job_detail *next; |
|---|
| 43 | | |
|---|
| 44 | | /* Postgres specific stuff */ |
|---|
| 45 | | int nparams; |
|---|
| 46 | | int metric_type; |
|---|
| 47 | | char *paramValues[MAX_PARAMS]; |
|---|
| 48 | | int paramLengths[MAX_PARAMS]; |
|---|
| 49 | | int paramFormats[MAX_PARAMS]; |
|---|
| 50 | | int paramAllocd[MAX_PARAMS]; |
|---|
| 51 | 57 | } ds_job_detail; |
|---|
| 52 | 58 | |
|---|
| … | … | |
| 61 | 67 | |
|---|
| 62 | 68 | static void |
|---|
| 63 | | free_params(ds_job_detail *d) { |
|---|
| | 69 | free_params(ds_single_detail *d) { |
|---|
| 64 | 70 | int i; |
|---|
| 65 | 71 | for(i=0; i<d->nparams; i++) |
|---|
| … | … | |
| 83 | 89 | next = q->head; |
|---|
| 84 | 90 | q->head = q->head->next; |
|---|
| 85 | | free_params(next); |
|---|
| | 91 | free_params((ds_single_detail *)next); |
|---|
| 86 | 92 | if(next->data) free(next->data); |
|---|
| 87 | 93 | free(next); |
|---|
| … | … | |
| 215 | 221 | if(len <= 0) { |
|---|
| 216 | 222 | noitL(noit_error, "noitd config base64 decoding error.\n"); |
|---|
| | 223 | free(final_buff); |
|---|
| 217 | 224 | goto bad_row; |
|---|
| 218 | 225 | } |
|---|
| … | … | |
| 221 | 228 | (unsigned char *)token, len)) { |
|---|
| 222 | 229 | noitL(noit_error, "noitd config decompression failure.\n"); |
|---|
| | 230 | free(final_buff); |
|---|
| 223 | 231 | goto bad_row; |
|---|
| 224 | 232 | } |
|---|
| 225 | 233 | if(final_len != actual_final_len) { |
|---|
| 226 | 234 | noitL(noit_error, "noitd config decompression error.\n"); |
|---|
| | 235 | free(final_buff); |
|---|
| 227 | 236 | goto bad_row; |
|---|
| 228 | 237 | } |
|---|
| … | … | |
| 485 | 494 | } |
|---|
| 486 | 495 | } |
|---|
| | 496 | |
|---|
| | 497 | int |
|---|
| | 498 | stratcon_datastore_saveconfig(void *unused) { |
|---|
| | 499 | int rv = -1; |
|---|
| | 500 | conn_q _cq = { 0 }, *cq = &_cq; |
|---|
| | 501 | char *buff; |
|---|
| | 502 | ds_single_detail _d = { 0 }, *d = &_d; |
|---|
| | 503 | |
|---|
| | 504 | if(stratcon_database_connect(cq) == 0) { |
|---|
| | 505 | char time_as_str[20]; |
|---|
| | 506 | size_t len; |
|---|
| | 507 | buff = noit_conf_xml_in_mem(&len); |
|---|
| | 508 | if(!buff) goto bad_row; |
|---|
| | 509 | |
|---|
| | 510 | snprintf(time_as_str, sizeof(time_as_str), "%lu", time(NULL)); |
|---|
| | 511 | DECLARE_PARAM_STR("0.0.0.0", 7); |
|---|
| | 512 | DECLARE_PARAM_STR("stratcond", 9); |
|---|
| | 513 | DECLARE_PARAM_STR(time_as_str, strlen(time_as_str)); |
|---|
| | 514 | DECLARE_PARAM_STR(buff, len); |
|---|
| | 515 | free(buff); |
|---|
| | 516 | |
|---|
| | 517 | GET_QUERY(config_insert); |
|---|
| | 518 | PG_EXEC(config_insert); |
|---|
| | 519 | rv = 0; |
|---|
| | 520 | |
|---|
| | 521 | bad_row: |
|---|
| | 522 | free_params(d); |
|---|
| | 523 | } |
|---|
| | 524 | if(cq->dbh) PQfinish(cq->dbh); |
|---|
| | 525 | return rv; |
|---|
| | 526 | } |
|---|
| ra7304b5 |
rdcd539d |
|
| 23 | 23 | struct sockaddr *, void *); |
|---|
| 24 | 24 | |
|---|
| | 25 | API_EXPORT(int) |
|---|
| | 26 | stratcon_datastore_saveconfig(void *unused); |
|---|
| | 27 | |
|---|
| 25 | 28 | #endif |
|---|
| ra7304b5 |
rdcd539d |
|
| 16 | 16 | #include "noit_conf.h" |
|---|
| 17 | 17 | #include "stratcon_jlog_streamer.h" |
|---|
| | 18 | #include "stratcon_datastore.h" |
|---|
| 18 | 19 | |
|---|
| 19 | 20 | #define APPNAME "stratcon" |
|---|
| … | … | |
| 101 | 102 | stratcon_jlog_streamer_init(APPNAME); |
|---|
| 102 | 103 | |
|---|
| | 104 | /* Write our log out, and setup a watchdog to write it out on change. */ |
|---|
| | 105 | stratcon_datastore_saveconfig(NULL); |
|---|
| | 106 | noit_conf_coalesce_changes(10); /* 10 seconds of no changes before we write */ |
|---|
| | 107 | noit_conf_watch_and_journal_watchdog(stratcon_datastore_saveconfig, NULL); |
|---|
| | 108 | |
|---|
| 103 | 109 | eventer_loop(); |
|---|
| 104 | 110 | return 0; |
|---|