Example #1
0
static int web_curl_set_url(struct web *opt)
{
	const char *__function_name = "web_curl_set_url";

	char *url = NULL;
	char *curl_err_str = NULL;
	int curl_err;
	size_t alloc;
	size_t offset;

	if (opt->is_https)
		url = zbx_strdup(url, "https://");
	else
		url = zbx_strdup(url, "http://");

	offset = strlen(url);
	alloc = sizeof(char) * offset + 1;

	if (opt->is_ip_hostname) {
		zbx_strncpy_alloc(&url, &alloc, &offset, opt->host, strlen(opt->host));
	} else {
		if (opt->is_ipv6) {
			zbx_strncpy_alloc(&url, &alloc, &offset, "[", 1);
			zbx_strncpy_alloc(&url, &alloc, &offset, opt->ip, strlen(opt->ip));
			zbx_strncpy_alloc(&url, &alloc, &offset, "]", 1);
		} else {
			zbx_strncpy_alloc(&url, &alloc, &offset, opt->ip, strlen(opt->ip));
		}
	}

	zbx_strncpy_alloc(&url, &alloc, &offset, ":", 1);
	zbx_strncpy_alloc(&url, &alloc, &offset, opt->port, strlen(opt->port));

	if (*opt->uri != '/')
		zbx_strncpy_alloc(&url, &alloc, &offset, "/", 1);

	zbx_strncpy_alloc(&url, &alloc, &offset, opt->uri, strlen(opt->uri));

	if ((curl_err = curl_easy_setopt(opt->curl->handler, CURLOPT_URL, url))) {
		curl_err_str = zbx_strdup(curl_err_str, curl_easy_strerror(curl_err));
		zabbix_log(LOG_LEVEL_ERR, "%s(): Could not set cURL option [%d]: %s for key %s",
					  __function_name, CURLOPT_URL, curl_err_str, opt->item->key);
		zbx_free(curl_err_str);
		zbx_free(url);
		return FAIL;
        }

	zbx_free(url);
	return SUCCEED;
}
Example #2
0
static int web_curl_set_header(struct web *opt)
{
	const char *__function_name = "web_curl_set_header";

	char *host = NULL;
	char *curl_err_str = NULL;
	int curl_err;
	int i;
	size_t offset;
	size_t alloc;

	if (opt->host) {
		host = zbx_strdup(host, "Host: ");
		offset = strlen(host);
		alloc = sizeof(char) * offset + 1;

		zbx_strncpy_alloc(&host, &alloc, &offset, opt->host, strlen(opt->host));

		if (!(opt->curl->header_lst = curl_slist_append(opt->curl->header_lst, host))) {
				zabbix_log(LOG_LEVEL_ERR, "%s(): Could not append to curl header list for key %s",
							  __function_name, opt->item->key);
				goto failed;
		}
	}

	for (i = 0; i < opt->header_count; i++) {
		if (!(opt->curl->header_lst = curl_slist_append(opt->curl->header_lst, opt->header[i]))) {
			zabbix_log(LOG_LEVEL_ERR, "%s(): Could not append to curl header list for key %s",
						  __function_name, opt->item->key);
			goto failed;
		}
	}


	if (opt->curl->header_lst) {
		if ((curl_err = curl_easy_setopt(opt->curl->handler, CURLOPT_HTTPHEADER, opt->curl->header_lst))) {
			curl_err_str = zbx_strdup(curl_err_str, curl_easy_strerror(curl_err));
			zabbix_log(LOG_LEVEL_ERR, "%s(): Could not set cURL option [%d]: %s for key %s",
						  __function_name, CURLOPT_HTTPHEADER, curl_err_str, opt->item->key);
			goto failed;
		}
	}

	zbx_free(host);
	return SUCCEED;
failed:
	zbx_free(host);
	zbx_free(curl_err_str);
	return FAIL;
}
Example #3
0
File: swap.c Project: zabbix/zabbix
int	SYSTEM_SWAP_SIZE(AGENT_REQUEST *request, AGENT_RESULT *result)
{
#ifdef HAVE_LIBPERFSTAT
	perfstat_memory_total_t	mem;
	char			*swapdev, *mode;

	if (2 < request->nparam)
	{
		SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters."));
		return SYSINFO_RET_FAIL;
	}

	swapdev = get_rparam(request, 0);
	mode = get_rparam(request, 1);

	if (NULL != swapdev && '\0' != *swapdev && 0 != strcmp(swapdev, "all"))
	{
		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid first parameter."));
		return SYSINFO_RET_FAIL;
	}

	if (1 != perfstat_memory_total(NULL, &mem, sizeof(perfstat_memory_total_t), 1))
	{
		SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain system information: %s", zbx_strerror(errno)));
		return SYSINFO_RET_FAIL;
	}

	if (NULL == mode || '\0' == *mode || 0 == strcmp(mode, "free"))
		SET_UI64_RESULT(result, mem.pgsp_free << ZBX_PERFSTAT_PAGE_SHIFT);
	else if (0 == strcmp(mode, "total"))
		SET_UI64_RESULT(result, mem.pgsp_total << ZBX_PERFSTAT_PAGE_SHIFT);
	else if (0 == strcmp(mode, "used"))
		SET_UI64_RESULT(result, (mem.pgsp_total - mem.pgsp_free) << ZBX_PERFSTAT_PAGE_SHIFT);
	else if (0 == strcmp(mode, "pfree"))
		SET_DBL_RESULT(result, mem.pgsp_total ? 100.0 * (mem.pgsp_free / (double)mem.pgsp_total) : 0.0);
	else if (0 == strcmp(mode, "pused"))
		SET_DBL_RESULT(result, mem.pgsp_total ? 100.0 - 100.0 * (mem.pgsp_free / (double)mem.pgsp_total) : 0.0);
	else
	{
		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
		return SYSINFO_RET_FAIL;
	}

	return SYSINFO_RET_OK;
#else
	SET_MSG_RESULT(result, zbx_strdup(NULL, "Agent was compiled without support for Perfstat API."));
	return SYSINFO_RET_FAIL;
#endif
}
Example #4
0
/******************************************************************************
 *                                                                            *
 * Function: set_defaults                                                     *
 *                                                                            *
 * Purpose: set configuration defaults                                        *
 *                                                                            *
 * Author: Vladimir Levijev, Rudolfs Kreicbergs                               *
 *                                                                            *
 ******************************************************************************/
