static int zbx_execute_script_on_agent(DC_HOST *host, const char *command, char **result, char *error, size_t max_error_len) { const char *__function_name = "zbx_execute_script_on_agent"; int ret; AGENT_RESULT agent_result; char *param, *port = NULL; DC_ITEM item; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); *error = '\0'; memset(&item, 0, sizeof(item)); memcpy(&item.host, host, sizeof(item.host)); if (SUCCEED != (ret = DCconfig_get_interface_by_type(&item.interface, host->hostid, INTERFACE_TYPE_AGENT))) { zbx_snprintf(error, max_error_len, "Whatap agent interface is not defined for host [%s]", host->host); goto fail; } port = zbx_strdup(port, item.interface.port_orig); substitute_simple_macros(NULL, NULL, NULL, NULL, &host->hostid, NULL, NULL, &port, MACRO_TYPE_COMMON, NULL, 0); if (SUCCEED != (ret = is_ushort(port, &item.interface.port))) { zbx_snprintf(error, max_error_len, "Invalid port number [%s]", item.interface.port_orig); goto fail; } param = zbx_dyn_escape_string(command, "\""); item.key = zbx_dsprintf(item.key, "system.run[\"%s\",\"%s\"]", param, NULL == result ? "nowait" : "wait"); item.value_type = ITEM_VALUE_TYPE_TEXT; zbx_free(param); init_result(&agent_result); alarm(CONFIG_TIMEOUT); if (SUCCEED != (ret = get_value_agent(&item, &agent_result))) { if (ISSET_MSG(&agent_result)) zbx_strlcpy(error, agent_result.msg, max_error_len); ret = FAIL; } else if (NULL != result && ISSET_TEXT(&agent_result)) *result = zbx_strdup(*result, agent_result.text); alarm(0); free_result(&agent_result); zbx_free(item.key); fail: zbx_free(port); zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret)); return ret; }
/****************************************************************************** * * * Function: get_result_value_by_type * * * * Purpose: return value of result in special type * * if value missing, convert existing value to requested type * * * * Return value: * * NULL - if value is missing or can't be converted * * * * Author: Eugene Grigorjev * * * * Comments: better use definitions * * GET_UI64_RESULT * * GET_DBL_RESULT * * GET_STR_RESULT * * GET_TEXT_RESULT * * GET_LOG_RESULT * * GET_MSG_RESULT * * * * AR_MESSAGE - skipped in conversion * * * ******************************************************************************/ void *get_result_value_by_type(AGENT_RESULT *result, int require_type) { assert(result); switch (require_type) { case AR_UINT64: return (void *)get_result_ui64_value(result); case AR_DOUBLE: return (void *)get_result_dbl_value(result); case AR_STRING: return (void *)get_result_str_value(result); case AR_TEXT: return (void *)get_result_text_value(result); case AR_LOG: return (void *)get_result_log_value(result); case AR_MESSAGE: if (0 != ISSET_MSG(result)) return (void *)(&result->msg); break; default: break; } return NULL; }
void test_parameter(const char *key) { #define ZBX_COL_WIDTH 45 AGENT_RESULT result; int n; n = printf("%s", key); if (0 < n && ZBX_COL_WIDTH > n) printf("%-*s", ZBX_COL_WIDTH - n, " "); init_result(&result); if (SUCCEED == process(key, 0, &result)) { if (0 != ISSET_UI64(&result)) printf(" [u|" ZBX_FS_UI64 "]", result.ui64); if (0 != ISSET_DBL(&result)) printf(" [d|" ZBX_FS_DBL "]", result.dbl); if (0 != ISSET_STR(&result)) printf(" [s|%s]", result.str); if (0 != ISSET_TEXT(&result)) printf(" [t|%s]", result.text); if (0 != ISSET_MSG(&result)) printf(" [m|%s]", result.msg); } else { if (0 != ISSET_MSG(&result)) printf(" [m|" ZBX_NOTSUPPORTED "] [%s]", result.msg); else printf(" [m|" ZBX_NOTSUPPORTED "]"); } free_result(&result); printf("\n"); fflush(stdout); }
int get_value_simple(DC_ITEM *item, AGENT_RESULT *result) { const char *__function_name = "get_value_simple"; AGENT_REQUEST request; vmfunc_t vmfunc; int ret = NOTSUPPORTED; zabbix_log(LOG_LEVEL_DEBUG, "In %s() key_orig:'%s' addr:'%s'", __function_name, item->key_orig, item->interface.addr); init_request(&request); parse_item_key(item->key, &request); request.lastlogsize = item->lastlogsize; if (0 == strcmp(request.key, "net.tcp.service")) { if (SYSINFO_RET_OK == check_service(&request, item->interface.addr, result, 0)) ret = SUCCEED; } else if (0 == strcmp(request.key, "net.tcp.service.perf")) { if (SYSINFO_RET_OK == check_service(&request, item->interface.addr, result, 1)) ret = SUCCEED; } else if (SUCCEED == get_vmware_function(request.key, &vmfunc)) { if (NULL != vmfunc) { if (SYSINFO_RET_OK == vmfunc(&request, item->username, item->password, result)) ret = SUCCEED; } else SET_MSG_RESULT(result, zbx_strdup(NULL, "Support for VMware checks was not compiled in.")); } else { /* it will execute item from a loadable module if any */ if (SUCCEED == process(item->key, PROCESS_MODULE_COMMAND, result)) ret = SUCCEED; } if (NOTSUPPORTED == ret && !ISSET_MSG(result)) SET_MSG_RESULT(result, zbx_strdup(NULL, "Simple check is not supported.")); free_request(&request); zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret)); return ret; }
int SYSTEM_CPU_UTIL(AGENT_REQUEST *request, AGENT_RESULT *result) { char *tmp; int cpu_num, state, mode; if (3 < request->nparam) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters.")); return SYSINFO_RET_FAIL; } tmp = get_rparam(request, 0); if (NULL == tmp || '\0' == *tmp || 0 == strcmp(tmp, "all")) cpu_num = ZBX_CPUNUM_ALL; else if (SUCCEED != is_uint31_1(tmp, &cpu_num)) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid first parameter.")); return SYSINFO_RET_FAIL; } tmp = get_rparam(request, 1); if (NULL == tmp || '\0' == *tmp || 0 == strcmp(tmp, "user")) state = ZBX_CPU_STATE_USER; else if (0 == strcmp(tmp, "system")) state = ZBX_CPU_STATE_SYSTEM; else if (0 == strcmp(tmp, "idle")) state = ZBX_CPU_STATE_IDLE; else if (0 == strcmp(tmp, "iowait")) state = ZBX_CPU_STATE_IOWAIT; else { SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter.")); return SYSINFO_RET_FAIL; } tmp = get_rparam(request, 2); if (NULL == tmp || '\0' == *tmp || 0 == strcmp(tmp, "avg1")) mode = ZBX_AVG1; else if (0 == strcmp(tmp, "avg5")) mode = ZBX_AVG5; else if (0 == strcmp(tmp, "avg15")) mode = ZBX_AVG15; else { SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid third parameter.")); return SYSINFO_RET_FAIL; } if (SYSINFO_RET_FAIL == get_cpustat(result, cpu_num, state, mode)) { if (!ISSET_MSG(result)) SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot obtain CPU information.")); return SYSINFO_RET_FAIL; } return SYSINFO_RET_OK; }
static int zbx_execute_script_on_terminal(DC_HOST *host, zbx_script_t *script, char **result, char *error, size_t max_error_len) { const char *__function_name = "zbx_execute_script_on_terminal"; int ret; AGENT_RESULT agent_result; DC_ITEM item; int (*function)(); #ifdef HAVE_SSH2 assert(ZBX_SCRIPT_TYPE_SSH == script->type || ZBX_SCRIPT_TYPE_TELNET == script->type); #else assert(ZBX_SCRIPT_TYPE_TELNET == script->type); #endif zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); *error = '\0'; memset(&item, 0, sizeof(item)); memcpy(&item.host, host, sizeof(item.host)); if (SUCCEED != (ret = DCconfig_get_interface_by_type(&item.interface, host->hostid, INTERFACE_TYPE_AGENT))) { zbx_snprintf(error, max_error_len, "Whatap agent interface is not defined for host [%s]", host->host); goto fail; } switch (script->type) { case ZBX_SCRIPT_TYPE_SSH: item.authtype = script->authtype; item.publickey = script->publickey; item.privatekey = script->privatekey; /* break; is not missing here */ case ZBX_SCRIPT_TYPE_TELNET: item.username = script->username; item.password = script->password; break; } substitute_simple_macros(NULL, NULL, NULL, NULL, &host->hostid, NULL, NULL, &script->port, MACRO_TYPE_COMMON, NULL, 0); if ('\0' != *script->port && SUCCEED != (ret = is_ushort(script->port, NULL))) { zbx_snprintf(error, max_error_len, "Invalid port number [%s]", script->port); goto fail; } #ifdef HAVE_SSH2 if (ZBX_SCRIPT_TYPE_SSH == script->type) { item.key = zbx_dsprintf(item.key, "ssh.run[,,%s]", script->port); function = get_value_ssh; } else { #endif item.key = zbx_dsprintf(item.key, "telnet.run[,,%s]", script->port); function = get_value_telnet; #ifdef HAVE_SSH2 } #endif item.value_type = ITEM_VALUE_TYPE_TEXT; item.params = zbx_strdup(item.params, script->command); init_result(&agent_result); alarm(CONFIG_TIMEOUT); if (SUCCEED != (ret = function(&item, &agent_result))) { if (ISSET_MSG(&agent_result)) zbx_strlcpy(error, agent_result.msg, max_error_len); ret = FAIL; } else if (NULL != result && ISSET_TEXT(&agent_result)) *result = zbx_strdup(*result, agent_result.text); alarm(0); free_result(&agent_result); zbx_free(item.params); zbx_free(item.key); fail: zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret)); return ret; }
/****************************************************************************** * * * Function: process * * * * Purpose: execute agent check * * * * Parameters: in_command - item key * * flags - PROCESS_LOCAL_COMMAND, allow execution of system.run * * PROCESS_MODULE_COMMAND, execute item from a module * * * * Return value: SUCCEED - successful execution * * NOTSUPPORTED - item key is not supported or other error * * result - contains item value or error message * * * ******************************************************************************/ int process(const char *in_command, unsigned flags, AGENT_RESULT *result) { int ret = NOTSUPPORTED; ZBX_METRIC *command = NULL; AGENT_REQUEST request; init_result(result); init_request(&request); if (SUCCEED != parse_item_key(zbx_alias_get(in_command), &request)) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid item key format.")); goto notsupported; } /* system.run is not allowed by default except for getting hostname for daemons */ if (1 != CONFIG_ENABLE_REMOTE_COMMANDS && 0 == (flags & PROCESS_LOCAL_COMMAND) && 0 == strcmp(request.key, "system.run")) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Remote commands are not enabled.")); goto notsupported; } for (command = commands; NULL != command->key; command++) { if (0 == strcmp(command->key, request.key)) break; } /* item key not found */ if (NULL == command->key) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Unsupported item key.")); goto notsupported; } /* expected item from a module */ if (0 != (flags & PROCESS_MODULE_COMMAND) && 0 == (command->flags & CF_MODULE)) goto notsupported; /* command does not accept parameters but was called with parameters */ if (0 == (command->flags & CF_HAVEPARAMS) && 0 != request.nparam) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Item does not allow parameters.")); goto notsupported; } if (0 != (command->flags & CF_USERPARAMETER)) { if (0 != (command->flags & CF_HAVEPARAMS)) { char *parameters = NULL, error[MAX_STRING_LEN]; if (FAIL == replace_param(command->test_param, &request, ¶meters, error, sizeof(error))) { SET_MSG_RESULT(result, zbx_strdup(NULL, error)); goto notsupported; } free_request_params(&request); add_request_param(&request, parameters); } else { free_request_params(&request); add_request_param(&request, zbx_strdup(NULL, command->test_param)); } } if (SYSINFO_RET_OK != command->function(&request, result)) { /* "return NOTSUPPORTED;" would be more appropriate here for preserving original error */ /* message in "result" but would break things relying on ZBX_NOTSUPPORTED message. */ if (0 != (command->flags & CF_MODULE) && 0 == ISSET_MSG(result)) SET_MSG_RESULT(result, zbx_strdup(NULL, "Item is not supported.")); goto notsupported; } ret = SUCCEED; notsupported: free_request(&request); return ret; }
void get_values_java(unsigned char request, DC_ITEM *items, AGENT_RESULT *results, int *errcodes, int num) { const char *__function_name = "get_values_java"; zbx_sock_t s; struct zbx_json json; char error[MAX_STRING_LEN]; char *buffer = NULL; int i, j, err = SUCCEED; zabbix_log(LOG_LEVEL_DEBUG, "In %s() host:'%s' addr:'%s' num:%d", __function_name, items[0].host.host, items[0].interface.addr, num); for (j = 0; j < num; j++) /* locate first supported item */ { if (SUCCEED == errcodes[j]) break; } if (j == num) /* all items already NOTSUPPORTED (with invalid key or port) */ goto out; zbx_json_init(&json, ZBX_JSON_STAT_BUF_LEN); if (NULL == CONFIG_JAVA_GATEWAY || '\0' == *CONFIG_JAVA_GATEWAY) { err = GATEWAY_ERROR; strscpy(error, "JavaGateway configuration parameter not set or empty"); goto exit; } if (ZBX_JAVA_GATEWAY_REQUEST_INTERNAL == request) { zbx_json_addstring(&json, ZBX_PROTO_TAG_REQUEST, ZBX_PROTO_VALUE_JAVA_GATEWAY_INTERNAL, ZBX_JSON_TYPE_STRING); } else if (ZBX_JAVA_GATEWAY_REQUEST_JMX == request) { for (i = j + 1; i < num; i++) { if (SUCCEED != errcodes[i]) continue; if (0 != strcmp(items[j].interface.addr, items[i].interface.addr) || items[j].interface.port != items[i].interface.port || 0 != strcmp(items[j].username, items[i].username) || 0 != strcmp(items[j].password, items[i].password)) { err = GATEWAY_ERROR; strscpy(error, "Java poller received items with different connection parameters"); goto exit; } } zbx_json_addstring(&json, ZBX_PROTO_TAG_REQUEST, ZBX_PROTO_VALUE_JAVA_GATEWAY_JMX, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&json, ZBX_PROTO_TAG_CONN, items[j].interface.addr, ZBX_JSON_TYPE_STRING); zbx_json_adduint64(&json, ZBX_PROTO_TAG_PORT, items[j].interface.port); if ('\0' != *items[j].username) zbx_json_addstring(&json, ZBX_PROTO_TAG_USERNAME, items[j].username, ZBX_JSON_TYPE_STRING); if ('\0' != *items[j].password) zbx_json_addstring(&json, ZBX_PROTO_TAG_PASSWORD, items[j].password, ZBX_JSON_TYPE_STRING); } else assert(0); zbx_json_addarray(&json, ZBX_PROTO_TAG_KEYS); for (i = 0; i < num; i++) { if (SUCCEED != errcodes[i]) continue; zbx_json_addstring(&json, NULL, items[i].key, ZBX_JSON_TYPE_STRING); } zbx_json_close(&json); if (SUCCEED == (err = zbx_tcp_connect(&s, CONFIG_SOURCE_IP, CONFIG_JAVA_GATEWAY, CONFIG_JAVA_GATEWAY_PORT, CONFIG_TIMEOUT))) { zabbix_log(LOG_LEVEL_DEBUG, "JSON before sending [%s]", json.buffer); if (SUCCEED == (err = zbx_tcp_send(&s, json.buffer))) { if (SUCCEED == (err = zbx_tcp_recv(&s, &buffer))) { zabbix_log(LOG_LEVEL_DEBUG, "JSON back [%s]", buffer); err = parse_response(items, results, errcodes, num, buffer, error, sizeof(error)); } } zbx_tcp_close(&s); } zbx_json_free(&json); if (FAIL == err) { strscpy(error, zbx_tcp_strerror()); err = GATEWAY_ERROR; } exit: if (NETWORK_ERROR == err || GATEWAY_ERROR == err) { zabbix_log(LOG_LEVEL_DEBUG, "Getting Java values failed: %s", error); for (i = 0; i < num; i++) { if (SUCCEED != errcodes[i]) continue; if (!ISSET_MSG(&results[i])) { SET_MSG_RESULT(&results[i], zbx_strdup(NULL, error)); errcodes[i] = err; } } } out: zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
static int get_value(DC_ITEM *item, AGENT_RESULT *result) { const char *__function_name = "get_value"; int res = FAIL; zabbix_log(LOG_LEVEL_DEBUG, "In %s() key:'%s'", __function_name, item->key_orig); switch (item->type) { case ITEM_TYPE_ZABBIX: alarm(CONFIG_TIMEOUT); res = get_value_agent(item, result); alarm(0); break; case ITEM_TYPE_SNMPv1: case ITEM_TYPE_SNMPv2c: case ITEM_TYPE_SNMPv3: #ifdef HAVE_SNMP alarm(CONFIG_TIMEOUT); res = get_value_snmp(item, result); alarm(0); #else SET_MSG_RESULT(result, zbx_strdup(NULL, "Support for SNMP checks was not compiled in")); res = NOTSUPPORTED; #endif break; case ITEM_TYPE_IPMI: #ifdef HAVE_OPENIPMI res = get_value_ipmi(item, result); #else SET_MSG_RESULT(result, zbx_strdup(NULL, "Support for IPMI checks was not compiled in")); res = NOTSUPPORTED; #endif break; case ITEM_TYPE_SIMPLE: /* simple checks use their own timeouts */ res = get_value_simple(item, result); break; case ITEM_TYPE_INTERNAL: res = get_value_internal(item, result); break; case ITEM_TYPE_DB_MONITOR: alarm(CONFIG_TIMEOUT); res = get_value_db(item, result); alarm(0); break; case ITEM_TYPE_AGGREGATE: res = get_value_aggregate(item, result); break; case ITEM_TYPE_EXTERNAL: /* external checks use their own timeouts */ res = get_value_external(item, result); break; case ITEM_TYPE_SSH: #ifdef HAVE_SSH2 alarm(CONFIG_TIMEOUT); res = get_value_ssh(item, result); alarm(0); #else SET_MSG_RESULT(result, zbx_strdup(NULL, "Support for SSH checks was not compiled in")); res = NOTSUPPORTED; #endif /* HAVE_SSH2 */ break; case ITEM_TYPE_TELNET: alarm(CONFIG_TIMEOUT); res = get_value_telnet(item, result); alarm(0); break; case ITEM_TYPE_CALCULATED: res = get_value_calculated(item, result); break; default: SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Not supported item type:%d", item->type)); res = NOTSUPPORTED; } if (SUCCEED != res) { if (!ISSET_MSG(result)) SET_MSG_RESULT(result, zbx_strdup(NULL, "ZBX_NOTSUPPORTED")); zabbix_log(LOG_LEVEL_DEBUG, "Item [%s:%s] error: %s", item->host.host, item->key_orig, result->msg); zabbix_syslog("Item [%s:%s] error: %s", item->host.host, item->key_orig, result->msg); } /* remove formatting characters from the end of the result */ /* so it could be checked by "is_uint64" and "is_double" functions */ /* when we try to get "int" or "float" values from "string" result */ if (ISSET_STR(result)) zbx_rtrim(result->str, " \r\n"); if (ISSET_TEXT(result)) zbx_rtrim(result->text, " \r\n"); zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(res)); return res; }
/****************************************************************************** * * * Function: discover_service * * * * Purpose: check if service is available and update database * * * * Parameters: service type, ip address, port number * * * * Author: Alexei Vladishev * * * ******************************************************************************/ static int discover_service(DB_DCHECK *dcheck, char *ip, int port, char *value) { const char *__function_name = "discover_service"; int ret = SUCCEED; char key[MAX_STRING_LEN], error[ITEM_ERROR_LEN_MAX]; const char *service = NULL; AGENT_RESULT result; DC_ITEM item; ZBX_FPING_HOST host; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); init_result(&result); *value = '\0'; switch (dcheck->type) { case SVC_SSH: service = "ssh"; break; case SVC_LDAP: service = "ldap"; break; case SVC_SMTP: service = "smtp"; break; case SVC_FTP: service = "ftp"; break; case SVC_HTTP: service = "http"; break; case SVC_POP: service = "pop"; break; case SVC_NNTP: service = "nntp"; break; case SVC_IMAP: service = "imap"; break; case SVC_TCP: service = "tcp"; break; case SVC_HTTPS: service = "https"; break; case SVC_TELNET: service = "telnet"; break; case SVC_AGENT: case SVC_SNMPv1: case SVC_SNMPv2c: case SVC_SNMPv3: case SVC_ICMPPING: break; default: ret = FAIL; break; } if (SUCCEED == ret) { alarm(CONFIG_TIMEOUT); switch (dcheck->type) { /* simple checks */ case SVC_SSH: case SVC_LDAP: case SVC_SMTP: case SVC_FTP: case SVC_HTTP: case SVC_POP: case SVC_NNTP: case SVC_IMAP: case SVC_TCP: case SVC_HTTPS: case SVC_TELNET: zbx_snprintf(key, sizeof(key), "net.tcp.service[%s,%s,%d]", service, ip, port); if (SUCCEED != process(key, 0, &result) || NULL == GET_UI64_RESULT(&result) || 0 == result.ui64) { ret = FAIL; } break; /* agent and SNMP checks */ case SVC_AGENT: case SVC_SNMPv1: case SVC_SNMPv2c: case SVC_SNMPv3: memset(&item, 0, sizeof(DC_ITEM)); strscpy(item.key_orig, dcheck->key_); item.key = item.key_orig; item.interface.useip = 1; item.interface.addr = ip; item.interface.port = port; item.value_type = ITEM_VALUE_TYPE_STR; switch (dcheck->type) { case SVC_SNMPv1: item.type = ITEM_TYPE_SNMPv1; break; case SVC_SNMPv2c: item.type = ITEM_TYPE_SNMPv2c; break; case SVC_SNMPv3: item.type = ITEM_TYPE_SNMPv3; break; default: item.type = ITEM_TYPE_ZABBIX; break; } if (SVC_AGENT == dcheck->type) { if (SUCCEED == get_value_agent(&item, &result) && NULL != GET_STR_RESULT(&result)) zbx_strlcpy(value, result.str, DSERVICE_VALUE_LEN_MAX); else ret = FAIL; } else #ifdef HAVE_SNMP { item.snmp_community = strdup(dcheck->snmp_community); item.snmp_oid = strdup(dcheck->key_); substitute_simple_macros(NULL, NULL, NULL, NULL, NULL, NULL, &item.snmp_community, MACRO_TYPE_ITEM_FIELD, NULL, 0); substitute_key_macros(&item.snmp_oid, NULL, NULL, NULL, MACRO_TYPE_SNMP_OID, NULL, 0); if (ITEM_TYPE_SNMPv3 == item.type) { item.snmpv3_securityname = strdup(dcheck->snmpv3_securityname); item.snmpv3_securitylevel = dcheck->snmpv3_securitylevel; item.snmpv3_authpassphrase = strdup(dcheck->snmpv3_authpassphrase); item.snmpv3_privpassphrase = strdup(dcheck->snmpv3_privpassphrase); substitute_simple_macros(NULL, NULL, NULL, NULL, NULL, NULL, &item.snmpv3_securityname, MACRO_TYPE_ITEM_FIELD, NULL, 0); substitute_simple_macros(NULL, NULL, NULL, NULL, NULL, NULL, &item.snmpv3_authpassphrase, MACRO_TYPE_ITEM_FIELD, NULL, 0); substitute_simple_macros(NULL, NULL, NULL, NULL, NULL, NULL, &item.snmpv3_privpassphrase, MACRO_TYPE_ITEM_FIELD, NULL, 0); } if (SUCCEED == get_value_snmp(&item, &result) && NULL != GET_STR_RESULT(&result)) zbx_strlcpy(value, result.str, DSERVICE_VALUE_LEN_MAX); else ret = FAIL; zbx_free(item.snmp_community); zbx_free(item.snmp_oid); if (ITEM_TYPE_SNMPv3 == item.type) { zbx_free(item.snmpv3_securityname); zbx_free(item.snmpv3_authpassphrase); zbx_free(item.snmpv3_privpassphrase); } } #else ret = FAIL; #endif /* HAVE_SNMP */ if (FAIL == ret && ISSET_MSG(&result)) { zabbix_log(LOG_LEVEL_DEBUG, "discovery: item [%s] error: %s", item.key, result.msg); } break; case SVC_ICMPPING: memset(&host, 0, sizeof(host)); host.addr = strdup(ip); if (SUCCEED != do_ping(&host, 1, 3, 0, 0, 0, error, sizeof(error)) || 0 == host.rcv) ret = FAIL; zbx_free(host.addr); break; default: break; } alarm(0); } free_result(&result); zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret)); return ret; }
/****************************************************************************** * * * Function: get_value_internal * * * * Purpose: retrieve data from Zabbix server (internally supported items) * * * * Parameters: item - item we are interested in * * * * Return value: SUCCEED - data successfully retrieved and stored in result * * NOTSUPPORTED - requested item is not supported * * * * Author: Alexei Vladishev * * * ******************************************************************************/ int get_value_internal(DC_ITEM *item, AGENT_RESULT *result) { int nparams; char params[MAX_STRING_LEN], *error = NULL; char tmp[MAX_STRING_LEN], tmp1[HOST_HOST_LEN_MAX]; init_result(result); if (0 != strncmp(item->key, "zabbix[", 7)) goto notsupported; if (2 != parse_command(item->key, NULL, 0, params, sizeof(params))) goto notsupported; if (0 != get_param(params, 1, tmp, sizeof(tmp))) goto notsupported; nparams = num_param(params); if (0 == strcmp(tmp, "triggers")) /* zabbix["triggers"] */ { if (1 != nparams) goto notsupported; SET_UI64_RESULT(result, DBget_row_count("triggers")); } else if (0 == strcmp(tmp, "items")) /* zabbix["items"] */ { if (1 != nparams) goto notsupported; SET_UI64_RESULT(result, DBget_row_count("items")); } else if (0 == strcmp(tmp, "items_unsupported")) /* zabbix["items_unsupported"] */ { if (1 != nparams) goto notsupported; SET_UI64_RESULT(result, DBget_items_unsupported_count()); } else if (0 == strcmp(tmp, "history") || /* zabbix["history"] */ 0 == strcmp(tmp, "history_log") || /* zabbix["history_log"] */ 0 == strcmp(tmp, "history_str") || /* zabbix["history_str"] */ 0 == strcmp(tmp, "history_text") || /* zabbix["history_text"] */ 0 == strcmp(tmp, "history_uint")) /* zabbix["history_uint"] */ { if (1 != nparams) goto notsupported; SET_UI64_RESULT(result, DBget_row_count(tmp)); } else if (0 == strcmp(tmp, "trends") || /* zabbix["trends"] */ 0 == strcmp(tmp, "trends_uint")) /* zabbix["trends_uint"] */ { if (1 != nparams) goto notsupported; SET_UI64_RESULT(result, DBget_row_count(tmp)); } else if (0 == strcmp(tmp, "queue")) /* zabbix["queue",<from>,<to>] */ { unsigned int from = 6, to = (unsigned int)-1; if (3 < nparams) { error = zbx_strdup(error, "Invalid number of parameters"); goto notsupported; } if (2 <= nparams) { if (0 != get_param(params, 2, tmp, sizeof(tmp))) goto notsupported; if ('\0' != *tmp && FAIL == is_uint_suffix(tmp, &from)) { error = zbx_strdup(error, "Invalid second parameter"); goto notsupported; } } if (3 <= nparams) { if (0 != get_param(params, 3, tmp, sizeof(tmp))) goto notsupported; if ('\0' != *tmp && FAIL == is_uint_suffix(tmp, &to)) { error = zbx_strdup(error, "Invalid third parameter"); goto notsupported; } } if ((unsigned int)-1 != to && from > to) { error = zbx_strdup(error, "Parameters represent an invalid interval"); goto notsupported; } SET_UI64_RESULT(result, DBget_queue_count((int)from, (int)to)); } else if (0 == strcmp(tmp, "requiredperformance")) /* zabbix["requiredperformance"] */ { if (1 != nparams) goto notsupported; SET_DBL_RESULT(result, DBget_requiredperformance()); } else if (0 == strcmp(tmp, "uptime")) /* zabbix["uptime"] */ { if (1 != nparams) goto notsupported; SET_UI64_RESULT(result, time(NULL) - CONFIG_SERVER_STARTUP_TIME); } else if (0 == strcmp(tmp, "boottime")) /* zabbix["boottime"] */ { if (1 != nparams) goto notsupported; SET_UI64_RESULT(result, CONFIG_SERVER_STARTUP_TIME); } else if (0 == strcmp(tmp, "host")) /* zabbix["host",<type>,"available"] */ { if (3 != nparams) goto notsupported; if (0 != get_param(params, 3, tmp, sizeof(tmp)) || 0 != strcmp(tmp, "available")) goto notsupported; if (0 != get_param(params, 2, tmp, sizeof(tmp))) goto notsupported; if (0 == strcmp(tmp, "agent")) SET_UI64_RESULT(result, item->host.available); else if (0 == strcmp(tmp, "snmp")) SET_UI64_RESULT(result, item->host.snmp_available); else if (0 == strcmp(tmp, "ipmi")) SET_UI64_RESULT(result, item->host.ipmi_available); else if (0 == strcmp(tmp, "jmx")) SET_UI64_RESULT(result, item->host.jmx_available); else goto notsupported; result->ui64 = 2 - result->ui64; } else if (0 == strcmp(tmp, "proxy")) /* zabbix["proxy",<hostname>,"lastaccess"] */ { int lastaccess; if (3 != nparams) goto notsupported; if (0 != get_param(params, 2, tmp1, sizeof(tmp1))) goto notsupported; if (0 != get_param(params, 3, tmp, sizeof(tmp)) || 0 != strcmp(tmp, "lastaccess")) goto notsupported; if (FAIL == DBget_proxy_lastaccess(tmp1, &lastaccess, &error)) goto notsupported; SET_UI64_RESULT(result, lastaccess); } else if (0 == strcmp(tmp, "java")) /* zabbix["java",...] */ { int res; alarm(CONFIG_TIMEOUT); res = get_value_java(ZBX_JAVA_GATEWAY_REQUEST_INTERNAL, item, result); alarm(0); if (SUCCEED != res) goto notsupported; } else if (0 == strcmp(tmp, "process")) /* zabbix["process",<type>,<mode>,<state>] */ { unsigned char process_type; int process_forks; double value; if (4 < nparams) { error = zbx_strdup(error, "Invalid number of parameters"); goto notsupported; } if (0 != get_param(params, 2, tmp, sizeof(tmp))) { error = zbx_strdup(error, "Required second parameter missing"); goto notsupported; } for (process_type = 0; process_type < ZBX_PROCESS_TYPE_COUNT; process_type++) if (0 == strcmp(tmp, get_process_type_string(process_type))) break; if (ZBX_PROCESS_TYPE_COUNT == process_type) { error = zbx_strdup(error, "Invalid second parameter"); goto notsupported; } process_forks = get_process_type_forks(process_type); if (0 != get_param(params, 3, tmp, sizeof(tmp))) *tmp = '\0'; if (0 == strcmp(tmp, "count")) { if (3 < nparams) { error = zbx_strdup(error, "Invalid number of parameters"); goto notsupported; } SET_UI64_RESULT(result, process_forks); } else { unsigned char aggr_func, state; unsigned short process_num = 0; if ('\0' == *tmp || 0 == strcmp(tmp, "avg")) aggr_func = ZBX_AGGR_FUNC_AVG; else if (0 == strcmp(tmp, "max")) aggr_func = ZBX_AGGR_FUNC_MAX; else if (0 == strcmp(tmp, "min")) aggr_func = ZBX_AGGR_FUNC_MIN; else if (SUCCEED == is_ushort(tmp, &process_num) && 0 < process_num) aggr_func = ZBX_AGGR_FUNC_ONE; else { error = zbx_strdup(error, "Invalid third parameter"); goto notsupported; } if (0 == process_forks) { error = zbx_dsprintf(error, "No \"%s\" processes started", get_process_type_string(process_type)); goto notsupported; } else if (process_num > process_forks) { error = zbx_dsprintf(error, "\"%s\" #%d is not started", get_process_type_string(process_type), process_num); goto notsupported; } if (0 != get_param(params, 4, tmp, sizeof(tmp))) *tmp = '\0'; if ('\0' == *tmp || 0 == strcmp(tmp, "busy")) state = ZBX_PROCESS_STATE_BUSY; else if (0 == strcmp(tmp, "idle")) state = ZBX_PROCESS_STATE_IDLE; else { error = zbx_strdup(error, "Invalid fourth parameter"); goto notsupported; } get_selfmon_stats(process_type, aggr_func, process_num, state, &value); SET_DBL_RESULT(result, value); } } else if (0 == strcmp(tmp, "wcache")) /* zabbix[wcache,<cache>,<mode>] */ { if (3 < nparams) goto notsupported; if (0 != get_param(params, 2, tmp, sizeof(tmp))) goto notsupported; if (0 != get_param(params, 3, tmp1, sizeof(tmp1))) *tmp1 = '\0'; if (0 == strcmp(tmp, "values")) { if ('\0' == *tmp1 || 0 == strcmp(tmp1, "all")) SET_UI64_RESULT(result, *(zbx_uint64_t *)DCget_stats(ZBX_STATS_HISTORY_COUNTER)); else if (0 == strcmp(tmp1, "float")) SET_UI64_RESULT(result, *(zbx_uint64_t *)DCget_stats(ZBX_STATS_HISTORY_FLOAT_COUNTER)); else if (0 == strcmp(tmp1, "uint")) SET_UI64_RESULT(result, *(zbx_uint64_t *)DCget_stats(ZBX_STATS_HISTORY_UINT_COUNTER)); else if (0 == strcmp(tmp1, "str")) SET_UI64_RESULT(result, *(zbx_uint64_t *)DCget_stats(ZBX_STATS_HISTORY_STR_COUNTER)); else if (0 == strcmp(tmp1, "log")) SET_UI64_RESULT(result, *(zbx_uint64_t *)DCget_stats(ZBX_STATS_HISTORY_LOG_COUNTER)); else if (0 == strcmp(tmp1, "text")) SET_UI64_RESULT(result, *(zbx_uint64_t *)DCget_stats(ZBX_STATS_HISTORY_TEXT_COUNTER)); else if (0 == strcmp(tmp1, "not supported")) SET_UI64_RESULT(result, *(zbx_uint64_t *)DCget_stats(ZBX_STATS_NOTSUPPORTED_COUNTER)); else goto notsupported; } else if (0 == strcmp(tmp, "history")) { if ('\0' == *tmp1 || 0 == strcmp(tmp1, "pfree")) SET_DBL_RESULT(result, *(double *)DCget_stats(ZBX_STATS_HISTORY_PFREE)); else if (0 == strcmp(tmp1, "total")) SET_UI64_RESULT(result, *(zbx_uint64_t *)DCget_stats(ZBX_STATS_HISTORY_TOTAL)); else if (0 == strcmp(tmp1, "used")) SET_UI64_RESULT(result, *(zbx_uint64_t *)DCget_stats(ZBX_STATS_HISTORY_USED)); else if (0 == strcmp(tmp1, "free")) SET_UI64_RESULT(result, *(zbx_uint64_t *)DCget_stats(ZBX_STATS_HISTORY_FREE)); else goto notsupported; } else if (0 == strcmp(tmp, "trend")) { if ('\0' == *tmp1 || 0 == strcmp(tmp1, "pfree")) SET_DBL_RESULT(result, *(double *)DCget_stats(ZBX_STATS_TREND_PFREE)); else if (0 == strcmp(tmp1, "total")) SET_UI64_RESULT(result, *(zbx_uint64_t *)DCget_stats(ZBX_STATS_TREND_TOTAL)); else if (0 == strcmp(tmp1, "used")) SET_UI64_RESULT(result, *(zbx_uint64_t *)DCget_stats(ZBX_STATS_TREND_USED)); else if (0 == strcmp(tmp1, "free")) SET_UI64_RESULT(result, *(zbx_uint64_t *)DCget_stats(ZBX_STATS_TREND_FREE)); else goto notsupported; } else if (0 == strcmp(tmp, "text")) { if ('\0' == *tmp1 || 0 == strcmp(tmp1, "pfree")) SET_DBL_RESULT(result, *(double *)DCget_stats(ZBX_STATS_TEXT_PFREE)); else if (0 == strcmp(tmp1, "total")) SET_UI64_RESULT(result, *(zbx_uint64_t *)DCget_stats(ZBX_STATS_TEXT_TOTAL)); else if (0 == strcmp(tmp1, "used")) SET_UI64_RESULT(result, *(zbx_uint64_t *)DCget_stats(ZBX_STATS_TEXT_USED)); else if (0 == strcmp(tmp1, "free")) SET_UI64_RESULT(result, *(zbx_uint64_t *)DCget_stats(ZBX_STATS_TEXT_FREE)); else goto notsupported; } else goto notsupported; } else if (0 == strcmp(tmp, "rcache")) /* zabbix[rcache,<cache>,<mode>] */ { if (3 < nparams) goto notsupported; if (0 != get_param(params, 2, tmp, sizeof(tmp))) goto notsupported; if (0 != get_param(params, 3, tmp1, sizeof(tmp1))) *tmp1 = '\0'; if (0 == strcmp(tmp, "buffer")) { if ('\0' == *tmp1 || 0 == strcmp(tmp1, "pfree")) SET_DBL_RESULT(result, *(double *)DCconfig_get_stats(ZBX_CONFSTATS_BUFFER_PFREE)); else if (0 == strcmp(tmp1, "total")) SET_UI64_RESULT(result, *(zbx_uint64_t *)DCconfig_get_stats(ZBX_CONFSTATS_BUFFER_TOTAL)); else if (0 == strcmp(tmp1, "used")) SET_UI64_RESULT(result, *(zbx_uint64_t *)DCconfig_get_stats(ZBX_CONFSTATS_BUFFER_USED)); else if (0 == strcmp(tmp1, "free")) SET_UI64_RESULT(result, *(zbx_uint64_t *)DCconfig_get_stats(ZBX_CONFSTATS_BUFFER_FREE)); else goto notsupported; } else goto notsupported; } else goto notsupported; return SUCCEED; notsupported: if (!ISSET_MSG(result)) { if (NULL == error) error = zbx_strdup(error, "Internal check is not supported"); SET_MSG_RESULT(result, error); } return NOTSUPPORTED; }
int get_value_simple(DC_ITEM *item, AGENT_RESULT *result, zbx_vector_ptr_t *add_results) { AGENT_REQUEST request; vmfunc_t vmfunc; int ret = NOTSUPPORTED; zabbix_log(LOG_LEVEL_DEBUG, "In %s() key_orig:'%s' addr:'%s'", __func__, item->key_orig, item->interface.addr); init_request(&request); if (SUCCEED != parse_item_key(item->key, &request)) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid item key format.")); goto out; } request.lastlogsize = item->lastlogsize; if (0 == strcmp(request.key, "net.tcp.service") || 0 == strcmp(request.key, "net.udp.service")) { if (SYSINFO_RET_OK == check_service(&request, item->interface.addr, result, 0)) ret = SUCCEED; } else if (0 == strcmp(request.key, "net.tcp.service.perf") || 0 == strcmp(request.key, "net.udp.service.perf")) { if (SYSINFO_RET_OK == check_service(&request, item->interface.addr, result, 1)) ret = SUCCEED; } else if (SUCCEED == get_vmware_function(request.key, &vmfunc)) { if (NULL != vmfunc) { if (0 == get_process_type_forks(ZBX_PROCESS_TYPE_VMWARE)) { SET_MSG_RESULT(result, zbx_strdup(NULL, "No \"vmware collector\" processes started.")); goto out; } if (SYSINFO_RET_OK == vmfunc(&request, item->username, item->password, result)) ret = SUCCEED; } else SET_MSG_RESULT(result, zbx_strdup(NULL, "Support for VMware checks was not compiled in.")); } else if (0 == strcmp(request.key, ZBX_VMWARE_PREFIX "eventlog")) { #if defined(HAVE_LIBXML2) && defined(HAVE_LIBCURL) if (SYSINFO_RET_OK == check_vcenter_eventlog(&request, item, result, add_results)) ret = SUCCEED; #else ZBX_UNUSED(add_results); SET_MSG_RESULT(result, zbx_strdup(NULL, "Support for VMware checks was not compiled in.")); #endif } else { /* it will execute item from a loadable module if any */ if (SUCCEED == process(item->key, PROCESS_MODULE_COMMAND, result)) ret = SUCCEED; } if (NOTSUPPORTED == ret && !ISSET_MSG(result)) SET_MSG_RESULT(result, zbx_strdup(NULL, "Simple check is not supported.")); out: free_request(&request); zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_result_string(ret)); return ret; }