/****************************************************************************** * * * Function: execute_script * * * * Purpose: executing command * * * * Return value: SUCCEED - processed successfully * * FAIL - an error occurred * * * ******************************************************************************/ static int execute_script(zbx_uint64_t scriptid, zbx_uint64_t hostid, char **result) { const char *__function_name = "execute_script"; char error[MAX_STRING_LEN]; int ret = FAIL, rc; DC_HOST host; DB_RESULT db_result; DB_ROW db_row; zbx_script_t script; zabbix_log(LOG_LEVEL_DEBUG, "In %s() scriptid:" ZBX_FS_UI64 " hostid:" ZBX_FS_UI64, __function_name, scriptid, hostid); *error = '\0'; if (SUCCEED != (rc = DCget_host_by_hostid(&host, hostid))) { /* let's try to get a host from a database (the host can be disabled) */ db_result = DBselect("select host,name from hosts where hostid=" ZBX_FS_UI64, hostid); if (NULL != (db_row = DBfetch(db_result))) { memset(&host, 0, sizeof(host)); host.hostid = hostid; strscpy(host.host, db_row[0]); strscpy(host.name, db_row[1]); rc = SUCCEED; } DBfree_result(db_result); } if (SUCCEED != rc) { zbx_snprintf(error, sizeof(error), "Unknown Host ID [" ZBX_FS_UI64 "].", hostid); goto fail; } zbx_script_init(&script); script.type = ZBX_SCRIPT_TYPE_GLOBAL_SCRIPT; script.scriptid = scriptid; ret = zbx_execute_script(&host, &script, result, error, sizeof(error)); zbx_script_clean(&script); fail: if (SUCCEED != ret) { if (0 != CONFIG_NODEID) *result = zbx_dsprintf(*result, "NODE %d: %s", CONFIG_NODEID, error); else *result = zbx_strdup(*result, error); } zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret)); return ret; }
/****************************************************************************** * * * Function: tm_execute_remote_command * * * * Purpose: execute remote command task * * * * Parameters: taskid - [IN] the task identifier * * clock - [IN] the task creation time * * ttl - [IN] the task expiration period in seconds * * now - [IN] the current time * * * * Return value: SUCCEED - the remote command was executed * * FAIL - otherwise * * * ******************************************************************************/ static int tm_execute_remote_command(zbx_uint64_t taskid, int clock, int ttl, int now) { DB_ROW row; DB_RESULT result; zbx_uint64_t parent_taskid, hostid; zbx_tm_task_t *task = NULL; int ret = FAIL; zbx_script_t script; char *info = NULL, error[MAX_STRING_LEN]; DC_HOST host; result = DBselect("select command_type,execute_on,port,authtype,username,password,publickey,privatekey," "command,parent_taskid,hostid" " from task_remote_command" " where taskid=" ZBX_FS_UI64, taskid); if (NULL == (row = DBfetch(result))) goto finish; task = zbx_tm_task_create(0, ZBX_TM_TASK_REMOTE_COMMAND_RESULT, ZBX_TM_STATUS_NEW, time(NULL), 0, 0); ZBX_STR2UINT64(parent_taskid, row[9]); if (0 != ttl && clock + ttl < now) { task->data = zbx_tm_remote_command_result_create(parent_taskid, FAIL, "The remote command has been expired."); goto finish; } ZBX_STR2UINT64(hostid, row[10]); if (FAIL == DCget_host_by_hostid(&host, hostid)) { task->data = zbx_tm_remote_command_result_create(parent_taskid, FAIL, "Unknown host."); goto finish; } zbx_script_init(&script); ZBX_STR2UCHAR(script.type, row[0]); ZBX_STR2UCHAR(script.execute_on, row[1]); script.port = (0 == atoi(row[2]) ? (char *)"" : row[2]); ZBX_STR2UCHAR(script.authtype, row[3]); script.username = row[4]; script.password = row[5]; script.publickey = row[6]; script.privatekey = row[7]; script.command = row[8]; if (ZBX_SCRIPT_TYPE_CUSTOM_SCRIPT == script.type && ZBX_SCRIPT_EXECUTE_ON_PROXY == script.execute_on) { if (0 == CONFIG_ENABLE_REMOTE_COMMANDS) { task->data = zbx_tm_remote_command_result_create(parent_taskid, FAIL, "Remote commands are not enabled"); goto finish; } if (1 == CONFIG_LOG_REMOTE_COMMANDS) zabbix_log(LOG_LEVEL_WARNING, "Executing command '%s'", script.command); else zabbix_log(LOG_LEVEL_DEBUG, "Executing command '%s'", script.command); } if (SUCCEED != (ret = zbx_script_execute(&script, &host, &info, error, sizeof(error)))) task->data = zbx_tm_remote_command_result_create(parent_taskid, ret, error); else task->data = zbx_tm_remote_command_result_create(parent_taskid, ret, info); zbx_free(info); finish: DBfree_result(result); DBbegin(); if (NULL != task) { zbx_tm_save_task(task); zbx_tm_task_free(task); } DBexecute("update task set status=%d where taskid=" ZBX_FS_UI64, ZBX_TM_STATUS_DONE, taskid); DBcommit(); return ret; }
/****************************************************************************** * * * Function: execute_script * * * * Purpose: executing command * * * * Parameters: * * * * Return value: SUCCEED - processed successfully * * FAIL - an error occurred * * * * Author: Alexander Vladishev * * * * Comments: * * * ******************************************************************************/ static int execute_script(zbx_uint64_t scriptid, zbx_uint64_t hostid, char **result) { char *p, *command, error[MAX_STRING_LEN]; int ret = FAIL; DC_HOST host; #ifdef HAVE_OPENIPMI DB_RESULT db_result; DB_ROW db_row; DC_ITEM item; int val; #endif zabbix_log(LOG_LEVEL_DEBUG, "In execute_script() scriptid:" ZBX_FS_UI64 " hostid:" ZBX_FS_UI64, scriptid, hostid); if (FAIL == DCget_host_by_hostid(&host, hostid)) { *result = zbx_dsprintf(*result, "NODE %d: Unknown Host ID [" ZBX_FS_UI64 "]", CONFIG_NODEID, hostid); return ret; } if (NULL == (command = get_command_by_scriptid(scriptid))) { *result = zbx_dsprintf(*result, "NODE %d: Unknown Script ID [" ZBX_FS_UI64 "]", CONFIG_NODEID, scriptid); return ret; } substitute_simple_macros(NULL, NULL, &host, NULL, NULL, &command, MACRO_TYPE_SCRIPT, NULL, 0); zabbix_log(LOG_LEVEL_WARNING, "NODE %d: Executing command: '%s'", CONFIG_NODEID, command); p = command; while (*p == ' ' && *p != '\0') p++; #ifdef HAVE_OPENIPMI if (0 == strncmp(p, "IPMI", 4)) { db_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 hostid=" ZBX_FS_UI64 DB_NODE, hostid, DBnode_local("hostid")); if (NULL != (db_row = DBfetch(db_result))) { memset(&item, 0, sizeof(item)); ZBX_STR2UINT64(item.host.hostid, db_row[0]); zbx_strlcpy(item.host.host, db_row[1], sizeof(item.host.host)); item.host.useip = (unsigned char)atoi(db_row[2]); zbx_strlcpy(item.host.ip, db_row[3], sizeof(item.host.ip)); zbx_strlcpy(item.host.dns, db_row[4], sizeof(item.host.dns)); item.host.port = (unsigned short)atoi(db_row[5]); if (1 == atoi(db_row[6])) { zbx_strlcpy(item.host.ipmi_ip_orig, db_row[7], sizeof(item.host.ipmi_ip)); item.host.ipmi_port = (unsigned short)atoi(db_row[8]); item.host.ipmi_authtype = atoi(db_row[9]); item.host.ipmi_privilege = atoi(db_row[10]); zbx_strlcpy(item.host.ipmi_username, db_row[11], sizeof(item.host.ipmi_username)); zbx_strlcpy(item.host.ipmi_password, db_row[12], sizeof(item.host.ipmi_password)); } if (SUCCEED == (ret = parse_ipmi_command(p, item.ipmi_sensor, &val))) { if (SUCCEED == (ret = set_ipmi_control_value(&item, val, error, sizeof(error)))) { *result = zbx_dsprintf(*result, "NODE %d: IPMI command successfully executed", CONFIG_NODEID); } else { *result = zbx_dsprintf(*result, "NODE %d: Cannot execute IPMI command: %s", CONFIG_NODEID, error); } } else { *result = zbx_dsprintf(*result, "NODE %d: Cannot parse IPMI command", CONFIG_NODEID); } } else { *result = zbx_dsprintf(*result, "NODE %d: Unknown Host ID [" ZBX_FS_UI64 "]", CONFIG_NODEID, hostid); } DBfree_result(db_result); } else { #endif /* HAVE_OPENIPMI */ if (SUCCEED != (ret = zbx_execute(p, result, error, sizeof(error), CONFIG_TRAPPER_TIMEOUT))) *result = zbx_dsprintf(*result, "NODE %d: Cannot execute command: %s", CONFIG_NODEID, error); #ifdef HAVE_OPENIPMI } #endif zbx_free(command); return ret; }