static void	set_defaults(void)
{
	AGENT_RESULT	result;
	char		**value = NULL;

	if (NULL == CONFIG_HOSTNAME)
	{
		if (NULL == CONFIG_HOSTNAME_ITEM)
			CONFIG_HOSTNAME_ITEM = zbx_strdup(CONFIG_HOSTNAME_ITEM, "system.hostname");

		init_result(&result);

		if (SUCCEED == process(CONFIG_HOSTNAME_ITEM, PROCESS_LOCAL_COMMAND, &result) &&
				NULL != (value = GET_STR_RESULT(&result)))
		{
			assert(*value);

			if (MAX_ZBX_HOSTNAME_LEN < strlen(*value))
			{
				(*value)[MAX_ZBX_HOSTNAME_LEN] = '\0';
				zabbix_log(LOG_LEVEL_WARNING, "hostname truncated to [%s])", *value);
			}

			CONFIG_HOSTNAME = zbx_strdup(CONFIG_HOSTNAME, *value);
		}
		else
			zabbix_log(LOG_LEVEL_WARNING, "failed to get system hostname from [%s])", CONFIG_HOSTNAME_ITEM);

		free_result(&result);
	}
	else if (NULL != CONFIG_HOSTNAME_ITEM)
		zabbix_log(LOG_LEVEL_WARNING, "both Hostname and HostnameItem defined, using [%s]", CONFIG_HOSTNAME);

	if (NULL != CONFIG_HOST_METADATA && NULL != CONFIG_HOST_METADATA_ITEM)
	{
		zabbix_log(LOG_LEVEL_WARNING, "both HostMetadata and HostMetadataItem defined, using [%s]",
				CONFIG_HOST_METADATA);
	}

#ifndef _WINDOWS
	if (NULL == CONFIG_LOAD_MODULE_PATH)
		CONFIG_LOAD_MODULE_PATH = zbx_strdup(CONFIG_LOAD_MODULE_PATH, LIBDIR "/modules");
#endif

#ifdef USE_PID_FILE
	if (NULL == CONFIG_PID_FILE)
		CONFIG_PID_FILE = "/tmp/zabbix_agentd.pid";
#endif
}
static char	*zbx_get_snmp_type_error(u_char type)
{
	switch (type)
	{
		case SNMP_NOSUCHOBJECT:
			return zbx_strdup(NULL, "No Such Object available on this agent at this OID");
		case SNMP_NOSUCHINSTANCE:
			return zbx_strdup(NULL, "No Such Instance currently exists at this OID");
		case SNMP_ENDOFMIBVIEW:
			return zbx_strdup(NULL, "No more variables left in this MIB View"
					" (it is past the end of the MIB tree)");
		default:
			return zbx_dsprintf(NULL, "Value has unknown type 0x%02X", type);
	}
}
Example #6
0
int	VM_MEMORY_SIZE(AGENT_REQUEST *request, AGENT_RESULT *result)
{
	char	*mode;
	int	ret = SYSINFO_RET_FAIL;

	if (1 < request->nparam)
	{
		SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters."));
		return SYSINFO_RET_FAIL;
	}

	if (0 == pagesize)
	{
		if (KERN_SUCCESS != host_page_size(mach_host_self(), &pagesize))
		{
			SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot obtain host page size."));
			return SYSINFO_RET_FAIL;
		}
	}

	mode = get_rparam(request, 0);

	if (NULL == mode || '\0' == *mode || 0 == strcmp(mode, "total"))
		ret = VM_MEMORY_TOTAL(result);
	else if (0 == strcmp(mode, "active"))
		ret = VM_MEMORY_ACTIVE(result);
	else if (0 == strcmp(mode, "inactive"))
		ret = VM_MEMORY_INACTIVE(result);
	else if (0 == strcmp(mode, "wired"))
		ret = VM_MEMORY_WIRED(result);
	else if (0 == strcmp(mode, "free"))
		ret = VM_MEMORY_FREE(result);
	else if (0 == strcmp(mode, "used"))
		ret = VM_MEMORY_USED(result);
	else if (0 == strcmp(mode, "pused"))
		ret = VM_MEMORY_PUSED(result);
	else if (0 == strcmp(mode, "available"))
		ret = VM_MEMORY_AVAILABLE(result);
	else if (0 == strcmp(mode, "pavailable"))
		ret = VM_MEMORY_PAVAILABLE(result);
	else
	{
		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid first parameter."));
		return SYSINFO_RET_FAIL;
	}

	return ret;
}
Example #7
0
int	VFS_FS_DISCOVERY(AGENT_REQUEST *request, AGENT_RESULT *result)
{
	int		i, rc;
	struct statvfs	*mntbuf;
	struct zbx_json	j;

	if (0 == (rc = getmntinfo(&mntbuf, MNT_WAIT)))
	{
		SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain system information: %s", zbx_strerror(errno)));
		return SYSINFO_RET_FAIL;
	}

	zbx_json_init(&j, ZBX_JSON_STAT_BUF_LEN);

	zbx_json_addarray(&j, ZBX_PROTO_TAG_DATA);

	for (i = 0; i < rc; i++)
	{
		zbx_json_addobject(&j, NULL);
		zbx_json_addstring(&j, "{#FSNAME}", mntbuf[i].f_mntonname, ZBX_JSON_TYPE_STRING);
		zbx_json_addstring(&j, "{#FSTYPE}", mntbuf[i].f_fstypename, ZBX_JSON_TYPE_STRING);
		zbx_json_close(&j);
	}

	zbx_json_close(&j);

	SET_STR_RESULT(result, zbx_strdup(NULL, j.buffer));

	zbx_json_free(&j);

	return SYSINFO_RET_OK;
}
Example #8
0
static int web_set_login(AGENT_RESULT *result, struct web *opt, const char *params, int param_id)
{
	char login_tmp[MAX_STRING_LEN] = {0};
	char *passwd_ptr = NULL;
	size_t user_lenght;

	if (get_param(params, param_id, login_tmp, MAX_STRING_LEN))
		goto failed;

	zbx_remove_whitespace(login_tmp);

	if (!(passwd_ptr = strchr(login_tmp, ':')))
		goto failed;

	if (!(user_lenght = strlen(login_tmp) - strlen(passwd_ptr)))
		goto failed;

	opt->username = strndup(login_tmp, user_lenght);
	opt->passwd = zbx_strdup(opt->passwd, ++passwd_ptr);

	return SUCCEED;
failed:
	SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Invalid LOGIN parameter", NULL));
	return FAIL;
}
int	VFS_FS_DISCOVERY(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result)
{
	int		i, rc, ret = SYSINFO_RET_FAIL;
	struct statfs	*mntbuf;
	struct zbx_json	j;

	zbx_json_init(&j, ZBX_JSON_STAT_BUF_LEN);

	zbx_json_addarray(&j, ZBX_PROTO_TAG_DATA);

	if (0 != (rc = getmntinfo(&mntbuf, MNT_WAIT)))
	{
		for (i = 0; i < rc; i++)
		{
			zbx_json_addobject(&j, NULL);
			zbx_json_addstring(&j, "{#FSNAME}", mntbuf[i].f_mntonname, ZBX_JSON_TYPE_STRING);
			zbx_json_addstring(&j, "{#FSTYPE}", mntbuf[i].f_fstypename, ZBX_JSON_TYPE_STRING);
			zbx_json_close(&j);
		}

		ret = SYSINFO_RET_OK;
	}

	zbx_json_close(&j);

	SET_STR_RESULT(result, zbx_strdup(NULL, j.buffer));

	zbx_json_free(&j);

	return ret;
}
Example #10
0
int	SYSTEM_HW_CHASSIS(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result)
{
	char	tmp[8], buf[MAX_STRING_LEN];
	int	ret = SYSINFO_RET_FAIL;

	if (1 < num_param(param))
		return ret;

	if (0 != get_param(param, 1, tmp, sizeof(tmp)))
		*tmp = '\0';

	if ('\0' == *tmp || 0 == strcmp(tmp, "full"))	/* show full info by default */
		ret = get_dmi_info(buf, sizeof(buf), DMI_GET_TYPE | DMI_GET_VENDOR | DMI_GET_MODEL | DMI_GET_SERIAL);
	else if (0 == strcmp(tmp, "type"))
		ret = get_dmi_info(buf, sizeof(buf), DMI_GET_TYPE);
	else if (0 == strcmp(tmp, "vendor"))
		ret = get_dmi_info(buf, sizeof(buf), DMI_GET_VENDOR);
	else if (0 == strcmp(tmp, "model"))
		ret = get_dmi_info(buf, sizeof(buf), DMI_GET_MODEL);
	else if (0 == strcmp(tmp, "serial"))
		ret = get_dmi_info(buf, sizeof(buf), DMI_GET_SERIAL);

	if (SYSINFO_RET_OK == ret)
		SET_STR_RESULT(result, zbx_strdup(NULL, buf + 1));	/* buf has a leading space */

	return ret;
}
Example #11
0
/******************************************************************************
 *                                                                            *
 * Function: DCadd_nextcheck                                                  *
 *                                                                            *
 * Purpose: add item nextcheck to the array                                   *
 *                                                                            *
 ******************************************************************************/
