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; }
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); }
static double *get_result_dbl_value(AGENT_RESULT *result) { double value; assert(result); if (0 != ISSET_DBL(result)) { /* nothing to do */ } else if (0 != ISSET_UI64(result)) { SET_DBL_RESULT(result, result->ui64); } else if (0 != ISSET_STR(result)) { zbx_rtrim(result->str, " \""); zbx_ltrim(result->str, " \"+"); if (SUCCEED != is_double(result->str)) return NULL; value = atof(result->str); SET_DBL_RESULT(result, value); } else if (0 != ISSET_TEXT(result)) { zbx_rtrim(result->text, " \""); zbx_ltrim(result->text, " \"+"); if (SUCCEED != is_double(result->text)) return NULL; value = atof(result->text); SET_DBL_RESULT(result, value); } /* skip AR_MESSAGE - it is information field */ if (0 != ISSET_DBL(result)) return &result->dbl; return NULL; }
static zbx_uint64_t *get_result_ui64_value(AGENT_RESULT *result) { zbx_uint64_t value; assert(result); if (0 != ISSET_UI64(result)) { /* nothing to do */ } else if (0 != ISSET_DBL(result)) { SET_UI64_RESULT(result, result->dbl); } else if (0 != ISSET_STR(result)) { zbx_rtrim(result->str, " \""); zbx_ltrim(result->str, " \"+"); del_zeroes(result->str); if (SUCCEED != is_uint64(result->str, &value)) return NULL; SET_UI64_RESULT(result, value); } else if (0 != ISSET_TEXT(result)) { zbx_rtrim(result->text, " \""); zbx_ltrim(result->text, " \"+"); del_zeroes(result->text); if (SUCCEED != is_uint64(result->text, &value)) return NULL; SET_UI64_RESULT(result, value); } /* skip AR_MESSAGE - it is information field */ if (0 != ISSET_UI64(result)) return &result->ui64; return NULL; }
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; }
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; }