Changeset 322ceb0139cf145a94006446310d0aa106998056

Show
Ignore:
Timestamp:
02/11/10 03:16:05 (5 years ago)
Author:
Theo Schlossnagle <jesus@omniti.com>
git-committer:
Theo Schlossnagle <jesus@omniti.com> 1265858165 +0000
git-parent:

[53540929ca978f9ac90d70d66409d40d46acf2d4]

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

This allows for a

<include file="fileinsamedirasmaster.conf"/>

tp be placed anywhere in the master config of either noitd or stratcond.
The include is processed by parsing the specified file and putting all
children of the root element of the included document as replacements for
the children of the <include> node itself.

Special care is taken to make it look like we did not do this when we go to
write the config anywhere.

refs #254

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • src/Makefile.in

    r5b61a6d r322ceb0  
    226226        $(INSTALL) -m 0755 noitd $(DESTDIR)$(sbindir)/noitd 
    227227        $(INSTALL) -m 0644 noit.conf $(DESTDIR)$(sysconfdir)/noit.conf.sample 
     228        $(INSTALL) -m 0644 config_templates.conf $(DESTDIR)$(sysconfdir)/config_templates.conf 
    228229        $(INSTALL) -m 0644 default-ca-chain.crt $(DESTDIR)$(sysconfdir)/default-ca-chain.crt 
    229230        (cd modules && make install-modules DESTDIR=$(DESTDIR)) 
  • src/noit.conf.in

    r9f668ab r322ceb0  
    153153  </filtersets> 
    154154  <config_templates> 
    155     <config id="SwitchPort"> 
    156       <oid_description>IF-MIB::ifDescr.%[:ccns:name]</oid_description> 
    157       <oid_alias>IF-MIB::ifAlias.%[:ccns:name]</oid_alias> 
    158       <oid_speed>IF-MIB::ifSpeed.%[:ccns:name]</oid_speed> 
    159       <oid_adminstatus>IF-MIB::ifAdminStatus.%[:ccns:name]</oid_adminstatus> 
    160       <oid_operstatus>IF-MIB::ifOperStatus.%[:ccns:name]</oid_operstatus> 
    161       <oid_inoctets>IF-MIB::ifInOctets.%[:ccns:name]</oid_inoctets> 
    162       <oid_outoctets>IF-MIB::ifOutOctets.%[:ccns:name]</oid_outoctets> 
    163       <oid_inerrors>IF-MIB::ifInErrors.%[:ccns:name]</oid_inerrors> 
    164       <oid_outerrors>IF-MIB::ifOutErrors.%[:ccns:name]</oid_outerrors> 
    165       <oid_indiscards>IF-MIB::ifInDiscards.%[:ccns:name]</oid_indiscards> 
    166       <oid_outdiscards>IF-MIB::ifOutDiscards.%[:ccns:name]</oid_outdiscards> 
    167       <oid_inucastpkts>IF-MIB::ifInUcastPkts.%[:ccns:name]</oid_inucastpkts> 
    168       <oid_outucastpkts>IF-MIB::ifOutUcastPkts.%[:ccns:name]</oid_outucastpkts> 
    169       <oid_inbcastpkts>IF-MIB::ifInNUcastPkts.%[:ccns:name]</oid_inbcastpkts> 
    170       <oid_outbcastpkts>IF-MIB::ifOutNUcastPkts.%[:ccns:name]</oid_outbcastpkts> 
    171     </config> 
    172     <config id="SwitchPortX"> 
    173       <oid_description>IF-MIB::ifName.%[:ccns:name]</oid_description> 
    174       <oid_alias>IF-MIB::ifAlias.%[:ccns:name]</oid_alias> 
    175       <oid_speed>IF-MIB::ifSpeed.%[:ccns:name]</oid_speed> 
    176       <oid_adminstatus>IF-MIB::ifAdminStatus.%[:ccns:name]</oid_adminstatus> 
    177       <oid_operstatus>IF-MIB::ifOperStatus.%[:ccns:name]</oid_operstatus> 
    178       <oid_inoctets>IF-MIB::ifHCInOctets.%[:ccns:name]</oid_inoctets> 
    179       <oid_outoctets>IF-MIB::ifHCOutOctets.%[:ccns:name]</oid_outoctets> 
    180       <oid_inerrors>IF-MIB::ifInErrors.%[:ccns:name]</oid_inerrors> 
    181       <oid_outerrors>IF-MIB::ifOutErrors.%[:ccns:name]</oid_outerrors> 
    182       <oid_indiscards>IF-MIB::ifInDiscards.%[:ccns:name]</oid_indiscards> 
    183       <oid_outdiscards>IF-MIB::ifOutDiscards.%[:ccns:name]</oid_outdiscards> 
    184       <oid_inucastpkts>IF-MIB::ifHCInUcastPkts.%[:ccns:name]</oid_inucastpkts> 
    185       <oid_outucastpkts>IF-MIB::ifHCOutUcastPkts.%[:ccns:name]</oid_outucastpkts> 
    186       <oid_inbcastpkts>IF-MIB::ifHCInBroadcastPkts.%[:ccns:name]</oid_inbcastpkts> 
    187       <oid_outbcastpkts>IF-MIB::ifHCOutBroadcastPkts.%[:ccns:name]</oid_outbcastpkts> 
    188     </config> 
    189     <config id="disk"> 
    190       <oid_path>UCD-SNMP-MIB::dskPath.%[:ccns:name]</oid_path> 
    191       <oid_device>UCD-SNMP-MIB::dskDevice.%[:ccns:name]</oid_device> 
    192       <oid_total>UCD-SNMP-MIB::dskTotal.%[:ccns:name]</oid_total> 
    193       <oid_avail>UCD-SNMP-MIB::dskAvail.%[:ccns:name]</oid_avail> 
    194       <oid_used>UCD-SNMP-MIB::dskUsed.%[:ccns:name]</oid_used> 
    195       <oid_pctspace>UCD-SNMP-MIB::dskPercent.%[:ccns:name]</oid_pctspace> 
    196       <oid_pctinodes>UCD-SNMP-MIB::dskPercentNode.%[:ccns:name]</oid_pctinodes> 
    197       <oid_errorflag>UCD-SNMP-MIB::dskErrorFlag.%[:ccns:name]</oid_errorflag> 
    198       <oid_errormsg>UCD-SNMP-MIB::dskErrorMsg.%[:ccns:name]</oid_errormsg> 
    199     </config> 
     155    <include file="config_templates.conf"/> 
    200156  </config_templates> 
    201157</noit> 
  • src/noit_conf.c

    r6de8198 r322ceb0  
    5353static noit_hash_table _tmp_config = NOIT_HASH_EMPTY; 
    5454static xmlDocPtr master_config = NULL; 
     55static int config_include_cnt = -1; 
     56static struct {  
     57  xmlNodePtr insertion_point; 
     58  xmlNodePtr old_children; 
     59  xmlDocPtr doc; 
     60  xmlNodePtr root; 
     61} *config_include_nodes = NULL; 
     62 
    5563static char *root_node_name = NULL; 
    5664static char master_config_file[PATH_MAX] = ""; 
     
    180188} 
    181189 
     190void 
     191noit_conf_magic_separate(xmlDocPtr doc) { 
     192  assert(config_include_cnt != -1); 
     193  if(config_include_nodes) { 
     194    int i; 
     195    for(i=0; i<config_include_cnt; i++) { 
     196      if(config_include_nodes[i].doc) { 
     197        xmlNodePtr n; 
     198        for(n=config_include_nodes[i].insertion_point->children; 
     199            n; n = n->next) 
     200          n->parent = config_include_nodes[i].root; 
     201        config_include_nodes[i].insertion_point->children = 
     202          config_include_nodes[i].old_children; 
     203        xmlFreeDoc(config_include_nodes[i].doc); 
     204      } 
     205    } 
     206    free(config_include_nodes); 
     207  } 
     208  config_include_nodes = NULL; 
     209  config_include_cnt = -1; 
     210} 
     211void 
     212noit_conf_kansas_city_shuffle_redo(xmlDocPtr doc) { 
     213  if(config_include_nodes) { 
     214    int i; 
     215    for(i=0; i<config_include_cnt; i++) { 
     216      if(config_include_nodes[i].doc) { 
     217        xmlNodePtr n; 
     218        config_include_nodes[i].insertion_point->children = 
     219          config_include_nodes[i].root->children; 
     220        for(n=config_include_nodes[i].insertion_point->children; 
     221            n; n = n->next) 
     222          n->parent = config_include_nodes[i].insertion_point; 
     223      } 
     224    } 
     225  } 
     226} 
     227void 
     228noit_conf_kansas_city_shuffle_undo(xmlDocPtr doc) { 
     229  if(config_include_nodes) { 
     230    int i; 
     231    for(i=0; i<config_include_cnt; i++) { 
     232      if(config_include_nodes[i].doc) { 
     233        xmlNodePtr n; 
     234        for(n=config_include_nodes[i].insertion_point->children; 
     235            n; n = n->next) 
     236          n->parent = config_include_nodes[i].root; 
     237        config_include_nodes[i].insertion_point->children = 
     238          config_include_nodes[i].old_children; 
     239      } 
     240    } 
     241  } 
     242} 
     243int 
     244noit_conf_magic_mix(const char *parentfile, xmlDocPtr doc) { 
     245  xmlXPathContextPtr mix_ctxt = NULL; 
     246  xmlXPathObjectPtr pobj = NULL; 
     247  xmlNodePtr node; 
     248  int i, cnt, rv = 0; 
     249 
     250  assert(config_include_cnt == -1); 
     251 
     252  config_include_cnt = 0; 
     253  mix_ctxt = xmlXPathNewContext(doc); 
     254  pobj = xmlXPathEval((xmlChar *)"//include[@file]", mix_ctxt); 
     255  if(!pobj) goto out; 
     256  if(pobj->type != XPATH_NODESET) goto out; 
     257  if(xmlXPathNodeSetIsEmpty(pobj->nodesetval)) goto out; 
     258  cnt = xmlXPathNodeSetGetLength(pobj->nodesetval); 
     259  if(cnt > 0) 
     260    config_include_nodes = calloc(cnt, sizeof(*config_include_nodes)); 
     261  for(i=0; i<cnt; i++) { 
     262    char *path, *infile; 
     263    node = xmlXPathNodeSetItem(pobj->nodesetval, i); 
     264    path = (char *)xmlGetProp(node, (xmlChar *)"file"); 
     265    if(!path) continue; 
     266    if(*path == '/') infile = strdup(path); 
     267    else { 
     268      char *cp; 
     269      infile = malloc(PATH_MAX); 
     270      strlcpy(infile, parentfile, PATH_MAX); 
     271      for(cp = infile + strlen(infile) - 1; cp > infile; cp--) { 
     272        if(*cp == '/') { *cp = '\0'; break; } 
     273        else *cp = '\0'; 
     274      } 
     275      strlcat(infile, "/", PATH_MAX); 
     276      strlcat(infile, path, PATH_MAX); 
     277    } 
     278    config_include_nodes[i].doc = xmlParseFile(infile); 
     279    if(config_include_nodes[i].doc) { 
     280      xmlNodePtr n; 
     281      config_include_nodes[i].insertion_point = node; 
     282      config_include_nodes[i].root = xmlDocGetRootElement(config_include_nodes[i].doc); 
     283      config_include_nodes[i].old_children = node->children; 
     284      node->children = config_include_nodes[i].root->children; 
     285      for(n=node->children; n; n = n->next) 
     286        n->parent = config_include_nodes[i].insertion_point; 
     287    } 
     288    else { 
     289      noitL(noit_error, "Could not load: '%s'\n", infile); 
     290      rv = -1; 
     291    } 
     292    free(infile); 
     293  } 
     294  config_include_cnt = cnt; 
     295  noitL(noit_debug, "Processed %d includes\n", config_include_cnt); 
     296 out: 
     297  if(pobj) xmlXPathFreeObject(pobj); 
     298  if(mix_ctxt) xmlXPathFreeContext(mix_ctxt); 
     299  return rv; 
     300} 
     301 
    182302int noit_conf_load(const char *path) { 
     303  int rv = 0; 
    183304  xmlDocPtr new_config; 
    184305  xmlNodePtr root; 
     
    188309    if(root_node_name) free(root_node_name); 
    189310    root_node_name = strdup((char *)root->name); 
    190     if(master_config) xmlFreeDoc(master_config); 
     311 
     312    if(master_config) { 
     313      /* separate all includes */ 
     314      noit_conf_magic_separate(master_config); 
     315      xmlFreeDoc(master_config); 
     316    } 
    191317    if(xpath_ctxt) xmlXPathFreeContext(xpath_ctxt); 
    192318 
    193319    master_config = new_config; 
     320    /* mixin all the includes */ 
     321    if(noit_conf_magic_mix(path, master_config)) rv = -1; 
     322 
    194323    xpath_ctxt = xmlXPathNewContext(master_config); 
    195324    if(path != master_config_file) 
     
    197326        noitL(noit_error, "realpath failed: %s\n", strerror(errno)); 
    198327    noit_conf_mark_changed(); 
    199     return 0; 
    200   } 
    201   return -1; 
     328    return rv; 
     329  } 
     330  rv = -1; 
     331  return rv; 
    202332} 
    203333 
     
    605735                                noit_console_close_xml, 
    606736                                ncct, enc); 
     737  noit_conf_kansas_city_shuffle_undo(master_config); 
    607738  xmlSaveFormatFileTo(out, master_config, "utf8", 1); 
     739  noit_conf_kansas_city_shuffle_redo(master_config); 
    608740  return 0; 
    609741} 
     
    645777    return -1; 
    646778  } 
     779  noit_conf_kansas_city_shuffle_undo(master_config); 
    647780  len = xmlSaveFormatFileTo(out, master_config, "utf8", 1); 
     781  noit_conf_kansas_city_shuffle_redo(master_config); 
    648782  close(fd); 
    649783  if(len <= 0) { 
     
    674808                                noit_config_log_close_xml, 
    675809                                clv, enc); 
     810  noit_conf_kansas_city_shuffle_undo(master_config); 
    676811  xmlSaveFormatFileTo(out, master_config, "utf8", 1); 
     812  noit_conf_kansas_city_shuffle_redo(master_config); 
    677813  if(clv->encoded != CONFIG_RAW) { 
    678814    noitL(noit_error, "Error logging configuration\n"); 
     
    707843                                noit_config_log_close_xml, 
    708844                                clv, enc); 
     845  noit_conf_kansas_city_shuffle_undo(master_config); 
    709846  xmlSaveFormatFileTo(out, master_config, "utf8", 1); 
     847  noit_conf_kansas_city_shuffle_redo(master_config); 
    710848  if(clv->encoded != CONFIG_B64) { 
    711849    noitL(noit_error, "Error logging configuration\n"); 
     
    8991037  } 
    9001038  if(pobj) xmlXPathFreeObject(pobj); 
    901  
     1039  pobj = NULL; 
     1040 
     1041  if(!strcmp(argv[0],"include")) { 
     1042    err = "include is a reserved section name"; 
     1043    goto bad; 
     1044  } 
    9021045  path = strcmp(info->path, "/") ? info->path : ""; 
    9031046  snprintf(xpath, sizeof(xpath), "/%s%s", root_node_name, path);