| 1 |
/* |
|---|
| 2 |
* Copyright (c) 2007, OmniTI Computer Consulting, Inc. |
|---|
| 3 |
* All rights reserved. |
|---|
| 4 |
* |
|---|
| 5 |
* Redistribution and use in source and binary forms, with or without |
|---|
| 6 |
* modification, are permitted provided that the following conditions are |
|---|
| 7 |
* met: |
|---|
| 8 |
* |
|---|
| 9 |
* * Redistributions of source code must retain the above copyright |
|---|
| 10 |
* notice, this list of conditions and the following disclaimer. |
|---|
| 11 |
* * Redistributions in binary form must reproduce the above |
|---|
| 12 |
* copyright notice, this list of conditions and the following |
|---|
| 13 |
* disclaimer in the documentation and/or other materials provided |
|---|
| 14 |
* with the distribution. |
|---|
| 15 |
* * Neither the name OmniTI Computer Consulting, Inc. nor the names |
|---|
| 16 |
* of its contributors may be used to endorse or promote products |
|---|
| 17 |
* derived from this software without specific prior written |
|---|
| 18 |
* permission. |
|---|
| 19 |
* |
|---|
| 20 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 21 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 22 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 23 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 24 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 25 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 26 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 27 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 28 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 29 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 30 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 31 |
*/ |
|---|
| 32 |
|
|---|
| 33 |
#include "noit_defines.h" |
|---|
| 34 |
#include "eventer/eventer.h" |
|---|
| 35 |
#include "utils/noit_log.h" |
|---|
| 36 |
#include "utils/noit_b64.h" |
|---|
| 37 |
#include "noit_jlog_listener.h" |
|---|
| 38 |
#include "stratcon_jlog_streamer.h" |
|---|
| 39 |
#include "stratcon_datastore.h" |
|---|
| 40 |
#include "stratcon_iep.h" |
|---|
| 41 |
#include "noit_conf.h" |
|---|
| 42 |
#include "noit_check.h" |
|---|
| 43 |
#include "noit_xml.h" |
|---|
| 44 |
|
|---|
| 45 |
#include <unistd.h> |
|---|
| 46 |
#include <sys/fcntl.h> |
|---|
| 47 |
#ifdef HAVE_SYS_FILIO_H |
|---|
| 48 |
#include <sys/filio.h> |
|---|
| 49 |
#endif |
|---|
| 50 |
#include <assert.h> |
|---|
| 51 |
#include <libxml/parser.h> |
|---|
| 52 |
#include <libxml/tree.h> |
|---|
| 53 |
#include <libxml/xmlsave.h> |
|---|
| 54 |
#ifdef OPENWIRE |
|---|
| 55 |
#include "amqcs.h" |
|---|
| 56 |
#else |
|---|
| 57 |
#include "stomp/stomp.h" |
|---|
| 58 |
#endif |
|---|
| 59 |
|
|---|
| 60 |
eventer_jobq_t iep_jobq; |
|---|
| 61 |
static noit_log_stream_t noit_iep = NULL; |
|---|
| 62 |
|
|---|
| 63 |
struct iep_thread_driver { |
|---|
| 64 |
#ifdef OPENWIRE |
|---|
| 65 |
amqcs_connect_options connect_options; |
|---|
| 66 |
amqcs_connection *connection; |
|---|
| 67 |
#else |
|---|
| 68 |
stomp_connection *connection; |
|---|
| 69 |
#endif |
|---|
| 70 |
apr_pool_t *pool; |
|---|
| 71 |
}; |
|---|
| 72 |
pthread_key_t iep_connection; |
|---|
| 73 |
|
|---|
| 74 |
struct iep_job_closure { |
|---|
| 75 |
char *line; /* This is a copy and gets trashed during processing */ |
|---|
| 76 |
char *remote; |
|---|
| 77 |
xmlDocPtr doc; |
|---|
| 78 |
char *doc_str; |
|---|
| 79 |
apr_pool_t *pool; |
|---|
| 80 |
}; |
|---|
| 81 |
|
|---|
| 82 |
static void |
|---|
| 83 |
start_iep_daemon(); |
|---|
| 84 |
|
|---|
| 85 |
static int |
|---|
| 86 |
bust_to_parts(char *in, char **p, int len) { |
|---|
| 87 |
int cnt = 0; |
|---|
| 88 |
char *s = in; |
|---|
| 89 |
while(cnt < len) { |
|---|
| 90 |
p[cnt++] = s; |
|---|
| 91 |
while(*s && *s != '\t') s++; |
|---|
| 92 |
if(!*s) break; |
|---|
| 93 |
*s++ = '\0'; |
|---|
| 94 |
} |
|---|
| 95 |
while(*s) s++; /* Move to end */ |
|---|
| 96 |
if(s > in && *(s-1) == '\n') *(s-1) = '\0'; /* chomp */ |
|---|
| 97 |
return cnt; |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
#define ADDCHILD(a,b) \ |
|---|
| 101 |
xmlNewTextChild(root, NULL, (xmlChar *)(a), (xmlChar *)(b)) |
|---|
| 102 |
#define NEWDOC(xmldoc,n,stanza) do { \ |
|---|
| 103 |
xmlNodePtr root; \ |
|---|
| 104 |
xmldoc = xmlNewDoc((xmlChar *)"1.0"); \ |
|---|
| 105 |
root = xmlNewDocNode(xmldoc, NULL, (xmlChar *)(n), NULL); \ |
|---|
| 106 |
xmlDocSetRootElement(xmldoc, root); \ |
|---|
| 107 |
stanza \ |
|---|
| 108 |
} while(0) |
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
static xmlDocPtr |
|---|
| 112 |
stratcon_iep_doc_from_status(char *data, char *remote) { |
|---|
| 113 |
xmlDocPtr doc; |
|---|
| 114 |
char *parts[7]; |
|---|
| 115 |
if(bust_to_parts(data, parts, 7) != 7) return NULL; |
|---|
| 116 |
/* 'S' TIMESTAMP UUID STATE AVAILABILITY DURATION STATUS_MESSAGE */ |
|---|
| 117 |
NEWDOC(doc, "NoitStatus", |
|---|
| 118 |
{ |
|---|
| 119 |
ADDCHILD("remote", remote); |
|---|
| 120 |
ADDCHILD("id", parts[2]); |
|---|
| 121 |
ADDCHILD("state", parts[3]); |
|---|
| 122 |
ADDCHILD("availability", parts[4]); |
|---|
| 123 |
ADDCHILD("duration", parts[5]); |
|---|
| 124 |
ADDCHILD("status", parts[6]); |
|---|
| 125 |
}); |
|---|
| 126 |
return doc; |
|---|
| 127 |
} |
|---|
| 128 |
|
|---|
| 129 |
static xmlDocPtr |
|---|
| 130 |
stratcon_iep_doc_from_check(char *data, char *remote) { |
|---|
| 131 |
xmlDocPtr doc; |
|---|
| 132 |
char *parts[6]; |
|---|
| 133 |
if(bust_to_parts(data, parts, 6) != 6) return NULL; |
|---|
| 134 |
/* 'C' TIMESTAMP UUID TARGET MODULE NAME */ |
|---|
| 135 |
NEWDOC(doc, "NoitCheck", |
|---|
| 136 |
{ |
|---|
| 137 |
ADDCHILD("remote", remote); |
|---|
| 138 |
ADDCHILD("id", parts[2]); |
|---|
| 139 |
ADDCHILD("target", parts[3]); |
|---|
| 140 |
ADDCHILD("module", parts[4]); |
|---|
| 141 |
ADDCHILD("name", parts[5]); |
|---|
| 142 |
}); |
|---|
| 143 |
return doc; |
|---|
| 144 |
} |
|---|
| 145 |
|
|---|
| 146 |
static xmlDocPtr |
|---|
| 147 |
stratcon_iep_doc_from_metric(char *data, char *remote) { |
|---|
| 148 |
xmlDocPtr doc; |
|---|
| 149 |
char *parts[6]; |
|---|
| 150 |
const char *rootname = "NoitMetricNumeric"; |
|---|
| 151 |
const char *valuename = "value"; |
|---|
| 152 |
if(bust_to_parts(data, parts, 6) != 6) return NULL; |
|---|
| 153 |
/* 'M' TIMESTAMP UUID NAME TYPE VALUE */ |
|---|
| 154 |
|
|---|
| 155 |
if(*parts[4] == METRIC_STRING) { |
|---|
| 156 |
rootname = "NoitMetricText"; |
|---|
| 157 |
valuename = "message"; |
|---|
| 158 |
} |
|---|
| 159 |
NEWDOC(doc, rootname, |
|---|
| 160 |
{ |
|---|
| 161 |
ADDCHILD("remote", remote); |
|---|
| 162 |
ADDCHILD("id", parts[2]); |
|---|
| 163 |
ADDCHILD("name", parts[3]); |
|---|
| 164 |
ADDCHILD(valuename, parts[5]); |
|---|
| 165 |
}); |
|---|
| 166 |
return doc; |
|---|
| 167 |
} |
|---|
| 168 |
|
|---|
| 169 |
static xmlDocPtr |
|---|
| 170 |
stratcon_iep_doc_from_query(char *data, char *remote) { |
|---|
| 171 |
xmlDocPtr doc; |
|---|
| 172 |
char *parts[4]; |
|---|
| 173 |
if(bust_to_parts(data, parts, 4) != 4) return NULL; |
|---|
| 174 |
/* 'Q' ID NAME QUERY */ |
|---|
| 175 |
|
|---|
| 176 |
NEWDOC(doc, "StratconQuery", |
|---|
| 177 |
{ |
|---|
| 178 |
ADDCHILD("id", parts[1]); |
|---|
| 179 |
ADDCHILD("name", parts[2]); |
|---|
| 180 |
ADDCHILD("expression", parts[3]); |
|---|
| 181 |
}); |
|---|
| 182 |
return doc; |
|---|
| 183 |
} |
|---|
| 184 |
|
|---|
| 185 |
static xmlDocPtr |
|---|
| 186 |
stratcon_iep_doc_from_querystop(char *data, char *remote) { |
|---|
| 187 |
xmlDocPtr doc; |
|---|
| 188 |
char *parts[2]; |
|---|
| 189 |
if(bust_to_parts(data, parts, 2) != 2) return NULL; |
|---|
| 190 |
/* 'Q' ID */ |
|---|
| 191 |
|
|---|
| 192 |
NEWDOC(doc, "StratconQueryStop", |
|---|
| 193 |
{ |
|---|
| 194 |
xmlNodeSetContent(root, (xmlChar *)parts[1]); |
|---|
| 195 |
}); |
|---|
| 196 |
return doc; |
|---|
| 197 |
} |
|---|
| 198 |
|
|---|
| 199 |
static xmlDocPtr |
|---|
| 200 |
stratcon_iep_doc_from_line(char *data, char *remote) { |
|---|
| 201 |
if(data) { |
|---|
| 202 |
switch(*data) { |
|---|
| 203 |
case 'C': return stratcon_iep_doc_from_check(data, remote); |
|---|
| 204 |
case 'S': return stratcon_iep_doc_from_status(data, remote); |
|---|
| 205 |
case 'M': return stratcon_iep_doc_from_metric(data, remote); |
|---|
| 206 |
case 'Q': return stratcon_iep_doc_from_query(data, remote); |
|---|
| 207 |
case 'q': return stratcon_iep_doc_from_querystop(data, remote); |
|---|
| 208 |
} |
|---|
| 209 |
} |
|---|
| 210 |
return NULL; |
|---|
| 211 |
} |
|---|
| 212 |
|
|---|
| 213 |
static float |
|---|
| 214 |
stratcon_iep_age_from_line(char *data, struct timeval now) { |
|---|
| 215 |
float n, t; |
|---|
| 216 |
if(data && (*data == 'S' || *data == 'M')) { |
|---|
| 217 |
if(data[1] != '\t') return 0; |
|---|
| 218 |
t = strtof(data + 2, NULL); |
|---|
| 219 |
n = (float)now.tv_sec + (float)now.tv_usec / 1000000.0; |
|---|
| 220 |
return n - t; |
|---|
| 221 |
} |
|---|
| 222 |
return 0; |
|---|
| 223 |
} |
|---|
| 224 |
|
|---|
| 225 |
void stratcon_iep_submit_queries() { |
|---|
| 226 |
int i, cnt = 0; |
|---|
| 227 |
noit_conf_section_t *query_configs; |
|---|
| 228 |
char path[256]; |
|---|
| 229 |
|
|---|
| 230 |
snprintf(path, sizeof(path), "/stratcon/iep/queries//query"); |
|---|
| 231 |
query_configs = noit_conf_get_sections(NULL, path, &cnt); |
|---|
| 232 |
noitL(noit_debug, "Found %d %s stanzas\n", cnt, path); |
|---|
| 233 |
for(i=0; i<cnt; i++) { |
|---|
| 234 |
char id[UUID_STR_LEN]; |
|---|
| 235 |
char topic[256]; |
|---|
| 236 |
char *query; |
|---|
| 237 |
char *line; |
|---|
| 238 |
int line_len; |
|---|
| 239 |
|
|---|
| 240 |
if(!noit_conf_get_stringbuf(query_configs[i], |
|---|
| 241 |
"self::node()/@id", |
|---|
| 242 |
id, sizeof(id))) { |
|---|
| 243 |
noitL(noit_iep, "No uuid specified in query\n"); |
|---|
| 244 |
continue; |
|---|
| 245 |
} |
|---|
| 246 |
if(!noit_conf_get_stringbuf(query_configs[i], |
|---|
| 247 |
"ancestor-or-self::node()/@topic", |
|---|
| 248 |
topic, sizeof(topic))) { |
|---|
| 249 |
noitL(noit_iep, "No topic specified in query\n"); |
|---|
| 250 |
continue; |
|---|
| 251 |
} |
|---|
| 252 |
if(!noit_conf_get_string(query_configs[i], "self::node()", |
|---|
| 253 |
&query)) { |
|---|
| 254 |
noitL(noit_iep, "No contents specified in query\n"); |
|---|
| 255 |
continue; |
|---|
| 256 |
} |
|---|
| 257 |
line_len = 4 /* 3 tabs + \0 */ + |
|---|
| 258 |
1 /* 'Q' */ + 1 /* '\n' */ + |
|---|
| 259 |
strlen(id) + strlen(topic) + strlen(query); |
|---|
| 260 |
line = malloc(line_len); |
|---|
| 261 |
snprintf(line, line_len, "Q\t%s\t%s\t%s\n", id, topic, query); |
|---|
| 262 |
free(query); |
|---|
| 263 |
query = line; |
|---|
| 264 |
while(query[0] && query[1]) { |
|---|
| 265 |
if(*query == '\n') *query = ' '; |
|---|
| 266 |
query++; |
|---|
| 267 |
} |
|---|
| 268 |
stratcon_iep_line_processor(DS_OP_INSERT, NULL, line); |
|---|
| 269 |
} |
|---|
| 270 |
free(query_configs); |
|---|
| 271 |
} |
|---|
| 272 |
|
|---|
| 273 |
static |
|---|
| 274 |
struct iep_thread_driver *stratcon_iep_get_connection() { |
|---|
| 275 |
apr_status_t rc; |
|---|
| 276 |
struct iep_thread_driver *driver; |
|---|
| 277 |
driver = pthread_getspecific(iep_connection); |
|---|
| 278 |
if(!driver) { |
|---|
| 279 |
driver = calloc(1, sizeof(*driver)); |
|---|
| 280 |
pthread_setspecific(iep_connection, driver); |
|---|
| 281 |
} |
|---|
| 282 |
|
|---|
| 283 |
if(!driver->pool) { |
|---|
| 284 |
if(apr_pool_create(&driver->pool, NULL) != APR_SUCCESS) return NULL; |
|---|
| 285 |
} |
|---|
| 286 |
|
|---|
| 287 |
if(!driver->connection) { |
|---|
| 288 |
int port; |
|---|
| 289 |
char hostname[128]; |
|---|
| 290 |
if(!noit_conf_get_int(NULL, "/stratcon/iep/port", &port)) |
|---|
| 291 |
port = 61613; |
|---|
| 292 |
if(!noit_conf_get_stringbuf(NULL, "/stratcon/iep/hostname", |
|---|
| 293 |
hostname, sizeof(hostname))) |
|---|
| 294 |
strlcpy(hostname, "127.0.0.1", sizeof(hostname)); |
|---|
| 295 |
#ifdef OPENWIRE |
|---|
| 296 |
memset(&driver->connect_options, 0, sizeof(driver->connect_options)); |
|---|
| 297 |
strlcpy(driver->connect_options.hostname, hostname, |
|---|
| 298 |
sizeof(driver->connect_options.hostname)); |
|---|
| 299 |
driver->connect_options.port = port; |
|---|
| 300 |
if(amqcs_connect(&driver->connection, &driver->connect_options, |
|---|
| 301 |
driver->pool) != APR_SUCCESS) { |
|---|
| 302 |
noitL(noit_error, "MQ connection failed\n"); |
|---|
| 303 |
return NULL; |
|---|
| 304 |
} |
|---|
| 305 |
#else |
|---|
| 306 |
if(stomp_connect(&driver->connection, hostname, port, |
|---|
| 307 |
driver->pool)!= APR_SUCCESS) { |
|---|
| 308 |
noitL(noit_error, "MQ connection failed\n"); |
|---|
| 309 |
stomp_disconnect(&driver->connection); |
|---|
| 310 |
return NULL; |
|---|
| 311 |
} |
|---|
| 312 |
|
|---|
| 313 |
{ |
|---|
| 314 |
stomp_frame frame; |
|---|
| 315 |
frame.command = "CONNECT"; |
|---|
| 316 |
frame.headers = apr_hash_make(driver->pool); |
|---|
| 317 |
/* |
|---|
| 318 |
We don't use login/pass |
|---|
| 319 |
apr_hash_set(frame.headers, "login", APR_HASH_KEY_STRING, ""); |
|---|
| 320 |
apr_hash_set(frame.headers, "passcode", APR_HASH_KEY_STRING, ""); |
|---|
| 321 |
*/ |
|---|
| 322 |
frame.body = NULL; |
|---|
| 323 |
frame.body_length = -1; |
|---|
| 324 |
rc = stomp_write(driver->connection, &frame, driver->pool); |
|---|
| 325 |
if(rc != APR_SUCCESS) { |
|---|
| 326 |
noitL(noit_error, "MQ STOMP CONNECT failed, %d\n", rc); |
|---|
| 327 |
stomp_disconnect(&driver->connection); |
|---|
| 328 |
return NULL; |
|---|
| 329 |
} |
|---|
| 330 |
} |
|---|
| 331 |
{ |
|---|
| 332 |
stomp_frame *frame; |
|---|
| 333 |
rc = stomp_read(driver->connection, &frame, driver->pool); |
|---|
| 334 |
if (rc != APR_SUCCESS) { |
|---|
| 335 |
noitL(noit_error, "MQ STOMP CONNECT bad response, %d\n", rc); |
|---|
| 336 |
stomp_disconnect(&driver->connection); |
|---|
| 337 |
return NULL; |
|---|
| 338 |
} |
|---|
| 339 |
} |
|---|
| 340 |
#endif |
|---|
| 341 |
stratcon_datastore_iep_check_preload(); |
|---|
| 342 |
stratcon_iep_submit_queries(); |
|---|
| 343 |
} |
|---|
| 344 |
|
|---|
| 345 |
return driver; |
|---|
| 346 |
} |
|---|
| 347 |
|
|---|
| 348 |
static int |
|---|
| 349 |
stratcon_iep_submitter(eventer_t e, int mask, void *closure, |
|---|
| 350 |
struct timeval *now) { |
|---|
| 351 |
float age; |
|---|
| 352 |
struct iep_job_closure *job = closure; |
|---|
| 353 |
/* We only play when it is an asynch event */ |
|---|
| 354 |
if(!(mask & EVENTER_ASYNCH_WORK)) return 0; |
|---|
| 355 |
|
|---|
| 356 |
if(mask & EVENTER_ASYNCH_CLEANUP) { |
|---|
| 357 |
/* free all the memory associated with the batch */ |
|---|
| 358 |
if(job) { |
|---|
| 359 |
if(job->line) free(job->line); |
|---|
| 360 |
if(job->remote) free(job->remote); |
|---|
| 361 |
if(job->doc_str) free(job->doc_str); |
|---|
| 362 |
if(job->doc) xmlFreeDoc(job->doc); |
|---|
| 363 |
if(job->pool) apr_pool_destroy(job->pool); |
|---|
| 364 |
free(job); |
|---|
| 365 |
} |
|---|
| 366 |
return 0; |
|---|
| 367 |
} |
|---|
| 368 |
if(!job->line || job->line[0] == '\0') return 0; |
|---|
| 369 |
|
|---|
| 370 |
if((age = stratcon_iep_age_from_line(job->line, *now)) > 60) { |
|---|
| 371 |
noitL(noit_debug, "Skipping old event %f second old.\n", age); |
|---|
| 372 |
return 0; |
|---|
| 373 |
} |
|---|
| 374 |
job->doc = stratcon_iep_doc_from_line(job->line, job->remote); |
|---|
| 375 |
if(job->doc) { |
|---|
| 376 |
job->doc_str = noit_xmlSaveToBuffer(job->doc); |
|---|
| 377 |
if(job->doc_str) { |
|---|
| 378 |
/* Submit */ |
|---|
| 379 |
struct iep_thread_driver *driver; |
|---|
| 380 |
driver = stratcon_iep_get_connection(); |
|---|
| 381 |
if(driver && driver->pool && driver->connection) { |
|---|
| 382 |
apr_status_t rc; |
|---|
| 383 |
#ifdef OPENWIRE |
|---|
| 384 |
ow_ActiveMQQueue *dest; |
|---|
| 385 |
ow_ActiveMQTextMessage *message; |
|---|
| 386 |
|
|---|
| 387 |
apr_pool_create(&job->pool, driver->pool); |
|---|
| 388 |
message = ow_ActiveMQTextMessage_create(job->pool); |
|---|
| 389 |
message->content = |
|---|
| 390 |
ow_byte_array_create_with_data(job->pool,strlen(job->doc_str), |
|---|
| 391 |
job->doc_str); |
|---|
| 392 |
dest = ow_ActiveMQQueue_create(job->pool); |
|---|
| 393 |
dest->physicalName = ow_string_create_from_cstring(job->pool,"TEST.QUEUE"); |
|---|
| 394 |
rc = amqcs_send(driver->connection, |
|---|
| 395 |
(ow_ActiveMQDestination*)dest, |
|---|
| 396 |
(ow_ActiveMQMessage*)message, |
|---|
| 397 |
1,4,0,job->pool); |
|---|
| 398 |
if(rc != APR_SUCCESS) { |
|---|
| 399 |
noitL(noit_error, "MQ send failed, disconnecting\n"); |
|---|
| 400 |
if(driver->connection) amqcs_disconnect(&driver->connection); |
|---|
| 401 |
driver->connection = NULL; |
|---|
| 402 |
} |
|---|
| 403 |
#else |
|---|
| 404 |
stomp_frame out; |
|---|
| 405 |
|
|---|
| 406 |
apr_pool_create(&job->pool, driver->pool); |
|---|
| 407 |
|
|---|
| 408 |
out.command = "SEND"; |
|---|
| 409 |
out.headers = apr_hash_make(job->pool); |
|---|
| 410 |
apr_hash_set(out.headers, "destination", APR_HASH_KEY_STRING, "/queue/noit.firehose"); |
|---|
| 411 |
apr_hash_set(out.headers, "ack", APR_HASH_KEY_STRING, "auto"); |
|---|
| 412 |
|
|---|
| 413 |
out.body_length = -1; |
|---|
| 414 |
out.body = job->doc_str; |
|---|
| 415 |
rc = stomp_write(driver->connection, &out, job->pool); |
|---|
| 416 |
if(rc != APR_SUCCESS) { |
|---|
| 417 |
noitL(noit_error, "STOMP send failed, disconnecting\n"); |
|---|
| 418 |
if(driver->connection) stomp_disconnect(&driver->connection); |
|---|
| 419 |
driver->connection = NULL; |
|---|
| 420 |
} |
|---|
| 421 |
#endif |
|---|
| 422 |
} |
|---|
| 423 |
else { |
|---|
| 424 |
noitL(noit_error, "Not submitting event, no MQ\n"); |
|---|
| 425 |
} |
|---|
| 426 |
} |
|---|
| 427 |
} |
|---|
| 428 |
else { |
|---|
| 429 |
noitL(noit_iep, "no iep handler for: '%s'\n", job->line); |
|---|
| 430 |
} |
|---|
| 431 |
return 0; |
|---|
| 432 |
} |
|---|
| 433 |
|
|---|
| 434 |
void |
|---|
| 435 |
stratcon_iep_line_processor(stratcon_datastore_op_t op, |
|---|
| 436 |
struct sockaddr *remote, void *operand) { |
|---|
| 437 |
int len; |
|---|
| 438 |
char remote_str[128]; |
|---|
| 439 |
struct iep_job_closure *jc; |
|---|
| 440 |
eventer_t newe; |
|---|
| 441 |
struct timeval __now, iep_timeout = { 20L, 0L }; |
|---|
| 442 |
/* We only care about inserts */ |
|---|
| 443 |
|
|---|
| 444 |
if(op == DS_OP_CHKPT) { |
|---|
| 445 |
eventer_add((eventer_t) operand); |
|---|
| 446 |
return; |
|---|
| 447 |
} |
|---|
| 448 |
if(op != DS_OP_INSERT) return; |
|---|
| 449 |
|
|---|
| 450 |
snprintf(remote_str, sizeof(remote_str), "%s", "0.0.0.0"); |
|---|
| 451 |
if(remote) { |
|---|
| 452 |
switch(remote->sa_family) { |
|---|
| 453 |
case AF_INET: |
|---|
| 454 |
len = sizeof(struct sockaddr_in); |
|---|
| 455 |
inet_ntop(remote->sa_family, &((struct sockaddr_in *)remote)->sin_addr, |
|---|
| 456 |
remote_str, len); |
|---|
| 457 |
break; |
|---|
| 458 |
case AF_INET6: |
|---|
| 459 |
len = sizeof(struct sockaddr_in6); |
|---|
| 460 |
inet_ntop(remote->sa_family, &((struct sockaddr_in6 *)remote)->sin6_addr, |
|---|
| 461 |
remote_str, len); |
|---|
| 462 |
break; |
|---|
| 463 |
case AF_UNIX: |
|---|
| 464 |
len = SUN_LEN(((struct sockaddr_un *)remote)); |
|---|
| 465 |
snprintf(remote_str, sizeof(remote_str), "%s", ((struct sockaddr_un *)remote)->sun_path); |
|---|
| 466 |
break; |
|---|
| 467 |
} |
|---|
| 468 |
} |
|---|
| 469 |
|
|---|
| 470 |
/* process operand and push onto queue */ |
|---|
| 471 |
gettimeofday(&__now, NULL); |
|---|
| 472 |
newe = eventer_alloc(); |
|---|
| 473 |
newe->mask = EVENTER_ASYNCH; |
|---|
| 474 |
add_timeval(__now, iep_timeout, &newe->whence); |
|---|
| 475 |
newe->callback = stratcon_iep_submitter; |
|---|
| 476 |
jc = calloc(1, sizeof(*jc)); |
|---|
| 477 |
jc->line = operand; |
|---|
| 478 |
jc->remote = strdup(remote_str); |
|---|
| 479 |
newe->closure = jc; |
|---|
| 480 |
|
|---|
| 481 |
eventer_add_asynch(&iep_jobq, newe); |
|---|
| 482 |
} |
|---|
| 483 |
|
|---|
| 484 |
static void connection_destroy(void *vd) { |
|---|
| 485 |
struct iep_thread_driver *driver = vd; |
|---|
| 486 |
#ifdef OPENWIRE |
|---|
| 487 |
if(driver->connection) amqcs_disconnect(&driver->connection); |
|---|
| 488 |
#else |
|---|
| 489 |
if(driver->connection) stomp_disconnect(&driver->connection); |
|---|
| 490 |
#endif |
|---|
| 491 |
if(driver->pool) apr_pool_destroy(driver->pool); |
|---|
| 492 |
free(driver); |
|---|
| 493 |
} |
|---|
| 494 |
|
|---|
| 495 |
jlog_streamer_ctx_t * |
|---|
| 496 |
stratcon_jlog_streamer_iep_ctx_alloc(void) { |
|---|
| 497 |
jlog_streamer_ctx_t *ctx; |
|---|
| 498 |
ctx = stratcon_jlog_streamer_ctx_alloc(); |
|---|
| 499 |
ctx->jlog_feed_cmd = htonl(NOIT_JLOG_DATA_TEMP_FEED); |
|---|
| 500 |
ctx->push = stratcon_iep_line_processor; |
|---|
| 501 |
return ctx; |
|---|
| 502 |
} |
|---|
| 503 |
|
|---|
| 504 |
struct iep_daemon_info { |
|---|
| 505 |
pid_t child; |
|---|
| 506 |
int stdin_pipe[2]; |
|---|
| 507 |
int stderr_pipe[2]; |
|---|
| 508 |
char *directory; |
|---|
| 509 |
char *command; |
|---|
| 510 |
}; |
|---|
| 511 |
|
|---|
| 512 |
static void |
|---|
| 513 |
iep_daemon_info_free(struct iep_daemon_info *info) { |
|---|
| 514 |
if(!info) return; |
|---|
| 515 |
if(info->directory) free(info->directory); |
|---|
| 516 |
if(info->command) free(info->command); |
|---|
| 517 |
if(info->stdin_pipe[0] >= 0) close(info->stdin_pipe[0]); |
|---|
| 518 |
if(info->stdin_pipe[1] >= 0) close(info->stdin_pipe[1]); |
|---|
| 519 |
if(info->stderr_pipe[0] >= 0) close(info->stderr_pipe[0]); |
|---|
| 520 |
if(info->stderr_pipe[1] >= 0) close(info->stderr_pipe[1]); |
|---|
| 521 |
free(info); |
|---|
| 522 |
} |
|---|
| 523 |
|
|---|
| 524 |
static int |
|---|
| 525 |
stratcon_iep_err_handler(eventer_t e, int mask, void *closure, |
|---|
| 526 |
struct timeval *now) { |
|---|
| 527 |
int len, newmask; |
|---|
| 528 |
char buff[4096]; |
|---|
| 529 |
struct iep_daemon_info *info = (struct iep_daemon_info *)closure; |
|---|
| 530 |
|
|---|
| 531 |
if(mask & EVENTER_EXCEPTION) { |
|---|
| 532 |
int rv; |
|---|
| 533 |
read_error: |
|---|
| 534 |
kill(SIGKILL, info->child); |
|---|
| 535 |
if(waitpid(info->child, &rv, 0) != info->child) { |
|---|
| 536 |
noitL(noit_error, "Failed to reap IEP daemon\n"); |
|---|
| 537 |
exit(-1); |
|---|
| 538 |
} |
|---|
| 539 |
noitL(noit_error, "IEP daemon is done, starting a new one\n"); |
|---|
| 540 |
start_iep_daemon(); |
|---|
| 541 |
eventer_remove_fd(e->fd); |
|---|
| 542 |
e->opset->close(e->fd, &newmask, e); |
|---|
| 543 |
return 0; |
|---|
| 544 |
} |
|---|
| 545 |
while(1) { |
|---|
| 546 |
len = e->opset->read(e->fd, buff, sizeof(buff)-1, &newmask, e); |
|---|
| 547 |
if(len == -1 && (errno == EAGAIN || errno == EINTR)) |
|---|
| 548 |
return newmask | EVENTER_EXCEPTION; |
|---|
| 549 |
if(len <= 0) goto read_error; |
|---|
| 550 |
assert(len < sizeof(buff)); |
|---|
| 551 |
buff[len] = '\0'; |
|---|
| 552 |
noitL(noit_iep, "%s", buff); |
|---|
| 553 |
} |
|---|
| 554 |
} |
|---|
| 555 |
|
|---|
| 556 |
static void |
|---|
| 557 |
start_iep_daemon() { |
|---|
| 558 |
eventer_t newe; |
|---|
| 559 |
struct iep_daemon_info *info; |
|---|
| 560 |
|
|---|
| 561 |
info = calloc(1, sizeof(*info)); |
|---|
| 562 |
info->stdin_pipe[0] = info->stdin_pipe[1] = -1; |
|---|
| 563 |
info->stderr_pipe[0] = info->stderr_pipe[1] = -1; |
|---|
| 564 |
|
|---|
| 565 |
if(!noit_conf_get_string(NULL, "/stratcon/iep/start/@directory", |
|---|
| 566 |
&info->directory)) |
|---|
| 567 |
info->directory = strdup("."); |
|---|
| 568 |
if(!noit_conf_get_string(NULL, "/stratcon/iep/start/@command", |
|---|
| 569 |
&info->command)) { |
|---|
| 570 |
noitL(noit_error, "No IEP start command provided. You're on your own.\n"); |
|---|
| 571 |
goto bail; |
|---|
| 572 |
} |
|---|
| 573 |
if(pipe(info->stdin_pipe) != 0 || |
|---|
| 574 |
pipe(info->stderr_pipe) != 0) { |
|---|
| 575 |
noitL(noit_error, "pipe: %s\n", strerror(errno)); |
|---|
| 576 |
goto bail; |
|---|
| 577 |
} |
|---|
| 578 |
info->child = fork(); |
|---|
| 579 |
if(info->child == -1) { |
|---|
| 580 |
noitL(noit_error, "fork: %s\n", strerror(errno)); |
|---|
| 581 |
goto bail; |
|---|
| 582 |
} |
|---|
| 583 |
if(info->child == 0) { |
|---|
| 584 |
char *argv[2] = { "run-iep", NULL }; |
|---|
| 585 |
int stdout_fileno; |
|---|
| 586 |
|
|---|
| 587 |
if(chdir(info->directory) != 0) { |
|---|
| 588 |
noitL(noit_error, "Starting IEP daemon, chdir failed: %s\n", |
|---|
| 589 |
strerror(errno)); |
|---|
| 590 |
exit(-1); |
|---|
| 591 |
} |
|---|
| 592 |
|
|---|
| 593 |
close(info->stdin_pipe[1]); |
|---|
| 594 |
close(info->stderr_pipe[0]); |
|---|
| 595 |
dup2(info->stdin_pipe[0], 0); |
|---|
| 596 |
dup2(info->stderr_pipe[1], 2); |
|---|
| 597 |
stdout_fileno = open("/dev/null", O_WRONLY); |
|---|
| 598 |
dup2(stdout_fileno, 1); |
|---|
| 599 |
|
|---|
| 600 |
exit(execv(info->command, argv)); |
|---|
| 601 |
} |
|---|
| 602 |
/* in the parent */ |
|---|
| 603 |
socklen_t on = 1; |
|---|
| 604 |
|
|---|
| 605 |
close(info->stdin_pipe[0]); |
|---|
| 606 |
info->stdin_pipe[0] = -1; |
|---|
| 607 |
close(info->stderr_pipe[1]); |
|---|
| 608 |
info->stderr_pipe[1] = -1; |
|---|
| 609 |
if(ioctl(info->stderr_pipe[0], FIONBIO, &on)) { |
|---|
| 610 |
goto bail; |
|---|
| 611 |
} |
|---|
| 612 |
|
|---|
| 613 |
newe = eventer_alloc(); |
|---|
| 614 |
newe->fd = info->stderr_pipe[0]; |
|---|
| 615 |
newe->mask = EVENTER_READ | EVENTER_EXCEPTION; |
|---|
| 616 |
newe->callback = stratcon_iep_err_handler; |
|---|
| 617 |
newe->closure = info; |
|---|
| 618 |
eventer_add(newe); |
|---|
| 619 |
info = NULL; |
|---|
| 620 |
|
|---|
| 621 |
return; |
|---|
| 622 |
|
|---|
| 623 |
bail: |
|---|
| 624 |
if(info) { |
|---|
| 625 |
iep_daemon_info_free(info); |
|---|
| 626 |
} |
|---|
| 627 |
noitL(noit_error, "Failed to start IEP daemon\n"); |
|---|
| 628 |
exit(-1); |
|---|
| 629 |
return; |
|---|
| 630 |
} |
|---|
| 631 |
|
|---|
| 632 |
void |
|---|
| 633 |
stratcon_iep_init() { |
|---|
| 634 |
noit_boolean disabled = noit_false; |
|---|
| 635 |
apr_initialize(); |
|---|
| 636 |
atexit(apr_terminate); |
|---|
| 637 |
|
|---|
| 638 |
if(noit_conf_get_boolean(NULL, "/stratcon/iep/@disabled", &disabled) && |
|---|
| 639 |
disabled == noit_true) { |
|---|
| 640 |
noitL(noit_error, "IEP system is disabled!\n"); |
|---|
| 641 |
return; |
|---|
| 642 |
} |
|---|
| 643 |
|
|---|
| 644 |
noit_iep = noit_log_stream_find("error/iep"); |
|---|
| 645 |
if(!noit_iep) noit_iep = noit_error; |
|---|
| 646 |
|
|---|
| 647 |
start_iep_daemon(); |
|---|
| 648 |
|
|---|
| 649 |
eventer_name_callback("stratcon_iep_submitter", stratcon_iep_submitter); |
|---|
| 650 |
pthread_key_create(&iep_connection, connection_destroy); |
|---|
| 651 |
|
|---|
| 652 |
/* start up a thread pool of one */ |
|---|
| 653 |
memset(&iep_jobq, 0, sizeof(iep_jobq)); |
|---|
| 654 |
eventer_jobq_init(&iep_jobq, "iep_submitter"); |
|---|
| 655 |
iep_jobq.backq = eventer_default_backq(); |
|---|
| 656 |
eventer_jobq_increase_concurrency(&iep_jobq); |
|---|
| 657 |
|
|---|
| 658 |
/* setup our live jlog stream */ |
|---|
| 659 |
stratcon_streamer_connection(NULL, NULL, |
|---|
| 660 |
stratcon_jlog_recv_handler, |
|---|
| 661 |
(void *(*)())stratcon_jlog_streamer_iep_ctx_alloc, |
|---|
| 662 |
NULL, |
|---|
| 663 |
jlog_streamer_ctx_free); |
|---|
| 664 |
} |
|---|
| 665 |
|
|---|