static int web_set_timeout(AGENT_RESULT *result, struct web *opt, const char *params, int param_id) { char timeout_tmp[WEB_MAX_TIMEOUT_STRLEN] = {0}; zbx_uint64_t timeout_test; /* maximum is one digit timeout */ if (get_param(params, param_id, timeout_tmp, WEB_MAX_TIMEOUT_STRLEN)) goto failed; zbx_remove_whitespace(timeout_tmp); if (strlen(timeout_tmp)) { /* check timeout range 1 to WEB_MAX_TIMEOUT */ /* and convert string to uint64 */ if (is_uint_range(timeout_tmp, &timeout_test, 1, WEB_MAX_TIMEOUT)) goto failed; opt->timeout = timeout_test; return SUCCEED; } else { opt->timeout = WEB_DEF_TIMEOUT; return SUCCEED; } failed: SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Invalid TIMEOUT parameter", NULL)); return FAIL; }
static int web_set_port(AGENT_RESULT *result, struct web *opt, const char *params, int param_id) { char port_tmp[WEB_MAX_PORT_STRLEN] = {0}; if (get_param(params, param_id, port_tmp, WEB_MAX_PORT_STRLEN)) goto failed; zbx_remove_whitespace(port_tmp); if (strlen(port_tmp)) { /* RFC6335 */ if (is_uint_range(port_tmp, NULL, 0, WEB_MAX_PORT)) goto failed; if (strncmp(port_tmp, "443", strlen(port_tmp))) opt->is_https = (zbx_uint64_t) 0; else opt->is_https = (zbx_uint64_t) 1; opt->port = zbx_strdup(NULL, port_tmp); } else { opt->port = zbx_strdup(NULL, WEB_DEF_PORT); } return SUCCEED; failed: SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Invalid PORT parameter", NULL)); return FAIL; }
static int web_set_required_response(AGENT_RESULT *result, struct web *opt, const char *params, int param_id) { char req_code_tmp[WEB_MAX_HTTP_CODE_STRLEN] = {0}; zbx_uint64_t req_code_test; if (get_param(params, param_id, req_code_tmp, WEB_MAX_HTTP_CODE_STRLEN)) goto failed; if (strlen(req_code_tmp)) { if (is_uint_range(req_code_tmp, &req_code_test, WEB_MIN_HTTP_CODE, WEB_MAX_HTTP_CODE)) goto failed; opt->required_response = (int) req_code_test; return SUCCEED; } else { opt->required_response = WEB_DEF_HTTP_CODE; return SUCCEED; } failed: SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Invalid RESPONSE CODE parameter", NULL)); return FAIL; }
int SYSTEM_CPU_UTIL(AGENT_REQUEST *request, AGENT_RESULT *result) { char *tmp, *error = NULL; int cpu_num, ret = FAIL; double value; if (0 == CPU_COLLECTOR_STARTED(collector)) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Collector is not started.")); return SYSINFO_RET_FAIL; } if (3 < request->nparam) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters.")); return SYSINFO_RET_FAIL; } if (NULL == (tmp = get_rparam(request, 0)) || '\0' == *tmp || 0 == strcmp(tmp, "all")) cpu_num = 0; else if (SUCCEED != is_uint_range(tmp, &cpu_num, 0, collector->cpus.count - 1)) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid first parameter.")); return SYSINFO_RET_FAIL; } else cpu_num++; /* only "system" (default) for parameter "type" is supported */ if (NULL != (tmp = get_rparam(request, 1)) && '\0' != *tmp && 0 != strcmp(tmp, "system")) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter.")); return SYSINFO_RET_FAIL; } if (NULL == (tmp = get_rparam(request, 2)) || '\0' == *tmp || 0 == strcmp(tmp, "avg1")) { ret = get_perf_counter_value(collector->cpus.cpu_counter[cpu_num], 1 * SEC_PER_MIN, &value, &error); } else if (0 == strcmp(tmp, "avg5")) { ret = get_perf_counter_value(collector->cpus.cpu_counter[cpu_num], 5 * SEC_PER_MIN, &value, &error); } else if (0 == strcmp(tmp, "avg15")) { ret = get_perf_counter_value(collector->cpus.cpu_counter[cpu_num], 15 * SEC_PER_MIN, &value, &error); } else { SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid third parameter.")); return SYSINFO_RET_FAIL; } if (SUCCEED == ret) { SET_DBL_RESULT(result, value); return SYSINFO_RET_OK; } SET_MSG_RESULT(result, NULL != error ? error : zbx_strdup(NULL, "Cannot obtain performance information from collector.")); return SYSINFO_RET_FAIL; }
/****************************************************************************** * * * Function: iprangev4_parse * * * * Purpose: parse IPv4 address into IP range structure * * * * Parameters: iprange - [OUT] the IP range * * address - [IN] the IP address * * * * Return value: SUCCEED - the IP range was successfully parsed * * FAIL - otherwise * * * ******************************************************************************/ static int iprangev4_parse(zbx_iprange_t *iprange, const char *address) { int index, len, bits = -1; const char *ptr = address, *dash, *end; iprange->type = ZBX_IPRANGE_V4; if (NULL != (end = strchr(address, '/'))) { if (FAIL == is_uint_range(end + 1, &bits, 0, 30)) return FAIL; iprange->mask = 1; } else { end = address + strlen(address); iprange->mask = 0; } /* iterate through address numbers (bit groups) */ for (index = 0; ptr < end && index <= 4; address = ptr + 1) { if (NULL == (ptr = strchr(address, '.'))) ptr = end; if (NULL != (dash = strchr(address, '-'))) { /* having range and mask together is not supported */ if (-1 != bits) return FAIL; /* check if the range specification is used by the current group */ if (dash > ptr) dash = NULL; } len = (NULL == dash ? ptr : dash) - address; /* extract the range start value */ if (FAIL == is_uint_n_range(address, len, &iprange->range[index].from, 4, 0, 255)) return FAIL; /* if range is specified, extract the end value, otherwise set end value equal to the start value */ if (NULL != dash) { dash++; if (FAIL == is_uint_n_range(dash, ptr - dash, &iprange->range[index].to, 4, 0, 255)) return FAIL; if (iprange->range[index].to < iprange->range[index].from) return FAIL; } else iprange->range[index].to = iprange->range[index].from; index++; } /* IPv4 address will always have 4 groups */ if (4 != index) return FAIL; if (-1 != bits) iprange_apply_mask(iprange, bits); return SUCCEED; }
/****************************************************************************** * * * Function: iprangev6_parse * * * * Purpose: parse IPv6 address into IP range structure * * * * Parameters: iprange - [OUT] the IP range * * address - [IN] the IP address * * * * Return value: SUCCEED - the IP range was successfully parsed * * FAIL - otherwise * * * ******************************************************************************/ static int iprangev6_parse(zbx_iprange_t *iprange, const char *address) { int index, len, fill = -1, bits = -1, target; const char *ptr = address, *dash, *end; iprange->type = ZBX_IPRANGE_V6; if (NULL != (end = strchr(address, '/'))) { if (FAIL == is_uint_range(end + 1, &bits, 0, 128)) return FAIL; iprange->mask = 1; } else { end = address + strlen(address); iprange->mask = 0; } /* iterate through address numbers (bit groups) */ for (index = 0; ptr < end && index <= 8; address = ptr + 1) { if (NULL == (ptr = strchr(address, ':'))) ptr = end; if (ptr == address) { /* handle the case when address starts with :: */ if (':' != ptr[1]) return FAIL; goto check_fill; } if (NULL != (dash = strchr(address, '-'))) { /* having range and mask together is not supported */ if (-1 != bits) return FAIL; /* check if the range specification is used by the current group */ if (dash > ptr) dash = NULL; } len = (NULL == dash ? ptr : dash) - address; /* extract the range start value */ if (FAIL == is_hex_n_range(address, len, &iprange->range[index].from, 4, 0, (1 << 16) - 1)) return FAIL; /* if range is specified, extract the end value, otherwise set end value equal to the start value */ if (NULL != dash) { dash++; if (FAIL == is_hex_n_range(dash, ptr - dash, &iprange->range[index].to, 4, 0, (1 << 16) - 1)) return FAIL; if (iprange->range[index].to < iprange->range[index].from) return FAIL; } else iprange->range[index].to = iprange->range[index].from; index++; check_fill: /* check if the next group is empty */ if ('\0' != ptr[0] && ':' == ptr[1]) { /* :: construct is allowed only once in address */ if (-1 != fill) return FAIL; iprange->range[index].from = 0; iprange->range[index].to = 0; fill = index++; ptr++; /* check if address ends with :: */ if (ptr == end - 1) break; } } /* fail if the address contains 9+ groups */ if (8 < index) return FAIL; /* expand the :: construct to the required number of zeroes */ if (8 > index) { /* fail if the address contains less than 8 groups and no :: construct was used */ if (-1 == fill) return FAIL; target = 7; /* shift the second part of address to the end */ while (--index > fill) iprange->range[target--] = iprange->range[index]; /* fill the middle with zeroes */ while (target > fill) { iprange->range[target].from = 0; iprange->range[target].to = 0; target--; } } if (-1 != bits) iprange_apply_mask(iprange, bits); return SUCCEED; }