Changeset be06bddffbd230aec8cb74a84859977d59173f06

Show
Ignore:
Timestamp:
09/27/09 03:10:32 (5 years ago)
Author:
Theo Schlossnagle <jesus@omniti.com>
git-committer:
Theo Schlossnagle <jesus@omniti.com> 1254021032 +0000
git-parent:

[5b7de2315924e3efa701f5ff9fa9238c673e62e7]

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

refactor this and expose some of the bits as helpful utility functions, refs #171

Files:

Legend:

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

    r75ee62d rbe06bdd  
    4848#define UUID_REGEX "[0-9a-fA-F]{4}(?:[0-9a-fA-F]{4}-){4}[0-9a-fA-F]{12}" 
    4949 
     50#define NODE_CONTENT(parent, k, v) do { \ 
     51  xmlNodePtr tmp; \ 
     52  if(v) { \ 
     53    tmp = xmlNewNode(NULL, (xmlChar *)(k)); \ 
     54    xmlNodeAddContent(tmp, (xmlChar *)(v)); \ 
     55    xmlAddChild(parent, tmp); \ 
     56  } \ 
     57} while(0) 
     58 
     59xmlNodePtr 
     60noit_check_state_as_xml(noit_check_t *check) { 
     61  xmlNodePtr state, tmp, metrics; 
     62  noit_hash_iter iter = NOIT_HASH_ITER_ZERO; 
     63  const char *k; 
     64  int klen; 
     65  void *data; 
     66  stats_t *c = &check->stats.current; 
     67 
     68  state = xmlNewNode(NULL, (xmlChar *)"state"); 
     69  NODE_CONTENT(state, "running", NOIT_CHECK_RUNNING(check)?"true":"false"); 
     70  NODE_CONTENT(state, "killed", NOIT_CHECK_KILLED(check)?"true":"false"); 
     71  NODE_CONTENT(state, "configured", 
     72               NOIT_CHECK_CONFIGURED(check)?"true":"false"); 
     73  NODE_CONTENT(state, "disabled", NOIT_CHECK_DISABLED(check)?"true":"false"); 
     74  xmlAddChild(state, (tmp = xmlNewNode(NULL, (xmlChar *)"last_run"))); 
     75  if(check->stats.current.whence.tv_sec) { 
     76    struct timeval f = check->stats.current.whence; 
     77    struct timeval n; 
     78    char timestr[20]; 
     79    gettimeofday(&n, NULL); 
     80    snprintf(timestr, sizeof(timestr), "%0.3f", 
     81             n.tv_sec + (n.tv_usec / 1000000.0)); 
     82    xmlSetProp(tmp, (xmlChar *)"now", (xmlChar *)timestr); 
     83    snprintf(timestr, sizeof(timestr), "%0.3f", 
     84             f.tv_sec + (f.tv_usec / 1000000.0)); 
     85    xmlNodeAddContent(tmp, (xmlChar *)timestr); 
     86  } 
     87  if(c->available) { /* truth here means the check has been run */ 
     88    char buff[20]; 
     89    snprintf(buff, sizeof(buff), "%0.3f", (float)c->duration/1000.0); 
     90    NODE_CONTENT(state, "runtime", buff); 
     91  } 
     92  NODE_CONTENT(state, "availability", 
     93               noit_check_available_string(c->available)); 
     94  NODE_CONTENT(state, "state", noit_check_state_string(c->state)); 
     95  NODE_CONTENT(state, "status", c->status ? c->status : ""); 
     96  memset(&iter, 0, sizeof(iter)); 
     97  xmlAddChild(state, (metrics = xmlNewNode(NULL, (xmlChar *)"metrics"))); 
     98  while(noit_hash_next(&c->metrics, &iter, &k, &klen, &data)) { 
     99    char buff[256]; 
     100    metric_t *m = (metric_t *)data; 
     101    xmlAddChild(metrics, (tmp = xmlNewNode(NULL, (xmlChar *)m->metric_name))); 
     102    buff[0] = m->metric_type; buff[1] = '\0'; 
     103    xmlSetProp(tmp, (xmlChar *)"type", (xmlChar *)buff); 
     104    if(m->metric_value.s) { 
     105      int rv; 
     106      rv = noit_stats_snprint_metric_value(buff, sizeof(buff), m); 
     107      if(rv < 0) 
     108        xmlSetProp(tmp, (xmlChar *)"error", (xmlChar *)"unknown type"); 
     109      else 
     110        xmlNodeAddContent(tmp, (xmlChar *)buff); 
     111    } 
     112  } 
     113  return state; 
     114} 
     115 
    50116static int 
    51117rest_show_check(noit_http_rest_closure_t *restc, 
     
    55121  xmlXPathContextPtr xpath_ctxt = NULL; 
    56122  xmlDocPtr doc = NULL; 
    57   xmlNodePtr node, root, attr, config, state, tmp, anode, metrics
     123  xmlNodePtr node, root, attr, config, state, tmp, anode
    58124  uuid_t checkid; 
    59125  noit_check_t *check; 
     
    112178  } \ 
    113179} while(0) 
    114 #define NODE_CONTENT(parent, k, v) do { \ 
    115   xmlNodePtr tmp; \ 
    116   if(v) { \ 
    117     tmp = xmlNewNode(NULL, (xmlChar *)(k)); \ 
    118     xmlNodeAddContent(tmp, (xmlChar *)(v)); \ 
    119     xmlAddChild(parent, tmp); \ 
    120   } \ 
    121 } while(0) 
    122180 
    123181  attr = xmlNewNode(NULL, (xmlChar *)"attributes"); 
     
    152210 
    153211  /* Add the state */ 
    154   xmlAddChild(root, (state = xmlNewNode(NULL, (xmlChar *)"state"))); 
    155212  check = noit_poller_lookup(checkid); 
    156   if(!check) 
     213  if(!check) { 
     214    state = xmlNewNode(NULL, (xmlChar *)"state"); 
    157215    xmlSetProp(state, (xmlChar *)"error", (xmlChar *)"true"); 
    158   else { 
    159     stats_t *c = &check->stats.current; 
    160     NODE_CONTENT(state, "running", NOIT_CHECK_RUNNING(check)?"true":"false"); 
    161     NODE_CONTENT(state, "killed", NOIT_CHECK_KILLED(check)?"true":"false"); 
    162     NODE_CONTENT(state, "configured", 
    163                  NOIT_CHECK_CONFIGURED(check)?"true":"false"); 
    164     NODE_CONTENT(state, "disabled", NOIT_CHECK_DISABLED(check)?"true":"false"); 
    165     xmlAddChild(state, (tmp = xmlNewNode(NULL, (xmlChar *)"last_run"))); 
    166     if(check->stats.current.whence.tv_sec) { 
    167       struct timeval f = check->stats.current.whence; 
    168       struct timeval n; 
    169       char timestr[20]; 
    170       gettimeofday(&n, NULL); 
    171       snprintf(timestr, sizeof(timestr), "%0.3f", 
    172                n.tv_sec + (n.tv_usec / 1000000.0)); 
    173       xmlSetProp(tmp, (xmlChar *)"now", (xmlChar *)timestr); 
    174       snprintf(timestr, sizeof(timestr), "%0.3f", 
    175                f.tv_sec + (f.tv_usec / 1000000.0)); 
    176       xmlNodeAddContent(tmp, (xmlChar *)timestr); 
    177     } 
    178     if(c->available) { /* truth here means the check has been run */ 
    179       char buff[20]; 
    180       snprintf(buff, sizeof(buff), "%0.3f", (float)c->duration/1000.0); 
    181       NODE_CONTENT(state, "runtime", buff); 
    182     } 
    183     NODE_CONTENT(state, "availability", 
    184                  noit_check_available_string(c->available)); 
    185     NODE_CONTENT(state, "state", noit_check_state_string(c->state)); 
    186     NODE_CONTENT(state, "status", c->status ? c->status : ""); 
    187     memset(&iter, 0, sizeof(iter)); 
    188     xmlAddChild(state, (metrics = xmlNewNode(NULL, (xmlChar *)"metrics"))); 
    189     while(noit_hash_next(&c->metrics, &iter, &k, &klen, &data)) { 
    190       char buff[256]; 
    191       metric_t *m = (metric_t *)data; 
    192       xmlAddChild(metrics, (tmp = xmlNewNode(NULL, (xmlChar *)m->metric_name))); 
    193       buff[0] = m->metric_type; buff[1] = '\0'; 
    194       xmlSetProp(tmp, (xmlChar *)"type", (xmlChar *)buff); 
    195       if(m->metric_value.s) { 
    196         int rv; 
    197         rv = noit_stats_snprint_metric_value(buff, sizeof(buff), m); 
    198         if(rv < 0) 
    199           xmlSetProp(tmp, (xmlChar *)"error", (xmlChar *)"unknown type"); 
    200         else 
    201           xmlNodeAddContent(tmp, (xmlChar *)buff); 
    202       } 
    203     } 
    204  
    205   } 
     216  } 
     217  else 
     218    state = noit_check_state_as_xml(check); 
     219  xmlAddChild(root, state); 
    206220  noit_http_response_ok(ctx, "text/xml"); 
    207221  noit_http_response_xml(ctx, doc); 
     
    225239} 
    226240 
    227 static int 
    228 validate_check_post(xmlDocPtr doc, xmlNodePtr *a, xmlNodePtr *c, 
    229                     const char **error) { 
     241int 
     242noit_validate_check_rest_post(xmlDocPtr doc, xmlNodePtr *a, xmlNodePtr *c, 
     243                              const char **error) { 
    230244  xmlNodePtr root, tl, an; 
    231245  int name=0, module=0, target=0, period=0, timeout=0, filterset=0; 
     
    461475  if(!complete) return mask; 
    462476  if(indoc == NULL) FAIL("xml parse error"); 
    463   if(!validate_check_post(indoc, &attr, &config, &error)) goto error; 
     477  if(!noit_validate_check_rest_post(indoc, &attr, &config, &error)) goto error; 
    464478 
    465479  if(uuid_parse(pats[1], checkid)) goto error; 
  • src/noit_check_rest.h

    r0256945 rbe06bdd  
    3838#include "noit_conf.h" 
    3939 
     40#include <libxml/tree.h> 
     41 
    4042#ifndef NOIT_CHECK_REST_H 
    4143#define NOIT_CHECK_REST_H 
     
    4446  noit_check_rest_init(); 
    4547 
     48API_EXPORT(int) 
     49  noit_validate_check_rest_post(xmlDocPtr doc, xmlNodePtr *a, xmlNodePtr *c, 
     50                                const char **error); 
     51 
     52API_EXPORT(xmlNodePtr) 
     53  noit_check_state_as_xml(noit_check_t *check); 
     54 
    4655#endif