void	DCadd_nextcheck(zbx_uint64_t itemid, const zbx_timespec_t *ts, const char *error_msg)
{
	const char	*__function_name = "DCadd_nextcheck";

	int		i;

	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);

	if (NULL == error_msg)
		return;

	i = get_nearestindex(nextchecks, sizeof(ZBX_DC_NEXTCHECK), nextcheck_num, itemid);

	if (i < nextcheck_num && nextchecks[i].itemid == itemid)	/* an item found */
		return;

	if (nextcheck_alloc == nextcheck_num)
	{
		nextcheck_alloc += 64;
		nextchecks = zbx_realloc(nextchecks, sizeof(ZBX_DC_NEXTCHECK) * nextcheck_alloc);
	}

	/* insert a new item */
	memmove(&nextchecks[i + 1], &nextchecks[i], sizeof(ZBX_DC_NEXTCHECK) * (nextcheck_num - i));

	nextchecks[i].itemid = itemid;
	nextchecks[i].ts = *ts;
	nextchecks[i].error_msg = zbx_strdup(NULL, error_msg);

	nextcheck_num++;

	zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name);
}
Example #12
0
int	SYSTEM_HW_CHASSIS(AGENT_REQUEST *request, AGENT_RESULT *result)
{
	char	*mode, buf[MAX_STRING_LEN];
	int	ret = SYSINFO_RET_FAIL;

	if (1 < request->nparam)
		return ret;

	mode = get_rparam(request, 0);

	if (NULL == mode || '\0' == *mode || 0 == strcmp(mode, "full"))	/* show full info by default */
		ret = get_dmi_info(buf, sizeof(buf), DMI_GET_TYPE | DMI_GET_VENDOR | DMI_GET_MODEL | DMI_GET_SERIAL);
	else if (0 == strcmp(mode, "type"))
		ret = get_dmi_info(buf, sizeof(buf), DMI_GET_TYPE);
	else if (0 == strcmp(mode, "vendor"))
		ret = get_dmi_info(buf, sizeof(buf), DMI_GET_VENDOR);
	else if (0 == strcmp(mode, "model"))
		ret = get_dmi_info(buf, sizeof(buf), DMI_GET_MODEL);
	else if (0 == strcmp(mode, "serial"))
		ret = get_dmi_info(buf, sizeof(buf), DMI_GET_SERIAL);

	if (SYSINFO_RET_OK == ret)
		SET_STR_RESULT(result, zbx_strdup(NULL, buf + 1));	/* buf has a leading space */

	return ret;
}
Example #13
0
static void	read_ipmi_control(zbx_ipmi_host_t *h, zbx_ipmi_control_t *c)
{
	const char		*__function_name = "read_ipmi_control";
	int			ret;
	struct timeval		tv;

	zabbix_log(LOG_LEVEL_DEBUG, "In %s() control:'%s@[%s]:%d'", __function_name, c->c_name, h->ip, h->port);

	if (0 == ipmi_control_is_readable(c->control))
	{
		h->err = zbx_strdup(h->err, "control is not readable");
		h->ret = NOTSUPPORTED;
		goto out;
	}

	h->ret = SUCCEED;
	h->done = 0;

	if (0 != (ret = ipmi_control_get_val(c->control, got_control_reading, h)))
	{
		h->err = zbx_dsprintf(h->err, "Cannot read control %s. ipmi_control_get_val() return error: 0x%x",
				c->c_name, ret);
		h->ret = NOTSUPPORTED;
		goto out;
	}

	tv.tv_sec = 10;
	tv.tv_usec = 0;

	while (0 == h->done)
		os_hnd->perform_one_op(os_hnd, &tv);
out:
	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(h->ret));
}
/******************************************************************************
 *                                                                            *
 * Function:                                                                  *
 *                                                                            *
 * Purpose:                                                                   *
 *                                                                            *
 * Parameters:                                                                *
 *                                                                            *
 * Return value:                                                              *
 *                                                                            *
 * Comments:                                                                  *
 *                                                                            *
 ******************************************************************************/
