diff options
| author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-09-10 11:08:52 +0200 |
|---|---|---|
| committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-09-10 11:08:52 +0200 |
| commit | c06ea4e44caee74486e2931154021a0d0e6c8066 (patch) | |
| tree | f50686dca76350381aa886be47865d51681bc338 | |
| parent | 01f353228424376b25f20d9b105aaf5b3c7a6f65 (diff) | |
| download | monitoring-plugins-c06ea4e44caee74486e2931154021a0d0e6c8066.tar.gz | |
check_curl: pre compile regex for string matching
| -rw-r--r-- | plugins/check_curl.c | 7 | ||||
| -rw-r--r-- | plugins/check_curl.d/config.h | 8 |
2 files changed, 12 insertions, 3 deletions
diff --git a/plugins/check_curl.c b/plugins/check_curl.c index f28bcdfc..ba38854a 100644 --- a/plugins/check_curl.c +++ b/plugins/check_curl.c | |||
| @@ -1261,9 +1261,8 @@ mp_state_enum check_http(const check_curl_config config, check_curl_working_stat | |||
| 1261 | } | 1261 | } |
| 1262 | 1262 | ||
| 1263 | if (strlen(config.regexp)) { | 1263 | if (strlen(config.regexp)) { |
| 1264 | regex_t preg; | ||
| 1265 | regmatch_t pmatch[REGS]; | 1264 | regmatch_t pmatch[REGS]; |
| 1266 | int errcode = regexec(&preg, global_state.body_buf.buf, REGS, pmatch, 0); | 1265 | int errcode = regexec(&config.compiled_regex, global_state.body_buf.buf, REGS, pmatch, 0); |
| 1267 | if ((errcode == 0 && !config.invert_regex) || | 1266 | if ((errcode == 0 && !config.invert_regex) || |
| 1268 | (errcode == REG_NOMATCH && config.invert_regex)) { | 1267 | (errcode == REG_NOMATCH && config.invert_regex)) { |
| 1269 | /* OK - No-op to avoid changing the logic around it */ | 1268 | /* OK - No-op to avoid changing the logic around it */ |
| @@ -1284,7 +1283,7 @@ mp_state_enum check_http(const check_curl_config config, check_curl_working_stat | |||
| 1284 | } | 1283 | } |
| 1285 | result = config.state_regex; | 1284 | result = config.state_regex; |
| 1286 | } else { | 1285 | } else { |
| 1287 | regerror(errcode, &preg, errbuf, MAX_INPUT_BUFFER); | 1286 | regerror(errcode, &config.compiled_regex, errbuf, MAX_INPUT_BUFFER); |
| 1288 | 1287 | ||
| 1289 | char tmp[DEFAULT_BUFFER_SIZE]; | 1288 | char tmp[DEFAULT_BUFFER_SIZE]; |
| 1290 | 1289 | ||
| @@ -1969,6 +1968,8 @@ check_curl_config_wrapper process_arguments(int argc, char **argv) { | |||
| 1969 | result.errorcode = ERROR; | 1968 | result.errorcode = ERROR; |
| 1970 | return result; | 1969 | return result; |
| 1971 | } | 1970 | } |
| 1971 | |||
| 1972 | result.config.compiled_regex = preg; | ||
| 1972 | break; | 1973 | break; |
| 1973 | case INVERT_REGEX: | 1974 | case INVERT_REGEX: |
| 1974 | result.config.invert_regex = true; | 1975 | result.config.invert_regex = true; |
diff --git a/plugins/check_curl.d/config.h b/plugins/check_curl.d/config.h index 9de68713..9acd4048 100644 --- a/plugins/check_curl.d/config.h +++ b/plugins/check_curl.d/config.h | |||
| @@ -8,6 +8,7 @@ | |||
| 8 | #include <string.h> | 8 | #include <string.h> |
| 9 | #include <sys/socket.h> | 9 | #include <sys/socket.h> |
| 10 | #include "curl/curl.h" | 10 | #include "curl/curl.h" |
| 11 | #include "regex.h" | ||
| 11 | 12 | ||
| 12 | enum { | 13 | enum { |
| 13 | MAX_RE_SIZE = 1024, | 14 | MAX_RE_SIZE = 1024, |
| @@ -87,7 +88,13 @@ typedef struct { | |||
| 87 | char *cookie_jar_file; | 88 | char *cookie_jar_file; |
| 88 | 89 | ||
| 89 | int maximum_age; | 90 | int maximum_age; |
| 91 | |||
| 92 | // the original regex string from the command line | ||
| 90 | char regexp[MAX_RE_SIZE]; | 93 | char regexp[MAX_RE_SIZE]; |
| 94 | |||
| 95 | // the compiled regex for usage later | ||
| 96 | regex_t compiled_regex; | ||
| 97 | |||
| 91 | mp_state_enum state_regex; | 98 | mp_state_enum state_regex; |
| 92 | bool invert_regex; | 99 | bool invert_regex; |
| 93 | bool verify_peer_and_host; | 100 | bool verify_peer_and_host; |
| @@ -136,6 +143,7 @@ check_curl_config check_curl_config_init() { | |||
| 136 | 143 | ||
| 137 | .maximum_age = -1, | 144 | .maximum_age = -1, |
| 138 | .regexp = {}, | 145 | .regexp = {}, |
| 146 | .compiled_regex = {}, | ||
| 139 | .state_regex = STATE_CRITICAL, | 147 | .state_regex = STATE_CRITICAL, |
| 140 | .invert_regex = false, | 148 | .invert_regex = false, |
| 141 | .verify_peer_and_host = false, | 149 | .verify_peer_and_host = false, |
