diff --git a/src/libs/zbxsysinfo/solaris/memory.c b/src/libs/zbxsysinfo/solaris/memory.c index 5989c6fdae5..aec4f662cd3 100644 --- a/src/libs/zbxsysinfo/solaris/memory.c +++ b/src/libs/zbxsysinfo/solaris/memory.c @@ -153,19 +153,14 @@ out: static int VM_MEMORY_USED(AGENT_RESULT *result) { int ret; - zbx_uint64_t freemem; + zbx_uint64_t freemem, totalmem; char *error = NULL; zabbix_log(LOG_LEVEL_DEBUG, "In %s (with HAVE_VMINFO_T_UPDATES)", __func__); - if (SUCCEED == zbx_kstat_get_freemem(&freemem, &error)) + if (SUCCEED == zbx_kstat_get_mem(&freemem, &totalmem, &error)) { - long res_SC_PHYS_PAGES, res_SC_PAGESIZE; - - CHECKED_SYSCONF_SYSCALL(_SC_PHYS_PAGES); - CHECKED_SYSCONF_SYSCALL(_SC_PAGESIZE); - - SET_UI64_RESULT(result, res_SC_PHYS_PAGES * res_SC_PAGESIZE - freemem); + SET_UI64_RESULT(result, totalmem - freemem); } else if (NULL != error) { @@ -174,7 +169,7 @@ static int VM_MEMORY_USED(AGENT_RESULT *result) goto out; } else - zabbix_log(LOG_LEVEL_DEBUG, "zbx_kstat_get_freemem() failed, but error is NULL"); + zabbix_log(LOG_LEVEL_DEBUG, "zbx_kstat_get_mem() failed, but error is NULL"); ret = SYSINFO_RET_OK; out: @@ -186,29 +181,14 @@ out: static int VM_MEMORY_PUSED(AGENT_RESULT *result) { int ret; - zbx_uint64_t freemem, total; + zbx_uint64_t freemem, totalmem; char *error = NULL; - long res_SC_PHYS_PAGES; zabbix_log(LOG_LEVEL_DEBUG, "In %s (with HAVE_VMINFO_T_UPDATES)", __func__); - CHECKED_SYSCONF_SYSCALL(_SC_PHYS_PAGES); - - if (0 == (total = res_SC_PHYS_PAGES)) - { - SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot calculate percentage because total is zero.")); - ret = SYSINFO_RET_FAIL; - goto out; - } - - if (SUCCEED == zbx_kstat_get_freemem(&freemem, &error)) + if (SUCCEED == zbx_kstat_get_mem(&freemem, &totalmem, &error)) { - long res_SC_PAGESIZE; - - CHECKED_SYSCONF_SYSCALL(_SC_PAGESIZE); - - total *= res_SC_PAGESIZE; - SET_DBL_RESULT(result, (total - freemem) / (double)total * 100); + SET_DBL_RESULT(result, (totalmem - freemem) / (double)totalmem * 100); } else if (NULL != error) { @@ -216,8 +196,14 @@ static int VM_MEMORY_PUSED(AGENT_RESULT *result) ret = SYSINFO_RET_FAIL; goto out; } + else if (0 == totalmem) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot calculate percentage because total is zero.")); + ret = SYSINFO_RET_FAIL; + goto out; + } else - zabbix_log(LOG_LEVEL_DEBUG, "zbx_kstat_get_freemem() failed, but error is NULL"); + zabbix_log(LOG_LEVEL_DEBUG, "zbx_kstat_get_mem() failed, but error is NULL"); ret = SYSINFO_RET_OK; out: @@ -234,7 +220,7 @@ static int VM_MEMORY_AVAILABLE(AGENT_RESULT *result) zabbix_log(LOG_LEVEL_DEBUG, "In %s, (with HAVE_VMINFO_T_UPDATES)", __func__); - if (SUCCEED == zbx_kstat_get_freemem(&freemem, &error)) + if (SUCCEED == zbx_kstat_get_mem(&freemem, NULL, &error)) { SET_UI64_RESULT(result, freemem); } @@ -245,7 +231,7 @@ static int VM_MEMORY_AVAILABLE(AGENT_RESULT *result) goto out; } else - zabbix_log(LOG_LEVEL_DEBUG, "zbx_kstat_get_freemem() failed, but error is NULL"); + zabbix_log(LOG_LEVEL_DEBUG, "zbx_kstat_get_mem() failed, but error is NULL"); ret = SYSINFO_RET_OK; out: @@ -257,29 +243,14 @@ out: static int VM_MEMORY_PAVAILABLE(AGENT_RESULT *result) { int ret; - zbx_uint64_t total, freemem; + zbx_uint64_t freemem, totalmem; char *error = NULL; - long res_SC_PHYS_PAGES; zabbix_log(LOG_LEVEL_DEBUG, "In %s, (with HAVE_VMINFO_T_UPDATES)", __func__); - CHECKED_SYSCONF_SYSCALL(_SC_PHYS_PAGES); - - if (0 == (total = res_SC_PHYS_PAGES)) + if (SUCCEED == zbx_kstat_get_mem(&freemem, &totalmem, &error)) { - SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot calculate percentage because total is zero.")); - ret = SYSINFO_RET_FAIL; - goto out; - } - - if (SUCCEED == zbx_kstat_get_freemem(&freemem, &error)) - { - long res_SC_PAGESIZE; - - CHECKED_SYSCONF_SYSCALL(_SC_PAGESIZE); - - total *= res_SC_PAGESIZE; - SET_DBL_RESULT(result, freemem / (double)total * 100); + SET_DBL_RESULT(result, freemem / (double)totalmem * 100); } else if (NULL != error) { @@ -287,8 +258,14 @@ static int VM_MEMORY_PAVAILABLE(AGENT_RESULT *result) ret = SYSINFO_RET_FAIL; goto out; } + else if (0 == totalmem) + { + SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot calculate percentage because total is zero.")); + ret = SYSINFO_RET_FAIL; + goto out; + } else - zabbix_log(LOG_LEVEL_DEBUG, "zbx_kstate_get_freemem() failed, but error is NULL"); + zabbix_log(LOG_LEVEL_DEBUG, "zbx_kstat_get_mem() failed, but error is NULL"); ret = SYSINFO_RET_OK; out: diff --git a/src/zabbix_agent/zbxkstat.c b/src/zabbix_agent/zbxkstat.c index 08bfce34053..cc458b414f4 100644 --- a/src/zabbix_agent/zbxkstat.c +++ b/src/zabbix_agent/zbxkstat.c @@ -66,6 +66,7 @@ static int zbx_kstat_refresh(char **error) ret = FAIL; goto out; } + ret = SUCCEED; out: zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __func__); @@ -168,11 +169,18 @@ void zbx_kstat_collect(zbx_kstat_t *kstat) } } + zabbix_log(LOG_LEVEL_DEBUG, "vminfo -> freemem: " ZBX_FS_UI64 ", swap_resv: " ZBX_FS_UI64 + ", swap_alloc: " ZBX_FS_UI64 ", swap_avail: " ZBX_FS_UI64 + ", swap_free: " ZBX_FS_UI64 ", updates: " ZBX_FS_UI64, + vminfo.freemem, vminfo.swap_resv, + vminfo.swap_alloc, vminfo.swap_avail, + vminfo.swap_free, vminfo.updates); + zbx_mutex_lock(kstat_lock); kstat->vminfo_index ^= 1; kstat->vminfo[kstat->vminfo_index].freemem = vminfo.freemem; - kstat->vminfo[kstat->vminfo_index].updates = time(NULL); + kstat->vminfo[kstat->vminfo_index].updates = vminfo.updates; zbx_mutex_unlock(kstat_lock); @@ -185,20 +193,22 @@ out: /****************************************************************************** * * - * Purpose: get free memory size * + * Purpose: gets free and total memory sizes * * * - * Parameters: value - [OUT] the free memory size in bytes * - * error - [OUT] the error message * + * Parameters: freemem - [OUT] free memory size in bytes * + * totalmem - [OUT] * + * error - [OUT] error message * * * - * Return value: SUCCEED - the free memory size was stored in value * - * FAIL - either an error occurred (error parameter is set) or * - * data was not collected yet (error parameter is left * - * unchanged) * + * Return value: SUCCEED - * + * FAIL - * * * ******************************************************************************/ -int zbx_kstat_get_freemem(zbx_uint64_t *value, char **error) +int zbx_kstat_get_mem(zbx_uint64_t *freemem, zbx_uint64_t *totalmem, char **error) { - int sysconf_pagesize, last, prev, ret = FAIL; + static int last_pagesize = FAIL; + int last, prev, ret = FAIL; + long sysconf_phys_pages, sysconf_avphys_pages, sysconf_pagesize; + zbx_uint64_t totaltemp; zbx_kstat_vminfo_t *vminfo; zabbix_log(LOG_LEVEL_DEBUG, "In %s(): collector:%p", __func__, collector); @@ -206,7 +216,11 @@ int zbx_kstat_get_freemem(zbx_uint64_t *value, char **error) zbx_mutex_lock(kstat_lock); if (NULL == collector) + { + *error = zbx_strdup(*error, "Collector is not started."); + zabbix_log(LOG_LEVEL_DEBUG, "Collector is not started"); goto out; + } last = collector->kstat.vminfo_index; prev = last ^ 1; @@ -216,12 +230,64 @@ int zbx_kstat_get_freemem(zbx_uint64_t *value, char **error) { if (-1 == (sysconf_pagesize = sysconf(_SC_PAGESIZE))) { - zabbix_log(LOG_LEVEL_DEBUG, "sysconf(_SC_PAGESIZE) failed, errno is: %s", zbx_strerror(errno)); + zabbix_log(LOG_LEVEL_DEBUG, "sysconf(_SC_PAGESIZE) failed, errno: %s", zbx_strerror(errno)); + goto out; + } + + if (sysconf_pagesize != last_pagesize) + { + if (FAIL != last_pagesize) + { + zabbix_log(LOG_LEVEL_WARNING, + "sysconf(_SC_PAGESIZE) value has changed from [%d] to [%d]", + last_pagesize, sysconf_pagesize); + } + + last_pagesize = sysconf_pagesize; + } + + if (-1 == (sysconf_phys_pages = sysconf(_SC_PHYS_PAGES))) + { + zabbix_log(LOG_LEVEL_DEBUG, "sysconf(_SC_PHYS_PAGES) failed, errno: %s", zbx_strerror(errno)); + goto out; + } + + if (0 == sysconf_phys_pages) + { + if (NULL != totalmem) + *totalmem = 0; goto out; } - *value = (vminfo[last].freemem - vminfo[prev].freemem) / + *freemem = (vminfo[last].freemem - vminfo[prev].freemem) / (vminfo[last].updates - vminfo[prev].updates) * sysconf_pagesize; + + totaltemp = sysconf_phys_pages * sysconf_pagesize; + + if (totaltemp < *freemem) + { + if (-1 == (sysconf_avphys_pages = sysconf(_SC_AVPHYS_PAGES))) + { + zabbix_log(LOG_LEVEL_DEBUG, "sysconf(_SC_PHYS_PAGES) failed, errno: %s", + zbx_strerror(errno)); + goto out; + } + + zabbix_log(LOG_LEVEL_WARNING, "%s() total < freemem, where total:" ZBX_FS_UI64 + ", freemem: " ZBX_FS_UI64 ", pagesize:" ZBX_FS_UI64 ", delta:" ZBX_FS_I64 + ", sysconf_pagesize: %ld", + __func__, totaltemp, *freemem, sysconf_pagesize, totaltemp - *freemem, + sysconf_avphys_pages); + + *freemem = sysconf_avphys_pages * sysconf_pagesize; + + if (totaltemp < *freemem) + goto out; + } + + if (NULL != totalmem) + *totalmem = totaltemp; + ret = SUCCEED; } else @@ -229,12 +295,7 @@ int zbx_kstat_get_freemem(zbx_uint64_t *value, char **error) out: zbx_mutex_unlock(kstat_lock); - if (NULL == collector) - { - *error = zbx_strdup(*error, "Collector is not started."); - zabbix_log(LOG_LEVEL_DEBUG, "Collector is not started"); - } - else + if (NULL == error) { zabbix_log(LOG_LEVEL_DEBUG, "last: %d, prev: %d, vminfo[prev].updates: " ZBX_FS_UI64 ", vminfo[last].updates: " ZBX_FS_UI64, last, prev, vminfo[prev].updates, diff --git a/src/zabbix_agent/zbxkstat.h b/src/zabbix_agent/zbxkstat.h index 4f8f90275da..e763ae56a54 100644 --- a/src/zabbix_agent/zbxkstat.h +++ b/src/zabbix_agent/zbxkstat.h @@ -41,7 +41,7 @@ zbx_kstat_t; int zbx_kstat_init(zbx_kstat_t *kstat, char **error); void zbx_kstat_destroy(void); void zbx_kstat_collect(zbx_kstat_t *kstat); -int zbx_kstat_get_freemem(zbx_uint64_t *value, char **error); +int zbx_kstat_get_mem(zbx_uint64_t *freemem, zbx_uint64_t *totalmem, char **error); #endif