Changeset 84fddd139d4c8f36cc7a25cac7a848e2e9a8e342
- Timestamp:
- 03/11/09 20:25:36 (4 years ago)
- git-parent:
- Files:
-
- src/modules/external.c (modified) (6 diffs)
- src/modules/external.xml (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
src/modules/external.c
rec182aa r84fddd1 20 20 #include <netinet/in_systm.h> 21 21 #endif 22 #include <math.h> 23 #ifndef MAXFLOAT 24 #include <float.h> 25 #define MAXFLOAT FLT_MAX 26 #endif 22 #include <pcre.h> 27 23 28 24 #include "noit_module.h" … … 47 43 char *output; 48 44 char *error; 45 pcre *matcher; 49 46 eventer_t timeout_event; 50 47 }; … … 123 120 current.state = (WEXITSTATUS(ci->exit_code) == 0) ? NP_GOOD : NP_BAD; 124 121 } 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(¤t, 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 125 147 current.status = ci->output; 126 148 noit_check_set_stats(self, check, ¤t); … … 129 151 * provide a full report. 130 152 */ 131 if( WTERMSIG(ci->exit_code) != SIGQUIT||153 if((WTERMSIG(ci->exit_code) != SIGQUIT && WTERMSIG(ci->exit_code) != 0) || 132 154 WCOREDUMP(ci->exit_code) || 133 ci->error) {155 (ci->error && *ci->error)) { 134 156 char uuid_str[37]; 135 157 uuid_unparse_lower(check->checkid, uuid_str); … … 164 186 if(ci->envlens) free(ci->envlens); 165 187 if(ci->envs) free(ci->envs); 188 if(ci->matcher) pcre_free(ci->matcher); 166 189 memset(ci, 0, sizeof(*ci)); 167 190 } … … 427 450 ci->check_no = noit_atomic_inc64(&data->check_no_seq); 428 451 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 429 465 noit_check_make_attrs(check, &check_attrs_hash); 430 466 src/modules/external.xml
rec182aa r84fddd1 25 25 required="optional" 26 26 allowed=".+">These parameters allow the setting of environment variables for the command execution. The text following env_ is the key and the parameter value is the value of the environment variable to be set via execve.</parameter> 27 <parameter name="output_extract" 28 required="optional" 29 allowed=".+">This is a regular expression that is globally applied to the stdout of the command. Each match is turned into a metric. It is a requirement to used named capturing in the regular expression where "key" is the named match of the metric name and "value" is the named match for the matric value. A sample for extracting performance data from Nagios commands would be: <![CDATA[(?<key>\S+)=(?<value>[^;\s]+)(?=[;\s])]]></parameter> 27 30 </checkconfig> 28 31 <examples>
