Show
Ignore:
Timestamp:
03/11/09 20:25:36 (5 years ago)
Author:
Theo Schlossnagle <jesus@omniti.com>
git-committer:
Theo Schlossnagle <jesus@omniti.com> 1236803136 +0000
git-parent:

[ec182aa491a0935820d4dd33dde474b7fe657568]

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

basic extraction, refs #103... might need to be more sophisticated

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • src/modules/external.c

    rec182aa r84fddd1  
    2020#include <netinet/in_systm.h> 
    2121#endif 
    22 #include <math.h> 
    23 #ifndef MAXFLOAT 
    24 #include <float.h> 
    25 #define MAXFLOAT FLT_MAX 
    26 #endif 
     22#include <pcre.h> 
    2723 
    2824#include "noit_module.h" 
     
    4743  char *output; 
    4844  char *error; 
     45  pcre *matcher; 
    4946  eventer_t timeout_event; 
    5047}; 
     
    123120    current.state = (WEXITSTATUS(ci->exit_code) == 0) ? NP_GOOD : NP_BAD; 
    124121  } 
     122 
     123  /* Hack the output into metrics */ 
     124  if(ci->output && ci->matcher) { 
     125    int rc, len, startoffset = 0; 
     126    int ovector[30]; 
     127    len = strlen(ci->output); 
     128    noitL(data->nldeb, "going to match output at %d/%d\n", startoffset, len); 
     129    while((rc = pcre_exec(ci->matcher, NULL, ci->output, len, startoffset, 0, 
     130                          ovector, sizeof(ovector)/sizeof(*ovector))) > 0) { 
     131      char metric[128]; 
     132      char value[128]; 
     133      startoffset = ovector[1]; 
     134      noitL(data->nldeb, "matched at offset %d\n", rc); 
     135      if(pcre_copy_named_substring(ci->matcher, ci->output, ovector, rc, 
     136                                   "key", metric, sizeof(metric)) > 0 && 
     137         pcre_copy_named_substring(ci->matcher, ci->output, ovector, rc, 
     138                                   "value", value, sizeof(value)) > 0) { 
     139        /* We're able to extract something... */ 
     140        noit_stats_set_metric(&current, metric, METRIC_GUESS, value); 
     141      } 
     142      noitL(data->nldeb, "going to match output at %d/%d\n", startoffset, len); 
     143    } 
     144    noitL(data->nldeb, "match failed.... %d\n", rc); 
     145  } 
     146 
    125147  current.status = ci->output; 
    126148  noit_check_set_stats(self, check, &current); 
     
    129151   * provide a full report. 
    130152   */ 
    131   if(WTERMSIG(ci->exit_code) != SIGQUIT || 
     153  if((WTERMSIG(ci->exit_code) != SIGQUIT && WTERMSIG(ci->exit_code) != 0) || 
    132154     WCOREDUMP(ci->exit_code) || 
    133      ci->error) { 
     155     (ci->error && *ci->error)) { 
    134156    char uuid_str[37]; 
    135157    uuid_unparse_lower(check->checkid, uuid_str); 
     
    164186  if(ci->envlens) free(ci->envlens); 
    165187  if(ci->envs) free(ci->envs); 
     188  if(ci->matcher) pcre_free(ci->matcher); 
    166189  memset(ci, 0, sizeof(*ci)); 
    167190} 
     
    427450  ci->check_no = noit_atomic_inc64(&data->check_no_seq); 
    428451  ci->check = check; 
     452  /* We might want to extract metrics */ 
     453  if(noit_hash_retrieve(check->config, 
     454                        "output_extract", strlen("output_extract"), 
     455                        (void **)&value) != 0) { 
     456    const char *error; 
     457    int erroffset; 
     458    ci->matcher = pcre_compile(value, 0, &error, &erroffset, NULL); 
     459    if(!ci->matcher) { 
     460      noitL(data->nlerr, "external pcre /%s/ failed @ %d: %s\n", 
     461            value, erroffset, error); 
     462    } 
     463  } 
     464 
    429465  noit_check_make_attrs(check, &check_attrs_hash); 
    430466