Changeset 2d2a01235fc4b87f4d1046e89762367af33f539d

Show
Ignore:
Timestamp:
05/14/08 18:30:48 (6 years ago)
Author:
Theo Schlossnagle <jesus@omniti.com>
git-committer:
Theo Schlossnagle <jesus@omniti.com> 1210789848 +0000
git-parent:

[152d473243c4c0e838439fd654a698515e8288fc]

git-author:
Theo Schlossnagle <jesus@omniti.com> 1210789848 +0000
Message:

update check configs on-the-fly. non-check configs are still sections which is ugly. (this now works within checks, but not sections). refs #15

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • src/noit_conf_checks.c

    r125a593 r2d2a012  
    321321  } 
    322322  for(i=0; i<cnt; i++) { 
     323    noit_hash_iter iter = NOIT_HASH_ITER_ZERO; 
     324    const char *k; 
     325    int klen; 
     326    void *data; 
    323327    uuid_t checkid; 
    324328    noit_check_t *check; 
     329    noit_hash_table *config; 
    325330    xmlNodePtr node, anode, mnode = NULL; 
    326331    char *uuid_conf; 
     
    357362    SHOW_ATTR(oncheck); 
    358363    SHOW_ATTR(disable); 
     364    /* Print out all the config settings */ 
     365    config = noit_conf_get_hash(node, "config"); 
     366    while(noit_hash_next(config, &iter, &k, &klen, &data)) { 
     367      nc_printf(ncct, " config::%s: %s\n", k, (const char *)data); 
     368    } 
     369    noit_hash_destroy(config, free, free); 
     370    free(config); 
     371 
    359372    check = noit_poller_lookup(checkid); 
    360373    if(!check) { 
     
    368381      if(!NOIT_CHECK_CONFIGURED(check)) nc_printf(ncct, "%sunconfig", idx++?",":""); 
    369382      if(NOIT_CHECK_DISABLED(check)) nc_printf(ncct, "%sdisabled", idx++?",":""); 
     383      if(!idx) nc_printf(ncct, "idle"); 
    370384      nc_write(ncct, "\n", 1); 
    371385      if(check->stats.current.status) 
     
    704718      int busted = 1; 
    705719      xmlAttr *attr; 
    706       char *uuid_str = "undefined";; 
     720      char *uuid_str = "undefined"; 
    707721 
    708722      if(!titled++) nc_printf(ncct, "== Checks ==\n"); 
     
    806820} 
    807821static int 
     822replace_config(noit_console_closure_t ncct, 
     823               noit_conf_t_userdata_t *info, const char *name, 
     824               const char *value) { 
     825  int i, cnt, rv = -1, active = 0; 
     826  xmlXPathObjectPtr pobj = NULL; 
     827  xmlXPathContextPtr xpath_ctxt = NULL; 
     828  xmlNodePtr node, confignode; 
     829  char xpath[1024], *path; 
     830 
     831  path = info->path; 
     832  if(!strcmp(path, "/")) path = ""; 
     833 
     834  noit_conf_xml_xpath(NULL, &xpath_ctxt); 
     835  if(1) { 
     836    /* Only if checks will fixate this attribute shall we check for 
     837     * child <check> nodes. 
     838     * NOTE: this return nothing and "seems" okay if we are _in_ 
     839     *       a <check> node.  That case is handled below. 
     840     */ 
     841    snprintf(xpath, sizeof(xpath), "/noit/%s//check[@uuid]", path); 
     842    pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt); 
     843    if(!pobj || pobj->type != XPATH_NODESET) goto out; 
     844    cnt = xmlXPathNodeSetGetLength(pobj->nodesetval); 
     845    for(i=0; i<cnt; i++) { 
     846      uuid_t checkid; 
     847      node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, i); 
     848      if(noit_conf_get_uuid(node, "@uuid", checkid)) { 
     849        noit_check_t *check; 
     850        check = noit_poller_lookup(checkid); 
     851        if(NOIT_CHECK_LIVE(check)) active++; 
     852      } 
     853    } 
     854    if(pobj) xmlXPathFreeObject(pobj); 
     855  } 
     856  snprintf(xpath, sizeof(xpath), "/noit/%s", path); 
     857  pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt); 
     858  if(!pobj || pobj->type != XPATH_NODESET) goto out; 
     859  cnt = xmlXPathNodeSetGetLength(pobj->nodesetval); 
     860  if(cnt != 1) { 
     861    nc_printf(ncct, "Internal error: context node disappeared\n"); 
     862    goto out; 
     863  } 
     864  node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, 0); 
     865  if(strcmp((const char *)node->name, "check")) { 
     866    uuid_t checkid; 
     867    /* Detect if  we are actually a <check> node and attempting to 
     868     * change something we shouldn't. 
     869     * This is the counterpart noted above. 
     870     */ 
     871    if(noit_conf_get_uuid(node, "@uuid", checkid)) { 
     872      noit_check_t *check; 
     873      check = noit_poller_lookup(checkid); 
     874      if(NOIT_CHECK_LIVE(check)) active++; 
     875    } 
     876  } 
     877  if(active) { 
     878    nc_printf(ncct, "Cannot set '%s', it would effect %d live check(s)\n", 
     879              name, active); 
     880    goto out; 
     881  } 
     882  if(pobj) xmlXPathFreeObject(pobj); 
     883 
     884  /* Here we want to remove /noit/path/config/name */ 
     885  snprintf(xpath, sizeof(xpath), "/noit/%s/config/%s", path, name); 
     886  pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt); 
     887  if(!pobj || pobj->type != XPATH_NODESET) goto out; 
     888  if(xmlXPathNodeSetGetLength(pobj->nodesetval) > 0) { 
     889    xmlNodePtr toremove; 
     890    toremove = xmlXPathNodeSetItem(pobj->nodesetval, 0); 
     891    xmlUnlinkNode(toremove); 
     892  } 
     893  /* TODO: if there are no more children of config, remove config? */ 
     894  if(value) { 
     895    if(pobj) xmlXPathFreeObject(pobj); 
     896    /* He we create config if needed and place a child node under it */ 
     897    snprintf(xpath, sizeof(xpath), "/noit/%s/config", path); 
     898    pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt); 
     899    if(!pobj || pobj->type != XPATH_NODESET) goto out; 
     900    if(xmlXPathNodeSetGetLength(pobj->nodesetval) == 0) { 
     901      if(pobj) xmlXPathFreeObject(pobj); 
     902      snprintf(xpath, sizeof(xpath), "/noit/%s", path); 
     903      pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt); 
     904      if(!pobj || pobj->type != XPATH_NODESET) goto out; 
     905      if(xmlXPathNodeSetGetLength(pobj->nodesetval) != 1) { 
     906        nc_printf(ncct, "Node disappeared from under you!\n"); 
     907        goto out; 
     908      } 
     909      confignode = xmlNewChild(xmlXPathNodeSetItem(pobj->nodesetval, 0), 
     910                               NULL, (xmlChar *)"config", NULL); 
     911      if(confignode == NULL) { 
     912        nc_printf(ncct, "Error creating config child node.\n"); 
     913        goto out; 
     914      } 
     915    } 
     916    else confignode = xmlXPathNodeSetItem(pobj->nodesetval, 0); 
     917 
     918    assert(confignode); 
     919    /* Now we create a child */ 
     920    xmlNewChild(xmlXPathNodeSetItem(pobj->nodesetval, 0), 
     921                NULL, (xmlChar *)name, (xmlChar *)value); 
     922     
     923  } 
     924  rv = 0; 
     925 out: 
     926  if(pobj) xmlXPathFreeObject(pobj); 
     927  return rv; 
     928} 
     929static int 
    808930replace_attr(noit_console_closure_t ncct, 
    809931             noit_conf_t_userdata_t *info, struct _valid_attr_t *attrinfo, 
     
    9391061} 
    9401062 
     1063int 
     1064noit_console_config_setconfig(noit_console_closure_t ncct, 
     1065                                int argc, char **argv, 
     1066                                noit_console_state_t *state, void *closure) { 
     1067  noit_conf_t_userdata_t *info; 
     1068 
     1069  info = noit_console_userdata_get(ncct, NOIT_CONF_T_USERDATA); 
     1070 
     1071  if(argc != 2) { 
     1072    nc_printf(ncct, "two arguments required.\n"); 
     1073    return -1; 
     1074  } 
     1075  /* Okay, we have an child name and it should be culled from 
     1076   * current path/config. 
     1077   */ 
     1078  if(replace_config(ncct, info, argv[0], argv[1])) { 
     1079    return -1; 
     1080  } 
     1081 
     1082  /* So, we updated an attribute, so we need to reload all checks 
     1083   * that are descendent-or-self of this node. 
     1084   */ 
     1085  refresh_subchecks(ncct, info); 
     1086  return 0; 
     1087} 
     1088 
     1089int 
     1090noit_console_config_unsetconfig(noit_console_closure_t ncct, 
     1091                                int argc, char **argv, 
     1092                                noit_console_state_t *state, void *closure) { 
     1093  noit_conf_t_userdata_t *info; 
     1094 
     1095  info = noit_console_userdata_get(ncct, NOIT_CONF_T_USERDATA); 
     1096 
     1097  if(argc != 1) { 
     1098    nc_printf(ncct, "one argument required.\n"); 
     1099    return -1; 
     1100  } 
     1101  /* Okay, we have an child name and it should be culled from 
     1102   * current path/config. 
     1103   */ 
     1104  if(replace_config(ncct, info, argv[0], NULL)) { 
     1105    return -1; 
     1106  } 
     1107 
     1108  /* So, we updated an attribute, so we need to reload all checks 
     1109   * that are descendent-or-self of this node. 
     1110   */ 
     1111  refresh_subchecks(ncct, info); 
     1112  return 0; 
     1113} 
     1114 
     1115 
    9411116#define NEW_STATE(a) (a) = noit_console_state_alloc() 
    9421117#define ADD_CMD(a,cmd,func,ss,c) \ 
     
    9711146  NEW_STATE(_uattr_state); 
    9721147  noit_console_state_add_check_attrs(_uattr_state, noit_conf_check_unset_attr); 
     1148 
    9731149  NEW_STATE(_unset_state); 
    9741150  DELEGATE_CMD(_unset_state, "attribute", _uattr_state); 
    9751151  ADD_CMD(_unset_state, "section", noit_console_config_section, NULL, (void *)1); 
     1152  ADD_CMD(_unset_state, "config", noit_console_config_unsetconfig, NULL, NULL); 
    9761153  ADD_CMD(_unset_state, "check", noit_console_config_nocheck, NULL, NULL); 
    9771154  
     
    9801157  DELEGATE_CMD(_conf_t_check_state, "attribute", _attr_state); 
    9811158  DELEGATE_CMD(_conf_t_check_state, "no", _unset_state); 
     1159  ADD_CMD(_conf_t_check_state, "config", noit_console_config_setconfig, NULL, NULL); 
    9821160  ADD_CMD(_conf_t_check_state, "status", noit_console_show_check, NULL, NULL); 
    9831161  ADD_CMD(_conf_t_check_state, "exit", noit_console_config_cd, NULL, "..");