Changeset 8d08f77c61b7d5163f3f7f4e3979cf81e63564ae
- Timestamp:
- 06/01/10 20:37:01
(3 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
| rb37e686 |
r8d08f77 |
|
| 85 | 85 | required="optional" |
|---|
| 86 | 86 | 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> |
|---|
| 87 | 90 | </checkconfig> |
|---|
| 88 | 91 | <examples> |
|---|
| … | … | |
| 447 | 450 | check.metric_int32("bytes", client.content_bytes) |
|---|
| 448 | 451 | |
|---|
| | 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 | |
|---|
| 449 | 463 | -- check body |
|---|
| 450 | 464 | if check.config.body ~= nil then |
|---|
| ra5e761d |
r8d08f77 |
|
| 1200 | 1200 | } |
|---|
| 1201 | 1201 | |
|---|
| | 1202 | struct pcre_global_info { |
|---|
| | 1203 | pcre *re; |
|---|
| | 1204 | int offset; |
|---|
| | 1205 | }; |
|---|
| 1202 | 1206 | static int |
|---|
| 1203 | 1207 | noit_lua_pcre_match(lua_State *L) { |
|---|
| 1204 | 1208 | const char *subject; |
|---|
| 1205 | | pcre **re; |
|---|
| | 1209 | struct pcre_global_info *pgi; |
|---|
| 1206 | 1210 | int i, cnt, ovector[30]; |
|---|
| 1207 | 1211 | size_t inlen; |
|---|
| 1208 | 1212 | |
|---|
| 1209 | | re = (pcre **)lua_touserdata(L, lua_upvalueindex(1)); |
|---|
| | 1213 | pgi = (struct pcre_global_info *)lua_touserdata(L, lua_upvalueindex(1)); |
|---|
| 1210 | 1214 | subject = lua_tolstring(L,1,&inlen); |
|---|
| 1211 | 1215 | if(!subject) { |
|---|
| … | … | |
| 1213 | 1217 | return 1; |
|---|
| 1214 | 1218 | } |
|---|
| 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, |
|---|
| 1216 | 1225 | ovector, sizeof(ovector)/sizeof(*ovector)); |
|---|
| 1217 | 1226 | if(cnt <= 0) { |
|---|
| … | … | |
| 1223 | 1232 | int start = ovector[i*2]; |
|---|
| 1224 | 1233 | 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 */ |
|---|
| 1227 | 1237 | return cnt+1; |
|---|
| 1228 | 1238 | } |
|---|
| 1229 | 1239 | static int |
|---|
| 1230 | 1240 | nl_pcre(lua_State *L) { |
|---|
| 1231 | | pcre *re, **holder; |
|---|
| | 1241 | pcre *re; |
|---|
| | 1242 | struct pcre_global_info *pgi; |
|---|
| 1232 | 1243 | const char *expr; |
|---|
| 1233 | 1244 | const char *errstr; |
|---|
| … | … | |
| 1242 | 1253 | return 3; |
|---|
| 1243 | 1254 | } |
|---|
| 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; |
|---|
| 1246 | 1258 | luaL_getmetatable(L, "noit.pcre"); |
|---|
| 1247 | 1259 | lua_setmetatable(L, -2); |
|---|
| … | … | |
| 1251 | 1263 | static int |
|---|
| 1252 | 1264 | noit_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); |
|---|
| 1256 | 1268 | return 0; |
|---|
| 1257 | 1269 | } |
|---|