static int rest_show_check(mtev_http_rest_closure_t *restc, int npats, char **pats) { mtev_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 = NULL, *value = NULL; int rv, mod, mod_cnt, cnt, error_code = 500; mtev_hash_iter iter = MTEV_HASH_ITER_ZERO; const char *k; int klen; void *data; mtev_hash_table *configh; if(npats != 2 && npats != 3) goto error; rv = noit_check_xpath(xpath, sizeof(xpath), pats[0], pats[1]); if(rv == 0) goto not_found; if(rv < 0) goto error; mtev_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 = (mtev_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, 0); uuid_conf = (char *)xmlGetProp(node, (xmlChar *)"uuid"); if(!uuid_conf || uuid_parse(uuid_conf, checkid)) goto error; if(npats == 3 && !strcmp(pats[2], ".json")) { return rest_show_check_json(restc, checkid); } doc = xmlNewDoc((xmlChar *)"1.0"); root = xmlNewDocNode(doc, NULL, (xmlChar *)"check", NULL); xmlDocSetRootElement(doc, root); #define MYATTR(node,a,n,b) _mtev_conf_get_string(node, &(n), "@" #a, &(b)) #define INHERIT(node,a,n,b) \ _mtev_conf_get_string(node, &(n), "ancestor-or-self::node()/@" #a, &(b)) #define SHOW_ATTR(parent, node, a) do { \ char *_value = NULL; \ INHERIT(node, a, anode, _value); \ if(_value != NULL) { \ int clen, plen;\ char *_cpath, *_apath; \ xmlNodePtr child; \ _cpath = node ? (char *)xmlGetNodePath(node) : strdup(""); \ _apath = anode ? (char *)xmlGetNodePath(anode) : strdup(""); \ 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); \ free(_cpath); \ free(_apath); \ free(_value); \ } \ } while(0) attr = xmlNewNode(NULL, (xmlChar *)"attributes"); xmlAddChild(root, attr); SHOW_ATTR(attr,node,uuid); SHOW_ATTR(attr,node,seq); /* 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); if(value) free(value); if(module) free(module); SHOW_ATTR(attr,node,module); SHOW_ATTR(attr,node,target); SHOW_ATTR(attr,node,resolve_rtype); SHOW_ATTR(attr,node,seq); 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 = mtev_conf_get_hash(node, "config"); while(mtev_hash_next(configh, &iter, &k, &klen, &data)) NODE_CONTENT(config, k, data); mtev_hash_destroy(configh, free, free); free(configh); mod_cnt = noit_check_registered_module_cnt(); for(mod=0; mod<mod_cnt; mod++) { xmlNsPtr ns; const char *nsname; char buff[256]; nsname = noit_check_registered_module(mod); snprintf(buff, sizeof(buff), "noit://module/%s", nsname); ns = xmlSearchNs(root->doc, root, (xmlChar *)nsname); if(!ns) ns = xmlNewNs(root, (xmlChar *)buff, (xmlChar *)nsname); if(ns) { configh = mtev_conf_get_namespaced_hash(node, "config", nsname); if(configh) { memset(&iter, 0, sizeof(iter)); while(mtev_hash_next(configh, &iter, &k, &klen, &data)) { NS_NODE_CONTENT(config, ns, "value", data, xmlSetProp(tmp, (xmlChar *)"name", (xmlChar *)k); ); } mtev_hash_destroy(configh, free, free); free(configh); } } }
noit_check_t * noit_fire_check(xmlNodePtr attr, xmlNodePtr config, const char **error) { char *target = NULL, *name = NULL, *module = NULL, *filterset = NULL; char *resolve_rtype = NULL; int timeout = 0, flags = NP_TRANSIENT, i, mod_cnt; noit_module_t *m; noit_check_t *c = NULL; xmlNodePtr a, co; noit_hash_table *conf_hash = NULL; noit_hash_table **moptions = NULL; for(a = attr->children; a; a = a->next) { if(!strcmp((char *)a->name, "target")) target = (char *)xmlNodeGetContent(a); else if(!strcmp((char *)a->name, "name")) name = (char *)xmlNodeGetContent(a); else if(!strcmp((char *)a->name, "module")) module = (char *)xmlNodeGetContent(a); else if(!strcmp((char *)a->name, "filterset")) filterset = (char *)xmlNodeGetContent(a); else if(!strcmp((char *)a->name, "timeout")) { char *timeout_str = (char *)xmlNodeGetContent(a); timeout = atoi(timeout_str); free(timeout_str); } else if(!strcmp((char *)a->name, "resolve_rtype")) resolve_rtype = (char *)xmlNodeGetContent(a); } m = noit_module_lookup(module); if(!m) { *error = "cannot find requested module"; goto error; } conf_hash = calloc(1, sizeof(*conf_hash)); if(config) { for(co = config->children; co; co = co->next) { char *name, *val; xmlChar *tmp_val; name = strdup((char *)co->name); tmp_val = xmlNodeGetContent(co); val = strdup(tmp_val ? (char *)tmp_val : ""); noit_hash_replace(conf_hash, name, strlen(name), val, free, free); xmlFree(tmp_val); } } mod_cnt = noit_check_registered_module_cnt(); if(mod_cnt > 0) { moptions = alloca(mod_cnt * sizeof(*moptions)); memset(moptions, 0, mod_cnt * sizeof(*moptions)); for(i=0; i<mod_cnt; i++) { const char *name; noit_conf_section_t checks; name = noit_check_registered_module(i); checks = noit_conf_get_section(NULL, "/noit/checks"); if(name) moptions[i] = noit_conf_get_namespaced_hash(checks, "config", name); } } if(!m->initiate_check) { *error = "that module cannot run checks"; goto error; } flags |= noit_calc_rtype_flag(resolve_rtype); c = calloc(1, sizeof(*c)); noit_check_update(c, target, name, filterset, conf_hash, moptions, 0, timeout, NULL, flags); c->module = strdup(module); uuid_generate(c->checkid); c->flags |= NP_DISABLED; /* this is hack to know we haven't run it yet */ if(NOIT_CHECK_SHOULD_RESOLVE(c)) noit_check_resolve(c); error: if(conf_hash) { noit_hash_destroy(conf_hash, free, free); free(conf_hash); } if(moptions) { for(i=0; i<mod_cnt; i++) { if(moptions[i]) { noit_hash_destroy(moptions[i], free, free); free(moptions[i]); } } } if(target) xmlFree(target); if(name) xmlFree(name); if(module) xmlFree(module); if(filterset) xmlFree(filterset); if (resolve_rtype) xmlFree(resolve_rtype); return c; }