From 88200af1f657d31aa8ac69e35a12ff5a1bae6e70 Mon Sep 17 00:00:00 2001 From: sasha Date: Thu, 8 May 2008 10:07:11 +0000 Subject: - [DEV-168] added more supported items in FreeBSD, NetBSD and OpenBSD git-svn-id: svn://svn.zabbix.com/trunk@5685 97f52cf1-0a1b-0410-bd0e-c28be96e8082 --- ChangeLog | 1 + configure.in | 77 +++- include/sysinc.h | 8 + src/libs/zbxsysinfo/common/file.c | 23 +- src/libs/zbxsysinfo/freebsd/boottime.c | 1 - src/libs/zbxsysinfo/freebsd/cpu.c | 568 ++++++++------------------ src/libs/zbxsysinfo/freebsd/diskspace.c | 246 +++++------ src/libs/zbxsysinfo/freebsd/inodes.c | 213 ++++------ src/libs/zbxsysinfo/freebsd/kernel.c | 78 +--- src/libs/zbxsysinfo/freebsd/memory.c | 459 +++++++-------------- src/libs/zbxsysinfo/freebsd/net.c | 382 +++++++++++++++++- src/libs/zbxsysinfo/freebsd/proc.c | 690 +++++++++++-------------------- src/libs/zbxsysinfo/freebsd/uptime.c | 33 +- src/libs/zbxsysinfo/netbsd/Makefile.am | 3 +- src/libs/zbxsysinfo/netbsd/cpu.c | 570 +++++++++----------------- src/libs/zbxsysinfo/netbsd/diskspace.c | 246 +++++------ src/libs/zbxsysinfo/netbsd/inodes.c | 213 ++++------ src/libs/zbxsysinfo/netbsd/kernel.c | 78 +--- src/libs/zbxsysinfo/netbsd/memory.c | 437 ++++++-------------- src/libs/zbxsysinfo/netbsd/net.c | 393 +++++++++++++++++- src/libs/zbxsysinfo/netbsd/netbsd.c | 1 + src/libs/zbxsysinfo/netbsd/proc.c | 694 ++++++++++++-------------------- src/libs/zbxsysinfo/netbsd/uptime.c | 33 +- src/libs/zbxsysinfo/openbsd/boottime.c | 1 - src/libs/zbxsysinfo/openbsd/cpu.c | 404 +++++++++---------- src/libs/zbxsysinfo/openbsd/diskspace.c | 1 - src/libs/zbxsysinfo/openbsd/inodes.c | 2 +- src/libs/zbxsysinfo/openbsd/kernel.c | 8 + src/libs/zbxsysinfo/openbsd/memory.c | 17 +- src/libs/zbxsysinfo/openbsd/net.c | 20 +- src/libs/zbxsysinfo/openbsd/proc.c | 4 +- src/libs/zbxsysinfo/openbsd/uptime.c | 4 +- src/zabbix_agent/cpustat.c | 15 + 33 files changed, 2578 insertions(+), 3345 deletions(-) diff --git a/ChangeLog b/ChangeLog index 9d9be68b..354c18d7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,6 @@ Changes for 1.5.3 + - [DEV-168] added more supported items in FreeBSD, NetBSD and OpenBSD (Sasha) - [DEV-161] changed greeting message (Artem) - [DEV-157] added fullscreen icon to monitoring screens (Artem) diff --git a/configure.in b/configure.in index 79af9993..356eee95 100644 --- a/configure.in +++ b/configure.in @@ -50,7 +50,8 @@ AC_CHECK_HEADERS(stdio.h stdlib.h string.h unistd.h netdb.h signal.h \ nlist.h net/if.h kvm.h linux/kernel.h ldap.h getopt.h procinfo.h sys/dk.h \ sys/resource.h pthread.h windows.h process.h conio.h sys/wait.h regex.h \ stdarg.h winsock2.h pdh.h psapi.h sys/sem.h sys/ipc.h sys/shm.h Winldap.h \ - sys/timeb.h Winber.h lber.h ws2tcpip.h inttypes.h sys/file.h grp.h) + sys/timeb.h Winber.h lber.h ws2tcpip.h inttypes.h sys/file.h grp.h \ + net/if_mib.h sys/user.h) dnl Checks for typedefs, structures, and compiler characteristics. AC_C_CONST @@ -310,13 +311,11 @@ AC_TRY_COMPILE( #endif /* HAVE_SYS_VMMETER_H */ ], [ + int mib[] = {CTL_VM, VM_METER}; size_t len; - int mib[2]; struct vmtotal v; len = sizeof(struct vmtotal); - mib[0] = CTL_VM; - mib[1] = VM_METER; sysctl(mib, 2, &v, &len, NULL, 0); ], @@ -324,6 +323,76 @@ AC_DEFINE(HAVE_SYS_VMMETER_VMTOTAL,1,[Define to 1 if struct 'vmtotal' exist .]) AC_MSG_RESULT(yes), AC_MSG_RESULT(no)) +dnl Check for uvmexp_sysctl in uvm/uvm_extern.h +AC_MSG_CHECKING(for uvmexp_sysctl in struct uvm/uvm_extern.h) +AC_TRY_COMPILE( +[ +#ifdef HAVE_SYS_TYPES_H +#include +#endif /* HAVE_SYS_TYPES_H */ + +#ifdef HAVE_SYS_PARAM_H +#include +#endif /* HAVE_SYS_PARAM_H */ + +#include + +#ifdef HAVE_VM_VM_PARAM_H +#include +#endif /* HAVE_VM_VM_PARAM_H */ + +#ifdef HAVE_SYS_VMMETER_H +#include +#endif /* HAVE_SYS_VMMETER_H */ +], +[ + int mib[] = {CTL_VM, VM_UVMEXP2}; + size_t len; + struct uvmexp_sysctl v; + + len = sizeof(struct uvmexp_sysctl); + + sysctl(mib, 2, &v, &len, NULL, 0); +], +AC_DEFINE(HAVE_UVM_UVMEXP2,1,[Define to 1 if struct 'uvmexp_sysctl' exist .]) +AC_MSG_RESULT(yes), +AC_MSG_RESULT(no)) + +dnl Check for uvmexp in uvm/uvm_extern.h +AC_MSG_CHECKING(for uvmexp in struct uvm/uvm_extern.h) +AC_TRY_COMPILE( +[ +#ifdef HAVE_SYS_TYPES_H +#include +#endif /* HAVE_SYS_TYPES_H */ + +#ifdef HAVE_SYS_PARAM_H +#include +#endif /* HAVE_SYS_PARAM_H */ + +#include + +#ifdef HAVE_VM_VM_PARAM_H +#include +#endif /* HAVE_VM_VM_PARAM_H */ + +#ifdef HAVE_SYS_VMMETER_H +#include +#endif /* HAVE_SYS_VMMETER_H */ +], +[ + int mib[] = {CTL_VM, VM_UVMEXP}; + size_t len; + struct uvmexp v; + + len = sizeof(struct uvmexp); + + sysctl(mib, 2, &v, &len, NULL, 0); +], +AC_DEFINE(HAVE_UVM_UVMEXP,1,[Define to 1 if struct 'uvmexp' exist .]) +AC_MSG_RESULT(yes), +AC_MSG_RESULT(no)) + dnl Check for struct swaptable in sys/swap.h AC_MSG_CHECKING(for struct swaptable in sys/swap.h) AC_TRY_COMPILE( diff --git a/include/sysinc.h b/include/sysinc.h index 72ab0964..fcdbea19 100644 --- a/include/sysinc.h +++ b/include/sysinc.h @@ -270,6 +270,14 @@ # include #endif +#ifdef HAVE_NET_IF_MIB_H +# include +#endif + +#ifdef HAVE_SYS_USER_H +# include +#endif + #ifdef HAVE_KVM_H # include #endif diff --git a/src/libs/zbxsysinfo/common/file.c b/src/libs/zbxsysinfo/common/file.c index 657ad9ad..b7ad3628 100644 --- a/src/libs/zbxsysinfo/common/file.c +++ b/src/libs/zbxsysinfo/common/file.c @@ -106,33 +106,24 @@ int VFS_FILE_TIME(const char *cmd, const char *param, unsigned flags, AGENT_RESU int VFS_FILE_EXISTS(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { struct stat buf; - char filename[MAX_STRING_LEN]; + char filename[MAX_STRING_LEN]; assert(result); init_result(result); - if(num_param(param) > 1) - { + if (num_param(param) > 1) return SYSINFO_RET_FAIL; - } - if(get_param(param, 1, filename, MAX_STRING_LEN) != 0) - { + if (0 != get_param(param, 1, filename, sizeof(filename))) return SYSINFO_RET_FAIL; - } SET_UI64_RESULT(result, 0); - /* File exists */ - if(stat(filename,&buf) == 0) - { - /* Regular file */ - if(S_ISREG(buf.st_mode)) - { + + if (0 == stat(filename, &buf)) /* File exists */ + if (S_ISREG(buf.st_mode)) /* Regular file */ SET_UI64_RESULT(result, 1); - } - } - /* File does not exist or any other error */ + return SYSINFO_RET_OK; } diff --git a/src/libs/zbxsysinfo/freebsd/boottime.c b/src/libs/zbxsysinfo/freebsd/boottime.c index c9814312..55b00251 100644 --- a/src/libs/zbxsysinfo/freebsd/boottime.c +++ b/src/libs/zbxsysinfo/freebsd/boottime.c @@ -46,4 +46,3 @@ int SYSTEM_BOOTTIME(const char *cmd, const char *param, unsigned flags, AGENT_RE return SYSINFO_RET_FAIL; #endif /* HAVE_FUNCTION_SYSCTL_KERN_BOOTTIME */ } - diff --git a/src/libs/zbxsysinfo/freebsd/cpu.c b/src/libs/zbxsysinfo/freebsd/cpu.c index 1dd5d3a4..801ed30c 100644 --- a/src/libs/zbxsysinfo/freebsd/cpu.c +++ b/src/libs/zbxsysinfo/freebsd/cpu.c @@ -25,8 +25,9 @@ int SYSTEM_CPU_NUM(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { #ifdef HAVE_FUNCTION_SYSCTL_HW_NCPU + /* FreeBSD 6.2 i386; FreeBSD 7.0 i386 */ size_t len; - int mib[2], ncpu; + int mib[] = {CTL_HW, HW_NCPU}, ncpu; char mode[MAX_STRING_LEN]; assert(result); @@ -36,507 +37,264 @@ int SYSTEM_CPU_NUM(const char *cmd, const char *param, unsigned flags, AGENT_RES if (num_param(param) > 1) return SYSINFO_RET_FAIL; - if (0 == get_param(param, 1, mode, sizeof(mode))) { - if (*mode != '\0') { - if (0 != strcmp(mode, "online")) - return SYSINFO_RET_FAIL; - } - } + if (0 != get_param(param, 1, mode, sizeof(mode))) + *mode = '\0'; + + if (*mode == '\0') + zbx_snprintf(mode, sizeof(mode), "online"); - mib[0] = CTL_HW; - mib[1] = HW_NCPU; + if (0 != strcmp(mode, "online")) + return SYSINFO_RET_FAIL; len = sizeof(ncpu); - if (-1 == sysctl(mib, 2, &ncpu, &len, NULL, 0)) + + if (0 != sysctl(mib, 2, &ncpu, &len, NULL, 0)) return SYSINFO_RET_FAIL; SET_UI64_RESULT(result, ncpu); return SYSINFO_RET_OK; #else - return SYSINFO_RET_FAIL; -#endif /*HAVE_FUNCTION_SYSCTL_HW_NCPU*/ + return SYSINFO_RET_FAIL; +#endif /* HAVE_FUNCTION_SYSCTL_HW_NCPU */ } -int SYSTEM_CPU_UTIL(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) +int SYSTEM_CPU_INTR(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { - char cpuname[MAX_STRING_LEN]; - char type[MAX_STRING_LEN]; - char mode[MAX_STRING_LEN]; - - int cpu_num = 0; - - assert(result); - - init_result(result); - - if(num_param(param) > 3) - { - return SYSINFO_RET_FAIL; - } +#if defined(HAVE_FUNCTION_SYSCTLBYNAME) + /* FreeBSD 6.2 i386; FreeBSD 7.0 i386 */ + u_int v_intr; + size_t len; - if(get_param(param, 1, cpuname, sizeof(cpuname)) != 0) - { - cpuname[0] = '\0'; - } + assert(result); - if(cpuname[0] == '\0') - { - /* default parameter */ - zbx_snprintf(cpuname, sizeof(cpuname), "all"); - } - - if(get_param(param, 2, type, sizeof(type)) != 0) - { - type[0] = '\0'; - } - if(type[0] == '\0') - { - /* default parameter */ - zbx_snprintf(type, sizeof(type), "user"); - } - - if(get_param(param, 3, mode, sizeof(mode)) != 0) - { - mode[0] = '\0'; - } + init_result(result); - if(mode[0] == '\0') - { - /* default parameter */ - zbx_snprintf(mode, sizeof(mode), "avg1"); - } - - if ( !CPU_COLLECTOR_STARTED(collector) ) - { - SET_MSG_RESULT(result, strdup("Collector is not started!")); - return SYSINFO_RET_OK; - } - - if(strcmp(cpuname,"all") == 0) - { - cpu_num = 0; - } - else - { - cpu_num = atoi(cpuname)+1; - if ((cpu_num < 1) || (cpu_num > collector->cpus.count)) - return SYSINFO_RET_FAIL; - } - - if( 0 == strcmp(type,"idle")) - { - if( 0 == strcmp(mode,"avg1")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].idle1) - else if( 0 == strcmp(mode,"avg5")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].idle5) - else if( 0 == strcmp(mode,"avg15")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].idle15) - else return SYSINFO_RET_FAIL; - - } - else if( 0 == strcmp(type,"nice")) - { - if( 0 == strcmp(mode,"avg1")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].nice1) - else if( 0 == strcmp(mode,"avg5")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].nice5) - else if( 0 == strcmp(mode,"avg15")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].nice15) - else return SYSINFO_RET_FAIL; + len = sizeof(v_intr); - } - else if( 0 == strcmp(type,"user")) - { - if( 0 == strcmp(mode,"avg1")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].user1) - else if( 0 == strcmp(mode,"avg5")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].user5) - else if( 0 == strcmp(mode,"avg15")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].user15) - else return SYSINFO_RET_FAIL; - } - else if( 0 == strcmp(type,"system")) - { - if( 0 == strcmp(mode,"avg1")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].system1) - else if( 0 == strcmp(mode,"avg5")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].system5) - else if( 0 == strcmp(mode,"avg15")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].system15) - else return SYSINFO_RET_FAIL; - } - else - { + if (0 != sysctlbyname("vm.stats.sys.v_intr", &v_intr, &len, NULL, 0)) return SYSINFO_RET_FAIL; - } + + SET_UI64_RESULT(result, v_intr); return SYSINFO_RET_OK; +#else + return SYSINFO_RET_FAIL; +#endif /* HAVE_FUNCTION_SYSCTLBYNAME */ } -/* AIX CPU info */ -#ifdef HAVE_KNLIST_H -static int getloadavg_kmem(double loadavg[], int nelem) +int SYSTEM_CPU_SWITCHES(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { - struct nlist nl; - int kmem, i; - long avenrun[3]; +#if defined(HAVE_FUNCTION_SYSCTLBYNAME) + /* FreeBSD 6.2 i386; FreeBSD 7.0 i386 */ + u_int v_swtch; + size_t len; - nl.n_name = "avenrun"; - nl.n_value = 0; + assert(result); - if(knlist(&nl, 1, sizeof(nl))) - { - return FAIL; - } - if((kmem = open("/dev/kmem", 0, 0)) <= 0) - { - return FAIL; - } + init_result(result); + + len = sizeof(v_swtch); - if(pread(kmem, avenrun, sizeof(avenrun), nl.n_value) < - sizeof(avenrun)) - { - return FAIL; - } + if (0 != sysctlbyname("vm.stats.sys.v_swtch", &v_swtch, &len, NULL, 0)) + return SYSINFO_RET_FAIL; - for(i=0;i 3) return SYSINFO_RET_FAIL; - } - else - { - SET_DBL_RESULT(result, dyn.psd_avg_1_min); - return SYSINFO_RET_OK; - } -#else -#ifdef HAVE_PROC_LOADAVG - return getPROC("/proc/loadavg",1,1, flags, result); -#else -#ifdef HAVE_KSTAT_H - static kstat_ctl_t *kc = NULL; - kstat_t *ks; - kstat_named_t *kn; - assert(result); + if (0 != get_param(param, 1, cpuname, sizeof(cpuname))) + *cpuname = '\0'; - init_result(result); - - if (!kc && !(kc = kstat_open())) - { - return SYSINFO_RET_FAIL; - } - if (!(ks = kstat_lookup(kc, "unix", 0, "system_misc")) || - kstat_read(kc, ks, 0) == -1 || - !(kn = kstat_data_lookup(ks,"avenrun_1min"))) - { - return SYSINFO_RET_FAIL; - } - SET_DBL_RESULT(result, ((double)kn->value.ul)/256.0); - return SYSINFO_RET_OK; -#else -#ifdef HAVE_KNLIST_H - double loadavg[3]; + /* default parameter */ + if (*cpuname == '\0') + zbx_snprintf(cpuname, sizeof(cpuname), "all"); - assert(result); + if (0 == strcmp(cpuname, "all")) + cpu_num = 0; + else { + cpu_num = atoi(cpuname) + 1; - init_result(result); - - if(getloadavg_kmem(loadavg,3) == FAIL) - { - return SYSINFO_RET_FAIL; + if (cpu_num < 1 || cpu_num > collector->cpus.count) + return SYSINFO_RET_FAIL; } - SET_DBL_RESULT(result, loadavg[0]); - return SYSINFO_RET_OK; -#else - assert(result); + if (0 != get_param(param, 2, type, sizeof(type))) + *type = '\0'; - init_result(result); - return SYSINFO_RET_FAIL; -#endif -#endif -#endif -#endif -#endif -} + /* default parameter */ + if (*type == '\0') + zbx_snprintf(type, sizeof(type), "user"); -int SYSTEM_CPU_LOAD5(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) -{ -#ifdef HAVE_GETLOADAVG - double load[3]; + if (0 != get_param(param, 3, mode, sizeof(mode))) + *mode = '\0'; - assert(result); + /* default parameter */ + if (*mode == '\0') + zbx_snprintf(mode, sizeof(mode), "avg1"); - init_result(result); - - if(getloadavg(load, 3)) - { - SET_DBL_RESULT(result, load[1]); + if (!CPU_COLLECTOR_STARTED(collector)) { + SET_MSG_RESULT(result, strdup("Collector is not started!")); return SYSINFO_RET_OK; } - else - { - return SYSINFO_RET_FAIL; - } -#else -#ifdef HAVE_SYS_PSTAT_H - struct pst_dynamic dyn; - - assert(result); - init_result(result); - - if (pstat_getdynamic(&dyn, sizeof(dyn), 1, 0) == -1) - { - return SYSINFO_RET_FAIL; - } - else - { - SET_DBL_RESULT(result, dyn.psd_avg_5_min); - return SYSINFO_RET_OK; + if (0 == strcmp(type, "idle")) { + if (0 == strcmp(mode, "avg1")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].idle1) + else if (0 == strcmp(mode, "avg5")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].idle5) + else if (0 == strcmp(mode, "avg15")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].idle15) + else return SYSINFO_RET_FAIL; } -#else -#ifdef HAVE_PROC_LOADAVG - return getPROC("/proc/loadavg",1,2, flags, result); -#else -#ifdef HAVE_KSTAT_H - static kstat_ctl_t *kc = NULL; - kstat_t *ks; - kstat_named_t *kn; - - assert(result); - - init_result(result); - - if (!kc && !(kc = kstat_open())) + else if (0 == strcmp(type, "nice")) { - return SYSINFO_RET_FAIL; + if (0 == strcmp(mode, "avg1")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].nice1) + else if (0 == strcmp(mode, "avg5")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].nice5) + else if (0 == strcmp(mode, "avg15")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].nice15) + else return SYSINFO_RET_FAIL; } - if (!(ks = kstat_lookup(kc, "unix", 0, "system_misc")) || - kstat_read(kc, ks, 0) == -1 || - !(kn = kstat_data_lookup(ks,"avenrun_5min"))) + else if (0 == strcmp(type, "user")) { - return SYSINFO_RET_FAIL; + if (0 == strcmp(mode, "avg1")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].user1) + else if (0 == strcmp(mode, "avg5")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].user5) + else if (0 == strcmp(mode, "avg15")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].user15) + else return SYSINFO_RET_FAIL; } - SET_DBL_RESULT(result, ((double)kn->value.ul)/256.0); - return SYSINFO_RET_OK; -#else -#ifdef HAVE_KNLIST_H - double loadavg[3]; - - assert(result); - - init_result(result); - - if(getloadavg_kmem(loadavg,3) == FAIL) + else if (0 == strcmp(type, "system")) { - return STSINFO_RET_FAIL; + if (0 == strcmp(mode, "avg1")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].system1) + else if (0 == strcmp(mode, "avg5")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].system5) + else if (0 == strcmp(mode, "avg15")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].system15) + else return SYSINFO_RET_FAIL; } + else + return SYSINFO_RET_FAIL; - SET_DBL_RESULT(result, loadavg[1]); return SYSINFO_RET_OK; -#else - assert(result); - - init_result(result); - return SYSINFO_RET_FAIL; -#endif -#endif -#endif -#endif -#endif } -int SYSTEM_CPU_LOAD15(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) +static int get_cpuload(double *load1, double *load5, double *load15) { #ifdef HAVE_GETLOADAVG + /* FreeBSD 6.2 i386; FreeBSD 7.0 i386 */ double load[3]; - assert(result); + if (-1 == getloadavg(load, 3)) + return SYSINFO_RET_FAIL; - init_result(result); + if (load1) + *load1 = load[0]; + if (load5) + *load5 = load[1]; + if (load15) + *load15 = load[2]; - if(getloadavg(load, 3)) - { - SET_DBL_RESULT(result, load[2]); - return SYSINFO_RET_OK; - } - else - { - return SYSINFO_RET_FAIL; - } + return SYSINFO_RET_OK; #else -#ifdef HAVE_SYS_PSTAT_H - struct pst_dynamic dyn; + return SYSINFO_RET_FAIL; +#endif /* HAVE_GETLOADAVG */ +} - assert(result); +static int SYSTEM_CPU_LOAD1(AGENT_RESULT *result) +{ + double value; - init_result(result); - - if (pstat_getdynamic(&dyn, sizeof(dyn), 1, 0) == -1) - { + if (SYSINFO_RET_OK != get_cpuload(&value, NULL, NULL)) return SYSINFO_RET_FAIL; - } - else - { - SET_DBL_RESULT(result, dyn.psd_avg_15_min); - return SYSINFO_RET_OK; - } -#else -#ifdef HAVE_PROC_LOADAVG - return getPROC("/proc/loadavg",1,3, flags, result); -#else -#ifdef HAVE_KSTAT_H - static kstat_ctl_t *kc = NULL; - kstat_t *ks; - kstat_named_t *kn; + + SET_DBL_RESULT(result, value); + + return SYSINFO_RET_OK; +} - assert(result); +static int SYSTEM_CPU_LOAD5(AGENT_RESULT *result) +{ + double value; - init_result(result); - - if (!kc && !(kc = kstat_open())) - { - return SYSINFO_RET_FAIL; - } - if (!(ks = kstat_lookup(kc, "unix", 0, "system_misc")) || - kstat_read(kc, ks, 0) == -1 || - !(kn = kstat_data_lookup(ks,"avenrun_15min"))) - { + if (SYSINFO_RET_OK != get_cpuload(NULL, &value, NULL)) return SYSINFO_RET_FAIL; - } - SET_DBL_RESULT(result, ((double)kn->value.ul)/256.0); + + SET_DBL_RESULT(result, value); + return SYSINFO_RET_OK; -#else -#ifdef HAVE_KNLIST_H - double loadavg[3]; +} - assert(result); +static int SYSTEM_CPU_LOAD15(AGENT_RESULT *result) +{ + double value; - init_result(result); + if (SYSINFO_RET_OK != get_cpuload(NULL, NULL, &value)) + return SYSINFO_RET_FAIL; - if(getloadavg_kmem(loadavg,3) == FAIL) - { - return STSINFO_RET_FAIL; - } - - SET_DBL_RESULT(result, loadavg[2]); + SET_DBL_RESULT(result, value); + return SYSINFO_RET_OK; -#else - assert(result); - - init_result(result); - return SYSINFO_RET_FAIL; -#endif -#endif -#endif -#endif -#endif } int SYSTEM_CPU_LOAD(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { - #define CPU_FNCLIST struct cpu_fnclist_s CPU_FNCLIST { - char *mode; - int (*function)(); + char *mode; + int (*function)(); }; CPU_FNCLIST fl[] = { - {"avg1" , SYSTEM_CPU_LOAD1}, - {"avg5" , SYSTEM_CPU_LOAD5}, + {"avg1", SYSTEM_CPU_LOAD1}, + {"avg5", SYSTEM_CPU_LOAD5}, {"avg15", SYSTEM_CPU_LOAD15}, {0, 0} }; - char cpuname[MAX_STRING_LEN]; - char mode[MAX_STRING_LEN]; - int i; - - assert(result); + char cpuname[MAX_STRING_LEN], + mode[MAX_STRING_LEN]; + int i; - init_result(result); - - if(num_param(param) > 2) - { - return SYSINFO_RET_FAIL; - } - - if(get_param(param, 1, cpuname, sizeof(cpuname)) != 0) - { - return SYSINFO_RET_FAIL; - } - if(cpuname[0] == '\0') - { - /* default parameter */ + assert(result); + + init_result(result); + + if (num_param(param) > 2) + return SYSINFO_RET_FAIL; + + if (0 != get_param(param, 1, cpuname, sizeof(cpuname))) + *cpuname = '\0'; + + /* default parameter */ + if (*cpuname == '\0') zbx_snprintf(cpuname, sizeof(cpuname), "all"); - } - if(strncmp(cpuname, "all", sizeof(cpuname))) - { + + if (0 != strncmp(cpuname, "all", sizeof(cpuname))) return SYSINFO_RET_FAIL; - } - - if(get_param(param, 2, mode, sizeof(mode)) != 0) - { - mode[0] = '\0'; - } - if(mode[0] == '\0') - { - /* default parameter */ - zbx_snprintf(mode, sizeof(mode), "avg1"); - } - for(i=0; fl[i].mode!=0; i++) - { - if(strncmp(mode, fl[i].mode, MAX_STRING_LEN)==0) - { - return (fl[i].function)(cmd, param, flags, result); - } - } - - return SYSINFO_RET_FAIL; -} -int SYSTEM_CPU_SWITCHES(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) -{ - assert(result); + if (0 != get_param(param, 2, mode, sizeof(mode))) + *mode = '\0'; - init_result(result); - - return SYSINFO_RET_FAIL; -} + /* default parameter */ + if (*mode == '\0') + zbx_snprintf(mode, sizeof(mode), "avg1"); -int SYSTEM_CPU_INTR(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) -{ - assert(result); + for (i = 0; fl[i].mode != 0; i++) + if (0 == strncmp(mode, fl[i].mode, MAX_STRING_LEN)) + return (fl[i].function)(result); - init_result(result); - return SYSINFO_RET_FAIL; } - diff --git a/src/libs/zbxsysinfo/freebsd/diskspace.c b/src/libs/zbxsysinfo/freebsd/diskspace.c index 6d0afd00..0ecdfadc 100644 --- a/src/libs/zbxsysinfo/freebsd/diskspace.c +++ b/src/libs/zbxsysinfo/freebsd/diskspace.c @@ -21,219 +21,155 @@ #include "sysinfo.h" -int get_fs_size_stat(char *fs, double *total, double *free, double *usage) +int get_fs_size_stat(const char *fs, zbx_uint64_t *total, zbx_uint64_t *free, zbx_uint64_t *used, double *pfree, double *pused) { #ifdef HAVE_SYS_STATVFS_H - struct statvfs s; + struct statvfs s; #else - struct statfs s; + struct statfs s; #endif - assert(fs); + assert(fs); #ifdef HAVE_SYS_STATVFS_H - if ( statvfs( fs, &s) != 0 ) + if (0 != statvfs(fs, &s)) #else - if ( statfs( fs, &s) != 0 ) + if (0 != statfs(fs, &s)) #endif - { - return SYSINFO_RET_FAIL; - } + { + return SYSINFO_RET_FAIL; + } #ifdef HAVE_SYS_STATVFS_H - if(total) - (*total) = (double)(s.f_blocks * (s.f_frsize / 1024.0)); - if(free) - (*free) = (double)(s.f_bavail * (s.f_frsize / 1024.0)); - if(usage) - (*usage) = (double)((s.f_blocks - s.f_bavail) * (s.f_frsize / 1024.0)); + if (total) + *total = (zbx_uint64_t)s.f_blocks * s.f_frsize; + if (free) + *free = (zbx_uint64_t)s.f_bfree * s.f_frsize; + if (used) + *used = (zbx_uint64_t)(s.f_blocks - s.f_bfree) * s.f_frsize; + if (pfree) + *pfree = (double)(100.0 * s.f_bfree) / s.f_blocks; + if (pused) + *pused = (double)(100.0 * (s.f_blocks - s.f_bfree)) / s.f_blocks; #else - if(total) - (*total) = (double)(s.f_blocks * (s.f_bsize / 1024.0)); - if(free) - (*free) = (double)(s.f_bfree * (s.f_bsize / 1024.0)); - if(usage) - (*usage) = (double)((s.f_blocks - s.f_bfree) * (s.f_bsize / 1024.0)); + if (total) + *total = (zbx_uint64_t)s.f_blocks * s.f_bsize; + if (free) + *free = (zbx_uint64_t)s.f_bfree * s.f_bsize; + if (used) + *used = (zbx_uint64_t)(s.f_blocks - s.f_bfree) * s.f_bsize; + if (pfree) + *pfree = (double)(100.0 * s.f_bfree) / s.f_blocks; + if (pused) + *pused = (double)(100.0 * (s.f_blocks - s.f_bfree)) / s.f_blocks; #endif - return SYSINFO_RET_OK; + return SYSINFO_RET_OK; } -static int VFS_FS_USED(const char *cmd, char *param, unsigned flags, AGENT_RESULT *result) +static int VFS_FS_USED(const char *fs, AGENT_RESULT *result) { -/* char mountPoint[MAX_STRING_LEN];*/ - double value = 0; - - assert(result); - - init_result(result); - -/* if(num_param(param) > 1) - return SYSINFO_RET_FAIL; + zbx_uint64_t value = 0; - if(get_param(param, 1, mountPoint, MAX_STRING_LEN) != 0) - return SYSINFO_RET_FAIL;*/ + if (SYSINFO_RET_OK != get_fs_size_stat(fs, NULL, NULL, &value, NULL, NULL)) + return SYSINFO_RET_FAIL; - if(get_fs_size_stat(param, NULL, NULL, &value) != SYSINFO_RET_OK) - return SYSINFO_RET_FAIL; + SET_UI64_RESULT(result, value); - SET_UI64_RESULT(result, value); - - return SYSINFO_RET_OK; + return SYSINFO_RET_OK; } -static int VFS_FS_FREE(const char *cmd, char *param, unsigned flags, AGENT_RESULT *result) +static int VFS_FS_FREE(const char *fs, AGENT_RESULT *result) { -/* char mountPoint[MAX_STRING_LEN];*/ - double value = 0; - - assert(result); - - init_result(result); - -/* if(num_param(param) > 1) - return SYSINFO_RET_FAIL; + zbx_uint64_t value = 0; - if(get_param(param, 1, mountPoint, MAX_STRING_LEN) != 0) - return SYSINFO_RET_FAIL;*/ + if (SYSINFO_RET_OK != get_fs_size_stat(fs, NULL, &value, NULL, NULL, NULL)) + return SYSINFO_RET_FAIL; - if(get_fs_size_stat(param, NULL, &value, NULL) != SYSINFO_RET_OK) - return SYSINFO_RET_FAIL; + SET_UI64_RESULT(result, value); - SET_UI64_RESULT(result, value); - - return SYSINFO_RET_OK; + return SYSINFO_RET_OK; } -static int VFS_FS_TOTAL(const char *cmd, char *param, unsigned flags, AGENT_RESULT *result) +static int VFS_FS_TOTAL(const char *fs, AGENT_RESULT *result) { -/* char mountPoint[MAX_STRING_LEN];*/ - double value = 0; - - assert(result); - - init_result(result); - -/* if(num_param(param) > 1) - return SYSINFO_RET_FAIL; - - if(get_param(param, 1, mountPoint, MAX_STRING_LEN) != 0) - { - return SYSINFO_RET_FAIL; - }*/ + zbx_uint64_t value = 0; - if(get_fs_size_stat(param, &value, NULL, NULL) != SYSINFO_RET_OK) - return SYSINFO_RET_FAIL; + if (SYSINFO_RET_OK != get_fs_size_stat(fs, &value, NULL, NULL, NULL, NULL)) + return SYSINFO_RET_FAIL; - SET_UI64_RESULT(result, value); + SET_UI64_RESULT(result, value); - return SYSINFO_RET_OK; + return SYSINFO_RET_OK; } -static int VFS_FS_PFREE(const char *cmd, char *param, unsigned flags, AGENT_RESULT *result) +static int VFS_FS_PFREE(const char *fs, AGENT_RESULT *result) { -/* char mountPoint[MAX_STRING_LEN];*/ - double tot_val = 0; - double free_val = 0; - - assert(result); - - init_result(result); - -/* if(num_param(param) > 1) - return SYSINFO_RET_FAIL; - - if(get_param(param, 1, mountPoint, MAX_STRING_LEN) != 0) - return SYSINFO_RET_FAIL;*/ + double value = 0; - if(get_fs_size_stat(param, &tot_val, &free_val, NULL) != SYSINFO_RET_OK) - return SYSINFO_RET_FAIL; + if (SYSINFO_RET_OK != get_fs_size_stat(fs, NULL, NULL, NULL, &value, NULL)) + return SYSINFO_RET_FAIL; - SET_DBL_RESULT(result, (100.0 * free_val) / tot_val); + SET_DBL_RESULT(result, value); - return SYSINFO_RET_OK; + return SYSINFO_RET_OK; } -static int VFS_FS_PUSED(const char *cmd, char *param, unsigned flags, AGENT_RESULT *result) +static int VFS_FS_PUSED(const char *fs, AGENT_RESULT *result) { -/* char mountPoint[MAX_STRING_LEN];*/ - double tot_val = 0; - double usg_val = 0; + double value = 0; - assert(result); + if (SYSINFO_RET_OK != get_fs_size_stat(fs, NULL, NULL, NULL, NULL, &value)) + return SYSINFO_RET_FAIL; - init_result(result); + SET_DBL_RESULT(result, value); -/* if(num_param(param) > 1) - return SYSINFO_RET_FAIL; - - if(get_param(param, 1, mountPoint, MAX_STRING_LEN) != 0) - return SYSINFO_RET_FAIL;*/ - - if(get_fs_size_stat(param, &tot_val, NULL, &usg_val) != SYSINFO_RET_OK) - return SYSINFO_RET_FAIL; - - SET_DBL_RESULT(result, (100.0 * usg_val) / tot_val); - - return SYSINFO_RET_OK; + return SYSINFO_RET_OK; } int VFS_FS_SIZE(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { - #define FS_FNCLIST struct fs_fnclist_s FS_FNCLIST { - char *mode; - int (*function)(); + char *mode; + int (*function)(); }; - FS_FNCLIST fl[] = + FS_FNCLIST fl[] = { - {"free" , VFS_FS_FREE}, - {"total" , VFS_FS_TOTAL}, + {"free", VFS_FS_FREE}, + {"total", VFS_FS_TOTAL}, {"used", VFS_FS_USED}, - {"pfree" , VFS_FS_PFREE}, - {"pused" , VFS_FS_PUSED}, + {"pfree", VFS_FS_PFREE}, + {"pused", VFS_FS_PUSED}, {0, 0} }; - char fsname[MAX_STRING_LEN]; - char mode[MAX_STRING_LEN]; - int i; - - assert(result); - - init_result(result); - - if(num_param(param) > 2) - { - return SYSINFO_RET_FAIL; - } - - if(get_param(param, 1, fsname, sizeof(fsname)) != 0) - { - return SYSINFO_RET_FAIL; - } - - if(get_param(param, 2, mode, sizeof(mode)) != 0) - { - mode[0] = '\0'; - } - if(mode[0] == '\0') - { - /* default parameter */ + char fsname[MAX_STRING_LEN]; + char mode[MAX_STRING_LEN]; + int i; + + assert(result); + + init_result(result); + + if (num_param(param) > 2) + return SYSINFO_RET_FAIL; + + if (0 != get_param(param, 1, fsname, sizeof(fsname))) + return SYSINFO_RET_FAIL; + + if (0 != get_param(param, 2, mode, sizeof(mode))) + *mode = '\0'; + + /* default parameter */ + if (*mode == '\0') zbx_snprintf(mode, sizeof(mode), "total"); - } - - for(i=0; fl[i].mode!=0; i++) - { - if(strncmp(mode, fl[i].mode, MAX_STRING_LEN)==0) - { - return (fl[i].function)(cmd, fsname, flags, result); - } - } - + + for (i = 0; fl[i].mode != 0; i++) + if (0 == strncmp(mode, fl[i].mode, MAX_STRING_LEN)) + return (fl[i].function)(fsname, result); + return SYSINFO_RET_FAIL; } - diff --git a/src/libs/zbxsysinfo/freebsd/inodes.c b/src/libs/zbxsysinfo/freebsd/inodes.c index af1f6d44..9f240b08 100644 --- a/src/libs/zbxsysinfo/freebsd/inodes.c +++ b/src/libs/zbxsysinfo/freebsd/inodes.c @@ -23,201 +23,154 @@ #include "md5.h" -static int get_fs_inodes_stat(char *fs, double *total, double *free, double *usage) +static int get_fs_inodes_stat(const char *fs, zbx_uint64_t *total, zbx_uint64_t *free, zbx_uint64_t *used, double *pfree, double *pused) { - struct statfs s; - +#ifdef HAVE_SYS_STATVFS_H + struct statvfs s; +#else + struct statfs s; +#endif + assert(fs); - - if ( statfs( fs, &s) != 0 ) + +#ifdef HAVE_SYS_STATVFS_H + if (0 != statvfs(fs, &s)) +#else + if (0 != statfs(fs, &s)) +#endif { - return SYSINFO_RET_FAIL; + return SYSINFO_RET_FAIL; } - - if(total) - (*total) = (double)(s.f_files); - if(free) - (*free) = (double)(s.f_ffree); - if(usage) - (*usage) = (double)(s.f_files - s.f_ffree); +#ifdef HAVE_SYS_STATVFS_H + if (total) + *total = (zbx_uint64_t)s.f_files; + if (free) + *free = (zbx_uint64_t)s.f_ffree; + if (used) + *used = (zbx_uint64_t)(s.f_files - s.f_ffree); + if (pfree) + *pfree = (double)(100.0 * s.f_ffree) / s.f_files; + if (pused) + *pused = (double)(100.0 * (s.f_files - s.f_ffree)) / s.f_files; +#else + if (total) + *total = (zbx_uint64_t)s.f_files; + if (free) + *free = (zbx_uint64_t)s.f_ffree; + if (used) + *used = (zbx_uint64_t)(s.f_files - s.f_ffree); + if (pfree) + *pfree = (double)(100.0 * s.f_ffree) / s.f_files; + if (pused) + *pused = (double)(100.0 * (s.f_files - s.f_ffree)) / s.f_files; +#endif return SYSINFO_RET_OK; } -static int VFS_FS_INODE_USED(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) +static int VFS_FS_INODE_USED(const char *fs, AGENT_RESULT *result) { - char mountPoint[MAX_STRING_LEN]; - double value = 0; + zbx_uint64_t value = 0; - assert(result); - - init_result(result); - - if(num_param(param) > 1) - return SYSINFO_RET_FAIL; - - if(get_param(param, 1, mountPoint, MAX_STRING_LEN) != 0) - return SYSINFO_RET_FAIL; - - if(get_fs_inodes_stat(mountPoint, NULL, NULL, &value) != SYSINFO_RET_OK) - return SYSINFO_RET_FAIL; + if (SYSINFO_RET_OK != get_fs_inodes_stat(fs, NULL, NULL, &value, NULL, NULL)) + return SYSINFO_RET_FAIL; SET_UI64_RESULT(result, value); return SYSINFO_RET_OK; } -static int VFS_FS_INODE_FREE(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) +static int VFS_FS_INODE_FREE(const char *fs, AGENT_RESULT *result) { - char mountPoint[MAX_STRING_LEN]; - double value = 0; + zbx_uint64_t value = 0; - assert(result); - - init_result(result); - - if(num_param(param) > 1) - return SYSINFO_RET_FAIL; - - if(get_param(param, 1, mountPoint, MAX_STRING_LEN) != 0) - return SYSINFO_RET_FAIL; - - if(get_fs_inodes_stat(mountPoint, NULL, &value, NULL) != SYSINFO_RET_OK) - return SYSINFO_RET_FAIL; + if (SYSINFO_RET_OK != get_fs_inodes_stat(fs, NULL, &value, NULL, NULL, NULL)) + return SYSINFO_RET_FAIL; SET_UI64_RESULT(result, value); return SYSINFO_RET_OK; } -static int VFS_FS_INODE_TOTAL(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) +static int VFS_FS_INODE_TOTAL(const char *fs, AGENT_RESULT *result) { - char mountPoint[MAX_STRING_LEN]; - double value = 0; + zbx_uint64_t value = 0; - assert(result); - - init_result(result); - - if(num_param(param) > 1) - return SYSINFO_RET_FAIL; - - if(get_param(param, 1, mountPoint, MAX_STRING_LEN) != 0) - { - return SYSINFO_RET_FAIL; - } - - if(get_fs_inodes_stat(mountPoint, &value, NULL, NULL) != SYSINFO_RET_OK) - return SYSINFO_RET_FAIL; + if (SYSINFO_RET_OK != get_fs_inodes_stat(fs, &value, NULL, NULL, NULL, NULL)) + return SYSINFO_RET_FAIL; SET_UI64_RESULT(result, value); return SYSINFO_RET_OK; } -static int VFS_FS_INODE_PFREE(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) +static int VFS_FS_INODE_PFREE(const char *fs, AGENT_RESULT *result) { - char mountPoint[MAX_STRING_LEN]; - double tot_val = 0; - double free_val = 0; + double value = 0; - assert(result); - - init_result(result); - - if(num_param(param) > 1) - return SYSINFO_RET_FAIL; - - if(get_param(param, 1, mountPoint, MAX_STRING_LEN) != 0) - return SYSINFO_RET_FAIL; - - if(get_fs_inodes_stat(mountPoint, &tot_val, &free_val, NULL) != SYSINFO_RET_OK) - return SYSINFO_RET_FAIL; + if (SYSINFO_RET_OK != get_fs_inodes_stat(fs, NULL, NULL, NULL, &value, NULL)) + return SYSINFO_RET_FAIL; - SET_DBL_RESULT(result, (100.0 * free_val) / tot_val); + SET_DBL_RESULT(result, value); return SYSINFO_RET_OK; } -static int VFS_FS_INODE_PUSED(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) +static int VFS_FS_INODE_PUSED(const char *fs, AGENT_RESULT *result) { - char mountPoint[MAX_STRING_LEN]; - double tot_val = 0; - double usg_val = 0; + double value = 0; - assert(result); - - init_result(result); - - if(num_param(param) > 1) - return SYSINFO_RET_FAIL; - - if(get_param(param, 1, mountPoint, MAX_STRING_LEN) != 0) - return SYSINFO_RET_FAIL; - - if(get_fs_inodes_stat(mountPoint, &tot_val, NULL, &usg_val) != SYSINFO_RET_OK) - return SYSINFO_RET_FAIL; + if (SYSINFO_RET_OK != get_fs_inodes_stat(fs, NULL, NULL, NULL, NULL, &value)) + return SYSINFO_RET_FAIL; - SET_DBL_RESULT(result, (100.0 * usg_val) / tot_val); + SET_DBL_RESULT(result, value); return SYSINFO_RET_OK; } int VFS_FS_INODE(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { - #define FS_FNCLIST struct fs_fnclist_s FS_FNCLIST { - char *mode; - int (*function)(); + char *mode; + int (*function)(); }; FS_FNCLIST fl[] = { - {"free" , VFS_FS_INODE_FREE}, - {"total" , VFS_FS_INODE_TOTAL}, + {"free", VFS_FS_INODE_FREE}, + {"total", VFS_FS_INODE_TOTAL}, {"used", VFS_FS_INODE_USED}, - {"pfree" , VFS_FS_INODE_PFREE}, - {"pused" , VFS_FS_INODE_PUSED}, + {"pfree", VFS_FS_INODE_PFREE}, + {"pused", VFS_FS_INODE_PUSED}, {0, 0} }; - char fsname[MAX_STRING_LEN]; - char mode[MAX_STRING_LEN]; - int i; + char fsname[MAX_STRING_LEN]; + char mode[MAX_STRING_LEN]; + int i; - assert(result); + assert(result); - init_result(result); + init_result(result); - if(num_param(param) > 2) - { - return SYSINFO_RET_FAIL; - } - - if(get_param(param, 1, fsname, sizeof(fsname)) != 0) - { - return SYSINFO_RET_FAIL; - } + if (num_param(param) > 2) + return SYSINFO_RET_FAIL; + + if (0 != get_param(param, 1, fsname, sizeof(mode))) + return SYSINFO_RET_FAIL; - if(get_param(param, 2, mode, sizeof(mode)) != 0) - { - mode[0] = '\0'; - } - if(mode[0] == '\0') - { - /* default parameter */ + if (0 != get_param(param, 2, mode, sizeof(mode))) + *mode = '\0'; + + /* default parameter */ + if (*mode == '\0') zbx_snprintf(mode, sizeof(mode), "total"); - } - for(i=0; fl[i].mode!=0; i++) - { - if(strncmp(mode, fl[i].mode, MAX_STRING_LEN)==0) - { - return (fl[i].function)(cmd, fsname, flags, result); - } - } + for (i = 0; fl[i].mode != 0; i++) + if (0 == strncmp(mode, fl[i].mode, MAX_STRING_LEN)) + return (fl[i].function)(fsname, result); return SYSINFO_RET_FAIL; } diff --git a/src/libs/zbxsysinfo/freebsd/kernel.c b/src/libs/zbxsysinfo/freebsd/kernel.c index e06c1d5e..245e76a4 100644 --- a/src/libs/zbxsysinfo/freebsd/kernel.c +++ b/src/libs/zbxsysinfo/freebsd/kernel.c @@ -24,24 +24,24 @@ int KERNEL_MAXFILES(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { #ifdef HAVE_FUNCTION_SYSCTL_KERN_MAXFILES - int mib[2],len; + int mib[2]; + size_t len; int maxfiles; assert(result); - init_result(result); + init_result(result); - mib[0]=CTL_KERN; - mib[1]=KERN_MAXFILES; + mib[0] = CTL_KERN; + mib[1] = KERN_MAXFILES; - len=sizeof(maxfiles); + len = sizeof(maxfiles); - if(sysctl(mib,2,&maxfiles,(size_t *)&len,NULL,0) != 0) - { - return SYSINFO_RET_FAIL; - } + if (0 != sysctl(mib, 2, &maxfiles, &len, NULL, 0)) + return SYSINFO_RET_FAIL; + + SET_UI64_RESULT(result, maxfiles); - SET_UI64_RESULT(result, maxfiles); return SYSINFO_RET_OK; #else return SYSINFO_RET_FAIL; @@ -51,64 +51,26 @@ int KERNEL_MAXFILES(const char *cmd, const char *param, unsigned flags, AGENT_RE int KERNEL_MAXPROC(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { #ifdef HAVE_FUNCTION_SYSCTL_KERN_MAXPROC - int mib[2],len; + int mib[2]; + size_t len; int maxproc; assert(result); - init_result(result); + init_result(result); - mib[0]=CTL_KERN; - mib[1]=KERN_MAXPROC; + mib[0] = CTL_KERN; + mib[1] = KERN_MAXPROC; - len=sizeof(maxproc); + len = sizeof(maxproc); - if(sysctl(mib,2,&maxproc,(size_t *)&len,NULL,0) != 0) - { - return SYSINFO_RET_FAIL; -/* printf("Errno [%m]");*/ - } + if (0 != sysctl(mib, 2, &maxproc, &len, NULL, 0)) + return SYSINFO_RET_FAIL; + + SET_UI64_RESULT(result, maxproc); - - SET_UI64_RESULT(result, maxproc); return SYSINFO_RET_OK; #else return SYSINFO_RET_FAIL; #endif } - -int OLD_KERNEL(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) -{ - char key[MAX_STRING_LEN]; - int ret; - - assert(result); - - init_result(result); - - if(num_param(param) > 1) - { - return SYSINFO_RET_FAIL; - } - - if(get_param(param, 1, key, MAX_STRING_LEN) != 0) - { - return SYSINFO_RET_FAIL; - } - - if(strcmp(key,"maxfiles") == 0) - { - ret = KERNEL_MAXFILES(cmd, param, flags, result); - } - else if(strcmp(key,"maxproc") == 0) - { - ret = KERNEL_MAXPROC(cmd, param, flags, result); - } - else - { - ret = SYSINFO_RET_FAIL; - } - - return ret; -} - diff --git a/src/libs/zbxsysinfo/freebsd/memory.c b/src/libs/zbxsysinfo/freebsd/memory.c index eb8ee371..6efd2b5b 100644 --- a/src/libs/zbxsysinfo/freebsd/memory.c +++ b/src/libs/zbxsysinfo/freebsd/memory.c @@ -21,349 +21,180 @@ #include "sysinfo.h" -static int VM_MEMORY_CACHED(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) +static int VM_MEMORY_TOTAL(AGENT_RESULT *result) { -#ifdef HAVE_PROC - FILE *f = NULL; - char *t; - char c[MAX_STRING_LEN]; - zbx_uint64_t res = 0; - - assert(result); - - init_result(result); - - if(NULL == (f = fopen("/proc/meminfo","r"))) - { - return SYSINFO_RET_FAIL; - } - - while(NULL != fgets(c,MAX_STRING_LEN,f)) - { - if(strncmp(c,"Cached:",7) == 0) - { - t=(char *)strtok(c," "); - t=(char *)strtok(NULL," "); - sscanf(t, ZBX_FS_UI64, &res ); - t=(char *)strtok(NULL," "); - - if(strcasecmp(t,"kb")) res <<= 10; - else if(strcasecmp(t, "mb")) res <<= 20; - else if(strcasecmp(t, "gb")) res <<= 30; - else if(strcasecmp(t, "tb")) res <<= 40; - - break; - } - } - - zbx_fclose(f); - - SET_UI64_RESULT(result, res); - return SYSINFO_RET_OK; -#else - assert(result); +#if defined(HAVE_FUNCTION_SYSCTLBYNAME) + /* FreeBSD 6.2 i386; FreeBSD 7.0 i386 */ + u_int totalpages, pagesize; + size_t len; - init_result(result); - - return SYSINFO_RET_FAIL; -#endif -} + len = sizeof(totalpages); -static int VM_MEMORY_BUFFERS(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) -{ - assert(result); + if (0 != sysctlbyname("vm.stats.vm.v_page_count", &totalpages, &len, NULL, 0)) + return SYSINFO_RET_FAIL; - init_result(result); - -#ifdef HAVE_SYSINFO_BUFFERRAM - struct sysinfo info; + len = sizeof(pagesize); - if( 0 == sysinfo(&info)) - { -#ifdef HAVE_SYSINFO_MEM_UNIT - SET_UI64_RESULT(result, (zbx_uint64_t)info.bufferram * (zbx_uint64_t)info.mem_unit); -#else - SET_UI64_RESULT(result, info.bufferram); -#endif - return SYSINFO_RET_OK; - } - else - { + if (0 != sysctlbyname("vm.stats.vm.v_page_size", &pagesize, &len, NULL, 0)) return SYSINFO_RET_FAIL; - } + + SET_UI64_RESULT(result, (zbx_uint64_t)totalpages * pagesize); + + return SYSINFO_RET_OK; #else - return SYSINFO_RET_FAIL; -#endif + return SYSINFO_RET_FAIL; +#endif /* HAVE_FUNCTION_SYSCTLBYNAME */ } -static int VM_MEMORY_SHARED(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) +static int VM_MEMORY_FREE(AGENT_RESULT *result) { -#ifdef HAVE_SYSINFO_SHAREDRAM - struct sysinfo info; -#elif defined(HAVE_SYS_VMMETER_VMTOTAL) - int mib[2],len; - struct vmtotal v; -#endif - - assert(result); +#if defined(HAVE_FUNCTION_SYSCTLBYNAME) + /* FreeBSD 6.2 i386; FreeBSD 7.0 i386 */ + u_int freepages, pagesize; + size_t len; - init_result(result); - -#ifdef HAVE_SYSINFO_SHAREDRAM + len = sizeof(freepages); - if( 0 == sysinfo(&info)) - { -#ifdef HAVE_SYSINFO_MEM_UNIT - SET_UI64_RESULT(result, (zbx_uint64_t)info.sharedram * (zbx_uint64_t)info.mem_unit); -#else - SET_UI64_RESULT(result, info.sharedram); -#endif - return SYSINFO_RET_OK; - } - else - { + if (0 != sysctlbyname("vm.stats.vm.v_free_count", &freepages, &len, NULL, 0)) return SYSINFO_RET_FAIL; - } -#elif defined(HAVE_SYS_VMMETER_VMTOTAL) - len=sizeof(struct vmtotal); - mib[0]=CTL_VM; - mib[1]=VM_METER; + len = sizeof(pagesize); - sysctl(mib,2,&v,&len,NULL,0); + if (0 != sysctlbyname("vm.stats.vm.v_page_size", &pagesize, &len, NULL, 0)) + return SYSINFO_RET_FAIL; + + SET_UI64_RESULT(result, (zbx_uint64_t)freepages * pagesize); - SET_UI64_RESULT(result, v.t_armshr * getpagesize()); return SYSINFO_RET_OK; #else - return SYSINFO_RET_FAIL; -#endif + return SYSINFO_RET_FAIL; +#endif /* HAVE_FUNCTION_SYSCTLBYNAME */ } -static int VM_MEMORY_TOTAL(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) +static int VM_MEMORY_USED(AGENT_RESULT *result) { -#if defined(HAVE_SYS_PSTAT_H) - struct pst_static pst; - long page; -#elif defined(HAVE_SYSINFO_TOTALRAM) - struct sysinfo info; -#elif defined(HAVE_SYS_VMMETER_VMTOTAL) - int mib[2], len, total; -#elif defined(HAVE_SYS_SYSCTL_H) - static int mib[] = { CTL_HW, HW_PHYSMEM }; - size_t len; - unsigned int memory; - int ret; -#endif +#if defined(HAVE_FUNCTION_SYSCTLBYNAME) + /* FreeBSD 6.2 i386; FreeBSD 7.0 i386 */ + u_int totalpages, freepages, pagesize; + size_t len; - assert(result); + len = sizeof(totalpages); + + if (0 != sysctlbyname("vm.stats.vm.v_page_count", &totalpages, &len, NULL, 0)) + return SYSINFO_RET_FAIL; - init_result(result); - -#if defined(HAVE_SYS_PSTAT_H) + len = sizeof(freepages); - if(pstat_getstatic(&pst, sizeof(pst), (size_t)1, 0) == -1) - { + if (0 != sysctlbyname("vm.stats.vm.v_free_count", &freepages, &len, NULL, 0)) return SYSINFO_RET_FAIL; - } - else - { - /* Get page size */ - page = pst.page_size; - /* Total physical memory in bytes */ - SET_UI64_RESULT(result, (zbx_uint64_t)page*(zbx_uint64_t)pst.physical_memory); - return SYSINFO_RET_OK; - } -#elif defined(HAVE_SYSINFO_TOTALRAM) - - if( 0 == sysinfo(&info)) - { -#ifdef HAVE_SYSINFO_MEM_UNIT - SET_UI64_RESULT(result, (zbx_uint64_t)info.totalram * (zbx_uint64_t)info.mem_unit); -#else - SET_UI64_RESULT(result, info.totalram); -#endif - return SYSINFO_RET_OK; - } - else - { + + len = sizeof(pagesize); + + if (0 != sysctlbyname("vm.stats.vm.v_page_size", &pagesize, &len, NULL, 0)) return SYSINFO_RET_FAIL; - } -#elif defined(HAVE_SYS_VMMETER_VMTOTAL) - len = sizeof(total); - mib[0]=CTL_HW; - mib[1]=HW_PHYSMEM; - sysctl(mib, 2, &total, &len, NULL, 0); + SET_UI64_RESULT(result, (zbx_uint64_t)(totalpages - freepages) * pagesize); - SET_UI64_RESULT(result, total); return SYSINFO_RET_OK; -#elif defined(HAVE_SYS_SYSCTL_H) - - len=sizeof(memory); - - if(0==sysctl(mib,2,&memory,&len,NULL,0)) - { - SET_UI64_RESULT(result, memory); - ret=SYSINFO_RET_OK; - } - else - { - ret=SYSINFO_RET_FAIL; - } - return ret; #else - return SYSINFO_RET_FAIL; -#endif + return SYSINFO_RET_FAIL; +#endif /* HAVE_FUNCTION_SYSCTLBYNAME */ } -static int VM_MEMORY_FREE(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) +static int VM_MEMORY_PFREE(AGENT_RESULT *result) { -#if defined(HAVE_SYS_PSTAT_H) - struct pst_static pst; - struct pst_dynamic dyn; - long page; -#elif defined(HAVE_SYSINFO_FREERAM) - struct sysinfo info; -#elif defined(HAVE_SYS_VMMETER_VMTOTAL) - int mib[2],len; - struct vmtotal v; -#elif defined(HAVE_MACH_HOST_INFO_H) - vm_statistics_data_t page_info; - vm_size_t pagesize; - mach_msg_type_number_t count; - kern_return_t kret; - int ret; -#endif +#if defined(HAVE_FUNCTION_SYSCTLBYNAME) + /* FreeBSD 6.2 i386; FreeBSD 7.0 i386 */ + u_int totalpages, freepages; + size_t len; - assert(result); - - init_result(result); - -#if defined(HAVE_SYS_PSTAT_H) + len = sizeof(totalpages); - if(pstat_getstatic(&pst, sizeof(pst), (size_t)1, 0) == -1) - { + if (0 != sysctlbyname("vm.stats.vm.v_page_count", &totalpages, &len, NULL, 0)) return SYSINFO_RET_FAIL; - } - else - { - /* Get page size */ - page = pst.page_size; -/* return pst.physical_memory;*/ - - if (pstat_getdynamic(&dyn, sizeof(dyn), 1, 0) == -1) - { - return SYSINFO_RET_FAIL; - } - else - { -/* cout<<"total virtual memory allocated is " << dyn.psd_vm << " - pages, " << dyn.psd_vm * page << " bytes" << endl; - cout<<"active virtual memory is " << dyn.psd_avm <<" pages, " << - dyn.psd_avm * page << " bytes" << endl; - cout<<"total real memory is " << dyn.psd_rm << " pages, " << - dyn.psd_rm * page << " bytes" << endl; - cout<<"active real memory is " << dyn.psd_arm << " pages, " << - dyn.psd_arm * page << " bytes" << endl; - cout<<"free memory is " << dyn.psd_free << " pages, " << -*/ - /* Free memory in bytes */ - - SET_UI64_RESULT(result, (zbx_uint64_t)dyn.psd_free * (zbx_uint64_t)page); - return SYSINFO_RET_OK; - } - } -#elif defined(HAVE_SYSINFO_FREERAM) - - if( 0 == sysinfo(&info)) - { -#ifdef HAVE_SYSINFO_MEM_UNIT - SET_UI64_RESULT(result, (zbx_uint64_t)info.freeram * (zbx_uint64_t)info.mem_unit); -#else - SET_UI64_RESULT(result, info.freeram); -#endif - return SYSINFO_RET_OK; - } - else - { - return SYSINFO_RET_FAIL; - } -#elif defined(HAVE_SYS_VMMETER_VMTOTAL) - len=sizeof(struct vmtotal); - mib[0]=CTL_VM; - mib[1]=VM_METER; + len = sizeof(freepages); + + if (0 != sysctlbyname("vm.stats.vm.v_free_count", &freepages, &len, NULL, 0)) + return SYSINFO_RET_FAIL; - sysctl(mib,2,&v,&len,NULL,0); + SET_DBL_RESULT(result, (double)(100.0 * freepages) / totalpages); - SET_UI64_RESULT(result, v.t_free * getpagesize()); return SYSINFO_RET_OK; -/* OS/X */ -#elif defined(HAVE_MACH_HOST_INFO_H) - - pagesize = 0; - kret = host_page_size (mach_host_self(), &pagesize); - - count = HOST_VM_INFO_COUNT; - kret = host_statistics (mach_host_self(), HOST_VM_INFO, - (host_info_t)&page_info, &count); - if (kret == KERN_SUCCESS) - { - double pw, pa, pi, pf, pu; +#else + return SYSINFO_RET_FAIL; +#endif /* HAVE_FUNCTION_SYSCTLBYNAME */ +} - pw = (double)page_info.wire_count*pagesize; - pa = (double)page_info.active_count*pagesize; - pi = (double)page_info.inactive_count*pagesize; - pf = (double)page_info.free_count*pagesize; +static int VM_MEMORY_PUSED(AGENT_RESULT *result) +{ +#if defined(HAVE_FUNCTION_SYSCTLBYNAME) + /* FreeBSD 6.2 i386; FreeBSD 7.0 i386 */ + u_int totalpages, freepages; + size_t len; - pu = pw+pa+pi; + len = sizeof(totalpages); - SET_UI64_RESULT(result, pf); - ret = SYSINFO_RET_OK; - } - else - { - ret = SYSINFO_RET_FAIL; - } - return ret; + if (0 != sysctlbyname("vm.stats.vm.v_page_count", &totalpages, &len, NULL, 0)) + return SYSINFO_RET_FAIL; + + len = sizeof(freepages); + + if (0 != sysctlbyname("vm.stats.vm.v_free_count", &freepages, &len, NULL, 0)) + return SYSINFO_RET_FAIL; + + SET_DBL_RESULT(result, (double)(100.0 * (totalpages - freepages)) / totalpages); + + return SYSINFO_RET_OK; #else - return SYSINFO_RET_FAIL; -#endif + return SYSINFO_RET_FAIL; +#endif /* HAVE_FUNCTION_SYSCTLBYNAME */ } -static int VM_MEMORY_PFREE(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) +static int VM_MEMORY_CACHED(AGENT_RESULT *result) { - AGENT_RESULT result_tmp; - zbx_uint64_t tot_val = 0; - zbx_uint64_t free_val = 0; +#if defined(HAVE_FUNCTION_SYSCTLBYNAME) + /* FreeBSD 6.2 i386; FreeBSD 7.0 i386 */ + u_int cachepages, pagesize; + size_t len; - assert(result); + len = sizeof(cachepages); - init_result(result); - init_result(&result_tmp); + if (0 != sysctlbyname("vm.stats.vm.v_cache_count", &cachepages, &len, NULL, 0)) + return SYSINFO_RET_FAIL; - if(VM_MEMORY_TOTAL(cmd, param, flags, &result_tmp) != SYSINFO_RET_OK || - !(result_tmp.type & AR_UINT64)) - return SYSINFO_RET_FAIL; - tot_val = result_tmp.ui64; + len = sizeof(pagesize); - /* Check fot division by zero */ - if(tot_val == 0) - { - free_result(&result_tmp); - return SYSINFO_RET_FAIL; - } + if (0 != sysctlbyname("vm.stats.vm.v_page_size", &pagesize, &len, NULL, 0)) + return SYSINFO_RET_FAIL; + + SET_UI64_RESULT(result, (zbx_uint64_t)cachepages * pagesize); + + return SYSINFO_RET_OK; +#else + return SYSINFO_RET_FAIL; +#endif /* HAVE_FUNCTION_SYSCTLBYNAME */ +} + +static int VM_MEMORY_SHARED(AGENT_RESULT *result) +{ +#if defined(HAVE_SYS_VMMETER_VMTOTAL) + /* FreeBSD 6.2 i386; FreeBSD 7.0 i386 */ + int mib[] = {CTL_VM, VM_METER}; + size_t len; + struct vmtotal v; - if(VM_MEMORY_FREE(cmd, param, flags, &result_tmp) != SYSINFO_RET_OK || - !(result_tmp.type & AR_UINT64)) - return SYSINFO_RET_FAIL; - free_val = result_tmp.ui64; + len = sizeof(v); - free_result(&result_tmp); + if (0 != sysctl(mib, 2, &v, &len, NULL, 0)) + return SYSINFO_RET_FAIL; - SET_DBL_RESULT(result, (100.0 * (double)free_val) / (double)tot_val); + SET_UI64_RESULT(result, (zbx_uint64_t)(v.t_vmshr + v.t_rmshr) * sysconf(_SC_PAGESIZE)); return SYSINFO_RET_OK; +#else + return SYSINFO_RET_FAIL; +#endif } int VM_MEMORY_SIZE(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) @@ -371,51 +202,43 @@ int VM_MEMORY_SIZE(const char *cmd, const char *param, unsigned flags, AGENT #define MEM_FNCLIST struct mem_fnclist_s MEM_FNCLIST { - char *mode; - int (*function)(); + char *mode; + int (*function)(); }; MEM_FNCLIST fl[] = { + {"total", VM_MEMORY_TOTAL}, {"free", VM_MEMORY_FREE}, - {"pfree", VM_MEMORY_PFREE}, + {"used", VM_MEMORY_USED}, + {"pfree", VM_MEMORY_PFREE}, + {"pused", VM_MEMORY_PUSED}, {"shared", VM_MEMORY_SHARED}, - {"total", VM_MEMORY_TOTAL}, - {"buffers", VM_MEMORY_BUFFERS}, {"cached", VM_MEMORY_CACHED}, - {0, 0} + {0, 0} }; - char mode[MAX_STRING_LEN]; - int i; - assert(result); + char mode[MAX_STRING_LEN]; + int i; + + assert(result); - init_result(result); + init_result(result); - if(num_param(param) > 1) - { - return SYSINFO_RET_FAIL; - } + if (num_param(param) > 1) + return SYSINFO_RET_FAIL; - if(get_param(param, 1, mode, sizeof(mode)) != 0) - { - mode[0] = '\0'; - } + if (0 != get_param(param, 1, mode, sizeof(mode))) + *mode = '\0'; - if(mode[0] == '\0') - { - /* default parameter */ + /* default parameter */ + if (*mode == '\0') zbx_snprintf(mode, sizeof(mode), "total"); - } - - for(i=0; fl[i].mode!=0; i++) - { - if(strncmp(mode, fl[i].mode, MAX_STRING_LEN)==0) - { - return (fl[i].function)(cmd, param, flags, result); - } - } - + + for(i = 0; fl[i].mode != 0; i++) + if (0 == strncmp(mode, fl[i].mode, MAX_STRING_LEN)) + return (fl[i].function)(result); + return SYSINFO_RET_FAIL; } diff --git a/src/libs/zbxsysinfo/freebsd/net.c b/src/libs/zbxsysinfo/freebsd/net.c index 1925f27d..7d982e2d 100644 --- a/src/libs/zbxsysinfo/freebsd/net.c +++ b/src/libs/zbxsysinfo/freebsd/net.c @@ -21,50 +21,402 @@ #include "sysinfo.h" +static struct nlist kernel_symbols[] = +{ + {"_ifnet", N_UNDF, 0, 0, 0}, + {"_tcbtable", N_UNDF, 0, 0, 0}, + {NULL, 0, 0, 0, 0} +}; + +#define IFNET_ID 0 + +static int get_ifdata(const char *if_name, zbx_uint64_t *ibytes, zbx_uint64_t *ipackets, zbx_uint64_t *ierrors, zbx_uint64_t *idropped, + zbx_uint64_t *obytes, zbx_uint64_t *opackets, zbx_uint64_t *oerrors, + zbx_uint64_t *tbytes, zbx_uint64_t *tpackets, zbx_uint64_t *terrors, zbx_uint64_t *tdropped, + zbx_uint64_t *icollisions) +{ + int mib[6], ifnum; + size_t len; + struct ifmibdata ifdata; + int ret = SYSINFO_RET_FAIL; + + mib[0] = CTL_NET; + mib[1] = PF_LINK; + mib[2] = NETLINK_GENERIC; + mib[3] = IFMIB_SYSTEM; + mib[4] = IFMIB_IFCOUNT; + + len = sizeof(ifnum); + if (0 != sysctl(mib, 5, &ifnum, &len, NULL, 0)) + return SYSINFO_RET_FAIL; + + mib[3] = IFMIB_IFDATA; + mib[4] = 0; + mib[5] = IFDATA_GENERAL; + + len = sizeof(ifdata); + + if (ibytes) + *ibytes = 0; + if (ipackets) + *ipackets = 0; + if (ierrors) + *ierrors = 0; + if (idropped) + *idropped = 0; + if (obytes) + *obytes = 0; + if (opackets) + *opackets = 0; + if (oerrors) + *oerrors = 0; + if (tbytes) + *tbytes = 0; + if (tpackets) + *tpackets = 0; + if (terrors) + *terrors = 0; + if (tdropped) + *tdropped = 0; + if (icollisions) + *icollisions = 0; + + for (; ifnum > 0; ifnum--) { + mib[4] = ifnum; + + if (0 != sysctl(mib, 6, &ifdata, &len, NULL, 0)) + continue; + + if (*if_name == '\0' || 0 == strcmp(if_name, ifdata.ifmd_name)) { + if (ibytes) + *ibytes += ifdata.ifmd_data.ifi_ibytes; + if (ipackets) + *ipackets += ifdata.ifmd_data.ifi_ipackets; + if (ierrors) + *ierrors += ifdata.ifmd_data.ifi_ierrors; + if (idropped) + *idropped += ifdata.ifmd_data.ifi_iqdrops; + if (obytes) + *obytes += ifdata.ifmd_data.ifi_obytes; + if (opackets) + *opackets += ifdata.ifmd_data.ifi_opackets; + if (oerrors) + *oerrors += ifdata.ifmd_data.ifi_oerrors; + if (tbytes) + *tbytes += ifdata.ifmd_data.ifi_ibytes + ifdata.ifmd_data.ifi_obytes; + if (tpackets) + *tpackets += ifdata.ifmd_data.ifi_ipackets + ifdata.ifmd_data.ifi_opackets; + if (terrors) + *terrors += ifdata.ifmd_data.ifi_ierrors + ifdata.ifmd_data.ifi_oerrors; + if (tdropped) + *tdropped += ifdata.ifmd_data.ifi_iqdrops; + if (icollisions) + *icollisions += ifdata.ifmd_data.ifi_collisions; + ret = SYSINFO_RET_OK; + } + } + + return ret; +} + +static int NET_IF_IN_BYTES(const char *if_name, AGENT_RESULT *result) +{ + zbx_uint64_t value; + + if (SYSINFO_RET_OK != get_ifdata(if_name, &value, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)) + return SYSINFO_RET_FAIL; + + SET_UI64_RESULT(result, value); + + return SYSINFO_RET_OK; +} + +static int NET_IF_IN_PACKETS(const char *if_name, AGENT_RESULT *result) +{ + zbx_uint64_t value; + + if (SYSINFO_RET_OK != get_ifdata(if_name, NULL, &value, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)) + return SYSINFO_RET_FAIL; + + SET_UI64_RESULT(result, value); + + return SYSINFO_RET_OK; +} + +static int NET_IF_IN_ERRORS(const char *if_name, AGENT_RESULT *result) +{ + zbx_uint64_t value; + + if (SYSINFO_RET_OK != get_ifdata(if_name, NULL, NULL, &value, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)) + return SYSINFO_RET_FAIL; + + SET_UI64_RESULT(result, value); + + return SYSINFO_RET_OK; +} + +static int NET_IF_IN_DROPPED(const char *if_name, AGENT_RESULT *result) +{ + zbx_uint64_t value; + + if (SYSINFO_RET_OK != get_ifdata(if_name, NULL, NULL, NULL, &value, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)) + return SYSINFO_RET_FAIL; + + SET_UI64_RESULT(result, value); + + return SYSINFO_RET_OK; +} + int NET_IF_IN(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { +#define NET_FNCLIST struct net_fnclist_s +NET_FNCLIST +{ + char *mode; + int (*function)(); +}; - assert(result); + NET_FNCLIST fl[] = + { + {"bytes", NET_IF_IN_BYTES}, + {"packets", NET_IF_IN_PACKETS}, + {"errors", NET_IF_IN_ERRORS}, + {"dropped", NET_IF_IN_DROPPED}, + {0, 0} + }; + + char if_name[MAX_STRING_LEN]; + char mode[MAX_STRING_LEN]; + int i; + + assert(result); + + init_result(result); + + if (num_param(param) > 2) + return SYSINFO_RET_FAIL; + + if (0 != get_param(param, 1, if_name, sizeof(if_name))) + *if_name = '\0'; + + if (0 != get_param(param, 2, mode, sizeof(mode))) + *mode = '\0'; + + /* default parameter */ + if (*mode == '\0') + zbx_snprintf(mode, sizeof(mode), "bytes"); + + for (i = 0; fl[i].mode != 0; i++) + if (0 == strncmp(mode, fl[i].mode, MAX_STRING_LEN)) + return (fl[i].function)(if_name, result); - init_result(result); - return SYSINFO_RET_FAIL; } +static int NET_IF_OUT_BYTES(const char *if_name, AGENT_RESULT *result) +{ + zbx_uint64_t value; + + if (SYSINFO_RET_OK != get_ifdata(if_name, NULL, NULL, NULL, NULL, &value, NULL, NULL, NULL, NULL, NULL, NULL, NULL)) + return SYSINFO_RET_FAIL; + + SET_UI64_RESULT(result, value); + + return SYSINFO_RET_OK; +} + +static int NET_IF_OUT_PACKETS(const char *if_name, AGENT_RESULT *result) +{ + zbx_uint64_t value; + + if (SYSINFO_RET_OK != get_ifdata(if_name, NULL, NULL, NULL, NULL, NULL, &value, NULL, NULL, NULL, NULL, NULL, NULL)) + return SYSINFO_RET_FAIL; + + SET_UI64_RESULT(result, value); + + return SYSINFO_RET_OK; +} + +static int NET_IF_OUT_ERRORS(const char *if_name, AGENT_RESULT *result) +{ + zbx_uint64_t value; + + if (SYSINFO_RET_OK != get_ifdata(if_name, NULL, NULL, NULL, NULL, NULL, NULL, &value, NULL, NULL, NULL, NULL, NULL)) + return SYSINFO_RET_FAIL; + + SET_UI64_RESULT(result, value); + + return SYSINFO_RET_OK; +} + int NET_IF_OUT(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { +#define NET_FNCLIST struct net_fnclist_s +NET_FNCLIST +{ + char *mode; + int (*function)(); +}; - assert(result); + NET_FNCLIST fl[] = + { + {"bytes", NET_IF_OUT_BYTES}, + {"packets", NET_IF_OUT_PACKETS}, + {"errors", NET_IF_OUT_ERRORS}, +/* {"dropped", NET_IF_OUT_DROPPED},*/ + {0, 0} + }; + + char if_name[MAX_STRING_LEN]; + char mode[MAX_STRING_LEN]; + int i; + + assert(result); + + init_result(result); + + if (num_param(param) > 2) + return SYSINFO_RET_FAIL; + + if (0 != get_param(param, 1, if_name, sizeof(if_name))) + *if_name = '\0'; + + if (0 != get_param(param, 2, mode, sizeof(mode))) + *mode = '\0'; + + /* default parameter */ + if (*mode == '\0') + zbx_snprintf(mode, sizeof(mode), "bytes"); + + for (i = 0; fl[i].mode != 0; i++) + if (0 == strncmp(mode, fl[i].mode, MAX_STRING_LEN)) + return (fl[i].function)(if_name, result); - init_result(result); - return SYSINFO_RET_FAIL; } +static int NET_IF_TOTAL_BYTES(const char *if_name, AGENT_RESULT *result) +{ + zbx_uint64_t value; + + if (SYSINFO_RET_OK != get_ifdata(if_name, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &value, NULL, NULL, NULL, NULL)) + return SYSINFO_RET_FAIL; + + SET_UI64_RESULT(result, value); + + return SYSINFO_RET_OK; +} + +static int NET_IF_TOTAL_PACKETS(const char *if_name, AGENT_RESULT *result) +{ + zbx_uint64_t value; + + if (SYSINFO_RET_OK != get_ifdata(if_name, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &value, NULL, NULL, NULL)) + return SYSINFO_RET_FAIL; + + SET_UI64_RESULT(result, value); + + return SYSINFO_RET_OK; +} + +static int NET_IF_TOTAL_ERRORS(const char *if_name, AGENT_RESULT *result) +{ + zbx_uint64_t value; + + if (SYSINFO_RET_OK != get_ifdata(if_name, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &value, NULL, NULL)) + return SYSINFO_RET_FAIL; + + SET_UI64_RESULT(result, value); + + return SYSINFO_RET_OK; +} + +static int NET_IF_TOTAL_DROPPED(const char *if_name, AGENT_RESULT *result) +{ + zbx_uint64_t value; + + if (SYSINFO_RET_OK != get_ifdata(if_name, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &value, NULL)) + return SYSINFO_RET_FAIL; + + SET_UI64_RESULT(result, value); + + return SYSINFO_RET_OK; +} + int NET_IF_TOTAL(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { +#define NET_FNCLIST struct net_fnclist_s +NET_FNCLIST +{ + char *mode; + int (*function)(); +}; - assert(result); + NET_FNCLIST fl[] = + { + {"bytes", NET_IF_TOTAL_BYTES}, + {"packets", NET_IF_TOTAL_PACKETS}, + {"errors", NET_IF_TOTAL_ERRORS}, + {"dropped", NET_IF_TOTAL_DROPPED}, + {0, 0} + }; + + char if_name[MAX_STRING_LEN]; + char mode[MAX_STRING_LEN]; + int i; + + assert(result); + + init_result(result); + + if (num_param(param) > 2) + return SYSINFO_RET_FAIL; + + if (0 != get_param(param, 1, if_name, sizeof(if_name))) + *if_name = '\0'; + + if (0 != get_param(param, 2, mode, sizeof(mode))) + *mode = '\0'; + + /* default parameter */ + if (*mode == '\0') + zbx_snprintf(mode, sizeof(mode), "bytes"); + + for (i = 0; fl[i].mode != 0; i++) + if (0 == strncmp(mode, fl[i].mode, MAX_STRING_LEN)) + return (fl[i].function)(if_name, result); - init_result(result); - return SYSINFO_RET_FAIL; } int NET_TCP_LISTEN(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { - assert(result); + assert(result); - init_result(result); + init_result(result); return SYSINFO_RET_FAIL; } int NET_IF_COLLISIONS(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { - assert(result); + zbx_uint64_t value; + char if_name[MAX_STRING_LEN]; - init_result(result); - - return SYSINFO_RET_FAIL; + assert(result); + + init_result(result); + + if (num_param(param) > 1) + return SYSINFO_RET_FAIL; + + if (0 != get_param(param, 1, if_name, sizeof(if_name))) + *if_name = '\0'; + + if (SYSINFO_RET_OK != get_ifdata(if_name, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &value)) + return SYSINFO_RET_FAIL; + + SET_UI64_RESULT(result, value); + + return SYSINFO_RET_OK; } diff --git a/src/libs/zbxsysinfo/freebsd/proc.c b/src/libs/zbxsysinfo/freebsd/proc.c index 5d6e2b89..616c5e42 100644 --- a/src/libs/zbxsysinfo/freebsd/proc.c +++ b/src/libs/zbxsysinfo/freebsd/proc.c @@ -18,482 +18,280 @@ * **/ #include "common.h" - #include "sysinfo.h" +#include + #define DO_SUM 0 #define DO_MAX 1 #define DO_MIN 2 #define DO_AVG 3 - -int PROC_MEMORY(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) -{ -#if defined(HAVE_PROC_1_STATUS) - - DIR *dir; - struct dirent *entries; - struct stat buf; - char filename[MAX_STRING_LEN]; - char line[MAX_STRING_LEN]; - char name1[MAX_STRING_LEN]; - char name2[MAX_STRING_LEN]; +#define ZBX_PROC_STAT_ALL 0 +#define ZBX_PROC_STAT_RUN 1 +#define ZBX_PROC_STAT_SLEEP 2 +#define ZBX_PROC_STAT_ZOMB 3 - char procname[MAX_STRING_LEN]; - char usrname[MAX_STRING_LEN]; - char mode[MAX_STRING_LEN]; - - int proc_ok = 0; - int usr_ok = 0; - int do_task = DO_SUM; +static kvm_t *kd = NULL; +static char *proc_argv(struct kinfo_proc *proc) +{ + static struct pargs pa; + size_t sz, s; + static char *argv = NULL; + static size_t argv_alloc = 0; + + sz = sizeof(pa); + if (sz == kvm_read(kd, (unsigned long)proc->ki_args, &pa, sz)) { + if (NULL == argv || argv_alloc < pa.ar_length) { + argv_alloc = pa.ar_length; + if (NULL == argv) + argv = zbx_malloc(argv, argv_alloc); + else + argv = zbx_realloc(argv, argv_alloc); + } + + sz = pa.ar_length; + if (sz == kvm_read(kd, (unsigned long)proc->ki_args + sizeof(pa.ar_ref) + sizeof(pa.ar_length), argv, sz)) { + for (s = 0; s < sz - 1; s++) + if (argv[s] == '\0') + argv[s] = ' '; + return argv; + } + } - struct passwd *usrinfo = NULL; - long long int llvalue = 0; + return NULL; +} - FILE *f; +int PROC_MEMORY(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) +{ + char procname[MAX_STRING_LEN], + buffer[MAX_STRING_LEN], + proccomm[MAX_STRING_LEN], + *args; + int do_task, count, i, + proc_ok, comm_ok, + op, arg; + + double value = 0.0, + memsize = 0; + int proccount = 0; - double memsize = -1; - int proccount = 0; + size_t sz; + + struct kinfo_proc *proc, *pproc; + struct passwd *usrinfo; + + assert(result); + + init_result(result); + + if (num_param(param) > 4) + return SYSINFO_RET_FAIL; + + if (0 != get_param(param, 1, procname, sizeof(procname))) + *procname = '\0'; + else if (strlen(procname) > MAXCOMLEN) + procname[MAXCOMLEN] = '\0'; + + if (0 != get_param(param, 2, buffer, sizeof(buffer))) + *buffer = '\0'; + + if (*buffer != '\0') { + usrinfo = getpwnam(buffer); + if (usrinfo == NULL) /* incorrect user name */ + return SYSINFO_RET_FAIL; + } else + usrinfo = NULL; + + if (0 != get_param(param, 3, buffer, sizeof(buffer))) + *buffer = '\0'; + + if (*buffer != '\0') { + if (0 == strcmp(buffer, "avg")) + do_task = DO_AVG; + else if (0 == strcmp(buffer, "max")) + do_task = DO_MAX; + else if (0 == strcmp(buffer, "min")) + do_task = DO_MIN; + else if (0 == strcmp(buffer, "sum")) + do_task = DO_SUM; + else + return SYSINFO_RET_FAIL; + } else + do_task = DO_SUM; + + if (0 != get_param(param, 4, proccomm, sizeof(proccomm))) + *proccomm = '\0'; + + if (NULL == kd && NULL == (kd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL))) + return SYSINFO_RET_FAIL; + + if (NULL != usrinfo) { + op = KERN_PROC_UID; + arg = (int)usrinfo->pw_uid; + } else { + op = KERN_PROC_ALL; + arg = 0; + } - assert(result); + if (NULL == (proc = kvm_getprocs(kd, op, arg, &count))) + return SYSINFO_RET_FAIL; + + for (pproc = proc, i = 0; i < count; pproc++, i++) { + proc_ok = 0; + comm_ok = 0; + + if (*procname == '\0' || 0 == strcmp(procname, pproc->ki_comm)) + proc_ok = 1; + + if (*proccomm != '\0') { + if (NULL != (args = proc_argv(pproc))) { + if (NULL != zbx_regexp_match(args, proccomm, NULL)) + comm_ok = 1; + } + } else + comm_ok = 1; + + if (proc_ok && comm_ok) { + value = pproc->ki_size; + + if (0 == proccount++) + memsize = value; + else { + if (do_task == DO_MAX) + memsize = MAX(memsize, value); + else if (do_task == DO_MIN) + memsize = MIN(memsize, value); + else + memsize += value; + } + } + } + zbx_free(proc); - init_result(result); - - if(num_param(param) > 3) - { - return SYSINFO_RET_FAIL; - } + if (do_task == DO_AVG) { + SET_DBL_RESULT(result, proccount == 0 ? 0 : memsize/proccount); + } else { + SET_UI64_RESULT(result, memsize); + } - if(get_param(param, 1, procname, MAX_STRING_LEN) != 0) - { - return SYSINFO_RET_FAIL; - } - - if(get_param(param, 2, usrname, MAX_STRING_LEN) != 0) - { - usrname[0] = 0; - } - else - { - if(usrname[0] != 0) - { - usrinfo = getpwnam(usrname); - if(usrinfo == NULL) - { - /* incorrect user name */ - return SYSINFO_RET_FAIL; - } - } - } - - if(get_param(param, 3, mode, MAX_STRING_LEN) != 0) - { - mode[0] = '\0'; - } - - if(mode[0] == '\0') - { - strscpy(mode, "sum"); - } - - if(strcmp(mode,"avg") == 0) - { - do_task = DO_AVG; - } - else if(strcmp(mode,"max") == 0) - { - do_task = DO_MAX; - } - else if(strcmp(mode,"min") == 0) - { - do_task = DO_MIN; - } - else if(strcmp(mode,"sum") == 0) - { - do_task = DO_SUM; - } - else - { - return SYSINFO_RET_FAIL; - } - - dir=opendir("/proc"); - if(NULL == dir) - { - return SYSINFO_RET_FAIL; - } - - while((entries=readdir(dir))!=NULL) - { - proc_ok = 0; - usr_ok = 0; - - strscpy(filename,"/proc/"); - zbx_strlcat(filename,entries->d_name,MAX_STRING_LEN); - zbx_strlcat(filename,"/status",MAX_STRING_LEN); - - /* Self is a symbolic link. It leads to incorrect results for proc_cnt[zabbix_agentd] */ - /* Better approach: check if /proc/x/ is symbolic link */ - if(strncmp(entries->d_name,"self",MAX_STRING_LEN) == 0) - { - continue; /* readdir */ - } - - if(stat(filename,&buf)==0) - { - if(NULL == (f = fopen(filename,"r"))) - { - continue; /* readdir */ - } - - if(procname[0] != 0) - { - fgets(line,MAX_STRING_LEN,f); - if(sscanf(line,"%s\t%s\n",name1,name2)==2) - { - if(strcmp(name1,"Name:") == 0) - { - if(strcmp(procname,name2)==0) - { - proc_ok = 1; - } - } - } - - if(proc_ok == 0) - { - zbx_fclose(f); - continue; /* readdir */ - } - } - else - { - proc_ok = 1; - } - - if(usrinfo != NULL) - { - while(fgets(line, MAX_STRING_LEN, f) != NULL) - { - - if(sscanf(line, "%s\t%lli\n", name1, &llvalue) != 2) - { - continue; /* fgets */ - } - - if(strcmp(name1,"Uid:") != 0) - { - continue; /* fgets */ - } - - if(usrinfo->pw_uid == (uid_t)(llvalue)) - { - usr_ok = 1; - break; - } - } - } - else - { - usr_ok = 1; - } - - if(proc_ok && usr_ok) - { - while(fgets(line, MAX_STRING_LEN, f) != NULL) - { - - if(sscanf(line, "%s\t%lli %s\n", name1, &llvalue, name2) != 3) - { - continue; /* fgets */ - } - - if(strcmp(name1,"VmSize:") != 0) - { - continue; /* fgets */ - } - - proccount++; - - if(strcasecmp(name2, "kB") == 0) - { - llvalue <<= 10; - } - else if(strcasecmp(name2, "mB") == 0) - { - llvalue <<= 20; - } - else if(strcasecmp(name2, "GB") == 0) - { - llvalue <<= 30; - } - else if(strcasecmp(name2, "TB") == 0) - { - llvalue <<= 40; - } - - if(memsize < 0) - { - memsize = (double) llvalue; - } - else - { - if(do_task == DO_MAX) - { - memsize = MAX(memsize, (double) llvalue); - } - else if(do_task == DO_MIN) - { - memsize = MIN(memsize, (double) llvalue); - } - else - { - memsize += (double) llvalue; - } - } - - break; - } - } - - zbx_fclose(f); - } - } - closedir(dir); - - if(memsize < 0) - { - /* incorrect process name */ - memsize = 0; - } - - if(do_task == DO_AVG) - { - SET_DBL_RESULT(result, proccount == 0 ? 0 : ((double)memsize/(double)proccount)); - } - else - { - SET_UI64_RESULT(result, memsize); - } - return SYSINFO_RET_OK; -#else - return SYSINFO_RET_FAIL; -#endif + return SYSINFO_RET_OK; } -int PROC_NUM(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) +int PROC_NUM(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { -#if defined(HAVE_PROC_1_STATUS) + char procname[MAX_STRING_LEN], + buffer[MAX_STRING_LEN], + proccomm[MAX_STRING_LEN], + *args; + int zbx_proc_stat, count, i, + proc_ok, stat_ok, comm_ok, + op, arg; - DIR *dir; - struct dirent *entries; - struct stat buf; - char filename[MAX_STRING_LEN]; - char line[MAX_STRING_LEN]; + int proccount = 0; - char name1[MAX_STRING_LEN]; - char name2[MAX_STRING_LEN]; + size_t sz; - char procname[MAX_STRING_LEN]; - char usrname[MAX_STRING_LEN]; - char procstat[MAX_STRING_LEN]; + struct kinfo_proc *proc, *pproc; + struct passwd *usrinfo; - int proc_ok = 0; - int usr_ok = 0; - int stat_ok = 0; + assert(result); - struct passwd *usrinfo = NULL; - long int lvalue = 0; + init_result(result); - FILE *f; - int proccount = 0; + if (num_param(param) > 4) + return SYSINFO_RET_FAIL; - assert(result); + if (0 != get_param(param, 1, procname, sizeof(procname))) + *procname = '\0'; + else if (strlen(procname) > MAXCOMLEN) + procname[MAXCOMLEN] = '\0'; - init_result(result); - - - if(num_param(param) > 3) - { - return SYSINFO_RET_FAIL; - } - - if(get_param(param, 1, procname, MAX_STRING_LEN) != 0) - { - return SYSINFO_RET_FAIL; - } - - if(get_param(param, 2, usrname, MAX_STRING_LEN) != 0) - { - usrname[0] = 0; - } - else - { - if(usrname[0] != 0) - { - usrinfo = getpwnam(usrname); - if(usrinfo == NULL) - { - /* incorrect user name */ - return SYSINFO_RET_FAIL; - } - } - } + if (0 != get_param(param, 2, buffer, sizeof(buffer))) + *buffer = '\0'; + + if (*buffer != '\0') { + usrinfo = getpwnam(buffer); + if (usrinfo == NULL) /* incorrect user name */ + return SYSINFO_RET_FAIL; + } else + usrinfo = NULL; - if(get_param(param, 3, procstat, MAX_STRING_LEN) != 0) - { - procstat[0] = '\0'; + if (0 != get_param(param, 3, buffer, sizeof(buffer))) + *buffer = '\0'; + + if (*buffer != '\0') { + if (0 == strcmp(buffer, "run")) + zbx_proc_stat = ZBX_PROC_STAT_RUN; + else if (0 == strcmp(buffer, "sleep")) + zbx_proc_stat = ZBX_PROC_STAT_SLEEP; + else if (0 == strcmp(buffer, "zomb")) + zbx_proc_stat = ZBX_PROC_STAT_ZOMB; + else if (0 == strcmp(buffer, "all")) + zbx_proc_stat = ZBX_PROC_STAT_ALL; + else + return SYSINFO_RET_FAIL; + } else + zbx_proc_stat = ZBX_PROC_STAT_ALL; + + if (0 != get_param(param, 4, proccomm, sizeof(proccomm))) + *proccomm = '\0'; + + if (NULL == kd && NULL == (kd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open"))) + return SYSINFO_RET_FAIL; + + if (NULL != usrinfo) { + op = KERN_PROC_UID; + arg = (int)usrinfo->pw_uid; + } else { + op = KERN_PROC_ALL; + arg = 0; } - if(procstat[0] == '\0') - { - strscpy(procstat, "all"); - } - - if(strcmp(procstat,"run") == 0) - { - strscpy(procstat,"R"); - } - else if(strcmp(procstat,"sleep") == 0) - { - strscpy(procstat,"S"); - } - else if(strcmp(procstat,"zomb") == 0) - { - strscpy(procstat,"Z"); - } - else if(strcmp(procstat,"all") == 0) - { - procstat[0] = 0; - } - else - { - return SYSINFO_RET_FAIL; - } + if (NULL == (proc = kvm_getprocs(kd, op, arg, &count))) + return SYSINFO_RET_FAIL; + + for (pproc = proc, i = 0; i < count; pproc++, i++) { + proc_ok = 0; + stat_ok = 0; + comm_ok = 0; + + if (*procname == '\0' || 0 == strcmp(procname, pproc->ki_comm)) + proc_ok = 1; + + if (zbx_proc_stat != ZBX_PROC_STAT_ALL) { + switch (zbx_proc_stat) { + case ZBX_PROC_STAT_RUN: + if (pproc->ki_stat == SRUN) + stat_ok = 1; + break; + case ZBX_PROC_STAT_SLEEP: + if (pproc->ki_stat == SSLEEP) + stat_ok = 1; + break; + case ZBX_PROC_STAT_ZOMB: + if (pproc->ki_stat == SZOMB) + stat_ok = 1; + break; + } + } else + stat_ok = 1; + + if (*proccomm != '\0') { + if (NULL != (args = proc_argv(pproc))) { + if (zbx_regexp_match(args, proccomm, NULL) != NULL) + comm_ok = 1; + } + } else + comm_ok = 1; - dir=opendir("/proc"); - if(NULL == dir) - { - return SYSINFO_RET_FAIL; - } - - while((entries=readdir(dir))!=NULL) - { - proc_ok = 0; - stat_ok = 0; - usr_ok = 0; - -/* Self is a symbolic link. It leads to incorrect results for proc_cnt[zabbix_agentd] */ -/* Better approach: check if /proc/x/ is symbolic link */ - if(strncmp(entries->d_name,"self",MAX_STRING_LEN) == 0) - { - continue; /* readdir */ - } - - strscpy(filename,"/proc/"); - zbx_strlcat(filename,entries->d_name,MAX_STRING_LEN); - zbx_strlcat(filename,"/status",MAX_STRING_LEN); - - if(stat(filename,&buf)==0) - { - if( NULL == (f = fopen(filename,"r"))) - { - continue; /* readdir */ - } - - if(procname[0] != 0) - { - fgets(line,MAX_STRING_LEN,f); - if(sscanf(line,"%s\t%s\n",name1,name2)==2) - { - if(strcmp(name1,"Name:") == 0) - { - if(strcmp(procname,name2)==0) - { - proc_ok = 1; - } - } - } - - if(proc_ok == 0) - { - zbx_fclose(f); - continue; /* readdir */ - } - } - else - { - proc_ok = 1; - } - - if(procstat[0] != 0) - { - while(fgets(line, MAX_STRING_LEN, f) != NULL) - { - - if(sscanf(line, "%s\t%s\n", name1, name2) != 2) - { - continue; /* fgets */ - } - - if(strcmp(name1,"State:") != 0) - { - continue; /* fgets */ - } - - if(strcmp(name2, procstat)) - { - stat_ok = 1; - break; - } - } - } - else - { - stat_ok = 1; - } - - if(usrinfo != NULL) - { - while(fgets(line, MAX_STRING_LEN, f) != NULL) - { - - if(sscanf(line, "%s\t%li\n", name1, &lvalue) != 2) - { - continue; /* fgets */ - } - - if(strcmp(name1,"Uid:") != 0) - { - continue; /* fgets */ - } - - if(usrinfo->pw_uid == (uid_t)(lvalue)) - { - usr_ok = 1; - break; - } - } - } - else - { - usr_ok = 1; - } - - if(proc_ok && stat_ok && usr_ok) - { - proccount++; - } - - zbx_fclose(f); - } - } - closedir(dir); + if (proc_ok && stat_ok && comm_ok) + proccount++; + } + zbx_free(proc); SET_UI64_RESULT(result, proccount); - return SYSINFO_RET_OK; -#else - return SYSINFO_RET_FAIL; -#endif + + return SYSINFO_RET_OK; } diff --git a/src/libs/zbxsysinfo/freebsd/uptime.c b/src/libs/zbxsysinfo/freebsd/uptime.c index eb75030f..331f9ac9 100644 --- a/src/libs/zbxsysinfo/freebsd/uptime.c +++ b/src/libs/zbxsysinfo/freebsd/uptime.c @@ -23,7 +23,7 @@ int SYSTEM_UPTIME(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { -#ifdef HAVE_SYSINFO_UPTIME +#if defined(HAVE_SYSINFO_UPTIME) struct sysinfo info; assert(result); @@ -39,34 +39,29 @@ int SYSTEM_UPTIME(const char *cmd, const char *param, unsigned flags, AGENT_RESU { return SYSINFO_RET_FAIL; } -#else -#ifdef HAVE_FUNCTION_SYSCTL_KERN_BOOTTIME - int mib[2],len; +#elif defined(HAVE_FUNCTION_SYSCTL_KERN_BOOTTIME) + int mib[2], now; + size_t len; struct timeval uptime; - int now; assert(result); init_result(result); - mib[0]=CTL_KERN; - mib[1]=KERN_BOOTTIME; + mib[0] = CTL_KERN; + mib[1] = KERN_BOOTTIME; - len=sizeof(uptime); + len = sizeof(uptime); - if(sysctl(mib,2,&uptime,(size_t *)&len,NULL,0) != 0) - { - return SYSINFO_RET_FAIL; -/* printf("Errno [%m]\n");*/ - } + if (0 != sysctl(mib, 2, &uptime, &len, NULL, 0)) + return SYSINFO_RET_FAIL; - now=time(NULL); + now = time(NULL); - SET_UI64_RESULT(result, now-uptime.tv_sec); + SET_UI64_RESULT(result, now - uptime.tv_sec); + return SYSINFO_RET_OK; -#else -/* Solaris */ -#ifdef HAVE_KSTAT_H +#elif defined(HAVE_KSTAT_H) /* Solaris */ kstat_ctl_t *kc; kstat_t *kp; kstat_named_t *kn; @@ -115,6 +110,4 @@ int SYSTEM_UPTIME(const char *cmd, const char *param, unsigned flags, AGENT_RESU return SYSINFO_RET_FAIL; #endif -#endif -#endif } diff --git a/src/libs/zbxsysinfo/netbsd/Makefile.am b/src/libs/zbxsysinfo/netbsd/Makefile.am index 471f201c..e33bb237 100644 --- a/src/libs/zbxsysinfo/netbsd/Makefile.am +++ b/src/libs/zbxsysinfo/netbsd/Makefile.am @@ -15,4 +15,5 @@ libspecsysinfo_a_SOURCES = \ proc.c \ sensors.c \ swap.c \ - uptime.c + uptime.c \ + boottime.c diff --git a/src/libs/zbxsysinfo/netbsd/cpu.c b/src/libs/zbxsysinfo/netbsd/cpu.c index fc7a51e0..e62730d4 100644 --- a/src/libs/zbxsysinfo/netbsd/cpu.c +++ b/src/libs/zbxsysinfo/netbsd/cpu.c @@ -24,489 +24,279 @@ int SYSTEM_CPU_NUM(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { - return SYSINFO_RET_FAIL; -} - -int SYSTEM_CPU_UTIL(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) -{ - char cpuname[MAX_STRING_LEN]; - char type[MAX_STRING_LEN]; - char mode[MAX_STRING_LEN]; - - int cpu_num = 0; - - assert(result); - - init_result(result); - - if(num_param(param) > 3) - { - return SYSINFO_RET_FAIL; - } +#ifdef HAVE_FUNCTION_SYSCTL_HW_NCPU + /* NetBSD 3.1 i386; NetBSD 4.0 i386 */ + size_t len; + int mib[] = {CTL_HW, HW_NCPU}, ncpu; + char mode[MAX_STRING_LEN]; - if(get_param(param, 1, cpuname, sizeof(cpuname)) != 0) - { - cpuname[0] = '\0'; - } + assert(result); - if(cpuname[0] == '\0') - { - /* default parameter */ - zbx_snprintf(cpuname, sizeof(cpuname), "all"); - } - - if(get_param(param, 2, type, sizeof(type)) != 0) - { - type[0] = '\0'; - } + init_result(result); - if(type[0] == '\0') - { - /* default parameter */ - zbx_snprintf(type, sizeof(type), "user"); - } - - if(get_param(param, 3, mode, sizeof(mode)) != 0) - { - mode[0] = '\0'; - } - - if(mode[0] == '\0') - { - /* default parameter */ - zbx_snprintf(mode, sizeof(mode), "avg1"); - } + if (num_param(param) > 1) + return SYSINFO_RET_FAIL; - if ( !CPU_COLLECTOR_STARTED(collector) ) - { - SET_MSG_RESULT(result, strdup("Collector is not started!")); - return SYSINFO_RET_OK; - } + if (0 != get_param(param, 1, mode, sizeof(mode))) + *mode = '\0'; - if(strcmp(cpuname,"all") == 0) - { - cpu_num = 0; - } - else - { - cpu_num = atoi(cpuname)+1; - if ((cpu_num < 1) || (cpu_num > collector->cpus.count)) - return SYSINFO_RET_FAIL; - } + if (*mode == '\0') + zbx_snprintf(mode, sizeof(mode), "online"); - if( 0 == strcmp(type,"idle")) - { - if( 0 == strcmp(mode,"avg1")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].idle1) - else if( 0 == strcmp(mode,"avg5")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].idle5) - else if( 0 == strcmp(mode,"avg15")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].idle15) - else return SYSINFO_RET_FAIL; + if (0 != strcmp(mode, "online")) + return SYSINFO_RET_FAIL; - } - else if( 0 == strcmp(type,"nice")) - { - if( 0 == strcmp(mode,"avg1")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].nice1) - else if( 0 == strcmp(mode,"avg5")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].nice5) - else if( 0 == strcmp(mode,"avg15")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].nice15) - else return SYSINFO_RET_FAIL; + len = sizeof(ncpu); - } - else if( 0 == strcmp(type,"user")) - { - if( 0 == strcmp(mode,"avg1")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].user1) - else if( 0 == strcmp(mode,"avg5")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].user5) - else if( 0 == strcmp(mode,"avg15")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].user15) - else return SYSINFO_RET_FAIL; - } - else if( 0 == strcmp(type,"system")) - { - if( 0 == strcmp(mode,"avg1")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].system1) - else if( 0 == strcmp(mode,"avg5")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].system5) - else if( 0 == strcmp(mode,"avg15")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].system15) - else return SYSINFO_RET_FAIL; - } - else - { + if (0 != sysctl(mib, 2, &ncpu, &len, NULL, 0)) return SYSINFO_RET_FAIL; - } + + SET_UI64_RESULT(result, ncpu); return SYSINFO_RET_OK; +#else + return SYSINFO_RET_FAIL; +#endif /* HAVE_FUNCTION_SYSCTL_HW_NCPU */ } -/* AIX CPU info */ -#ifdef HAVE_KNLIST_H -static int getloadavg_kmem(double loadavg[], int nelem) +int SYSTEM_CPU_INTR(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { - struct nlist nl; - int kmem, i; - long avenrun[3]; +#if defined(HAVE_UVM_UVMEXP2) + /* NetBSD 3.1 i386; NetBSD 4.0 i386 */ + int mib[] = {CTL_VM, VM_UVMEXP2}; + size_t len; + struct uvmexp_sysctl v; - nl.n_name = "avenrun"; - nl.n_value = 0; + assert(result); - if(knlist(&nl, 1, sizeof(nl))) - { - return FAIL; - } - if((kmem = open("/dev/kmem", 0, 0)) <= 0) - { - return FAIL; - } + init_result(result); - if(pread(kmem, avenrun, sizeof(avenrun), nl.n_value) < - sizeof(avenrun)) - { - return FAIL; - } + len = sizeof(struct uvmexp_sysctl); - for(i=0;ivalue.ul)/256.0); - return SYSINFO_RET_OK; + return SYSINFO_RET_OK; #else -#ifdef HAVE_KNLIST_H - double loadavg[3]; + return SYSINFO_RET_FAIL; +#endif /* HAVE_UVM_UVMEXP2 */ +} + +int SYSTEM_CPU_UTIL(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) +{ + char cpuname[MAX_STRING_LEN], + type[MAX_STRING_LEN], + mode[MAX_STRING_LEN]; + int cpu_num; assert(result); - init_result(result); - - if(getloadavg_kmem(loadavg,3) == FAIL) - { - return SYSINFO_RET_FAIL; - } + init_result(result); - SET_DBL_RESULT(result, loadavg[0]); - return SYSINFO_RET_OK; -#else - assert(result); + if (num_param(param) > 3) + return SYSINFO_RET_FAIL; - init_result(result); - return SYSINFO_RET_FAIL; -#endif -#endif -#endif -#endif -#endif -} + if (0 != get_param(param, 1, cpuname, sizeof(cpuname))) + *cpuname = '\0'; -int SYSTEM_CPU_LOAD5(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) -{ -#ifdef HAVE_GETLOADAVG - double load[3]; + /* default parameter */ + if (*cpuname == '\0') + zbx_snprintf(cpuname, sizeof(cpuname), "all"); - assert(result); + if (0 == strcmp(cpuname, "all")) + cpu_num = 0; + else { + cpu_num = atoi(cpuname) + 1; - init_result(result); - - if(getloadavg(load, 3)) - { - SET_DBL_RESULT(result, load[1]); - return SYSINFO_RET_OK; - } - else - { - return SYSINFO_RET_FAIL; + if (cpu_num < 1 || cpu_num > collector->cpus.count) + return SYSINFO_RET_FAIL; } -#else -#ifdef HAVE_SYS_PSTAT_H - struct pst_dynamic dyn; - assert(result); + if (0 != get_param(param, 2, type, sizeof(type))) + *type = '\0'; - init_result(result); - - if (pstat_getdynamic(&dyn, sizeof(dyn), 1, 0) == -1) - { - return SYSINFO_RET_FAIL; - } - else - { - SET_DBL_RESULT(result, dyn.psd_avg_5_min); + /* default parameter */ + if (*type == '\0') + zbx_snprintf(type, sizeof(type), "user"); + + if (0 != get_param(param, 3, mode, sizeof(mode))) + *mode = '\0'; + + /* default parameter */ + if (*mode == '\0') + zbx_snprintf(mode, sizeof(mode), "avg1"); + + if (!CPU_COLLECTOR_STARTED(collector)) { + SET_MSG_RESULT(result, strdup("Collector is not started!")); return SYSINFO_RET_OK; } -#else -#ifdef HAVE_PROC_LOADAVG - return getPROC("/proc/loadavg",1,2, flags, result); -#else -#ifdef HAVE_KSTAT_H - static kstat_ctl_t *kc = NULL; - kstat_t *ks; - kstat_named_t *kn; - - assert(result); - init_result(result); - - if (!kc && !(kc = kstat_open())) + if (0 == strcmp(type, "idle")) { + if (0 == strcmp(mode, "avg1")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].idle1) + else if (0 == strcmp(mode, "avg5")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].idle5) + else if (0 == strcmp(mode, "avg15")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].idle15) + else return SYSINFO_RET_FAIL; + } + else if (0 == strcmp(type, "nice")) { - return SYSINFO_RET_FAIL; + if (0 == strcmp(mode, "avg1")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].nice1) + else if (0 == strcmp(mode, "avg5")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].nice5) + else if (0 == strcmp(mode, "avg15")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].nice15) + else return SYSINFO_RET_FAIL; } - if (!(ks = kstat_lookup(kc, "unix", 0, "system_misc")) || - kstat_read(kc, ks, 0) == -1 || - !(kn = kstat_data_lookup(ks,"avenrun_5min"))) + else if (0 == strcmp(type, "user")) { - return SYSINFO_RET_FAIL; + if (0 == strcmp(mode, "avg1")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].user1) + else if (0 == strcmp(mode, "avg5")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].user5) + else if (0 == strcmp(mode, "avg15")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].user15) + else return SYSINFO_RET_FAIL; } - SET_DBL_RESULT(result, ((double)kn->value.ul)/256.0); - return SYSINFO_RET_OK; -#else -#ifdef HAVE_KNLIST_H - double loadavg[3]; - - assert(result); - - init_result(result); - - if(getloadavg_kmem(loadavg,3) == FAIL) + else if (0 == strcmp(type, "system")) { - return STSINFO_RET_FAIL; + if (0 == strcmp(mode, "avg1")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].system1) + else if (0 == strcmp(mode, "avg5")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].system5) + else if (0 == strcmp(mode, "avg15")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].system15) + else return SYSINFO_RET_FAIL; } + else + return SYSINFO_RET_FAIL; - SET_DBL_RESULT(result, loadavg[1]); return SYSINFO_RET_OK; -#else - assert(result); - - init_result(result); - return SYSINFO_RET_FAIL; -#endif -#endif -#endif -#endif -#endif } -int SYSTEM_CPU_LOAD15(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) +static int get_cpuload(double *load1, double *load5, double *load15) { #ifdef HAVE_GETLOADAVG + /* NetBSD 3.1 i386; NetBSD 4.0 i386 */ double load[3]; - assert(result); + if (-1 == getloadavg(load, 3)) + return SYSINFO_RET_FAIL; - init_result(result); + if (load1) + *load1 = load[0]; + if (load5) + *load5 = load[1]; + if (load15) + *load15 = load[2]; - if(getloadavg(load, 3)) - { - SET_DBL_RESULT(result, load[2]); - return SYSINFO_RET_OK; - } - else - { - return SYSINFO_RET_FAIL; - } + return SYSINFO_RET_OK; #else -#ifdef HAVE_SYS_PSTAT_H - struct pst_dynamic dyn; + return SYSINFO_RET_FAIL; +#endif /* HAVE_GETLOADAVG */ +} - assert(result); +static int SYSTEM_CPU_LOAD1(AGENT_RESULT *result) +{ + double value; - init_result(result); - - if (pstat_getdynamic(&dyn, sizeof(dyn), 1, 0) == -1) - { + if (SYSINFO_RET_OK != get_cpuload(&value, NULL, NULL)) return SYSINFO_RET_FAIL; - } - else - { - SET_DBL_RESULT(result, dyn.psd_avg_15_min); - return SYSINFO_RET_OK; - } -#else -#ifdef HAVE_PROC_LOADAVG - return getPROC("/proc/loadavg",1,3, flags, result); -#else -#ifdef HAVE_KSTAT_H - static kstat_ctl_t *kc = NULL; - kstat_t *ks; - kstat_named_t *kn; + + SET_DBL_RESULT(result, value); + + return SYSINFO_RET_OK; +} - assert(result); +static int SYSTEM_CPU_LOAD5(AGENT_RESULT *result) +{ + double value; - init_result(result); - - if (!kc && !(kc = kstat_open())) - { - return SYSINFO_RET_FAIL; - } - if (!(ks = kstat_lookup(kc, "unix", 0, "system_misc")) || - kstat_read(kc, ks, 0) == -1 || - !(kn = kstat_data_lookup(ks,"avenrun_15min"))) - { + if (SYSINFO_RET_OK != get_cpuload(NULL, &value, NULL)) return SYSINFO_RET_FAIL; - } - SET_DBL_RESULT(result, ((double)kn->value.ul)/256.0); + + SET_DBL_RESULT(result, value); + return SYSINFO_RET_OK; -#else -#ifdef HAVE_KNLIST_H - double loadavg[3]; +} - assert(result); +static int SYSTEM_CPU_LOAD15(AGENT_RESULT *result) +{ + double value; - init_result(result); + if (SYSINFO_RET_OK != get_cpuload(NULL, NULL, &value)) + return SYSINFO_RET_FAIL; - if(getloadavg_kmem(loadavg,3) == FAIL) - { - return STSINFO_RET_FAIL; - } - - SET_DBL_RESULT(result, loadavg[2]); + SET_DBL_RESULT(result, value); + return SYSINFO_RET_OK; -#else - assert(result); - - init_result(result); - return SYSINFO_RET_FAIL; -#endif -#endif -#endif -#endif -#endif } int SYSTEM_CPU_LOAD(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { - #define CPU_FNCLIST struct cpu_fnclist_s CPU_FNCLIST { - char *mode; - int (*function)(); + char *mode; + int (*function)(); }; CPU_FNCLIST fl[] = { - {"avg1" , SYSTEM_CPU_LOAD1}, - {"avg5" , SYSTEM_CPU_LOAD5}, + {"avg1", SYSTEM_CPU_LOAD1}, + {"avg5", SYSTEM_CPU_LOAD5}, {"avg15", SYSTEM_CPU_LOAD15}, {0, 0} }; - char cpuname[MAX_STRING_LEN]; - char mode[MAX_STRING_LEN]; - int i; - - assert(result); + char cpuname[MAX_STRING_LEN], + mode[MAX_STRING_LEN]; + int i; - init_result(result); - - if(num_param(param) > 2) - { - return SYSINFO_RET_FAIL; - } - - if(get_param(param, 1, cpuname, sizeof(cpuname)) != 0) - { - return SYSINFO_RET_FAIL; - } - if(cpuname[0] == '\0') - { - /* default parameter */ + assert(result); + + init_result(result); + + if (num_param(param) > 2) + return SYSINFO_RET_FAIL; + + if (0 != get_param(param, 1, cpuname, sizeof(cpuname))) + *cpuname = '\0'; + + /* default parameter */ + if (*cpuname == '\0') zbx_snprintf(cpuname, sizeof(cpuname), "all"); - } - if(strncmp(cpuname, "all", sizeof(cpuname))) - { + + if (0 != strncmp(cpuname, "all", sizeof(cpuname))) return SYSINFO_RET_FAIL; - } - - if(get_param(param, 2, mode, sizeof(mode)) != 0) - { - mode[0] = '\0'; - } - if(mode[0] == '\0') - { - /* default parameter */ - zbx_snprintf(mode, sizeof(mode), "avg1"); - } - for(i=0; fl[i].mode!=0; i++) - { - if(strncmp(mode, fl[i].mode, MAX_STRING_LEN)==0) - { - return (fl[i].function)(cmd, param, flags, result); - } - } - - return SYSINFO_RET_FAIL; -} -int SYSTEM_CPU_SWITCHES(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) -{ - assert(result); + if (0 != get_param(param, 2, mode, sizeof(mode))) + *mode = '\0'; - init_result(result); - - return SYSINFO_RET_FAIL; -} + /* default parameter */ + if (*mode == '\0') + zbx_snprintf(mode, sizeof(mode), "avg1"); -int SYSTEM_CPU_INTR(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) -{ - assert(result); + for (i = 0; fl[i].mode != 0; i++) + if (0 == strncmp(mode, fl[i].mode, MAX_STRING_LEN)) + return (fl[i].function)(result); - init_result(result); - return SYSINFO_RET_FAIL; } - diff --git a/src/libs/zbxsysinfo/netbsd/diskspace.c b/src/libs/zbxsysinfo/netbsd/diskspace.c index 3eda44e1..0ecdfadc 100644 --- a/src/libs/zbxsysinfo/netbsd/diskspace.c +++ b/src/libs/zbxsysinfo/netbsd/diskspace.c @@ -21,219 +21,155 @@ #include "sysinfo.h" -int get_fs_size_stat(char *fs, double *total, double *free, double *usage) +int get_fs_size_stat(const char *fs, zbx_uint64_t *total, zbx_uint64_t *free, zbx_uint64_t *used, double *pfree, double *pused) { #ifdef HAVE_SYS_STATVFS_H - struct statvfs s; + struct statvfs s; #else - struct statfs s; + struct statfs s; #endif - assert(fs); + assert(fs); #ifdef HAVE_SYS_STATVFS_H - if ( statvfs( fs, &s) != 0 ) + if (0 != statvfs(fs, &s)) #else - if ( statfs( fs, &s) != 0 ) + if (0 != statfs(fs, &s)) #endif - { - return SYSINFO_RET_FAIL; - } + { + return SYSINFO_RET_FAIL; + } #ifdef HAVE_SYS_STATVFS_H - if(total) - (*total) = (double)(s.f_blocks * (s.f_frsize / 1024.0)); - if(free) - (*free) = (double)(s.f_bavail * (s.f_frsize / 1024.0)); - if(usage) - (*usage) = (double)((s.f_blocks - s.f_bavail) * (s.f_frsize / 1024.0)); + if (total) + *total = (zbx_uint64_t)s.f_blocks * s.f_frsize; + if (free) + *free = (zbx_uint64_t)s.f_bfree * s.f_frsize; + if (used) + *used = (zbx_uint64_t)(s.f_blocks - s.f_bfree) * s.f_frsize; + if (pfree) + *pfree = (double)(100.0 * s.f_bfree) / s.f_blocks; + if (pused) + *pused = (double)(100.0 * (s.f_blocks - s.f_bfree)) / s.f_blocks; #else - if(total) - (*total) = (double)(s.f_blocks * (s.f_bsize / 1024.0)); - if(free) - (*free) = (double)(s.f_bfree * (s.f_bsize / 1024.0)); - if(usage) - (*usage) = (double)((s.f_blocks - s.f_bfree) * (s.f_bsize / 1024.0)); + if (total) + *total = (zbx_uint64_t)s.f_blocks * s.f_bsize; + if (free) + *free = (zbx_uint64_t)s.f_bfree * s.f_bsize; + if (used) + *used = (zbx_uint64_t)(s.f_blocks - s.f_bfree) * s.f_bsize; + if (pfree) + *pfree = (double)(100.0 * s.f_bfree) / s.f_blocks; + if (pused) + *pused = (double)(100.0 * (s.f_blocks - s.f_bfree)) / s.f_blocks; #endif - return SYSINFO_RET_OK; + return SYSINFO_RET_OK; } -static int VFS_FS_USED(const char *cmd, char *param, unsigned flags, AGENT_RESULT *result) +static int VFS_FS_USED(const char *fs, AGENT_RESULT *result) { -/* char mountPoint[MAX_STRING_LEN];*/ - double value = 0; - - assert(result); - - init_result(result); - -/* if(num_param(param) > 1) - return SYSINFO_RET_FAIL; + zbx_uint64_t value = 0; - if(get_param(param, 1, mountPoint, MAX_STRING_LEN) != 0) - return SYSINFO_RET_FAIL;*/ + if (SYSINFO_RET_OK != get_fs_size_stat(fs, NULL, NULL, &value, NULL, NULL)) + return SYSINFO_RET_FAIL; - if(get_fs_size_stat(param, NULL, NULL, &value) != SYSINFO_RET_OK) - return SYSINFO_RET_FAIL; + SET_UI64_RESULT(result, value); - SET_UI64_RESULT(result, value); - - return SYSINFO_RET_OK; + return SYSINFO_RET_OK; } -static int VFS_FS_FREE(const char *cmd, char *param, unsigned flags, AGENT_RESULT *result) +static int VFS_FS_FREE(const char *fs, AGENT_RESULT *result) { -/* char mountPoint[MAX_STRING_LEN];*/ - double value = 0; - - assert(result); - - init_result(result); - -/* if(num_param(param) > 1) - return SYSINFO_RET_FAIL; + zbx_uint64_t value = 0; - if(get_param(param, 1, mountPoint, MAX_STRING_LEN) != 0) - return SYSINFO_RET_FAIL;*/ + if (SYSINFO_RET_OK != get_fs_size_stat(fs, NULL, &value, NULL, NULL, NULL)) + return SYSINFO_RET_FAIL; - if(get_fs_size_stat(param, NULL, &value, NULL) != SYSINFO_RET_OK) - return SYSINFO_RET_FAIL; + SET_UI64_RESULT(result, value); - SET_UI64_RESULT(result, value); - - return SYSINFO_RET_OK; + return SYSINFO_RET_OK; } -static int VFS_FS_TOTAL(const char *cmd, char *param, unsigned flags, AGENT_RESULT *result) +static int VFS_FS_TOTAL(const char *fs, AGENT_RESULT *result) { -/* char mountPoint[MAX_STRING_LEN];*/ - double value = 0; - - assert(result); - - init_result(result); - -/* if(num_param(param) > 1) - return SYSINFO_RET_FAIL; - - if(get_param(param, 1, mountPoint, MAX_STRING_LEN) != 0) - { - return SYSINFO_RET_FAIL; - }*/ + zbx_uint64_t value = 0; - if(get_fs_size_stat(param, &value, NULL, NULL) != SYSINFO_RET_OK) - return SYSINFO_RET_FAIL; + if (SYSINFO_RET_OK != get_fs_size_stat(fs, &value, NULL, NULL, NULL, NULL)) + return SYSINFO_RET_FAIL; - SET_UI64_RESULT(result, value); + SET_UI64_RESULT(result, value); - return SYSINFO_RET_OK; + return SYSINFO_RET_OK; } -static int VFS_FS_PFREE(const char *cmd, char *param, unsigned flags, AGENT_RESULT *result) +static int VFS_FS_PFREE(const char *fs, AGENT_RESULT *result) { -/* char mountPoint[MAX_STRING_LEN];*/ - double tot_val = 0; - double free_val = 0; - - assert(result); - - init_result(result); - -/* if(num_param(param) > 1) - return SYSINFO_RET_FAIL; - - if(get_param(param, 1, mountPoint, MAX_STRING_LEN) != 0) - return SYSINFO_RET_FAIL;*/ + double value = 0; - if(get_fs_size_stat(param, &tot_val, &free_val, NULL) != SYSINFO_RET_OK) - return SYSINFO_RET_FAIL; + if (SYSINFO_RET_OK != get_fs_size_stat(fs, NULL, NULL, NULL, &value, NULL)) + return SYSINFO_RET_FAIL; - SET_DBL_RESULT(result, (100.0 * free_val) / tot_val); + SET_DBL_RESULT(result, value); - return SYSINFO_RET_OK; + return SYSINFO_RET_OK; } -static int VFS_FS_PUSED(const char *cmd, char *param, unsigned flags, AGENT_RESULT *result) +static int VFS_FS_PUSED(const char *fs, AGENT_RESULT *result) { -/* char mountPoint[MAX_STRING_LEN];*/ - double tot_val = 0; - double usg_val = 0; + double value = 0; - assert(result); + if (SYSINFO_RET_OK != get_fs_size_stat(fs, NULL, NULL, NULL, NULL, &value)) + return SYSINFO_RET_FAIL; - init_result(result); + SET_DBL_RESULT(result, value); -/* if(num_param(param) > 1) - return SYSINFO_RET_FAIL; - - if(get_param(param, 1, mountPoint, MAX_STRING_LEN) != 0) - return SYSINFO_RET_FAIL;*/ - - if(get_fs_size_stat(param, &tot_val, NULL, &usg_val) != SYSINFO_RET_OK) - return SYSINFO_RET_FAIL; - - SET_DBL_RESULT(result, (100.0 * usg_val) / tot_val); - - return SYSINFO_RET_OK; + return SYSINFO_RET_OK; } int VFS_FS_SIZE(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { - #define FS_FNCLIST struct fs_fnclist_s FS_FNCLIST { - char *mode; - int (*function)(); + char *mode; + int (*function)(); }; - FS_FNCLIST fl[] = + FS_FNCLIST fl[] = { - {"free" , VFS_FS_FREE}, - {"total" , VFS_FS_TOTAL}, + {"free", VFS_FS_FREE}, + {"total", VFS_FS_TOTAL}, {"used", VFS_FS_USED}, - {"pfree" , VFS_FS_PFREE}, - {"pused" , VFS_FS_PUSED}, + {"pfree", VFS_FS_PFREE}, + {"pused", VFS_FS_PUSED}, {0, 0} }; - char fsname[MAX_STRING_LEN]; - char mode[MAX_STRING_LEN]; - int i; - - assert(result); - - init_result(result); - - if(num_param(param) > 2) - { - return SYSINFO_RET_FAIL; - } - - if(get_param(param, 1, fsname, sizeof(mode)) != 0) - { - return SYSINFO_RET_FAIL; - } - - if(get_param(param, 2, mode, sizeof(mode)) != 0) - { - mode[0] = '\0'; - } - if(mode[0] == '\0') - { - /* default parameter */ + char fsname[MAX_STRING_LEN]; + char mode[MAX_STRING_LEN]; + int i; + + assert(result); + + init_result(result); + + if (num_param(param) > 2) + return SYSINFO_RET_FAIL; + + if (0 != get_param(param, 1, fsname, sizeof(fsname))) + return SYSINFO_RET_FAIL; + + if (0 != get_param(param, 2, mode, sizeof(mode))) + *mode = '\0'; + + /* default parameter */ + if (*mode == '\0') zbx_snprintf(mode, sizeof(mode), "total"); - } - - for(i=0; fl[i].mode!=0; i++) - { - if(strncmp(mode, fl[i].mode, MAX_STRING_LEN)==0) - { - return (fl[i].function)(cmd, fsname, flags, result); - } - } - + + for (i = 0; fl[i].mode != 0; i++) + if (0 == strncmp(mode, fl[i].mode, MAX_STRING_LEN)) + return (fl[i].function)(fsname, result); + return SYSINFO_RET_FAIL; } - diff --git a/src/libs/zbxsysinfo/netbsd/inodes.c b/src/libs/zbxsysinfo/netbsd/inodes.c index af1f6d44..9f240b08 100644 --- a/src/libs/zbxsysinfo/netbsd/inodes.c +++ b/src/libs/zbxsysinfo/netbsd/inodes.c @@ -23,201 +23,154 @@ #include "md5.h" -static int get_fs_inodes_stat(char *fs, double *total, double *free, double *usage) +static int get_fs_inodes_stat(const char *fs, zbx_uint64_t *total, zbx_uint64_t *free, zbx_uint64_t *used, double *pfree, double *pused) { - struct statfs s; - +#ifdef HAVE_SYS_STATVFS_H + struct statvfs s; +#else + struct statfs s; +#endif + assert(fs); - - if ( statfs( fs, &s) != 0 ) + +#ifdef HAVE_SYS_STATVFS_H + if (0 != statvfs(fs, &s)) +#else + if (0 != statfs(fs, &s)) +#endif { - return SYSINFO_RET_FAIL; + return SYSINFO_RET_FAIL; } - - if(total) - (*total) = (double)(s.f_files); - if(free) - (*free) = (double)(s.f_ffree); - if(usage) - (*usage) = (double)(s.f_files - s.f_ffree); +#ifdef HAVE_SYS_STATVFS_H + if (total) + *total = (zbx_uint64_t)s.f_files; + if (free) + *free = (zbx_uint64_t)s.f_ffree; + if (used) + *used = (zbx_uint64_t)(s.f_files - s.f_ffree); + if (pfree) + *pfree = (double)(100.0 * s.f_ffree) / s.f_files; + if (pused) + *pused = (double)(100.0 * (s.f_files - s.f_ffree)) / s.f_files; +#else + if (total) + *total = (zbx_uint64_t)s.f_files; + if (free) + *free = (zbx_uint64_t)s.f_ffree; + if (used) + *used = (zbx_uint64_t)(s.f_files - s.f_ffree); + if (pfree) + *pfree = (double)(100.0 * s.f_ffree) / s.f_files; + if (pused) + *pused = (double)(100.0 * (s.f_files - s.f_ffree)) / s.f_files; +#endif return SYSINFO_RET_OK; } -static int VFS_FS_INODE_USED(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) +static int VFS_FS_INODE_USED(const char *fs, AGENT_RESULT *result) { - char mountPoint[MAX_STRING_LEN]; - double value = 0; + zbx_uint64_t value = 0; - assert(result); - - init_result(result); - - if(num_param(param) > 1) - return SYSINFO_RET_FAIL; - - if(get_param(param, 1, mountPoint, MAX_STRING_LEN) != 0) - return SYSINFO_RET_FAIL; - - if(get_fs_inodes_stat(mountPoint, NULL, NULL, &value) != SYSINFO_RET_OK) - return SYSINFO_RET_FAIL; + if (SYSINFO_RET_OK != get_fs_inodes_stat(fs, NULL, NULL, &value, NULL, NULL)) + return SYSINFO_RET_FAIL; SET_UI64_RESULT(result, value); return SYSINFO_RET_OK; } -static int VFS_FS_INODE_FREE(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) +static int VFS_FS_INODE_FREE(const char *fs, AGENT_RESULT *result) { - char mountPoint[MAX_STRING_LEN]; - double value = 0; + zbx_uint64_t value = 0; - assert(result); - - init_result(result); - - if(num_param(param) > 1) - return SYSINFO_RET_FAIL; - - if(get_param(param, 1, mountPoint, MAX_STRING_LEN) != 0) - return SYSINFO_RET_FAIL; - - if(get_fs_inodes_stat(mountPoint, NULL, &value, NULL) != SYSINFO_RET_OK) - return SYSINFO_RET_FAIL; + if (SYSINFO_RET_OK != get_fs_inodes_stat(fs, NULL, &value, NULL, NULL, NULL)) + return SYSINFO_RET_FAIL; SET_UI64_RESULT(result, value); return SYSINFO_RET_OK; } -static int VFS_FS_INODE_TOTAL(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) +static int VFS_FS_INODE_TOTAL(const char *fs, AGENT_RESULT *result) { - char mountPoint[MAX_STRING_LEN]; - double value = 0; + zbx_uint64_t value = 0; - assert(result); - - init_result(result); - - if(num_param(param) > 1) - return SYSINFO_RET_FAIL; - - if(get_param(param, 1, mountPoint, MAX_STRING_LEN) != 0) - { - return SYSINFO_RET_FAIL; - } - - if(get_fs_inodes_stat(mountPoint, &value, NULL, NULL) != SYSINFO_RET_OK) - return SYSINFO_RET_FAIL; + if (SYSINFO_RET_OK != get_fs_inodes_stat(fs, &value, NULL, NULL, NULL, NULL)) + return SYSINFO_RET_FAIL; SET_UI64_RESULT(result, value); return SYSINFO_RET_OK; } -static int VFS_FS_INODE_PFREE(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) +static int VFS_FS_INODE_PFREE(const char *fs, AGENT_RESULT *result) { - char mountPoint[MAX_STRING_LEN]; - double tot_val = 0; - double free_val = 0; + double value = 0; - assert(result); - - init_result(result); - - if(num_param(param) > 1) - return SYSINFO_RET_FAIL; - - if(get_param(param, 1, mountPoint, MAX_STRING_LEN) != 0) - return SYSINFO_RET_FAIL; - - if(get_fs_inodes_stat(mountPoint, &tot_val, &free_val, NULL) != SYSINFO_RET_OK) - return SYSINFO_RET_FAIL; + if (SYSINFO_RET_OK != get_fs_inodes_stat(fs, NULL, NULL, NULL, &value, NULL)) + return SYSINFO_RET_FAIL; - SET_DBL_RESULT(result, (100.0 * free_val) / tot_val); + SET_DBL_RESULT(result, value); return SYSINFO_RET_OK; } -static int VFS_FS_INODE_PUSED(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) +static int VFS_FS_INODE_PUSED(const char *fs, AGENT_RESULT *result) { - char mountPoint[MAX_STRING_LEN]; - double tot_val = 0; - double usg_val = 0; + double value = 0; - assert(result); - - init_result(result); - - if(num_param(param) > 1) - return SYSINFO_RET_FAIL; - - if(get_param(param, 1, mountPoint, MAX_STRING_LEN) != 0) - return SYSINFO_RET_FAIL; - - if(get_fs_inodes_stat(mountPoint, &tot_val, NULL, &usg_val) != SYSINFO_RET_OK) - return SYSINFO_RET_FAIL; + if (SYSINFO_RET_OK != get_fs_inodes_stat(fs, NULL, NULL, NULL, NULL, &value)) + return SYSINFO_RET_FAIL; - SET_DBL_RESULT(result, (100.0 * usg_val) / tot_val); + SET_DBL_RESULT(result, value); return SYSINFO_RET_OK; } int VFS_FS_INODE(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { - #define FS_FNCLIST struct fs_fnclist_s FS_FNCLIST { - char *mode; - int (*function)(); + char *mode; + int (*function)(); }; FS_FNCLIST fl[] = { - {"free" , VFS_FS_INODE_FREE}, - {"total" , VFS_FS_INODE_TOTAL}, + {"free", VFS_FS_INODE_FREE}, + {"total", VFS_FS_INODE_TOTAL}, {"used", VFS_FS_INODE_USED}, - {"pfree" , VFS_FS_INODE_PFREE}, - {"pused" , VFS_FS_INODE_PUSED}, + {"pfree", VFS_FS_INODE_PFREE}, + {"pused", VFS_FS_INODE_PUSED}, {0, 0} }; - char fsname[MAX_STRING_LEN]; - char mode[MAX_STRING_LEN]; - int i; + char fsname[MAX_STRING_LEN]; + char mode[MAX_STRING_LEN]; + int i; - assert(result); + assert(result); - init_result(result); + init_result(result); - if(num_param(param) > 2) - { - return SYSINFO_RET_FAIL; - } - - if(get_param(param, 1, fsname, sizeof(fsname)) != 0) - { - return SYSINFO_RET_FAIL; - } + if (num_param(param) > 2) + return SYSINFO_RET_FAIL; + + if (0 != get_param(param, 1, fsname, sizeof(mode))) + return SYSINFO_RET_FAIL; - if(get_param(param, 2, mode, sizeof(mode)) != 0) - { - mode[0] = '\0'; - } - if(mode[0] == '\0') - { - /* default parameter */ + if (0 != get_param(param, 2, mode, sizeof(mode))) + *mode = '\0'; + + /* default parameter */ + if (*mode == '\0') zbx_snprintf(mode, sizeof(mode), "total"); - } - for(i=0; fl[i].mode!=0; i++) - { - if(strncmp(mode, fl[i].mode, MAX_STRING_LEN)==0) - { - return (fl[i].function)(cmd, fsname, flags, result); - } - } + for (i = 0; fl[i].mode != 0; i++) + if (0 == strncmp(mode, fl[i].mode, MAX_STRING_LEN)) + return (fl[i].function)(fsname, result); return SYSINFO_RET_FAIL; } diff --git a/src/libs/zbxsysinfo/netbsd/kernel.c b/src/libs/zbxsysinfo/netbsd/kernel.c index e06c1d5e..245e76a4 100644 --- a/src/libs/zbxsysinfo/netbsd/kernel.c +++ b/src/libs/zbxsysinfo/netbsd/kernel.c @@ -24,24 +24,24 @@ int KERNEL_MAXFILES(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { #ifdef HAVE_FUNCTION_SYSCTL_KERN_MAXFILES - int mib[2],len; + int mib[2]; + size_t len; int maxfiles; assert(result); - init_result(result); + init_result(result); - mib[0]=CTL_KERN; - mib[1]=KERN_MAXFILES; + mib[0] = CTL_KERN; + mib[1] = KERN_MAXFILES; - len=sizeof(maxfiles); + len = sizeof(maxfiles); - if(sysctl(mib,2,&maxfiles,(size_t *)&len,NULL,0) != 0) - { - return SYSINFO_RET_FAIL; - } + if (0 != sysctl(mib, 2, &maxfiles, &len, NULL, 0)) + return SYSINFO_RET_FAIL; + + SET_UI64_RESULT(result, maxfiles); - SET_UI64_RESULT(result, maxfiles); return SYSINFO_RET_OK; #else return SYSINFO_RET_FAIL; @@ -51,64 +51,26 @@ int KERNEL_MAXFILES(const char *cmd, const char *param, unsigned flags, AGENT_RE int KERNEL_MAXPROC(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { #ifdef HAVE_FUNCTION_SYSCTL_KERN_MAXPROC - int mib[2],len; + int mib[2]; + size_t len; int maxproc; assert(result); - init_result(result); + init_result(result); - mib[0]=CTL_KERN; - mib[1]=KERN_MAXPROC; + mib[0] = CTL_KERN; + mib[1] = KERN_MAXPROC; - len=sizeof(maxproc); + len = sizeof(maxproc); - if(sysctl(mib,2,&maxproc,(size_t *)&len,NULL,0) != 0) - { - return SYSINFO_RET_FAIL; -/* printf("Errno [%m]");*/ - } + if (0 != sysctl(mib, 2, &maxproc, &len, NULL, 0)) + return SYSINFO_RET_FAIL; + + SET_UI64_RESULT(result, maxproc); - - SET_UI64_RESULT(result, maxproc); return SYSINFO_RET_OK; #else return SYSINFO_RET_FAIL; #endif } - -int OLD_KERNEL(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) -{ - char key[MAX_STRING_LEN]; - int ret; - - assert(result); - - init_result(result); - - if(num_param(param) > 1) - { - return SYSINFO_RET_FAIL; - } - - if(get_param(param, 1, key, MAX_STRING_LEN) != 0) - { - return SYSINFO_RET_FAIL; - } - - if(strcmp(key,"maxfiles") == 0) - { - ret = KERNEL_MAXFILES(cmd, param, flags, result); - } - else if(strcmp(key,"maxproc") == 0) - { - ret = KERNEL_MAXPROC(cmd, param, flags, result); - } - else - { - ret = SYSINFO_RET_FAIL; - } - - return ret; -} - diff --git a/src/libs/zbxsysinfo/netbsd/memory.c b/src/libs/zbxsysinfo/netbsd/memory.c index d6f9e941..4d60f35e 100644 --- a/src/libs/zbxsysinfo/netbsd/memory.c +++ b/src/libs/zbxsysinfo/netbsd/memory.c @@ -21,338 +21,152 @@ #include "sysinfo.h" -static int VM_MEMORY_CACHED(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) +static int get_vm_stat(zbx_uint64_t *total, zbx_uint64_t *free, zbx_uint64_t *used, double *pfree, double *pused, zbx_uint64_t *cached) { -#ifdef HAVE_PROC - FILE *f; - char *t; - char c[MAX_STRING_LEN]; - zbx_uint64_t res = 0; - - assert(result); - - init_result(result); - - if( NULL == (f = fopen("/proc/meminfo","r") )) - { - return SYSINFO_RET_FAIL; - } - while(NULL!=fgets(c,MAX_STRING_LEN,f)) - { - if(strncmp(c,"Cached:",7) == 0) - { - t=(char *)strtok(c," "); - t=(char *)strtok(NULL," "); - sscanf(t, ZBX_FS_UI64, &res ); - t=(char *)strtok(NULL," "); - - if(strcasecmp(t,"kb")) res <<= 10; - else if(strcasecmp(t, "mb")) res <<= 20; - else if(strcasecmp(t, "gb")) res <<= 30; - else if(strcasecmp(t, "tb")) res <<= 40; - - break; - } - } - zbx_fclose(f); - - SET_UI64_RESULT(result, res); - return SYSINFO_RET_OK; -#else - assert(result); - - init_result(result); - - return SYSINFO_RET_FAIL; -#endif -} +#if defined(HAVE_UVM_UVMEXP2) + /* NetBSD 3.1 i386; NetBSD 4.0 i386 */ + int mib[] = {CTL_VM, VM_UVMEXP2}; + size_t len; + struct uvmexp_sysctl v; -static int VM_MEMORY_BUFFERS(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) -{ -#ifdef HAVE_SYSINFO_BUFFERRAM - struct sysinfo info; + len = sizeof(struct uvmexp_sysctl); - assert(result); - - init_result(result); - - if( 0 == sysinfo(&info)) - { -#ifdef HAVE_SYSINFO_MEM_UNIT - SET_UI64_RESULT(result, (zbx_uint64_t)info.bufferram * (zbx_uint64_t)info.mem_unit); -#else - SET_UI64_RESULT(result, info.bufferram); -#endif - return SYSINFO_RET_OK; - } - else - { + if (0 != sysctl(mib, 2, &v, &len, NULL, 0)) return SYSINFO_RET_FAIL; - } -#else - assert(result); - init_result(result); - + if (total) + *total = v.npages << v.pageshift; + if (free) + *free = v.free << v.pageshift; + if (used) + *used = (v.npages << v.pageshift) - (v.free << v.pageshift); + if (pfree) + *pfree = (double)(100.0 * v.free) / v.npages; + if (pused) + *pused = (double)(100.0 * (v.npages - v.free)) / v.npages; + if (cached) + *cached = (v.filepages << v.pageshift) + (v.execpages << v.pageshift); + + return SYSINFO_RET_OK; +#else return SYSINFO_RET_FAIL; -#endif +#endif /* HAVE_UVM_UVMEXP2 */ } -static int VM_MEMORY_SHARED(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) +static int VM_MEMORY_TOTAL(AGENT_RESULT *result) { -#ifdef HAVE_SYSINFO_SHAREDRAM - struct sysinfo info; - - assert(result); + zbx_uint64_t value = 0; - init_result(result); - - if( 0 == sysinfo(&info)) - { -#ifdef HAVE_SYSINFO_MEM_UNIT - SET_UI64_RESULT(result, (zbx_uint64_t)info.sharedram * (zbx_uint64_t)info.mem_unit); -#else - SET_UI64_RESULT(result, info.sharedram); -#endif - return SYSINFO_RET_OK; - } - else - { + if (SYSINFO_RET_OK != get_vm_stat(&value, NULL, NULL, NULL, NULL, NULL)) return SYSINFO_RET_FAIL; - } -#elif defined(HAVE_SYS_VMMETER_VMTOTAL) - int mib[2],len; - struct vmtotal v; - - assert(result); - - init_result(result); + + SET_UI64_RESULT(result, value); - len=sizeof(struct vmtotal); - mib[0]=CTL_VM; - mib[1]=VM_METER; - - sysctl(mib,2,&v,&len,NULL,0); - - SET_UI64_RESULT(result, v.t_armshr<<2); return SYSINFO_RET_OK; -#else - return SYSINFO_RET_FAIL; -#endif } -static int VM_MEMORY_TOTAL(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) +static int VM_MEMORY_FREE(AGENT_RESULT *result) { -#if defined(HAVE_SYS_PSTAT_H) - struct pst_static pst; - long page; - - assert(result); + zbx_uint64_t value = 0; - init_result(result); - - if(pstat_getstatic(&pst, sizeof(pst), (size_t)1, 0) == -1) - { + if (SYSINFO_RET_OK != get_vm_stat(NULL, &value, NULL, NULL, NULL, NULL)) return SYSINFO_RET_FAIL; - } - else - { - /* Get page size */ - page = pst.page_size; - /* Total physical memory in bytes */ - SET_UI64_RESULT(result, (zbx_uint64_t)page*(zbx_uint64_t)pst.physical_memory); - return SYSINFO_RET_OK; - } -#elif defined(HAVE_SYSINFO_TOTALRAM) - struct sysinfo info; - - assert(result); - - init_result(result); + + SET_UI64_RESULT(result, value); - if( 0 == sysinfo(&info)) - { -#ifdef HAVE_SYSINFO_MEM_UNIT - SET_UI64_RESULT(result, (zbx_uint64_t)info.totalram * (zbx_uint64_t)info.mem_unit); -#else - SET_UI64_RESULT(result, info.totalram); -#endif - return SYSINFO_RET_OK; - } - else - { - return SYSINFO_RET_FAIL; - } -#elif defined(HAVE_SYS_VMMETER_VMTOTAL) - int mib[2],len; - struct vmtotal v; + return SYSINFO_RET_OK; +} - assert(result); +static int VM_MEMORY_USED(AGENT_RESULT *result) +{ + zbx_uint64_t value = 0; - init_result(result); + if (SYSINFO_RET_OK != get_vm_stat(NULL, NULL, &value, NULL, NULL, NULL)) + return SYSINFO_RET_FAIL; + + SET_UI64_RESULT(result, value); - len=sizeof(struct vmtotal); - mib[0]=CTL_VM; - mib[1]=VM_METER; + return SYSINFO_RET_OK; +} - sysctl(mib,2,&v,&len,NULL,0); +static int VM_MEMORY_PFREE(AGENT_RESULT *result) +{ + double value = 0; - SET_UI64_RESULT(result, v.t_rm<<2); - return SYSINFO_RET_OK; -#elif defined(HAVE_SYS_SYSCTL_H) - static int mib[] = { CTL_HW, HW_PHYSMEM }; - size_t len; - unsigned int memory; - int ret; + if (SYSINFO_RET_OK != get_vm_stat(NULL, NULL, NULL, &value, NULL, NULL)) + return SYSINFO_RET_FAIL; - assert(result); - - init_result(result); + SET_DBL_RESULT(result, value); - len=sizeof(memory); + return SYSINFO_RET_OK; +} - if(0==sysctl(mib,2,&memory,&len,NULL,0)) - { - SET_UI64_RESULT(result, memory); - ret=SYSINFO_RET_OK; - } - else - { - ret=SYSINFO_RET_FAIL; - } - return ret; -#else - assert(result); +static int VM_MEMORY_PUSED(AGENT_RESULT *result) +{ + double value = 0; - init_result(result); + if (SYSINFO_RET_OK != get_vm_stat(NULL, NULL, NULL, NULL, &value, NULL)) + return SYSINFO_RET_FAIL; + + SET_DBL_RESULT(result, value); - return SYSINFO_RET_FAIL; -#endif + return SYSINFO_RET_OK; } -static int VM_MEMORY_FREE(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) +static int VM_MEMORY_BUFFERS(AGENT_RESULT *result) { -#if defined(HAVE_SYS_PSTAT_H) - struct pst_static pst; - struct pst_dynamic dyn; - long page; - - assert(result); - - init_result(result); - - if(pstat_getstatic(&pst, sizeof(pst), (size_t)1, 0) == -1) - { - return SYSINFO_RET_FAIL; - } - else - { - /* Get page size */ - page = pst.page_size; -/* return pst.physical_memory;*/ - - if (pstat_getdynamic(&dyn, sizeof(dyn), 1, 0) == -1) - { - return SYSINFO_RET_FAIL; - } - else - { -/* cout<<"total virtual memory allocated is " << dyn.psd_vm << " - pages, " << dyn.psd_vm * page << " bytes" << endl; - cout<<"active virtual memory is " << dyn.psd_avm <<" pages, " << - dyn.psd_avm * page << " bytes" << endl; - cout<<"total real memory is " << dyn.psd_rm << " pages, " << - dyn.psd_rm * page << " bytes" << endl; - cout<<"active real memory is " << dyn.psd_arm << " pages, " << - dyn.psd_arm * page << " bytes" << endl; - cout<<"free memory is " << dyn.psd_free << " pages, " << -*/ - /* Free memory in bytes */ - - SET_UI64_RESULT(result, (zbx_uint64_t)dyn.psd_free * (zbx_uint64_t)page); - return SYSINFO_RET_OK; - } - } -#elif defined(HAVE_SYSINFO_FREERAM) - struct sysinfo info; + /* NetBSD 3.1 i386; NetBSD 4.0 i386 */ + int mib[] = {CTL_VM, VM_NKMEMPAGES}, pages; + size_t len; + zbx_uint64_t value; - assert(result); + len = sizeof(pages); - init_result(result); - - if( 0 == sysinfo(&info)) - { -#ifdef HAVE_SYSINFO_MEM_UNIT - SET_UI64_RESULT(result, (zbx_uint64_t)info.freeram * (zbx_uint64_t)info.mem_unit); -#else - SET_UI64_RESULT(result, info.freeram); -#endif - return SYSINFO_RET_OK; - } - else - { + if (0 != sysctl(mib, 2, &pages, &len, NULL, 0)) return SYSINFO_RET_FAIL; - } -#elif defined(HAVE_SYS_VMMETER_VMTOTAL) - int mib[2],len; - struct vmtotal v; - - assert(result); - init_result(result); - - len=sizeof(struct vmtotal); - mib[0]=CTL_VM; - mib[1]=VM_METER; + value = (zbx_uint64_t)pages * sysconf(_SC_PAGESIZE); - sysctl(mib,2,&v,&len,NULL,0); + SET_UI64_RESULT(result, value); - SET_UI64_RESULT(result, v.t_free<<2); return SYSINFO_RET_OK; -/* OS/X */ -#elif defined(HAVE_MACH_HOST_INFO_H) - vm_statistics_data_t page_info; - vm_size_t pagesize; - mach_msg_type_number_t count; - kern_return_t kret; - int ret; - - assert(result); +} - init_result(result); - - pagesize = 0; - kret = host_page_size (mach_host_self(), &pagesize); +static int VM_MEMORY_SHARED(AGENT_RESULT *result) +{ +#if defined(HAVE_SYS_VMMETER_VMTOTAL) + /* NetBSD 3.1 i386; NetBSD 4.0 i386 */ + int mib[] = {CTL_VM, VM_METER}; + struct vmtotal v; + size_t len; + zbx_uint64_t value; - count = HOST_VM_INFO_COUNT; - kret = host_statistics (mach_host_self(), HOST_VM_INFO, - (host_info_t)&page_info, &count); - if (kret == KERN_SUCCESS) - { - double pw, pa, pi, pf, pu; + len = sizeof(v); - pw = (double)page_info.wire_count*pagesize; - pa = (double)page_info.active_count*pagesize; - pi = (double)page_info.inactive_count*pagesize; - pf = (double)page_info.free_count*pagesize; + if (0 != sysctl(mib, 2, &v, &len, NULL, 0)) + return SYSINFO_RET_FAIL; - pu = pw+pa+pi; + value = (zbx_uint64_t)(v.t_vmshr + v.t_rmshr) * sysconf(_SC_PAGESIZE); - SET_UI64_RESULT(result, pf); - ret = SYSINFO_RET_OK; - } - else - { - ret = SYSINFO_RET_FAIL; - } - return ret; + SET_UI64_RESULT(result, value); + + return SYSINFO_RET_OK; #else - assert(result); + return SYSINFO_RET_FAIL; +#endif /* HAVE_SYS_VMMETER_VMTOTAL */ +} - init_result(result); +static int VM_MEMORY_CACHED(AGENT_RESULT *result) +{ + /* NetBSD 3.1 i386; NetBSD 4.0 i386 */ + zbx_uint64_t value = 0; + + if (SYSINFO_RET_OK != get_vm_stat(NULL, NULL, NULL, NULL, NULL, &value)) + return SYSINFO_RET_FAIL; + + SET_UI64_RESULT(result, value); - return SYSINFO_RET_FAIL; -#endif + return SYSINFO_RET_OK; } int VM_MEMORY_SIZE(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) @@ -360,50 +174,43 @@ int VM_MEMORY_SIZE(const char *cmd, const char *param, unsigned flags, AGENT #define MEM_FNCLIST struct mem_fnclist_s MEM_FNCLIST { - char *mode; - int (*function)(); + char *mode; + int (*function)(); }; MEM_FNCLIST fl[] = { + {"total", VM_MEMORY_TOTAL}, {"free", VM_MEMORY_FREE}, + {"used", VM_MEMORY_USED}, + {"pfree", VM_MEMORY_PFREE}, + {"pused", VM_MEMORY_PUSED}, {"shared", VM_MEMORY_SHARED}, - {"total", VM_MEMORY_TOTAL}, {"buffers", VM_MEMORY_BUFFERS}, {"cached", VM_MEMORY_CACHED}, - {0, 0} + {0, 0} }; - char mode[MAX_STRING_LEN]; - int i; - assert(result); + char mode[MAX_STRING_LEN]; + int i; - init_result(result); + assert(result); - if(num_param(param) > 1) - { - return SYSINFO_RET_FAIL; - } + init_result(result); - if(get_param(param, 1, mode, MAX_STRING_LEN) != 0) - { - mode[0] = '\0'; - } + if (num_param(param) > 1) + return SYSINFO_RET_FAIL; - if(mode[0] == '\0') - { - /* default parameter */ + if (0 != get_param(param, 1, mode, sizeof(mode))) + *mode = '\0'; + + /* default parameter */ + if (*mode == '\0') zbx_snprintf(mode, sizeof(mode), "total"); - } - - for(i=0; fl[i].mode!=0; i++) - { - if(strncmp(mode, fl[i].mode, MAX_STRING_LEN)==0) - { - return (fl[i].function)(cmd, param, flags, result); - } - } - + + for(i = 0; fl[i].mode != 0; i++) + if (0 == strncmp(mode, fl[i].mode, MAX_STRING_LEN)) + return (fl[i].function)(result); + return SYSINFO_RET_FAIL; } - diff --git a/src/libs/zbxsysinfo/netbsd/net.c b/src/libs/zbxsysinfo/netbsd/net.c index 1925f27d..92493c22 100644 --- a/src/libs/zbxsysinfo/netbsd/net.c +++ b/src/libs/zbxsysinfo/netbsd/net.c @@ -21,50 +21,413 @@ #include "sysinfo.h" -int NET_IF_IN(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) +static struct nlist kernel_symbols[] = { + {"_ifnet", N_UNDF, 0, 0, 0}, + {"_tcbtable", N_UNDF, 0, 0, 0}, + {NULL, 0, 0, 0, 0} +}; - assert(result); +#define IFNET_ID 0 - init_result(result); +static int get_ifdata(const char *if_name, zbx_uint64_t *ibytes, zbx_uint64_t *ipackets, zbx_uint64_t *ierrors, zbx_uint64_t *idropped, + zbx_uint64_t *obytes, zbx_uint64_t *opackets, zbx_uint64_t *oerrors, + zbx_uint64_t *tbytes, zbx_uint64_t *tpackets, zbx_uint64_t *terrors, zbx_uint64_t *tdropped, + zbx_uint64_t *icollisions) +{ + struct ifnet_head head; + struct ifnet *ifp; + struct ifnet v; + kvm_t *kp; + int len = 0; + int ret = SYSINFO_RET_FAIL; + + kp = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL); + + if (kp) { + if (N_UNDF == kernel_symbols[IFNET_ID].n_type) + if (0 != kvm_nlist(kp, &kernel_symbols[0])) + kernel_symbols[IFNET_ID].n_type = N_UNDF; + + if (N_UNDF != kernel_symbols[IFNET_ID].n_type) { + len = sizeof(struct ifnet_head); + + if (kvm_read(kp, kernel_symbols[IFNET_ID].n_value, &head, len) >= len) { + len = sizeof(struct ifnet); + + /* if_ibytes; total number of octets received */ + /* if_ipackets; packets received on interface */ + /* if_ierrors; input errors on interface */ + /* if_iqdrops; dropped on input, this interface */ + /* if_obytes; total number of octets sent */ + /* if_opackets; packets sent on interface */ + /* if_oerrors; output errors on interface */ + /* if_collisions; collisions on csma interfaces */ + + if (ibytes) + *ibytes = 0; + if (ipackets) + *ipackets = 0; + if (ierrors) + *ierrors = 0; + if (idropped) + *idropped = 0; + if (obytes) + *obytes = 0; + if (opackets) + *opackets = 0; + if (oerrors) + *oerrors = 0; + if (tbytes) + *tbytes = 0; + if (tpackets) + *tpackets = 0; + if (terrors) + *terrors = 0; + if (tdropped) + *tdropped = 0; + if (icollisions) + *icollisions = 0; + + for(ifp = head.tqh_first; ifp; ifp = v.if_list.tqe_next) { + if (kvm_read(kp, (u_long)ifp, &v, len) < len) + break; + + if (*if_name == '\0' || 0 == strcmp(if_name, v.if_xname)) { + if (ibytes) + *ibytes += v.if_ibytes; + if (ipackets) + *ipackets += v.if_ipackets; + if (ierrors) + *ierrors += v.if_ierrors; + if (idropped) + *idropped += v.if_iqdrops; + if (obytes) + *obytes += v.if_obytes; + if (opackets) + *opackets += v.if_opackets; + if (oerrors) + *oerrors += v.if_oerrors; + if (tbytes) + *tbytes += v.if_ibytes + v.if_obytes; + if (tpackets) + *tpackets += v.if_ipackets + v.if_opackets; + if (terrors) + *terrors += v.if_ierrors + v.if_oerrors; + if (tdropped) + *tdropped += v.if_iqdrops; + if (icollisions) + *icollisions += v.if_collisions; + ret = SYSINFO_RET_OK; + } + } + } + } + kvm_close(kp); + } + + return ret; +} + +static int NET_IF_IN_BYTES(const char *if_name, AGENT_RESULT *result) +{ + zbx_uint64_t value; + + if (SYSINFO_RET_OK != get_ifdata(if_name, &value, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)) + return SYSINFO_RET_FAIL; + + SET_UI64_RESULT(result, value); + + return SYSINFO_RET_OK; +} + +static int NET_IF_IN_PACKETS(const char *if_name, AGENT_RESULT *result) +{ + zbx_uint64_t value; + + if (SYSINFO_RET_OK != get_ifdata(if_name, NULL, &value, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)) + return SYSINFO_RET_FAIL; + + SET_UI64_RESULT(result, value); + + return SYSINFO_RET_OK; +} + +static int NET_IF_IN_ERRORS(const char *if_name, AGENT_RESULT *result) +{ + zbx_uint64_t value; + + if (SYSINFO_RET_OK != get_ifdata(if_name, NULL, NULL, &value, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)) + return SYSINFO_RET_FAIL; + + SET_UI64_RESULT(result, value); + + return SYSINFO_RET_OK; +} + +static int NET_IF_IN_DROPPED(const char *if_name, AGENT_RESULT *result) +{ + zbx_uint64_t value; + + if (SYSINFO_RET_OK != get_ifdata(if_name, NULL, NULL, NULL, &value, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)) + return SYSINFO_RET_FAIL; + + SET_UI64_RESULT(result, value); + + return SYSINFO_RET_OK; +} + +int NET_IF_IN(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) +{ +#define NET_FNCLIST struct net_fnclist_s +NET_FNCLIST +{ + char *mode; + int (*function)(); +}; + + NET_FNCLIST fl[] = + { + {"bytes", NET_IF_IN_BYTES}, + {"packets", NET_IF_IN_PACKETS}, + {"errors", NET_IF_IN_ERRORS}, + {"dropped", NET_IF_IN_DROPPED}, + {0, 0} + }; + + char if_name[MAX_STRING_LEN]; + char mode[MAX_STRING_LEN]; + int i; + + assert(result); + + init_result(result); + + if (num_param(param) > 2) + return SYSINFO_RET_FAIL; + + if (0 != get_param(param, 1, if_name, sizeof(if_name))) + *if_name = '\0'; + + if (0 != get_param(param, 2, mode, sizeof(mode))) + *mode = '\0'; + + /* default parameter */ + if (*mode == '\0') + zbx_snprintf(mode, sizeof(mode), "bytes"); + + for (i = 0; fl[i].mode != 0; i++) + if (0 == strncmp(mode, fl[i].mode, MAX_STRING_LEN)) + return (fl[i].function)(if_name, result); + return SYSINFO_RET_FAIL; } +static int NET_IF_OUT_BYTES(const char *if_name, AGENT_RESULT *result) +{ + zbx_uint64_t value; + + if (SYSINFO_RET_OK != get_ifdata(if_name, NULL, NULL, NULL, NULL, &value, NULL, NULL, NULL, NULL, NULL, NULL, NULL)) + return SYSINFO_RET_FAIL; + + SET_UI64_RESULT(result, value); + + return SYSINFO_RET_OK; +} + +static int NET_IF_OUT_PACKETS(const char *if_name, AGENT_RESULT *result) +{ + zbx_uint64_t value; + + if (SYSINFO_RET_OK != get_ifdata(if_name, NULL, NULL, NULL, NULL, NULL, &value, NULL, NULL, NULL, NULL, NULL, NULL)) + return SYSINFO_RET_FAIL; + + SET_UI64_RESULT(result, value); + + return SYSINFO_RET_OK; +} + +static int NET_IF_OUT_ERRORS(const char *if_name, AGENT_RESULT *result) +{ + zbx_uint64_t value; + + if (SYSINFO_RET_OK != get_ifdata(if_name, NULL, NULL, NULL, NULL, NULL, NULL, &value, NULL, NULL, NULL, NULL, NULL)) + return SYSINFO_RET_FAIL; + + SET_UI64_RESULT(result, value); + + return SYSINFO_RET_OK; +} + int NET_IF_OUT(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { +#define NET_FNCLIST struct net_fnclist_s +NET_FNCLIST +{ + char *mode; + int (*function)(); +}; - assert(result); + NET_FNCLIST fl[] = + { + {"bytes", NET_IF_OUT_BYTES}, + {"packets", NET_IF_OUT_PACKETS}, + {"errors", NET_IF_OUT_ERRORS}, +/* {"dropped", NET_IF_OUT_DROPPED},*/ + {0, 0} + }; + + char if_name[MAX_STRING_LEN]; + char mode[MAX_STRING_LEN]; + int i; + + assert(result); + + init_result(result); + + if (num_param(param) > 2) + return SYSINFO_RET_FAIL; + + if (0 != get_param(param, 1, if_name, sizeof(if_name))) + *if_name = '\0'; + + if (0 != get_param(param, 2, mode, sizeof(mode))) + *mode = '\0'; + + /* default parameter */ + if (*mode == '\0') + zbx_snprintf(mode, sizeof(mode), "bytes"); + + for (i = 0; fl[i].mode != 0; i++) + if (0 == strncmp(mode, fl[i].mode, MAX_STRING_LEN)) + return (fl[i].function)(if_name, result); - init_result(result); - return SYSINFO_RET_FAIL; } +static int NET_IF_TOTAL_BYTES(const char *if_name, AGENT_RESULT *result) +{ + zbx_uint64_t value; + + if (SYSINFO_RET_OK != get_ifdata(if_name, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &value, NULL, NULL, NULL, NULL)) + return SYSINFO_RET_FAIL; + + SET_UI64_RESULT(result, value); + + return SYSINFO_RET_OK; +} + +static int NET_IF_TOTAL_PACKETS(const char *if_name, AGENT_RESULT *result) +{ + zbx_uint64_t value; + + if (SYSINFO_RET_OK != get_ifdata(if_name, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &value, NULL, NULL, NULL)) + return SYSINFO_RET_FAIL; + + SET_UI64_RESULT(result, value); + + return SYSINFO_RET_OK; +} + +static int NET_IF_TOTAL_ERRORS(const char *if_name, AGENT_RESULT *result) +{ + zbx_uint64_t value; + + if (SYSINFO_RET_OK != get_ifdata(if_name, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &value, NULL, NULL)) + return SYSINFO_RET_FAIL; + + SET_UI64_RESULT(result, value); + + return SYSINFO_RET_OK; +} + +static int NET_IF_TOTAL_DROPPED(const char *if_name, AGENT_RESULT *result) +{ + zbx_uint64_t value; + + if (SYSINFO_RET_OK != get_ifdata(if_name, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &value, NULL)) + return SYSINFO_RET_FAIL; + + SET_UI64_RESULT(result, value); + + return SYSINFO_RET_OK; +} + int NET_IF_TOTAL(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { +#define NET_FNCLIST struct net_fnclist_s +NET_FNCLIST +{ + char *mode; + int (*function)(); +}; - assert(result); + NET_FNCLIST fl[] = + { + {"bytes", NET_IF_TOTAL_BYTES}, + {"packets", NET_IF_TOTAL_PACKETS}, + {"errors", NET_IF_TOTAL_ERRORS}, + {"dropped", NET_IF_TOTAL_DROPPED}, + {0, 0} + }; + + char if_name[MAX_STRING_LEN]; + char mode[MAX_STRING_LEN]; + int i; + + assert(result); + + init_result(result); + + if (num_param(param) > 2) + return SYSINFO_RET_FAIL; + + if (0 != get_param(param, 1, if_name, sizeof(if_name))) + *if_name = '\0'; + + if (0 != get_param(param, 2, mode, sizeof(mode))) + *mode = '\0'; + + /* default parameter */ + if (*mode == '\0') + zbx_snprintf(mode, sizeof(mode), "bytes"); + + for (i = 0; fl[i].mode != 0; i++) + if (0 == strncmp(mode, fl[i].mode, MAX_STRING_LEN)) + return (fl[i].function)(if_name, result); - init_result(result); - return SYSINFO_RET_FAIL; } int NET_TCP_LISTEN(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { - assert(result); + assert(result); - init_result(result); + init_result(result); return SYSINFO_RET_FAIL; } int NET_IF_COLLISIONS(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { - assert(result); + zbx_uint64_t value; + char if_name[MAX_STRING_LEN]; - init_result(result); - - return SYSINFO_RET_FAIL; + assert(result); + + init_result(result); + + if (num_param(param) > 1) + return SYSINFO_RET_FAIL; + + if (0 != get_param(param, 1, if_name, sizeof(if_name))) + *if_name = '\0'; + + if (SYSINFO_RET_OK != get_ifdata(if_name, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &value)) + return SYSINFO_RET_FAIL; + + SET_UI64_RESULT(result, value); + + return SYSINFO_RET_OK; } diff --git a/src/libs/zbxsysinfo/netbsd/netbsd.c b/src/libs/zbxsysinfo/netbsd/netbsd.c index 562827d2..3e08e001 100644 --- a/src/libs/zbxsysinfo/netbsd/netbsd.c +++ b/src/libs/zbxsysinfo/netbsd/netbsd.c @@ -56,6 +56,7 @@ ZBX_METRIC parameters_specific[]= {"system.swap.out", CF_USEUPARAM, SYSTEM_SWAP_OUT, 0, "all,count"}, {"system.uptime", 0, SYSTEM_UPTIME, 0, 0}, + {"system.boottime", 0, SYSTEM_BOOTTIME, 0, 0}, {0} }; diff --git a/src/libs/zbxsysinfo/netbsd/proc.c b/src/libs/zbxsysinfo/netbsd/proc.c index 081243a1..540d79f7 100644 --- a/src/libs/zbxsysinfo/netbsd/proc.c +++ b/src/libs/zbxsysinfo/netbsd/proc.c @@ -18,483 +18,291 @@ * **/ #include "common.h" - #include "sysinfo.h" +#include + #define DO_SUM 0 #define DO_MAX 1 #define DO_MIN 2 #define DO_AVG 3 - + +#define ZBX_PROC_STAT_ALL 0 +#define ZBX_PROC_STAT_RUN 1 +#define ZBX_PROC_STAT_SLEEP 2 +#define ZBX_PROC_STAT_ZOMB 3 + +static kvm_t *kd = NULL; + +static char *proc_argv(pid_t pid) +{ + size_t sz = 0; + int mib[4], ret; + int i, len; + static char *argv = NULL; + static size_t argv_alloc = 0; + + mib[0] = CTL_KERN; + mib[1] = KERN_PROC_ARGS; + mib[2] = (int)pid; + mib[3] = KERN_PROC_ARGV; + + if (0 != sysctl(mib, 4, NULL, &sz, NULL, 0)) + return NULL; + + if (argv_alloc < sz) { + argv_alloc = sz; + if (NULL == argv) + argv = zbx_malloc(argv, argv_alloc); + else + argv = zbx_realloc(argv, argv_alloc); + } + + sz = argv_alloc; + if (0 != sysctl(mib, 4, argv, &sz, NULL, 0)) + return NULL; + + for (i = 0; i < (int)(sz - 1); i++ ) + if (argv[i] == '\0') + argv[i] = ' '; + + return argv; +} + int PROC_MEMORY(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { -#if defined(HAVE_PROC_1_STATUS) + char procname[MAX_STRING_LEN], + buffer[MAX_STRING_LEN], + proccomm[MAX_STRING_LEN], + *args; + int do_task, pagesize, count, i, + proc_ok, comm_ok, + op, arg; + + double value = 0.0, + memsize = 0; + int proccount = 0; - DIR *dir; - struct dirent *entries; - struct stat buf; - char filename[MAX_STRING_LEN]; - char line[MAX_STRING_LEN]; + size_t sz; - char name1[MAX_STRING_LEN]; - char name2[MAX_STRING_LEN]; + struct kinfo_proc2 *proc, *pproc; + struct passwd *usrinfo; - char procname[MAX_STRING_LEN]; - char usrname[MAX_STRING_LEN]; - char mode[MAX_STRING_LEN]; + assert(result); - int proc_ok = 0; - int usr_ok = 0; - int do_task = DO_SUM; + init_result(result); + if (num_param(param) > 4) + return SYSINFO_RET_FAIL; - struct passwd *usrinfo = NULL; - long long int llvalue = 0; + if (0 != get_param(param, 1, procname, sizeof(procname))) + *procname = '\0'; + else if (strlen(procname) > MAXCOMLEN) + procname[MAXCOMLEN] = '\0'; - FILE *f; + if (0 != get_param(param, 2, buffer, sizeof(buffer))) + *buffer = '\0'; - double memsize = -1; - int proccount = 0; + if (*buffer != '\0') { + usrinfo = getpwnam(buffer); + if (usrinfo == NULL) /* incorrect user name */ + return SYSINFO_RET_FAIL; + } else + usrinfo = NULL; - assert(result); + if (0 != get_param(param, 3, buffer, sizeof(buffer))) + *buffer = '\0'; - init_result(result); - - if(num_param(param) > 3) - { - return SYSINFO_RET_FAIL; - } + if (*buffer != '\0') { + if (0 == strcmp(buffer, "avg")) + do_task = DO_AVG; + else if (0 == strcmp(buffer, "max")) + do_task = DO_MAX; + else if (0 == strcmp(buffer, "min")) + do_task = DO_MIN; + else if (0 == strcmp(buffer, "sum")) + do_task = DO_SUM; + else + return SYSINFO_RET_FAIL; + } else + do_task = DO_SUM; - if(get_param(param, 1, procname, MAX_STRING_LEN) != 0) - { - return SYSINFO_RET_FAIL; - } - - if(get_param(param, 2, usrname, MAX_STRING_LEN) != 0) - { - usrname[0] = 0; - } - else - { - if(usrname[0] != 0) - { - usrinfo = getpwnam(usrname); - if(usrinfo == NULL) - { - /* incorrect user name */ - return SYSINFO_RET_FAIL; - } - } - } - - if(get_param(param, 3, mode, MAX_STRING_LEN) != 0) - { - mode[0] = '\0'; - } - - if(mode[0] == '\0') - { - strscpy(mode, "sum"); - } - - if(strcmp(mode,"avg") == 0) - { - do_task = DO_AVG; - } - else if(strcmp(mode,"max") == 0) - { - do_task = DO_MAX; - } - else if(strcmp(mode,"min") == 0) - { - do_task = DO_MIN; - } - else if(strcmp(mode,"sum") == 0) - { - do_task = DO_SUM; - } - else - { - return SYSINFO_RET_FAIL; - } - - dir=opendir("/proc"); - if(NULL == dir) - { - return SYSINFO_RET_FAIL; - } - - while((entries=readdir(dir))!=NULL) - { - proc_ok = 0; - usr_ok = 0; - - strscpy(filename,"/proc/"); - zbx_strlcat(filename,entries->d_name,MAX_STRING_LEN); - zbx_strlcat(filename,"/status",MAX_STRING_LEN); - - /* Self is a symbolic link. It leads to incorrect results for proc_cnt[zabbix_agentd] */ - /* Better approach: check if /proc/x/ is symbolic link */ - if(strncmp(entries->d_name,"self",MAX_STRING_LEN) == 0) - { - continue; - } - - if(stat(filename,&buf)==0) - { - if( NULL == (f = fopen(filename,"r") )) - { - continue; - } - - if(procname[0] != 0) - { - fgets(line,MAX_STRING_LEN,f); - if(sscanf(line,"%s\t%s\n",name1,name2)==2) - { - if(strcmp(name1,"Name:") == 0) - { - if(strcmp(procname,name2)==0) - { - proc_ok = 1; - } - } - } - - if(proc_ok == 0) - { - zbx_fclose(f); - continue; - } - } - else - { - proc_ok = 1; - } - - if(usrinfo != NULL) - { - while(fgets(line, MAX_STRING_LEN, f) != NULL) - { - - if(sscanf(line, "%s\t%lli\n", name1, &llvalue) != 2) - { - continue; - } - - if(strcmp(name1,"Uid:") != 0) - { - continue; - } - - if(usrinfo->pw_uid == (uid_t)(llvalue)) - { - usr_ok = 1; - break; - } - } - } - else - { - usr_ok = 1; - } - - if(proc_ok && usr_ok) - { - while(fgets(line, MAX_STRING_LEN, f) != NULL) - { - - if(sscanf(line, "%s\t%lli %s\n", name1, &llvalue, name2) != 3) - { - continue; - } - - if(strcmp(name1,"VmSize:") != 0) - { - continue; - } - - proccount++; - - if(strcasecmp(name2, "kB") == 0) - { - llvalue <<= 10; - } - else if(strcasecmp(name2, "mB") == 0) - { - llvalue <<= 20; - } - else if(strcasecmp(name2, "GB") == 0) - { - llvalue <<= 30; - } - else if(strcasecmp(name2, "TB") == 0) - { - llvalue <<= 40; - } - - if(memsize < 0) - { - memsize = (double) llvalue; - } - else - { - if(do_task == DO_MAX) - { - memsize = MAX(memsize, (double) llvalue); - } - else if(do_task == DO_MIN) - { - memsize = MIN(memsize, (double) llvalue); - } - else - { - memsize += (double) llvalue; - } - } - - break; - } - } - - - zbx_fclose(f); - } - } - closedir(dir); - - if(memsize < 0) - { - /* incorrect process name */ - memsize = 0; - } - - if(do_task == DO_AVG) - { - SET_DBL_RESULT(result, proccount == 0 ? 0 : ((double)memsize/(double)proccount)); - } - else - { - SET_UI64_RESULT(result, memsize); - } - return SYSINFO_RET_OK; -#else - return SYSINFO_RET_FAIL; -#endif + if (0 != get_param(param, 4, proccomm, sizeof(proccomm))) + *proccomm = '\0'; + + pagesize = getpagesize(); + + if (NULL == kd && NULL == (kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, NULL))) + return SYSINFO_RET_FAIL; + + if (NULL != usrinfo) { + op = KERN_PROC_UID; + arg = (int)usrinfo->pw_uid; + } else { + op = KERN_PROC_ALL; + arg = 0; + } + + if (NULL == (proc = kvm_getproc2(kd, op, arg, sizeof(struct kinfo_proc2), &count))) + return SYSINFO_RET_FAIL; + + for (pproc = proc, i = 0; i < count; pproc++, i++) { + proc_ok = 0; + comm_ok = 0; + + if (*procname == '\0' || 0 == strcmp(procname, pproc->p_comm)) + proc_ok = 1; + + if (*proccomm != '\0') { + if (NULL != (args = proc_argv(pproc->p_pid))) { + if (NULL != zbx_regexp_match(args, proccomm, NULL)) + comm_ok = 1; + } + } else + comm_ok = 1; + + if (proc_ok && comm_ok) { + value = pproc->p_vm_tsize + + pproc->p_vm_dsize + + pproc->p_vm_ssize; + value *= pagesize; + + if (0 == proccount++) + memsize = value; + else { + if (do_task == DO_MAX) + memsize = MAX(memsize, value); + else if (do_task == DO_MIN) + memsize = MIN(memsize, value); + else + memsize += value; + } + } + } + zbx_free(proc); + + if (do_task == DO_AVG) { + SET_DBL_RESULT(result, proccount == 0 ? 0 : memsize/proccount); + } else { + SET_UI64_RESULT(result, memsize); + } + + return SYSINFO_RET_OK; } -int PROC_NUM(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) +int PROC_NUM(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { -#if defined(HAVE_PROC_1_STATUS) + char procname[MAX_STRING_LEN], + buffer[MAX_STRING_LEN], + proccomm[MAX_STRING_LEN], + *args; + int zbx_proc_stat, count, i, + proc_ok, stat_ok, comm_ok, + op, arg; - DIR *dir; - struct dirent *entries; - struct stat buf; - char filename[MAX_STRING_LEN]; - char line[MAX_STRING_LEN]; + int proccount = 0; - char name1[MAX_STRING_LEN]; - char name2[MAX_STRING_LEN]; + size_t sz; - char procname[MAX_STRING_LEN]; - char usrname[MAX_STRING_LEN]; - char procstat[MAX_STRING_LEN]; + struct kinfo_proc2 *proc, *pproc; + struct passwd *usrinfo; - int proc_ok = 0; - int usr_ok = 0; - int stat_ok = 0; + assert(result); - struct passwd *usrinfo = NULL; - long int lvalue = 0; + init_result(result); - FILE *f; - int proccount = 0; + if (num_param(param) > 4) + return SYSINFO_RET_FAIL; - assert(result); + if (0 != get_param(param, 1, procname, sizeof(procname))) + *procname = '\0'; + else if (strlen(procname) > MAXCOMLEN) + procname[MAXCOMLEN] = '\0'; - init_result(result); - - - if(num_param(param) > 3) - { - return SYSINFO_RET_FAIL; - } - - if(get_param(param, 1, procname, MAX_STRING_LEN) != 0) - { - return SYSINFO_RET_FAIL; - } - - if(get_param(param, 2, usrname, MAX_STRING_LEN) != 0) - { - usrname[0] = 0; - } - else - { - if(usrname[0] != 0) - { - usrinfo = getpwnam(usrname); - if(usrinfo == NULL) - { - /* incorrect user name */ - return SYSINFO_RET_FAIL; - } - } - } + if (0 != get_param(param, 2, buffer, sizeof(buffer))) + *buffer = '\0'; + + if (*buffer != '\0') { + usrinfo = getpwnam(buffer); + if (usrinfo == NULL) /* incorrect user name */ + return SYSINFO_RET_FAIL; + } else + usrinfo = NULL; - if(get_param(param, 3, procstat, MAX_STRING_LEN) != 0) - { - procstat[0] = '\0'; + if (0 != get_param(param, 3, buffer, sizeof(buffer))) + *buffer = '\0'; + + if (*buffer != '\0') { + if (0 == strcmp(buffer, "run")) + zbx_proc_stat = ZBX_PROC_STAT_RUN; + else if (0 == strcmp(buffer, "sleep")) + zbx_proc_stat = ZBX_PROC_STAT_SLEEP; + else if (0 == strcmp(buffer, "zomb")) + zbx_proc_stat = ZBX_PROC_STAT_ZOMB; + else if (0 == strcmp(buffer, "all")) + zbx_proc_stat = ZBX_PROC_STAT_ALL; + else + return SYSINFO_RET_FAIL; + } else + zbx_proc_stat = ZBX_PROC_STAT_ALL; + + if (0 != get_param(param, 4, proccomm, sizeof(proccomm))) + *proccomm = '\0'; + + if (NULL == kd && NULL == (kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, NULL))) + return SYSINFO_RET_FAIL; + + if (NULL != usrinfo) { + op = KERN_PROC_UID; + arg = (int)usrinfo->pw_uid; + } else { + op = KERN_PROC_ALL; + arg = 0; } - if(procstat[0] == '\0') - { - strscpy(procstat, "all"); - } - - if(strcmp(procstat,"run") == 0) - { - strscpy(procstat,"R"); - } - else if(strcmp(procstat,"sleep") == 0) - { - strscpy(procstat,"S"); - } - else if(strcmp(procstat,"zomb") == 0) - { - strscpy(procstat,"Z"); - } - else if(strcmp(procstat,"all") == 0) - { - procstat[0] = 0; - } - else - { - return SYSINFO_RET_FAIL; - } + if (NULL == (proc = kvm_getproc2(kd, op, arg, sizeof(struct kinfo_proc2), &count))) + return SYSINFO_RET_FAIL; + + for (pproc = proc, i = 0; i < count; pproc++, i++) { + proc_ok = 0; + stat_ok = 0; + comm_ok = 0; + + if (*procname == '\0' || 0 == strcmp(procname, pproc->p_comm)) + proc_ok = 1; + + if (zbx_proc_stat != ZBX_PROC_STAT_ALL) { + switch (zbx_proc_stat) { + case ZBX_PROC_STAT_RUN: + if (pproc->p_stat == LSRUN || pproc->p_stat == LSONPROC) + stat_ok = 1; + break; + case ZBX_PROC_STAT_SLEEP: + if (pproc->p_stat == LSSLEEP) + stat_ok = 1; + break; + case ZBX_PROC_STAT_ZOMB: + if (pproc->p_stat == SZOMB || pproc->p_stat == LSDEAD) + stat_ok = 1; + break; + } + } else + stat_ok = 1; + + if (*proccomm != '\0') { + if (NULL != (args = proc_argv(pproc->p_pid))) { + if (zbx_regexp_match(args, proccomm, NULL) != NULL) + comm_ok = 1; + } + } else + comm_ok = 1; - dir=opendir("/proc"); - if(NULL == dir) - { - return SYSINFO_RET_FAIL; - } - - while((entries=readdir(dir))!=NULL) - { - proc_ok = 0; - stat_ok = 0; - usr_ok = 0; - -/* Self is a symbolic link. It leads to incorrect results for proc_cnt[zabbix_agentd] */ -/* Better approach: check if /proc/x/ is symbolic link */ - if(strncmp(entries->d_name,"self",MAX_STRING_LEN) == 0) - { - continue; - } - - strscpy(filename,"/proc/"); - zbx_strlcat(filename,entries->d_name,MAX_STRING_LEN); - zbx_strlcat(filename,"/status",MAX_STRING_LEN); - - if(stat(filename,&buf)==0) - { - if(NULL == (f = fopen(filename,"r") )) - { - continue; - } - - if(procname[0] != 0) - { - fgets(line,MAX_STRING_LEN,f); - if(sscanf(line,"%s\t%s\n",name1,name2)==2) - { - if(strcmp(name1,"Name:") == 0) - { - if(strcmp(procname,name2)==0) - { - proc_ok = 1; - } - } - } - - if(proc_ok == 0) - { - zbx_fclose(f); - continue; - } - } - else - { - proc_ok = 1; - } - - if(procstat[0] != 0) - { - while(fgets(line, MAX_STRING_LEN, f) != NULL) - { - - if(sscanf(line, "%s\t%s\n", name1, name2) != 2) - { - continue; - } - - if(strcmp(name1,"State:") != 0) - { - continue; - } - - if(strcmp(name2, procstat)) - { - stat_ok = 1; - break; - } - } - } - else - { - stat_ok = 1; - } - - if(usrinfo != NULL) - { - while(fgets(line, MAX_STRING_LEN, f) != NULL) - { - - if(sscanf(line, "%s\t%li\n", name1, &lvalue) != 2) - { - continue; - } - - if(strcmp(name1,"Uid:") != 0) - { - continue; - } - - if(usrinfo->pw_uid == (uid_t)(lvalue)) - { - usr_ok = 1; - break; - } - } - } - else - { - usr_ok = 1; - } - - if(proc_ok && stat_ok && usr_ok) - { - proccount++; - } - - zbx_fclose(f); - } - } - closedir(dir); + if (proc_ok && stat_ok && comm_ok) + proccount++; + } + zbx_free(proc); SET_UI64_RESULT(result, proccount); - return SYSINFO_RET_OK; -#else - return SYSINFO_RET_FAIL; -#endif + + return SYSINFO_RET_OK; } diff --git a/src/libs/zbxsysinfo/netbsd/uptime.c b/src/libs/zbxsysinfo/netbsd/uptime.c index eb75030f..331f9ac9 100644 --- a/src/libs/zbxsysinfo/netbsd/uptime.c +++ b/src/libs/zbxsysinfo/netbsd/uptime.c @@ -23,7 +23,7 @@ int SYSTEM_UPTIME(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { -#ifdef HAVE_SYSINFO_UPTIME +#if defined(HAVE_SYSINFO_UPTIME) struct sysinfo info; assert(result); @@ -39,34 +39,29 @@ int SYSTEM_UPTIME(const char *cmd, const char *param, unsigned flags, AGENT_RESU { return SYSINFO_RET_FAIL; } -#else -#ifdef HAVE_FUNCTION_SYSCTL_KERN_BOOTTIME - int mib[2],len; +#elif defined(HAVE_FUNCTION_SYSCTL_KERN_BOOTTIME) + int mib[2], now; + size_t len; struct timeval uptime; - int now; assert(result); init_result(result); - mib[0]=CTL_KERN; - mib[1]=KERN_BOOTTIME; + mib[0] = CTL_KERN; + mib[1] = KERN_BOOTTIME; - len=sizeof(uptime); + len = sizeof(uptime); - if(sysctl(mib,2,&uptime,(size_t *)&len,NULL,0) != 0) - { - return SYSINFO_RET_FAIL; -/* printf("Errno [%m]\n");*/ - } + if (0 != sysctl(mib, 2, &uptime, &len, NULL, 0)) + return SYSINFO_RET_FAIL; - now=time(NULL); + now = time(NULL); - SET_UI64_RESULT(result, now-uptime.tv_sec); + SET_UI64_RESULT(result, now - uptime.tv_sec); + return SYSINFO_RET_OK; -#else -/* Solaris */ -#ifdef HAVE_KSTAT_H +#elif defined(HAVE_KSTAT_H) /* Solaris */ kstat_ctl_t *kc; kstat_t *kp; kstat_named_t *kn; @@ -115,6 +110,4 @@ int SYSTEM_UPTIME(const char *cmd, const char *param, unsigned flags, AGENT_RESU return SYSINFO_RET_FAIL; #endif -#endif -#endif } diff --git a/src/libs/zbxsysinfo/openbsd/boottime.c b/src/libs/zbxsysinfo/openbsd/boottime.c index c9814312..55b00251 100644 --- a/src/libs/zbxsysinfo/openbsd/boottime.c +++ b/src/libs/zbxsysinfo/openbsd/boottime.c @@ -46,4 +46,3 @@ int SYSTEM_BOOTTIME(const char *cmd, const char *param, unsigned flags, AGENT_RE return SYSINFO_RET_FAIL; #endif /* HAVE_FUNCTION_SYSCTL_KERN_BOOTTIME */ } - diff --git a/src/libs/zbxsysinfo/openbsd/cpu.c b/src/libs/zbxsysinfo/openbsd/cpu.c index 31189034..c1d81ed3 100644 --- a/src/libs/zbxsysinfo/openbsd/cpu.c +++ b/src/libs/zbxsysinfo/openbsd/cpu.c @@ -18,13 +18,16 @@ **/ #include "common.h" + #include "sysinfo.h" +#include "stats.h" int SYSTEM_CPU_NUM(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { #ifdef HAVE_FUNCTION_SYSCTL_HW_NCPU + /* OpenBSD 4.2 i386 */ size_t len; - int mib[2], ncpu; + int mib[] = {CTL_HW, HW_NCPU}, ncpu; char mode[MAX_STRING_LEN]; assert(result); @@ -43,9 +46,6 @@ int SYSTEM_CPU_NUM(const char *cmd, const char *param, unsigned flags, AGENT_RES if (0 != strcmp(mode, "online")) return SYSINFO_RET_FAIL; - mib[0] = CTL_HW; - mib[1] = HW_NCPU; - len = sizeof(ncpu); if (0 != sysctl(mib, 2, &ncpu, &len, NULL, 0)) @@ -59,268 +59,244 @@ int SYSTEM_CPU_NUM(const char *cmd, const char *param, unsigned flags, AGENT_RES #endif /* HAVE_FUNCTION_SYSCTL_HW_NCPU */ } -static int get_cpu_data(unsigned long long *idle, - unsigned long long *user, - unsigned long long *nice, - unsigned long long *system, - unsigned long long *intr) +int SYSTEM_CPU_INTR(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { - u_int64_t value[CPUSTATES]; - int ret = SYSINFO_RET_FAIL; - int mib[2]; - size_t l; +#if defined(HAVE_UVM_UVMEXP) + /* OpenBSD 4.2 i386 */ + int mib[] = {CTL_VM, VM_UVMEXP}; + size_t len; + struct uvmexp v; + + assert(result); - mib[0] = CTL_KERN; - mib[1] = KERN_CLOCKRATE; + init_result(result); - l = sizeof(value); + len = sizeof(struct uvmexp); - if (sysctl(mib, 2, value, &l, NULL, 0) == 0 ) - { - (*idle) = value[CP_IDLE]; - (*user) = value[CP_USER]; - (*nice) = value[CP_NICE]; - (*system) = value[CP_SYS]; - (*intr) = value[CP_INTR]; - ret = SYSINFO_RET_OK; - } - - return ret; -} + if (0 != sysctl(mib, 2, &v, &len, NULL, 0)) + return SYSINFO_RET_FAIL; -#define CPU_I 0 -#define CPU_U 1 -#define CPU_N 2 -#define CPU_S 3 -#define CPU_T 4 + SET_UI64_RESULT(result, v.intrs); -int SYSTEM_CPU_UTIL(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) -{ -#define CPU_PARAMLIST struct cpy_paramlist_s -CPU_PARAMLIST + return SYSINFO_RET_OK; +#else + return SYSINFO_RET_FAIL; +#endif /* HAVE_UVM_UVMEXP2 */ +} + +int SYSTEM_CPU_SWITCHES(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { - char *mode; - int id; -}; +#if defined(HAVE_UVM_UVMEXP) + /* OpenBSD 4.2 i386 */ + int mib[] = {CTL_VM, VM_UVMEXP}; + size_t len; + struct uvmexp v; - CPU_PARAMLIST pl[] = - { - {"idle" , CPU_I}, - {"user" , CPU_U}, - {"nice", CPU_N}, - {"system", CPU_S}, - {"intr", CPU_T}, - {0, 0} - }; + assert(result); - unsigned long long cpu_val[5]; - unsigned long long interval_size; - - char cpuname[MAX_STRING_LEN]; - char mode[MAX_STRING_LEN]; - - int i; - - int ret = SYSINFO_RET_FAIL; - - if(num_param(param) > 2) - { - return SYSINFO_RET_FAIL; - } - - if(get_param(param, 1, cpuname, sizeof(cpuname)) != 0) - { - cpuname[0] = '\0'; - } - if(cpuname[0] == '\0') - { - /* default parameter */ - zbx_snprintf(cpuname, sizeof(cpuname), "all"); - } - if(strncmp(cpuname, "all", sizeof(cpuname))) - { - return SYSINFO_RET_FAIL; - } - - if(get_param(param, 2, mode, sizeof(mode)) != 0) - { - mode[0] = '\0'; - } - - if(mode[0] == '\0') - { - /* default parameter */ - strscpy(mode, pl[0].mode); - } - - for(i=0; pl[i].mode!=0; i++) - { - if(strncmp(mode, pl[i].mode, MAX_STRING_LEN)==0) - { - ret = get_cpu_data( - &cpu_val[CPU_I], - &cpu_val[CPU_U], - &cpu_val[CPU_N], - &cpu_val[CPU_S], - &cpu_val[CPU_T]); - - if(ret == SYSINFO_RET_OK) - { - interval_size = - cpu_val[CPU_I] + - cpu_val[CPU_U] + - cpu_val[CPU_N] + - cpu_val[CPU_S] + - cpu_val[CPU_T]; - - if (interval_size > 0) - { - SET_DBL_RESULT(result, ((double)cpu_val[pl[i].id] * 100.0)/(double)interval_size); - - ret = SYSINFO_RET_OK; - } - } - break; - } - } + init_result(result); - return ret; + len = sizeof(struct uvmexp); + + if (0 != sysctl(mib, 2, &v, &len, NULL, 0)) + return SYSINFO_RET_FAIL; + + SET_UI64_RESULT(result, v.swtch); + + return SYSINFO_RET_OK; +#else + return SYSINFO_RET_FAIL; +#endif /* HAVE_UVM_UVMEXP2 */ } -int SYSTEM_CPU_LOAD1(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) +int SYSTEM_CPU_UTIL(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { - double load[3]; - int ret = SYSINFO_RET_FAIL; + char cpuname[MAX_STRING_LEN], + type[MAX_STRING_LEN], + mode[MAX_STRING_LEN]; + int cpu_num; assert(result); - init_result(result); - - if(getloadavg(load, 3)) + init_result(result); + + if (num_param(param) > 3) + return SYSINFO_RET_FAIL; + + if (0 != get_param(param, 1, cpuname, sizeof(cpuname))) + *cpuname = '\0'; + + /* default parameter */ + if (*cpuname == '\0') + zbx_snprintf(cpuname, sizeof(cpuname), "all"); + + if (0 == strcmp(cpuname, "all")) + cpu_num = 0; + else { + cpu_num = atoi(cpuname) + 1; + + if (cpu_num < 1 || cpu_num > collector->cpus.count) + return SYSINFO_RET_FAIL; + } + + if (0 != get_param(param, 2, type, sizeof(type))) + *type = '\0'; + + /* default parameter */ + if (*type == '\0') + zbx_snprintf(type, sizeof(type), "user"); + + if (0 != get_param(param, 3, mode, sizeof(mode))) + *mode = '\0'; + + /* default parameter */ + if (*mode == '\0') + zbx_snprintf(mode, sizeof(mode), "avg1"); + + if (!CPU_COLLECTOR_STARTED(collector)) { + SET_MSG_RESULT(result, strdup("Collector is not started!")); + return SYSINFO_RET_OK; + } + + if (0 == strcmp(type, "idle")) { + if (0 == strcmp(mode, "avg1")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].idle1) + else if (0 == strcmp(mode, "avg5")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].idle5) + else if (0 == strcmp(mode, "avg15")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].idle15) + else return SYSINFO_RET_FAIL; + } + else if (0 == strcmp(type, "nice")) { - SET_DBL_RESULT(result, load[0]); - ret = SYSINFO_RET_OK; + if (0 == strcmp(mode, "avg1")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].nice1) + else if (0 == strcmp(mode, "avg5")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].nice5) + else if (0 == strcmp(mode, "avg15")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].nice15) + else return SYSINFO_RET_FAIL; } - - return ret; + else if (0 == strcmp(type, "user")) + { + if (0 == strcmp(mode, "avg1")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].user1) + else if (0 == strcmp(mode, "avg5")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].user5) + else if (0 == strcmp(mode, "avg15")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].user15) + else return SYSINFO_RET_FAIL; + } + else if (0 == strcmp(type, "system")) + { + if (0 == strcmp(mode, "avg1")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].system1) + else if (0 == strcmp(mode, "avg5")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].system5) + else if (0 == strcmp(mode, "avg15")) SET_DBL_RESULT(result, collector->cpus.cpu[cpu_num].system15) + else return SYSINFO_RET_FAIL; + } + else + return SYSINFO_RET_FAIL; + + return SYSINFO_RET_OK; } -int SYSTEM_CPU_LOAD5(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) +static int get_cpuload(double *load1, double *load5, double *load15) { +#ifdef HAVE_GETLOADAVG + /* OpenBSD 4.2 i386 */ double load[3]; - int ret = SYSINFO_RET_FAIL; - assert(result); + if (-1 == getloadavg(load, 3)) + return SYSINFO_RET_FAIL; - init_result(result); - - if(getloadavg(load, 3)) - { - SET_DBL_RESULT(result, load[1]); - ret = SYSINFO_RET_OK; - } - - return ret; + if (load1) + *load1 = load[0]; + if (load5) + *load5 = load[1]; + if (load15) + *load15 = load[2]; + + return SYSINFO_RET_OK; +#else + return SYSINFO_RET_FAIL; +#endif /* HAVE_GETLOADAVG */ } -int SYSTEM_CPU_LOAD15(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) +static int SYSTEM_CPU_LOAD1(AGENT_RESULT *result) { - double load[3]; - int ret = SYSINFO_RET_FAIL; + double value; - assert(result); + if (SYSINFO_RET_OK != get_cpuload(&value, NULL, NULL)) + return SYSINFO_RET_FAIL; + + SET_DBL_RESULT(result, value); + + return SYSINFO_RET_OK; +} + +static int SYSTEM_CPU_LOAD5(AGENT_RESULT *result) +{ + double value; - init_result(result); + if (SYSINFO_RET_OK != get_cpuload(NULL, &value, NULL)) + return SYSINFO_RET_FAIL; + + SET_DBL_RESULT(result, value); - if(getloadavg(load, 3)) - { - SET_DBL_RESULT(result, load[2]); - ret = SYSINFO_RET_OK; - } + return SYSINFO_RET_OK; +} + +static int SYSTEM_CPU_LOAD15(AGENT_RESULT *result) +{ + double value; + + if (SYSINFO_RET_OK != get_cpuload(NULL, NULL, &value)) + return SYSINFO_RET_FAIL; - return ret; + SET_DBL_RESULT(result, value); + + return SYSINFO_RET_OK; } int SYSTEM_CPU_LOAD(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { - #define CPU_FNCLIST struct cpu_fnclist_s CPU_FNCLIST { - char *mode; - int (*function)(); + char *mode; + int (*function)(); }; CPU_FNCLIST fl[] = { - {"avg1" , SYSTEM_CPU_LOAD1}, - {"avg5" , SYSTEM_CPU_LOAD5}, + {"avg1", SYSTEM_CPU_LOAD1}, + {"avg5", SYSTEM_CPU_LOAD5}, {"avg15", SYSTEM_CPU_LOAD15}, {0, 0} }; - char cpuname[MAX_STRING_LEN]; - char mode[MAX_STRING_LEN]; - int i; - - assert(result); + char cpuname[MAX_STRING_LEN], + mode[MAX_STRING_LEN]; + int i; - init_result(result); - - if(num_param(param) > 2) - { - return SYSINFO_RET_FAIL; - } - - if(get_param(param, 1, cpuname, sizeof(cpuname)) != 0) - { - return SYSINFO_RET_FAIL; - } - if(cpuname[0] == '\0') - { - /* default parameter */ + assert(result); + + init_result(result); + + if (num_param(param) > 2) + return SYSINFO_RET_FAIL; + + if (0 != get_param(param, 1, cpuname, sizeof(cpuname))) + *cpuname = '\0'; + + /* default parameter */ + if (*cpuname == '\0') zbx_snprintf(cpuname, sizeof(cpuname), "all"); - } - if(strncmp(cpuname, "all", sizeof(cpuname))) - { + + if (0 != strncmp(cpuname, "all", sizeof(cpuname))) return SYSINFO_RET_FAIL; - } - - if(get_param(param, 2, mode, sizeof(mode)) != 0) - { - mode[0] = '\0'; - } - if(mode[0] == '\0') - { - /* default parameter */ - zbx_snprintf(mode, sizeof(mode), "avg1"); - } - for(i=0; fl[i].mode!=0; i++) - { - if(strncmp(mode, fl[i].mode, MAX_STRING_LEN)==0) - { - return (fl[i].function)(cmd, param, flags, result); - } - } - - return SYSINFO_RET_FAIL; -} -int SYSTEM_CPU_SWITCHES(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) -{ - assert(result); + if (0 != get_param(param, 2, mode, sizeof(mode))) + *mode = '\0'; - init_result(result); - - return SYSINFO_RET_FAIL; -} + /* default parameter */ + if (*mode == '\0') + zbx_snprintf(mode, sizeof(mode), "avg1"); -int SYSTEM_CPU_INTR(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) -{ - assert(result); + for (i = 0; fl[i].mode != 0; i++) + if (0 == strncmp(mode, fl[i].mode, MAX_STRING_LEN)) + return (fl[i].function)(result); - init_result(result); - return SYSINFO_RET_FAIL; } - diff --git a/src/libs/zbxsysinfo/openbsd/diskspace.c b/src/libs/zbxsysinfo/openbsd/diskspace.c index 73d0d467..0ecdfadc 100644 --- a/src/libs/zbxsysinfo/openbsd/diskspace.c +++ b/src/libs/zbxsysinfo/openbsd/diskspace.c @@ -173,4 +173,3 @@ FS_FNCLIST return SYSINFO_RET_FAIL; } - diff --git a/src/libs/zbxsysinfo/openbsd/inodes.c b/src/libs/zbxsysinfo/openbsd/inodes.c index 9f240b08..feb39727 100644 --- a/src/libs/zbxsysinfo/openbsd/inodes.c +++ b/src/libs/zbxsysinfo/openbsd/inodes.c @@ -158,7 +158,7 @@ FS_FNCLIST if (num_param(param) > 2) return SYSINFO_RET_FAIL; - if (0 != get_param(param, 1, fsname, sizeof(mode))) + if (0 != get_param(param, 1, fsname, sizeof(fsname))) return SYSINFO_RET_FAIL; if (0 != get_param(param, 2, mode, sizeof(mode))) diff --git a/src/libs/zbxsysinfo/openbsd/kernel.c b/src/libs/zbxsysinfo/openbsd/kernel.c index cbe738db..245e76a4 100644 --- a/src/libs/zbxsysinfo/openbsd/kernel.c +++ b/src/libs/zbxsysinfo/openbsd/kernel.c @@ -23,6 +23,7 @@ int KERNEL_MAXFILES(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { +#ifdef HAVE_FUNCTION_SYSCTL_KERN_MAXFILES int mib[2]; size_t len; int maxfiles; @@ -42,10 +43,14 @@ int KERNEL_MAXFILES(const char *cmd, const char *param, unsigned flags, AGENT_RE SET_UI64_RESULT(result, maxfiles); return SYSINFO_RET_OK; +#else + return SYSINFO_RET_FAIL; +#endif } int KERNEL_MAXPROC(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { +#ifdef HAVE_FUNCTION_SYSCTL_KERN_MAXPROC int mib[2]; size_t len; int maxproc; @@ -65,4 +70,7 @@ int KERNEL_MAXPROC(const char *cmd, const char *param, unsigned flags, AGENT_RES SET_UI64_RESULT(result, maxproc); return SYSINFO_RET_OK; +#else + return SYSINFO_RET_FAIL; +#endif } diff --git a/src/libs/zbxsysinfo/openbsd/memory.c b/src/libs/zbxsysinfo/openbsd/memory.c index 443f6d57..e59f72af 100644 --- a/src/libs/zbxsysinfo/openbsd/memory.c +++ b/src/libs/zbxsysinfo/openbsd/memory.c @@ -23,13 +23,12 @@ static int get_vmmemory_stat(zbx_uint64_t *total, zbx_uint64_t *free, zbx_uint64_t *used, zbx_uint64_t *shared, double *pfree, double *pused) { - int mib[2]; +#if defined(HAVE_SYS_VMMETER_VMTOTAL) + int mib[] = {CTL_VM, VM_METER}; struct vmtotal v; size_t len; len = sizeof(v); - mib[0] = CTL_VM; - mib[1] = VM_METER; if (0 != sysctl(mib, 2, &v, &len, NULL, 0)) return SYSINFO_RET_FAIL; @@ -48,6 +47,9 @@ static int get_vmmemory_stat(zbx_uint64_t *total, zbx_uint64_t *free, zbx_uint64 *pused = (double)(100.0 * v.t_rm) / (v.t_rm + v.t_free); return SYSINFO_RET_OK; +#else + return SYSINFO_RET_FAIL; +#endif /* HAVE_SYS_VMMETER_VMTOTAL */ } static int VM_MEMORY_TOTAL(AGENT_RESULT *result) @@ -124,13 +126,11 @@ static int VM_MEMORY_PUSED(AGENT_RESULT *result) static int VM_MEMORY_BUFFERS(AGENT_RESULT *result) { - int mib[2], pages; + int mib[] = {CTL_VM, VM_NKMEMPAGES}, pages; size_t len; zbx_uint64_t value; len = sizeof(pages); - mib[0] = CTL_VM; - mib[1] = VM_NKMEMPAGES; if (0 != sysctl(mib, 2, &pages, &len, NULL, 0)) return SYSINFO_RET_FAIL; @@ -144,14 +144,12 @@ static int VM_MEMORY_BUFFERS(AGENT_RESULT *result) static int VM_MEMORY_CACHED(AGENT_RESULT *result) { - int mib[2]; + int mib[] = {CTL_VM, VM_UVMEXP}; struct uvmexp v; size_t len; zbx_uint64_t value; len = sizeof(v); - mib[0] = CTL_VM; - mib[1] = VM_UVMEXP; if (0 != sysctl(mib, 2, &v, &len, NULL, 0)) return SYSINFO_RET_FAIL; @@ -207,4 +205,3 @@ MEM_FNCLIST return SYSINFO_RET_FAIL; } - diff --git a/src/libs/zbxsysinfo/openbsd/net.c b/src/libs/zbxsysinfo/openbsd/net.c index 284ae94f..92493c22 100644 --- a/src/libs/zbxsysinfo/openbsd/net.c +++ b/src/libs/zbxsysinfo/openbsd/net.c @@ -94,9 +94,7 @@ static int get_ifdata(const char *if_name, zbx_uint64_t *ibytes, zbx_uint64_t *i if (kvm_read(kp, (u_long)ifp, &v, len) < len) break; - if (0 == strcmp(if_name, "all") || 0 == strcmp(if_name, v.if_xname)) { - fprintf(stderr, "----- %s\n", v.if_xname); - + if (*if_name == '\0' || 0 == strcmp(if_name, v.if_xname)) { if (ibytes) *ibytes += v.if_ibytes; if (ipackets) @@ -212,10 +210,6 @@ NET_FNCLIST if (0 != get_param(param, 1, if_name, sizeof(if_name))) *if_name = '\0'; - /* default parameter */ - if (*if_name == '\0') - zbx_snprintf(if_name, sizeof(if_name), "all"); - if (0 != get_param(param, 2, mode, sizeof(mode))) *mode = '\0'; @@ -298,10 +292,6 @@ NET_FNCLIST if (0 != get_param(param, 1, if_name, sizeof(if_name))) *if_name = '\0'; - /* default parameter */ - if (*if_name == '\0') - zbx_snprintf(if_name, sizeof(if_name), "all"); - if (0 != get_param(param, 2, mode, sizeof(mode))) *mode = '\0'; @@ -396,10 +386,6 @@ NET_FNCLIST if (0 != get_param(param, 1, if_name, sizeof(if_name))) *if_name = '\0'; - /* default parameter */ - if (*if_name == '\0') - zbx_snprintf(if_name, sizeof(if_name), "all"); - if (0 != get_param(param, 2, mode, sizeof(mode))) *mode = '\0'; @@ -438,10 +424,6 @@ int NET_IF_COLLISIONS(const char *cmd, const char *param, unsigned flags, AG if (0 != get_param(param, 1, if_name, sizeof(if_name))) *if_name = '\0'; - /* default parameter */ - if (*if_name == '\0') - zbx_snprintf(if_name, sizeof(if_name), "all"); - if (SYSINFO_RET_OK != get_ifdata(if_name, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &value)) return SYSINFO_RET_FAIL; diff --git a/src/libs/zbxsysinfo/openbsd/proc.c b/src/libs/zbxsysinfo/openbsd/proc.c index 246f1315..e433fd46 100644 --- a/src/libs/zbxsysinfo/openbsd/proc.c +++ b/src/libs/zbxsysinfo/openbsd/proc.c @@ -34,7 +34,7 @@ #define ARGS_START_SIZE 64 -int proc_argv(pid_t pid, char ***argv, size_t *argv_alloc, int *argc) +static int proc_argv(pid_t pid, char ***argv, size_t *argv_alloc, int *argc) { size_t sz; int mib[4], ret; @@ -68,7 +68,7 @@ retry: return SUCCEED; } -void collect_args(char **argv, int argc, char **args, size_t *args_alloc) +static void collect_args(char **argv, int argc, char **args, size_t *args_alloc) { int i, args_offset, len; diff --git a/src/libs/zbxsysinfo/openbsd/uptime.c b/src/libs/zbxsysinfo/openbsd/uptime.c index 71d42fb8..88487c6d 100644 --- a/src/libs/zbxsysinfo/openbsd/uptime.c +++ b/src/libs/zbxsysinfo/openbsd/uptime.c @@ -24,8 +24,8 @@ int SYSTEM_UPTIME(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { #ifdef HAVE_FUNCTION_SYSCTL_KERN_BOOTTIME - size_t len; int mib[2], now; + size_t len; struct timeval uptime; assert(result); @@ -37,7 +37,7 @@ int SYSTEM_UPTIME(const char *cmd, const char *param, unsigned flags, AGENT_RESU len = sizeof(struct timeval); - if (-1 == sysctl(mib, 2, &uptime, &len, NULL, 0)) + if (0 != sysctl(mib, 2, &uptime, &len, NULL, 0)) return SYSINFO_RET_FAIL; now = time(NULL); diff --git a/src/zabbix_agent/cpustat.c b/src/zabbix_agent/cpustat.c index e32329e9..e226ef9b 100644 --- a/src/zabbix_agent/cpustat.c +++ b/src/zabbix_agent/cpustat.c @@ -72,6 +72,21 @@ static int zbx_get_cpu_num(void) return ncpu; +#elif defined(HAVE_FUNCTION_SYSCTL_HW_NCPU) + /* NetBSD 3.1 i386; NetBSD 4.0 i386 */ + /* OpenBSD 4.2 i386 */ + /* FreeBSD 6.2 i386; FreeBSD 7.0 i386 */ + size_t len; + int mib[] = {CTL_HW, HW_NCPU}, ncpu; + + len = sizeof(ncpu); + + if (0 != sysctl(mib, 2, &ncpu, &len, NULL, 0)) { + zabbix_log(LOG_LEVEL_WARNING , "Failed sysctl to determine number of CPUs, adjust to 1"); + return 1; + } + + return ncpu; #elif defined(HAVE_PROC_CPUINFO) FILE *f = NULL; -- cgit