static void parse_commandline(int argc, char **argv)
{
    char ch;

    /* parse the command-line */
    CONFIG_FILE = NULL;
    while ((char) EOF != (ch = (char) zbx_getopt_long(argc, argv, shortopts, longopts, NULL))) {
        switch (ch) {
        case 'c':
            CONFIG_FILE = zbx_strdup(CONFIG_FILE, zbx_optarg);
            break;
        case 'h':
            help_jobarg();
            exit(FAIL);
            break;
        case 'V':
            version_jobarg();
            exit(FAIL);
            break;
        default:
            usage();
            exit(FAIL);
            break;
        }
    }

    if (NULL == CONFIG_FILE) {
        CONFIG_FILE = DEFAULT_CONFIG_FILE;
    }
}
Example #15
0
int	SYSTEM_BOOTTIME(AGENT_REQUEST *request, AGENT_RESULT *result)
{
#ifdef HAVE_FUNCTION_SYSCTL_KERN_BOOTTIME
	size_t		len;
	int		mib[2];
	struct timeval	boottime;

	mib[0] = CTL_KERN;
	mib[1] = KERN_BOOTTIME;

	len = sizeof(struct timeval);

	if (-1 == sysctl(mib, 2, &boottime, &len, NULL, 0))
	{
		SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain system information: %s", zbx_strerror(errno)));
		return SYSINFO_RET_FAIL;
	}

	SET_UI64_RESULT(result, boottime.tv_sec);

	return SYSINFO_RET_OK;
#else
	SET_MSG_RESULT(result, zbx_strdup(NULL, "Agent was compiled without support for \"kern.boottime\" system"
			" parameter."));
	return SYSINFO_RET_FAIL;
#endif
}
Example #16
0
static int web_curl_set_common(struct web *opt)
{
	const char *__function_name = "web_curl_set_common";

	char *curl_err_str = NULL;
	int curl_err;
	int curl_opt;

	if (!(opt->curl->handler = curl_easy_init())) {
		zabbix_log(LOG_LEVEL_ERR,"%s(): Could not init cURL for key %s", __function_name, opt->item->key);
		goto failed;
	}

	if ((curl_err = curl_easy_setopt(opt->curl->handler, curl_opt = CURLOPT_COOKIEFILE, "")) ||
	    (curl_err = curl_easy_setopt(opt->curl->handler, curl_opt = CURLOPT_SSL_VERIFYPEER, 0L)) ||
	    (curl_err = curl_easy_setopt(opt->curl->handler, curl_opt = CURLOPT_SSL_VERIFYHOST, 0L)) ||
	    (curl_err = curl_easy_setopt(opt->curl->handler, curl_opt = CURLOPT_USERAGENT,WEB_CURL_USERAGENT)) ||
	    (curl_err = curl_easy_setopt(opt->curl->handler, curl_opt = CURLOPT_TIMEOUT, (long) opt->timeout))) {
                curl_err_str = zbx_strdup(curl_err_str,curl_easy_strerror(curl_err));
                zabbix_log(LOG_LEVEL_ERR, "%s(): Could not set cURL option [%d]: %s for key %s",
					  __function_name, curl_opt, curl_err_str, opt->item->key);
                goto failed;
	}

	return SUCCEED;
failed:
	zbx_free(curl_err_str);
	return FAIL;
}
Example #17
0
void	add_regexp_ex(zbx_vector_ptr_t *regexps, const char *name, const char *expression, int expression_type,
		char exp_delimiter, int case_sensitive)
{
	zbx_expression_t	*regexp;

	regexp = zbx_malloc(NULL, sizeof(zbx_expression_t));

	regexp->name = zbx_strdup(NULL, name);
	regexp->expression = zbx_strdup(NULL, expression);

	regexp->expression_type = expression_type;
	regexp->exp_delimiter = exp_delimiter;
	regexp->case_sensitive = case_sensitive;

	zbx_vector_ptr_append(regexps, regexp);
}
Example #18
0
int	KERNEL_MAXPROC(AGENT_REQUEST *request, AGENT_RESULT *result)
{
#ifdef HAVE_FUNCTION_SYSCTL_KERN_MAXPROC
	int	mib[2];
	size_t	len;
	int	maxproc;

	mib[0] = CTL_KERN;
	mib[1] = KERN_MAXPROC;

	len = sizeof(maxproc);

	if (0 != sysctl(mib, 2, &maxproc, &len, NULL, 0))
	{
		SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain system information: %s", zbx_strerror(errno)));
		return SYSINFO_RET_FAIL;
	}

	SET_UI64_RESULT(result, maxproc);

	return SYSINFO_RET_OK;
#else
	SET_MSG_RESULT(result, zbx_strdup(NULL, "Agent was compiled without support for \"kern.maxproc\" system"
			" parameter."));
#endif
}
Example #19
0
/******************************************************************************
 *                                                                            *
 * Function: proc_get_process_name                                            *
 *                                                                            *
 * Purpose: returns process name                                              *
 *                                                                            *
 * Parameters: pid -      [IN] the process identifier                         *
 *             procname - [OUT] the process name                              *
 *                                                                            *
 * Return value: SUCCEED                                                      *
 *               FAIL                                                         *
 *                                                                            *
 * Comments: The process name is allocated by this function and must be freed *
 *           by the caller.                                                   *
 *                                                                            *
 ******************************************************************************/
