Changeset 64e4b06c954e1727bf19b222f32cea893b5cc4c1
- Timestamp:
- 01/31/08 20:55:23 (5 years ago)
- git-parent:
- Files:
-
- src/modules/http.c (modified) (10 diffs)
- src/noit_module.c (modified) (1 diff)
- src/sample.conf (modified) (3 diffs)
- src/utils/noit_hash.c (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
src/modules/http.c
r1afde4e r64e4b06 12 12 #include <math.h> 13 13 14 #include <libxml/parser.h> 15 #include <libxml/tree.h> 16 #include <libxml/xpath.h> 17 14 18 #include "noit_module.h" 15 19 #include "noit_poller.h" 16 20 #include "utils/noit_log.h" 21 #include "utils/noit_hash.h" 17 22 18 23 #include <apr_uri.h> … … 22 27 23 28 #define NOIT_HTTP_VERSION_STRING "0.1" 29 30 typedef struct { 31 noit_hash_table *options; 32 void (*results)(noit_module_t *, noit_check_t); 33 } serf_module_conf_t; 24 34 25 35 typedef struct { … … 81 91 static int serf_recur_handler(eventer_t e, int mask, void *closure, 82 92 struct timeval *now); 93 static void serf_log_results(noit_module_t *self, noit_check_t check); 94 static void resmon_log_results(noit_module_t *self, noit_check_t check); 83 95 84 96 static int serf_config(noit_module_t *self, noit_hash_table *options) { 85 return 0; 97 serf_module_conf_t *conf; 98 conf = calloc(1, sizeof(*conf)); 99 conf->options = options; 100 conf->results = serf_log_results; 101 noit_module_set_userdata(self, conf); 102 return 0; 103 } 104 static int resmon_config(noit_module_t *self, noit_hash_table *options) { 105 serf_module_conf_t *conf; 106 conf = calloc(1, sizeof(*conf)); 107 conf->options = options; 108 if(!conf->options) conf->options = calloc(1, sizeof(*conf->options)); 109 noit_hash_store(conf->options, strdup("url"), strlen("url"), 110 strdup("http://localhost:81/")); 111 conf->results = resmon_log_results; 112 noit_module_set_userdata(self, conf); 113 return 0; 114 } 115 static void generic_log_results(noit_module_t *self, noit_check_t check) { 116 serf_module_conf_t *module_conf; 117 module_conf = noit_module_get_userdata(self); 118 module_conf->results(self, check); 86 119 } 87 120 static void serf_log_results(noit_module_t *self, noit_check_t check) { 88 int expect_code = 200;89 char *code_str;90 121 check_info_t *ci = check->closure; 91 122 struct timeval duration; 92 123 stats_t current; 93 char human_buffer[256];94 char code[4];95 char rt[14];124 int expect_code = 200; 125 char *code_str; 126 char human_buffer[256], code[4], rt[14]; 96 127 97 128 if(noit_hash_retrieve(check->config, "code", strlen("code"), … … 117 148 noit_poller_set_state(check, ¤t); 118 149 } 150 static void resmon_log_results(noit_module_t *self, noit_check_t check) { 151 check_info_t *ci = check->closure; 152 struct timeval duration; 153 stats_t current; 154 int expect_code = 200; 155 int services = 0; 156 char *code_str; 157 char human_buffer[256], code[4], rt[14]; 158 xmlDocPtr resmon_results = NULL; 159 xmlXPathContextPtr xpath_ctxt = NULL; 160 161 if(ci->body.b) resmon_results = xmlParseMemory(ci->body.b, ci->body.l); 162 if(resmon_results) { 163 xmlXPathObjectPtr pobj; 164 xpath_ctxt = xmlXPathNewContext(resmon_results); 165 pobj = xmlXPathEval((xmlChar *)"/ResmonResults/ResmonResult", xpath_ctxt); 166 if(pobj) 167 if(pobj->type == XPATH_NODESET) 168 services = xmlXPathNodeSetGetLength(pobj->nodesetval); 169 xmlXPathFreeObject(pobj); 170 } else { 171 noitL(nlerr, "Error in resmon doc: %s\n", ci->body.b); 172 } 173 if(xpath_ctxt) xmlXPathFreeContext(xpath_ctxt); 174 if(resmon_results) xmlFreeDoc(resmon_results); 175 176 sub_timeval(ci->finish_time, check->last_fire_time, &duration); 177 178 snprintf(rt, sizeof(rt), "%.3fms", 179 (float)duration.tv_sec + (float)duration.tv_usec / 1000000.0); 180 snprintf(human_buffer, sizeof(human_buffer), 181 "services=%d,rt=%s", 182 services, 183 ci->timed_out ? "timeout" : rt); 184 noitL(nldeb, "resmon(%s) [%s]\n", check->target, human_buffer); 185 186 current.duration = duration.tv_sec * 1000 + duration.tv_usec / 1000; 187 current.available = (ci->timed_out || ci->status.code != 200) ? 188 NP_UNAVAILABLE : NP_AVAILABLE; 189 current.state = services ? NP_GOOD : NP_BAD; 190 current.status = human_buffer; 191 noit_poller_set_state(check, ¤t); 192 } 119 193 static int serf_complete(eventer_t e, int mask, 120 194 void *closure, struct timeval *now) { … … 123 197 124 198 noitLT(nldeb, now, "serf_complete(%s)\n", ccl->check->target); 125 serf_log_results(ccl->self, ccl->check);199 generic_log_results(ccl->self, ccl->check); 126 200 if(ci->connection) { 127 201 serf_connection_close(ci->connection); … … 156 230 serf_event_trigger(ci->context, sct->serf_baton, &desc); 157 231 serf_context_prerun(ci->context); 158 if(e->mask == 0) { 159 eventer_remove_fd(e->fd); 160 eventer_free(e); 161 } 232 233 /* We're about to deschedule and free the event, drop our reference */ 234 if(!e->mask) 235 ci->fd_event = NULL; 236 162 237 return e->mask; 163 238 } … … 212 287 b->b = n; 213 288 else { 214 memcpy( b->b, n, b->l);289 memcpy(n, b->b, b->l); 215 290 b->b = n; 216 291 } … … 405 480 apr_status_t status; 406 481 eventer_t newe; 482 serf_module_conf_t *mod_config; 407 483 char *config_url; 408 484 485 mod_config = noit_module_get_userdata(self); 409 486 ci = (check_info_t *)check->closure; 410 487 /* We cannot be running */ … … 437 514 if(!noit_hash_retrieve(check->config, "url", strlen("url"), 438 515 (void **)&config_url)) 439 config_url = "http://localhost/"; 516 if(!mod_config->options || 517 !noit_hash_retrieve(mod_config->options, "url", strlen("url"), 518 (void **)&config_url)) 519 config_url = "http://localhost/"; 440 520 apr_uri_parse(ci->pool, config_url, &ci->url); 441 521 … … 580 660 }; 581 661 662 noit_module_t resmon = { 663 NOIT_MODULE_MAGIC, 664 NOIT_MODULE_ABI_VERSION, 665 "resmon", 666 "libserf-based resmon resource checker", 667 serf_onload, 668 resmon_config, 669 serf_init, 670 serf_initiate_check 671 }; 672 src/noit_module.c
r1afde4e r64e4b06 34 34 else 35 35 snprintf(module_file, sizeof(module_file), "%s/%s.%s", 36 base, name, MODULEEXT);36 base, file, MODULEEXT); 37 37 free(base); 38 38 src/sample.conf
r1afde4e r64e4b06 9 9 <image>ping_icmp</image> 10 10 <name>ping_icmp</name> 11 <config> 12 <key1>value2</key1> 13 <key2>value2</key2> 14 </config> 15 </module> 16 <module> 17 <image>http</image> 18 <name>resmon</name> 11 19 <config> 12 20 <key1>value2</key1> … … 46 54 <checkgroup> 47 55 <module>http</module> 48 <period> 2000</period>49 <timeout> 1000</timeout>56 <period>30000</period> 57 <timeout>5000</timeout> 50 58 <check uuid="1b4e28ba-2fa1-11d2-883f-b9a761bde3fc"> 51 59 <target>66.225.209.31</target> … … 56 64 </check> 57 65 </checkgroup> 66 <checkgroup> 67 <module>resmon</module> 68 <period>30000</period> 69 <timeout>5000</timeout> 70 <check uuid="1b4e28ba-2fa1-11d2-883f-b9a761bde3fd"> 71 <target>10.225.209.36</target> 72 </check> 73 </checkgroup> 58 74 </checks> 59 75 </noit> src/utils/noit_hash.c
r01751d3 r64e4b06 206 206 noit_hash_bucket *b; 207 207 208 if(!h) return 0; 208 209 if(h->table_size == 0) noit_hash_init(h); 209 210 off = __hash(k, klen, h->initval) & (h->table_size-1);
