Changeset 8d08f77c61b7d5163f3f7f4e3979cf81e63564ae

Show
Ignore:
Timestamp:
06/01/10 20:37:01 (5 years ago)
Author:
Theo Schlossnagle <jesus@omniti.com>
git-committer:
Theo Schlossnagle <jesus@omniti.com> 1275424621 +0000
git-parent:

[52b8bc4e1ecc74b21f4db46822bfe9c609fb818a]

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

support arbitrary extraction from documents, closes #287

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • src/modules-lua/noit/module/http.lua

    rb37e686 r8d08f77  
    8585               required="optional" 
    8686               allowed=".+">This regular expression is matched against the body of the response.  If a match is not found, the check will be marked as "bad."</parameter> 
     87    <parameter name="extract" 
     88               required="optional" 
     89               allowed=".+">This regular expression is matched against the body of the response globally.  The first capturing match is the key and the second capturing match is the value.  Each key/value extracted is registered as a metric for the check.</parameter> 
    8790  </checkconfig> 
    8891  <examples> 
     
    447450    check.metric_int32("bytes", client.content_bytes) 
    448451 
     452    if check.config.extract ~= nil then 
     453      local exre = noit.pcre(check.config.extract) 
     454      local rv = true 
     455      while rv do 
     456        rv, m, key, value = exre(output or '') 
     457        if rv and key ~= nil then 
     458          check.metric(key, value) 
     459        end 
     460      end 
     461    end 
     462 
    449463    -- check body 
    450464    if check.config.body ~= nil then 
  • src/modules/lua_noit.c

    ra5e761d r8d08f77  
    12001200} 
    12011201 
     1202struct pcre_global_info { 
     1203  pcre *re; 
     1204  int offset; 
     1205}; 
    12021206static int 
    12031207noit_lua_pcre_match(lua_State *L) { 
    12041208  const char *subject; 
    1205   pcre **re
     1209  struct pcre_global_info *pgi
    12061210  int i, cnt, ovector[30]; 
    12071211  size_t inlen; 
    12081212 
    1209   re = (pcre **)lua_touserdata(L, lua_upvalueindex(1)); 
     1213  pgi = (struct pcre_global_info *)lua_touserdata(L, lua_upvalueindex(1)); 
    12101214  subject = lua_tolstring(L,1,&inlen); 
    12111215  if(!subject) { 
     
    12131217    return 1; 
    12141218  } 
    1215   cnt = pcre_exec(*re, NULL, subject, inlen, 0, 0, 
     1219  if (pgi->offset >= inlen) { 
     1220    lua_pushboolean(L,0); 
     1221    return 1; 
     1222  } 
     1223  cnt = pcre_exec(pgi->re, NULL, subject + pgi->offset, 
     1224                  inlen - pgi->offset, 0, 0, 
    12161225                  ovector, sizeof(ovector)/sizeof(*ovector)); 
    12171226  if(cnt <= 0) { 
     
    12231232    int start = ovector[i*2]; 
    12241233    int end = ovector[i*2+1]; 
    1225     lua_pushlstring(L, subject+start, end-start); 
    1226   } 
     1234    lua_pushlstring(L, subject+pgi->offset+start, end-start); 
     1235  } 
     1236  pgi->offset += ovector[1]; /* endof the overall match */ 
    12271237  return cnt+1; 
    12281238} 
    12291239static int 
    12301240nl_pcre(lua_State *L) { 
    1231   pcre *re, **holder; 
     1241  pcre *re; 
     1242  struct pcre_global_info *pgi; 
    12321243  const char *expr; 
    12331244  const char *errstr; 
     
    12421253    return 3; 
    12431254  } 
    1244   holder = (pcre **)lua_newuserdata(L, sizeof(*holder)); 
    1245   *holder = re; 
     1255  pgi = (struct pcre_global_info *)lua_newuserdata(L, sizeof(*pgi)); 
     1256  pgi->re = re; 
     1257  pgi->offset = 0; 
    12461258  luaL_getmetatable(L, "noit.pcre"); 
    12471259  lua_setmetatable(L, -2); 
     
    12511263static int 
    12521264noit_lua_pcre_gc(lua_State *L) { 
    1253   pcre **holder
    1254   holder = (pcre **)lua_touserdata(L,1); 
    1255   pcre_free(*holder); 
     1265  struct pcre_global_info *pgi
     1266  pgi = (struct pcre_global_info *)lua_touserdata(L,1); 
     1267  pcre_free(pgi->re); 
    12561268  return 0; 
    12571269}