static int	proc_get_process_name(pid_t pid, char **procname)
{
	int	n, fd;
	char	tmp[MAX_STRING_LEN], *pend, *pstart;

	zbx_snprintf(tmp, sizeof(tmp), "/proc/%d/stat", (int)pid);

	if (-1 == (fd = open(tmp, O_RDONLY)))
		return FAIL;

	n = read(fd, tmp, sizeof(tmp));
	close(fd);

	if (-1 == n)
		return FAIL;

	for (pend = tmp + n - 1; ')' != *pend && pend > tmp; pend--)
		;

	*pend = '\0';

	if (NULL == (pstart = strchr(tmp, '(')))
		return FAIL;

	*procname = zbx_strdup(NULL, pstart + 1);

	return SUCCEED;
}
Example #20
0
/******************************************************************************
 *                                                                            *
 * Function: replace_key_param                                                *
 *                                                                            *
 * Comments: auxiliary function for DBpatch_2010195()                         *
 *                                                                            *
 ******************************************************************************/
static char	*replace_key_param(const char *data, int key_type, int level, int num, int quoted, void *cb_data)
{
	char	*param, *new_param;

	if (1 != level || 4 != num)	/* the fourth parameter on first level should be updated */
		return NULL;

	param = zbx_strdup(NULL, data);

	unquote_key_param(param);

	if ('\0' == *param)
	{
		zbx_free(param);
		return NULL;
	}

	new_param = zbx_dsprintf(NULL, "^%s$", param);

	zbx_free(param);

	quote_key_param(&new_param, quoted);

	return new_param;
}
Example #21
0
static int	VM_MEMORY_CACHED(AGENT_RESULT *result)
{
	FILE		*f;
	zbx_uint64_t	value;
	int		res;

	if (NULL == (f = fopen("/proc/meminfo", "r")))
	{
		SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot open /proc/meminfo: %s", zbx_strerror(errno)));
		return SYSINFO_RET_FAIL;
	}

	if (FAIL == (res = byte_value_from_proc_file(f, "Cached:", NULL, &value)))
	{
		SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot obtain the value of Cached from /proc/meminfo."));
		goto close;
	}

	if (NOTSUPPORTED == res)
		value = 0;
close:
	zbx_fclose(f);

	SET_UI64_RESULT(result, value);

	return SYSINFO_RET_OK;
}
Example #22
0
static int web_set_host(AGENT_RESULT *result, struct web *opt, const char *params, int param_id)
{
	char host_tmp[WEB_MAX_DNS_STRLEN] = {0};

	if (get_param(params, param_id, host_tmp, WEB_MAX_DNS_STRLEN))
		goto failed;

	zbx_remove_whitespace(host_tmp);

	if (strlen(host_tmp)) {
		if (!strncmp(host_tmp, "none", strlen(host_tmp))) {
			if (opt->host)
                                zbx_free(opt->host);
		} else {
			/* RFC2616 14.23 non compliant. We dont accept port number in Host field */
			if (zbx_check_hostname(host_tmp))
				goto failed;
			/* if hostname is previously set in IP / DNS then replace it with user param */
			if (opt->host)
				zbx_free(opt->host);

			opt->host = zbx_strdup(NULL, host_tmp);
		}
	}

	return SUCCEED;
failed:
	SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Invalid HOST parameter", NULL));
	return FAIL;
}
Example #23
0
static int	DBget_script_by_scriptid(zbx_uint64_t scriptid, zbx_script_t *script, zbx_uint64_t *groupid)
{
	const char	*__function_name = "DBget_script_by_scriptid";
	DB_RESULT	result;
	DB_ROW		row;
	int		ret = FAIL;

	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);

	result = DBselect(
			"select type,execute_on,command,groupid"
			" from scripts"
			" where scriptid=" ZBX_FS_UI64,
			scriptid);

	if (NULL != (row = DBfetch(result)))
	{
		script->type = (unsigned char)atoi(row[0]);
		script->execute_on = (unsigned char)atoi(row[1]);
		script->command = zbx_strdup(script->command, row[2]);
		ZBX_DBROW2UINT64(*groupid, row[3]);
		ret = SUCCEED;
	}
	DBfree_result(result);

	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret));

	return ret;
}
Example #24
0
int	SYSTEM_UPTIME(AGENT_REQUEST *request, AGENT_RESULT *result)
{
#ifdef HAVE_FUNCTION_SYSCTL_KERN_BOOTTIME
	int		mib[2], now;
	size_t		len;
	struct timeval	uptime;

	mib[0] = CTL_KERN;
	mib[1] = KERN_BOOTTIME;

	len = sizeof(struct timeval);

	if (0 != sysctl(mib, 2, &uptime, &len, NULL, 0))
	{
		SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain system information: %s", zbx_strerror(errno)));
		return SYSINFO_RET_FAIL;
	}

	now = time(NULL);

	SET_UI64_RESULT(result, now - uptime.tv_sec);

	return SYSINFO_RET_OK;
#else
	SET_MSG_RESULT(result, zbx_strdup(NULL, "Agent was compiled without support for uptime information."));
	return SYSINFO_RET_FAIL;
#endif
}
Example #25
0
/* callback function invoked from OpenIPMI */
static void	got_control_setting(ipmi_control_t *control, int err, void *cb_data)
{
	const char		*__function_name = "got_control_setting";
	zbx_ipmi_host_t		*h = cb_data;
	zbx_ipmi_control_t	*c;

	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);

	if (0 != err)
	{
		zabbix_log(LOG_LEVEL_DEBUG, "%s() fail: %s", __function_name, zbx_strerror(err));

		h->err = zbx_dsprintf(h->err, "error 0x%x while set control", err);
		h->ret = NETWORK_ERROR;
		h->done = 1;
		return;
	}

	c = get_ipmi_control(h, control);

	if (NULL == c)
	{
		THIS_SHOULD_NEVER_HAPPEN;
		h->err = zbx_strdup(h->err, "fatal error");
		h->ret = NOTSUPPORTED;
		h->done = 1;
		return;
	}

	zabbix_log(LOG_LEVEL_DEBUG, "set value completed for control %s@[%s]:%d", c->c_name, h->ip, h->port);

	h->done = 1;

	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(h->ret));
}
Example #26
0
int     SYSTEM_SW_OS(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result)
{
	char	type[8], line[MAX_STRING_LEN];
	int	ret = SYSINFO_RET_FAIL;
	FILE	*f = NULL;

	if (1 < num_param(param))
		return ret;

	if (0 != get_param(param, 1, type, sizeof(type)))
		*type = '\0';

	if ('\0' == *type || 0 == strcmp(type, "full"))
		f = fopen(SW_OS_FULL, "r");
	else if (0 == strcmp(type, "short"))
		f = fopen(SW_OS_SHORT, "r");
	else if (0 == strcmp(type, "name"))
		f = fopen(SW_OS_NAME, "r");

	if (NULL == f)
		return ret;

	if (NULL != fgets(line, sizeof(line), f))
	{
		ret = SYSINFO_RET_OK;
		zbx_rtrim(line, ZBX_WHITESPACE);
		SET_STR_RESULT(result, zbx_strdup(NULL, line));
	}
	zbx_fclose(f);

	return ret;
}
Example #27
0
int     SYSTEM_CPU_INTR(AGENT_REQUEST *request, AGENT_RESULT *result)
{
	int		ret = SYSINFO_RET_FAIL;
	char		line[MAX_STRING_LEN];
	zbx_uint64_t	value = 0;
	FILE		*f;

	if (NULL == (f = fopen("/proc/stat", "r")))
	{
		SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot open /proc/stat: %s", zbx_strerror(errno)));
		return SYSINFO_RET_FAIL;
	}

	while (NULL != fgets(line, sizeof(line), f))
	{
		if (0 != strncmp(line, "intr", 4))
			continue;

		if (1 != sscanf(line, "%*s " ZBX_FS_UI64, &value))
			continue;

		SET_UI64_RESULT(result, value);
		ret = SYSINFO_RET_OK;
		break;
	}
	zbx_fclose(f);

	if (SYSINFO_RET_FAIL == ret)
		SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot find a line with \"intr\" in /proc/stat."));

	return ret;
}
Example #28
0
/******************************************************************************
 *                                                                            *
 * Function: parse_item_key                                                   *
 *                                                                            *
 * Purpose: parse item command (key) and fill AGENT_REQUEST structure         *
 *                                                                            *
 * Parameters: itemkey - complete item key                                    *
 *                                                                            *
 * Return value: request - structure filled with data from item key           *
 *                                                                            *
 ******************************************************************************/
