static int mtev_console_state_lua(mtev_console_closure_t ncct, int argc, char **argv, mtev_console_state_t *state, void *closure) { mtev_lua_repl_userdata_t *info; if(argc > 2) { nc_printf(ncct, "extra arguments not expected.\n"); return -1; } if(argc) { eventer_set_owner(ncct->e, eventer_choose_owner(atoi(argv[0]))); } info = calloc(1, sizeof(*info)); info->self = (mtev_dso_generic_t *)closure; mtev_console_userdata_set(ncct, MTEV_CONSOLE_RAW_MODE, MTEV_CONSOLE_RAW_MODE_ON, NULL); mtev_console_userdata_set(ncct, MTEV_LUA_REPL_USERDATA, info, lua_repl_userdata_free); mtev_console_state_push_state(ncct, state); mtev_console_state_init(ncct); return 0; }
static int noit_console_check(mtev_console_closure_t ncct, int argc, char **argv, mtev_console_state_t *state, void *closure) { int cnt; mtev_conf_t_userdata_t *info; char xpath[1024], newuuid_str[37]; char *uuid_conf = NULL, *wanted; uuid_t checkid; xmlXPathContextPtr xpath_ctxt = NULL; xmlXPathObjectPtr pobj = NULL; xmlNodePtr node = NULL; mtev_boolean creating_new = mtev_false; if(closure) { char *fake_argv[1] = { ".." }; mtev_console_state_pop(ncct, 0, argv, NULL, NULL); mtev_console_config_cd(ncct, 1, fake_argv, NULL, NULL); } mtev_conf_xml_xpath(NULL, &xpath_ctxt); if(argc < 1) { nc_printf(ncct, "requires at least one argument\n"); return -1; } if(argc % 2 == 0) { nc_printf(ncct, "wrong number of arguments\n"); return -1; } info = mtev_console_userdata_get(ncct, MTEV_CONF_T_USERDATA); wanted = strcmp(argv[0], "new") ? argv[0] : NULL; if(info && !wanted) { /* We are creating a new node */ uuid_t out; creating_new = mtev_true; if(strncmp(info->path, "/checks/", strlen("/checks/")) && strcmp(info->path, "/checks")) { nc_printf(ncct, "New checks must be under /checks/\n"); return -1; } if(noit_conf_mkcheck_under(info->path, argc - 1, argv + 1, out)) { nc_printf(ncct, "Error creating new check\n"); return -1; } newuuid_str[0] = '\0'; uuid_unparse_lower(out, newuuid_str); wanted = newuuid_str; } /* We many not be in conf-t mode -- that's fine */ if(noit_console_mkcheck_xpath(xpath, sizeof(xpath), info, wanted)) { nc_printf(ncct, "could not find check '%s'\n", wanted); return -1; } pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt); if(!pobj || pobj->type != XPATH_NODESET || xmlXPathNodeSetIsEmpty(pobj->nodesetval)) { nc_printf(ncct, "no checks found for '%s'\n", wanted); goto out; } cnt = xmlXPathNodeSetGetLength(pobj->nodesetval); if(info && cnt != 1) { nc_printf(ncct, "Ambiguous check specified\n"); goto out; } node = (mtev_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, 0); uuid_conf = (char *)xmlGetProp(node, (xmlChar *)"uuid"); if(!node || !uuid_conf || uuid_parse(uuid_conf, checkid)) { nc_printf(ncct, "%s has invalid or missing UUID!\n", (char *)xmlGetNodePath(node) + strlen("/noit")); goto out; } if(argc > 1 && !creating_new) { if(noit_config_check_update_attrs(node, argc - 1, argv + 1)) nc_printf(ncct, "Partially successful, error setting some attributes\n"); noit_conf_check_bump_seq(node); } if(info) { char *xmlpath; if(info->path) free(info->path); xmlpath = (char *)xmlGetNodePath(node); info->path = strdup(xmlpath + strlen("/noit")); free(xmlpath); uuid_copy(info->current_check, checkid); if(argc > 1) refresh_subchecks(ncct, info); if(state) { mtev_console_state_push_state(ncct, state); mtev_console_state_init(ncct); } goto out; } out: if(uuid_conf) free(uuid_conf); if(pobj) xmlXPathFreeObject(pobj); return 0; }
static int noit_console_filter_configure(mtev_console_closure_t ncct, int argc, char **argv, mtev_console_state_t *state, void *closure) { xmlNodePtr parent, fsnode = NULL; int rv = -1; mtev_conf_t_userdata_t *info; char xpath[1024]; info = mtev_console_userdata_get(ncct, MTEV_CONF_T_USERDATA); if(!info) { nc_printf(ncct, "internal error\n"); goto cleanup; } if(strncmp(info->path, "/filtersets/", strlen("/filtersets/")) && strcmp(info->path, "/filtersets")) { nc_printf(ncct, "filterset only allows inside /filtersets (not %s)\n", info->path); goto cleanup; } if(argc != 1) { nc_printf(ncct, "filterset requires one argument\n"); goto cleanup; } snprintf(xpath, sizeof(xpath), "/%s", info->path); parent = mtev_conf_get_section(NULL, xpath); if(!parent) { nc_printf(ncct, "internal error, can't final current working path\n"); goto cleanup; } snprintf(xpath, sizeof(xpath), "filterset[@name=\"%s\"]", argv[0]); fsnode = mtev_conf_get_section(parent, xpath); if(closure) { int removed; removed = noit_filter_remove(fsnode); nc_printf(ncct, "%sremoved filterset '%s'\n", removed ? "" : "failed to ", argv[0]); if(removed) { CONF_REMOVE(fsnode); xmlUnlinkNode(fsnode); xmlFreeNode(fsnode); } rv = !removed; goto cleanup; } if(!fsnode) { void *vfs; nc_printf(ncct, "Cannot find filterset '%s'\n", argv[0]); LOCKFS(); if(mtev_hash_retrieve(filtersets, argv[0], strlen(argv[0]), &vfs)) { UNLOCKFS(); nc_printf(ncct, "filter of the same name already exists\n"); goto cleanup; } UNLOCKFS(); /* Fine the parent path */ fsnode = xmlNewNode(NULL, (xmlChar *)"filterset"); xmlSetProp(fsnode, (xmlChar *)"name", (xmlChar *)argv[0]); xmlAddChild(parent, fsnode); nc_printf(ncct, "created new filterset\n"); } if(info) { char *xmlpath = NULL; free(info->path); xmlpath = (char *)xmlGetNodePath(fsnode); info->path = strdup(xmlpath + strlen("/noit")); free(xmlpath); strlcpy(info->filter_name, argv[0], sizeof(info->filter_name)); if(state) { mtev_console_state_push_state(ncct, state); mtev_console_state_init(ncct); } } cleanup: return rv; }