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; }
static void run_remote_command(char* host_name, char* command) { int ret = 9; AGENT_RESULT agent_result; DC_ITEM item; DB_RESULT result; DB_ROW row; char *p, *host_esc, *param; #ifdef HAVE_OPENIPMI int val; char error[MAX_STRING_LEN]; #endif assert(host_name); assert(command); zabbix_log(LOG_LEVEL_DEBUG, "In run_remote_command(hostname:%s,command:%s)", host_name, command); host_esc = DBdyn_escape_string(host_name); result = DBselect( "select hostid,host,useip,ip,dns,port,useipmi,ipmi_ip,ipmi_port,ipmi_authtype," "ipmi_privilege,ipmi_username,ipmi_password" " from hosts" " where status in (%d)" " and host='%s'" DB_NODE, HOST_STATUS_MONITORED, host_esc, DBnode_local("hostid")); zbx_free(host_esc); if (NULL != (row = DBfetch(result))) { memset(&item, 0, sizeof(item)); ZBX_STR2UINT64(item.host.hostid, row[0]); zbx_strlcpy(item.host.host, row[1], sizeof(item.host.host)); item.host.useip = (unsigned char)atoi(row[2]); zbx_strlcpy(item.host.ip, row[3], sizeof(item.host.ip)); zbx_strlcpy(item.host.dns, row[4], sizeof(item.host.dns)); item.host.port = (unsigned short)atoi(row[5]); p = command; while (*p == ' ' && *p != '\0') p++; #ifdef HAVE_OPENIPMI if (0 == strncmp(p, "IPMI", 4)) { if (1 == atoi(row[6])) { zbx_strlcpy(item.host.ipmi_ip_orig, row[7], sizeof(item.host.ipmi_ip)); item.host.ipmi_port = (unsigned short)atoi(row[8]); item.host.ipmi_authtype = atoi(row[9]); item.host.ipmi_privilege = atoi(row[10]); zbx_strlcpy(item.host.ipmi_username, row[11], sizeof(item.host.ipmi_username)); zbx_strlcpy(item.host.ipmi_password, row[12], sizeof(item.host.ipmi_password)); } if (SUCCEED == (ret = parse_ipmi_command(p, item.ipmi_sensor, &val))) { item.key = item.ipmi_sensor; ret = set_ipmi_control_value(&item, val, error, sizeof(error)); } } else { #endif param = zbx_dyn_escape_string(p, "\""); item.key = zbx_dsprintf(NULL, "system.run[\"%s\",\"nowait\"]", param); zbx_free(param); init_result(&agent_result); alarm(CONFIG_TIMEOUT); ret = get_value_agent(&item, &agent_result); alarm(0); free_result(&agent_result); zbx_free(item.key); #ifdef HAVE_OPENIPMI } #endif } DBfree_result(result); zabbix_log(LOG_LEVEL_DEBUG, "End run_remote_command(result:%d)", ret); }
/****************************************************************************** * * * Function: discover_service * * * * Purpose: check if service is avaiable and update database * * * * Parameters: service typ,e ip address, port number * * * * Return value: * * * * Author: Alexei Vladishev * * * * Comments: * * * ******************************************************************************/ static int discover_service(DB_DCHECK *check, char *ip, int port) { int ret = SUCCEED; char key[MAX_STRING_LEN]; AGENT_RESULT value; DB_ITEM item; struct sigaction phan; assert(check); assert(ip); zabbix_log(LOG_LEVEL_DEBUG, "In discover_service(ip:%s, port:%d, type:%d)", ip, port, check->type); init_result(&value); switch(check->type) { case SVC_SSH: zbx_snprintf(key,sizeof(key),"net.tcp.service[ssh,%s,%d]", ip, port); break; case SVC_LDAP: zbx_snprintf(key,sizeof(key),"net.tcp.service[ldap,%s,%d]", ip, port); break; case SVC_SMTP: zbx_snprintf(key,sizeof(key),"net.tcp.service[smtp,%s,%d]", ip, port); break; case SVC_FTP: zbx_snprintf(key,sizeof(key),"net.tcp.service[ftp,%s,%d]", ip, port); break; case SVC_HTTP: zbx_snprintf(key,sizeof(key),"net.tcp.service[http,%s,%d]", ip, port); break; case SVC_POP: zbx_snprintf(key,sizeof(key),"net.tcp.service[pop,%s,%d]", ip, port); break; case SVC_NNTP: zbx_snprintf(key,sizeof(key),"net.tcp.service[nntp,%s,%d]", ip, port); break; case SVC_IMAP: zbx_snprintf(key,sizeof(key),"net.tcp.service[imap,%s,%d]", ip, port); break; case SVC_TCP: zbx_snprintf(key,sizeof(key),"net.tcp.service[tcp,%s,%d]", ip, port); break; case SVC_AGENT: case SVC_SNMPv1: case SVC_SNMPv2c: break; default: ret = FAIL; break; } if(ret == SUCCEED) { phan.sa_handler = &child_signal_handler; sigemptyset(&phan.sa_mask); phan.sa_flags = 0; sigaction(SIGALRM, &phan, NULL); alarm(10); switch(check->type) { /* Agent and SNMP checks */ case SVC_AGENT: case SVC_SNMPv1: case SVC_SNMPv2c: memset(&item,0,sizeof(DB_ITEM)); strscpy(item.key,check->key_); item.host_name = ip; item.host_ip = ip; item.host_dns = ip; item.useip = 1; item.port = port; item.value_type = ITEM_VALUE_TYPE_STR; if(check->type == SVC_SNMPv1) { item.type = ITEM_TYPE_SNMPv1; } else { item.type = ITEM_TYPE_SNMPv2c; } item.snmp_oid = check->key_; item.snmp_community = check->snmp_community; item.snmp_port = port; if(check->type==SVC_AGENT) { if(SUCCEED == get_value_agent(&item, &value)) { if(GET_STR_RESULT(&value)) { strscpy(check->value, value.str); } else ret = FAIL; } else { ret = FAIL; } } else #ifdef HAVE_SNMP { if(SUCCEED == get_value_snmp(&item, &value)) { if(GET_STR_RESULT(&value)) { strscpy(check->value, value.str); } else ret = FAIL; } else { ret = FAIL; } } #else { ret = FAIL; } #endif break; /* Simple checks */ default: if(process(key, 0, &value) == SUCCEED) { if(GET_UI64_RESULT(&value)) { if(value.ui64 == 0) ret = FAIL; } else ret = FAIL; } else ret = FAIL; break; } alarm(0); } free_result(&value); zabbix_log(LOG_LEVEL_DEBUG, "End discover_service()"); return ret; }
int get_value(DB_ITEM *item, AGENT_RESULT *result) { int res = FAIL; zabbix_log(LOG_LEVEL_DEBUG, "In get_value(key:%s)", item->key); switch (item->type) { case ITEM_TYPE_ZABBIX: alarm(CONFIG_TIMEOUT); res = get_value_agent(item, result); alarm(0); if (SUCCEED != res && GET_MSG_RESULT(result)) zabbix_log(LOG_LEVEL_WARNING, "Item [%s] error: %s", zbx_host_key_string_by_item(item), result->msg); 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, strdup("Support of SNMP parameters was not compiled in")); res = NOTSUPPORTED; #endif if (SUCCEED != res && GET_MSG_RESULT(result)) zabbix_log(LOG_LEVEL_WARNING, "Item [%s] error: %s", zbx_host_key_string_by_item(item), result->msg); break; case ITEM_TYPE_IPMI: #ifdef HAVE_OPENIPMI res = get_value_ipmi(item, result); #else SET_MSG_RESULT(result, strdup("Support of IPMI parameters was not compiled in")); res = NOTSUPPORTED; #endif break; case ITEM_TYPE_SIMPLE: alarm(CONFIG_TIMEOUT); res = get_value_simple(item, result); alarm(0); break; case ITEM_TYPE_INTERNAL: alarm(CONFIG_TIMEOUT); res = get_value_internal(item, result); alarm(0); break; case ITEM_TYPE_DB_MONITOR: alarm(CONFIG_TIMEOUT); res = get_value_db(item, result); alarm(0); if (SUCCEED != res && GET_MSG_RESULT(result)) zabbix_log(LOG_LEVEL_WARNING, "Item [%s] error: %s", zbx_host_key_string_by_item(item), result->msg); break; case ITEM_TYPE_AGGREGATE: alarm(CONFIG_TIMEOUT); res = get_value_aggregate(item, result); alarm(0); break; case ITEM_TYPE_EXTERNAL: alarm(CONFIG_TIMEOUT); res = get_value_external(item, result); alarm(0); if (SUCCEED != res && GET_MSG_RESULT(result)) zabbix_log(LOG_LEVEL_WARNING, "Item [%s] error: %s", zbx_host_key_string_by_item(item), result->msg); break; default: zabbix_log(LOG_LEVEL_WARNING, "Not supported item type:%d", item->type); zabbix_syslog("Not supported item type:%d", item->type); res = NOTSUPPORTED; } zabbix_log(LOG_LEVEL_DEBUG, "End get_value()"); return res; }
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; }