static void rest_test_check_result(struct check_test_closure *cl) { mtev_http_session_ctx *ctx = cl->restc->http_ctx; mtevL(nlerr, "Flushing check test result\n"); if(cl->restc->call_closure_free) cl->restc->call_closure_free(cl->restc->call_closure); cl->restc->call_closure_free = NULL; cl->restc->call_closure = NULL; cl->restc->fastpath = NULL; if(ctx) { eventer_t conne; if(cl->output == WANTS_JSON) { struct json_object *doc; const char *jsonstr; doc = noit_check_state_as_json(cl->check, 1); mtev_http_response_ok(ctx, "application/json"); jsonstr = json_object_to_json_string(doc); mtev_http_response_append(ctx, jsonstr, strlen(jsonstr)); mtev_http_response_append(ctx, "\n", 1); json_object_put(doc); } else { xmlDocPtr doc = NULL; xmlNodePtr root, state; doc = xmlNewDoc((xmlChar *)"1.0"); root = xmlNewDocNode(doc, NULL, (xmlChar *)"check", NULL); xmlDocSetRootElement(doc, root); state = noit_check_state_as_xml(cl->check, 1); xmlAddChild(root, state); mtev_http_response_ok(ctx, "text/xml"); mtev_http_response_xml(ctx, doc); xmlFreeDoc(doc); } mtev_http_response_end(ctx); conne = mtev_http_connection_event(mtev_http_session_connection(ctx)); if(conne) { // The event already exists, why re-add it? Did we want to update it? //eventer_add(conne); eventer_trigger(conne, EVENTER_READ | EVENTER_WRITE); } } noit_poller_free_check(cl->check); free(cl); }
static void rest_test_check_result(struct check_test_closure *cl) { xmlDocPtr doc = NULL; xmlNodePtr root, state; noit_http_session_ctx *ctx = cl->restc->http_ctx; noitL(nlerr, "Flushing check test result\n"); if(cl->restc->call_closure_free) cl->restc->call_closure_free(cl->restc->call_closure); cl->restc->call_closure_free = NULL; cl->restc->call_closure = NULL; cl->restc->fastpath = NULL; if(ctx) { eventer_t conne = noit_http_connection_event(noit_http_session_connection(ctx)); doc = xmlNewDoc((xmlChar *)"1.0"); root = xmlNewDocNode(doc, NULL, (xmlChar *)"check", NULL); xmlDocSetRootElement(doc, root); state = noit_check_state_as_xml(cl->check); xmlAddChild(root, state); noit_http_response_ok(ctx, "text/xml"); noit_http_response_xml(ctx, doc); noit_http_response_end(ctx); if(conne) { // The event already exists, why re-add it? Did we want to update it? //eventer_add(conne); eventer_trigger(conne, EVENTER_READ | EVENTER_WRITE); } } noit_poller_free_check(cl->check); xmlFreeDoc(doc); free(cl); }
static int rest_show_check(noit_http_rest_closure_t *restc, int npats, char **pats) { noit_http_session_ctx *ctx = restc->http_ctx; xmlXPathObjectPtr pobj = NULL; xmlXPathContextPtr xpath_ctxt = NULL; xmlDocPtr doc = NULL; xmlNodePtr node, root, attr, config, state, tmp, anode; uuid_t checkid; noit_check_t *check; char xpath[1024], *uuid_conf, *module, *value; int rv, cnt, error_code = 500; noit_hash_iter iter = NOIT_HASH_ITER_ZERO; const char *k; int klen; void *data; noit_hash_table *configh; if(npats != 2) goto error; rv = noit_check_xpath(xpath, sizeof(xpath), pats[0], pats[1]); if(rv == 0) goto not_found; if(rv < 0) goto error; noit_conf_xml_xpath(NULL, &xpath_ctxt); pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt); if(!pobj || pobj->type != XPATH_NODESET || xmlXPathNodeSetIsEmpty(pobj->nodesetval)) goto not_found; cnt = xmlXPathNodeSetGetLength(pobj->nodesetval); if(cnt != 1) goto error; node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, 0); uuid_conf = (char *)xmlGetProp(node, (xmlChar *)"uuid"); if(!uuid_conf || uuid_parse(uuid_conf, checkid)) goto error; doc = xmlNewDoc((xmlChar *)"1.0"); root = xmlNewDocNode(doc, NULL, (xmlChar *)"check", NULL); xmlDocSetRootElement(doc, root); #define MYATTR(node,a,n,b) _noit_conf_get_string(node, &(n), "@" #a, &(b)) #define INHERIT(node,a,n,b) \ _noit_conf_get_string(node, &(n), "ancestor-or-self::node()/@" #a, &(b)) #define SHOW_ATTR(parent, node, a) do { \ xmlNodePtr anode = NULL; \ char *value = NULL; \ INHERIT(node, a, anode, value); \ if(value != NULL) { \ int clen, plen;\ const char *cpath, *apath; \ xmlNodePtr child; \ cpath = node ? (char *)xmlGetNodePath(node) : ""; \ apath = anode ? (char *)xmlGetNodePath(anode) : ""; \ clen = strlen(cpath); \ plen = strlen("/noit/checks"); \ child = xmlNewNode(NULL, (xmlChar *)#a); \ xmlNodeAddContent(child, (xmlChar *)value); \ if(!strncmp(cpath, apath, clen) && apath[clen] == '/') { \ } \ else { \ xmlSetProp(child, (xmlChar *)"inherited", (xmlChar *)apath+plen); \ } \ xmlAddChild(parent, child); \ } \ } while(0) attr = xmlNewNode(NULL, (xmlChar *)"attributes"); xmlAddChild(root, attr); SHOW_ATTR(attr,node,uuid); /* Name is odd, it falls back transparently to module */ if(!INHERIT(node, module, tmp, module)) module = NULL; xmlAddChild(attr, (tmp = xmlNewNode(NULL, (xmlChar *)"name"))); if(MYATTR(node, name, anode, value)) xmlNodeAddContent(tmp, (xmlChar *)value); else if(module) xmlNodeAddContent(tmp, (xmlChar *)module); SHOW_ATTR(attr,node,module); SHOW_ATTR(attr,node,target); SHOW_ATTR(attr,node,period); SHOW_ATTR(attr,node,timeout); SHOW_ATTR(attr,node,oncheck); SHOW_ATTR(attr,node,filterset); SHOW_ATTR(attr,node,disable); /* Add the config */ config = xmlNewNode(NULL, (xmlChar *)"config"); configh = noit_conf_get_hash(node, "config"); while(noit_hash_next(configh, &iter, &k, &klen, &data)) NODE_CONTENT(config, k, data); noit_hash_destroy(configh, free, free); free(configh); xmlAddChild(root, config); /* Add the state */ check = noit_poller_lookup(checkid); if(!check) { state = xmlNewNode(NULL, (xmlChar *)"state"); xmlSetProp(state, (xmlChar *)"error", (xmlChar *)"true"); } else state = noit_check_state_as_xml(check); xmlAddChild(root, state); noit_http_response_ok(ctx, "text/xml"); noit_http_response_xml(ctx, doc); noit_http_response_end(ctx); goto cleanup; not_found: noit_http_response_not_found(ctx, "text/html"); noit_http_response_end(ctx); goto cleanup; error: noit_http_response_standard(ctx, error_code, "ERROR", "text/html"); noit_http_response_end(ctx); goto cleanup; cleanup: if(pobj) xmlXPathFreeObject(pobj); if(doc) xmlFreeDoc(doc); return 0; }