int	parse_item_key(const char *itemkey, AGENT_REQUEST *request)
{
	int	i, ret = FAIL;
	char	*key = NULL, *params = NULL;

	switch (parse_command_dyn(itemkey, &key, &params))
	{
		case ZBX_COMMAND_WITH_PARAMS:
			if (0 == (request->nparam = num_param(params)))
				goto out;	/* key is badly formatted */
			request->params = zbx_malloc(request->params, request->nparam * sizeof(char *));
			for (i = 0; i < request->nparam; i++)
				request->params[i] = get_param_dyn(params, i + 1);
			break;
		case ZBX_COMMAND_ERROR:
			goto out;	/* key is badly formatted */
	}

	request->key = zbx_strdup(NULL, key);

	ret = SUCCEED;
out:
	zbx_free(params);
	zbx_free(key);

	return ret;
}
Example #29
0
static int	get_result_columns(ZBX_ODBC_DBH *dbh, char **buffer)
{
	int		ret = SUCCEED, i, j;
	char		str[MAX_STRING_LEN];
	SQLRETURN	rc;
	SQLSMALLINT	len;

	for (i = 0; i < dbh->col_num; i++)
	{
		rc = SQLColAttribute(dbh->hstmt, i + 1, SQL_DESC_LABEL, str, sizeof(str), &len, NULL);

		if (SQL_SUCCESS != rc || sizeof(str) <= len || '\0' == *str)
		{
			for (j = 0; j < i; j++)
				zbx_free(buffer[j]);

			ret = FAIL;
			break;
		}

		buffer[i] = zbx_strdup(NULL, str);
	}

	return ret;
}
Example #30
0
static int web_key_check_img(struct web *opt)
{
	const char *__function_name = "web_key_check_img";

	char *content_type = NULL;
	char *curl_err_str = NULL;
	int curl_err;

	if (opt->required_response == WEB_CHECK_IMG_DEF_RESP) {

		if ((curl_err = curl_easy_getinfo(opt->curl->handler, CURLINFO_CONTENT_TYPE, &content_type))) {
			curl_err_str = zbx_strdup(curl_err_str, curl_easy_strerror(curl_err));
			zabbix_log(LOG_LEVEL_ERR, "%s(): Error during curl_easy_getinfo: %s for key %s",
						  __function_name, curl_err_str, opt->item->key);
			opt->result = (zbx_uint64_t) WEB_ERR_CURL;
			goto failed;
		}

		if (!zbx_strcasestr(content_type, WEB_CHECK_IMG_MATCH_CTYPE)) {
			zabbix_log(LOG_LEVEL_WARNING, "%s(): Required content type %s not found for key %s",
						      __function_name, WEB_CHECK_IMG_MATCH_CTYPE, opt->item->key);
			opt->result = (zbx_uint64_t) WEB_ERR_CONTENT_TYPE;
			goto failed;
		}
	}

	return SUCCEED;
failed:
	zbx_free(curl_err_str);
	return FAIL;
}