summaryrefslogtreecommitdiffstats
path: root/src/libs/zbxsysinfo/unknown/proc.c
diff options
context:
space:
mode:
authorosmiy <osmiy@97f52cf1-0a1b-0410-bd0e-c28be96e8082>2005-11-01 15:52:10 +0000
committerosmiy <osmiy@97f52cf1-0a1b-0410-bd0e-c28be96e8082>2005-11-01 15:52:10 +0000
commit2c9ca1001703a1ba0ae7b6d4cd238ebd44148fdc (patch)
treefc0cf11a6f9aff4e59bcef0f20986dc08fed12f3 /src/libs/zbxsysinfo/unknown/proc.c
parent12b9d9858224301d1abb19f08956964c6fabd9a0 (diff)
downloadzabbix-2c9ca1001703a1ba0ae7b6d4cd238ebd44148fdc.tar.gz
zabbix-2c9ca1001703a1ba0ae7b6d4cd238ebd44148fdc.tar.xz
zabbix-2c9ca1001703a1ba0ae7b6d4cd238ebd44148fdc.zip
- added new interface of sysinfo function for others platforms (Eugene)
git-svn-id: svn://svn.zabbix.com/trunk@2237 97f52cf1-0a1b-0410-bd0e-c28be96e8082
Diffstat (limited to 'src/libs/zbxsysinfo/unknown/proc.c')
-rw-r--r--src/libs/zbxsysinfo/unknown/proc.c488
1 files changed, 234 insertions, 254 deletions
diff --git a/src/libs/zbxsysinfo/unknown/proc.c b/src/libs/zbxsysinfo/unknown/proc.c
index 4d7cfe5d..d80dac13 100644
--- a/src/libs/zbxsysinfo/unknown/proc.c
+++ b/src/libs/zbxsysinfo/unknown/proc.c
@@ -19,172 +19,57 @@
#include "config.h"
-#include <errno.h>
-
-#include <string.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-
-#ifdef HAVE_PWD_H
-# include <pwd.h>
-#endif
-
-/* Definitions of uint32_t under OS/X */
-#ifdef HAVE_STDINT_H
- #include <stdint.h>
-#endif
-#ifdef HAVE_STRINGS_H
- #include <strings.h>
-#endif
-#ifdef HAVE_FCNTL_H
- #include <fcntl.h>
-#endif
-#ifdef HAVE_DIRENT_H
- #include <dirent.h>
-#endif
-/* Linux */
-#ifdef HAVE_SYS_VFS_H
- #include <sys/vfs.h>
-#endif
-#ifdef HAVE_SYS_SYSINFO_H
- #include <sys/sysinfo.h>
-#endif
-/* Solaris */
-#ifdef HAVE_SYS_STATVFS_H
- #include <sys/statvfs.h>
-#endif
-
-#ifdef HAVE_SYS_PROC_H
-# include <sys/proc.h>
-#endif
-/* Solaris */
-#ifdef HAVE_SYS_PROCFS_H
-/* This is needed to access the correct procfs.h definitions */
- #define _STRUCTURED_PROC 1
- #include <sys/procfs.h>
-#endif
-#ifdef HAVE_SYS_LOADAVG_H
- #include <sys/loadavg.h>
-#endif
-#ifdef HAVE_SYS_SOCKET_H
- #include <sys/socket.h>
-#endif
-#ifdef HAVE_NETINET_IN_H
- #include <netinet/in.h>
-#endif
-#ifdef HAVE_ARPA_INET_H
- #include <arpa/inet.h>
-#endif
-/* OpenBSD/Solaris */
-#ifdef HAVE_SYS_PARAM_H
- #include <sys/param.h>
-#endif
-
-#ifdef HAVE_SYS_MOUNT_H
- #include <sys/mount.h>
-#endif
-
-/* HP-UX */
-#ifdef HAVE_SYS_PSTAT_H
- #include <sys/pstat.h>
-#endif
-
-#ifdef HAVE_NETDB_H
- #include <netdb.h>
-#endif
-
-/* Solaris */
-#ifdef HAVE_SYS_SWAP_H
- #include <sys/swap.h>
-#endif
-
-/* FreeBSD */
-#ifdef HAVE_SYS_SYSCTL_H
- #include <sys/sysctl.h>
-#endif
+#include "common.h"
+#include "sysinfo.h"
-/* Solaris */
-#ifdef HAVE_SYS_SYSCALL_H
- #include <sys/syscall.h>
-#endif
+#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)
-/* FreeBSD */
-#ifdef HAVE_VM_VM_PARAM_H
- #include <vm/vm_param.h>
-#endif
-/* FreeBSD */
-#ifdef HAVE_SYS_VMMETER_H
- #include <sys/vmmeter.h>
-#endif
-/* FreeBSD */
-#ifdef HAVE_SYS_TIME_H
- #include <sys/time.h>
-#endif
+ DIR *dir;
+ struct dirent *entries;
+ struct stat buf;
+ char filename[MAX_STRING_LEN];
+ char line[MAX_STRING_LEN];
-#ifdef HAVE_MACH_HOST_INFO_H
- #include <mach/host_info.h>
-#endif
-#ifdef HAVE_MACH_MACH_HOST_H
- #include <mach/mach_host.h>
-#endif
+ char name1[MAX_STRING_LEN];
+ char name2[MAX_STRING_LEN];
+ char procname[MAX_STRING_LEN];
+ char usrname[MAX_STRING_LEN];
+ char mode[MAX_STRING_LEN];
-#ifdef HAVE_KSTAT_H
- #include <kstat.h>
-#endif
+ int proc_ok = 0;
+ int usr_ok = 0;
+ int do_task = DO_SUM;
-#ifdef HAVE_LDAP
- #include <ldap.h>
-#endif
-#include "common.h"
-#include "sysinfo.h"
+ struct passwd *usrinfo = NULL;
+ long int lvalue = 0;
-/*
-#define FDI(f, m) fprintf(stderr, "DEBUG INFO: " f "\n" , m) // show debug info to stderr
-#define SDI(m) FDI("%s", m) // string info
-#define IDI(i) FDI("%i", i) // integer info
-*/
-int PROC_MEMORY(const char *cmd, const char *param,double *value, const char *msg, int mlen_max)
-{
- /* in this moment this function for this platform unsupported */
- return SYSINFO_RET_FAIL;
-}
+ FILE *f;
-int PROC_NUM(const char *cmd, const char *param,double *value, const char *msg, int mlen_max)
-{
-#if defined(HAVE_PROC_0_PSINFO)
- DIR *dir;
- struct dirent *entries;
- struct stat buf;
-
- char filename[MAX_STRING_LEN];
- char procname[MAX_STRING_LEN];
-
- int proc_ok = 0;
- int usr_ok = 0;
- int stat_ok = 0;
+ double memsize = -1;
+ int proccount = 0;
- char pr_state = 0;
-
- int fd;
-/* In the correct procfs.h, the structure name is psinfo_t */
- psinfo_t psinfo;
+ assert(result);
- int proccount=0;
-
+ clean_result(result);
+
if(num_param(param) > 3)
{
- return SYSINFO_RET_FAIL;
+ return SYSINFO_RET_FAIL;
}
-
+
if(get_param(param, 1, procname, MAX_STRING_LEN) != 0)
{
- return SYSINFO_RET_FAIL;
- }
+ return SYSINFO_RET_FAIL;
+ }
if(get_param(param, 2, usrname, MAX_STRING_LEN) != 0)
{
@@ -192,130 +77,216 @@ int PROC_NUM(const char *cmd, const char *param,double *value, const char *msg,
}
else
{
- if(usrname[0] != 0)
- {
- usrinfo = getpwnam(usrname);
- if(usrinfo == NULL)
- {
- /* incorrect user name */
- return SYSINFO_RET_FAIL;
- }
- }
- }
-
- if(get_param(param, 3, procstat, MAX_STRING_LEN) != 0)
+ if(usrname[0] != 0)
{
- strscpy(procstat,"all");
+ 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(procstat,"run") == 0)
- {
- /* running */
- pr_state = SRUN;
- }
- else if(strcmp(procstat,"sleep") == 0)
- {
- /* awaiting an event */
- pr_state = SSLEEP;
- }
- else if(strcmp(procstat,"zomb") == 0)
- {
- pr_state = SZOMB;
- }
- else if(strcmp(procstat,"all") == 0)
- {
- procstat[0] = 0;
- }
- else
- {
+ 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;
- }
-
- dir=opendir("/proc");
- if(NULL == dir)
+ }
+
+ while((entries=readdir(dir))!=NULL)
+ {
+ proc_ok = 0;
+ usr_ok = 0;
+
+ strscpy(filename,"/proc/");
+ strncat(filename,entries->d_name,MAX_STRING_LEN);
+ strncat(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)
{
- return SYSINFO_RET_FAIL;
+ continue;
}
- while((entries=readdir(dir))!=NULL)
+ if(stat(filename,&buf)==0)
{
- proc_ok = 0;
- usr_ok = 0;
- stat_ok = 0;
-
- strscpy(filename,"/proc/");
- strncat(filename,entries->d_name,MAX_STRING_LEN);
- strncat(filename,"/psinfo",MAX_STRING_LEN);
+ f=fopen(filename,"r");
+ if(f==NULL)
+ {
+ continue;
+ }
- if(stat(filename,&buf)==0)
+ if(procname[0] != 0)
{
- fd = open (filename, O_RDONLY);
- if (fd != -1)
+ 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)
{
- if(read(fd, &psinfo, sizeof(psinfo)) == -1)
+ fclose(f);
+ continue;
+ }
+ }
+ else
+ {
+ proc_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 && usr_ok)
+ {
+ while(fgets(line, MAX_STRING_LEN, f) != NULL)
+ {
+
+ if(sscanf(line, "%s\t%li %s\n", name1, &lvalue, name2) != 3)
+ {
+ continue;
+ }
+
+ if(strcmp(name1,"VmSize:") != 0)
+ {
+ continue;
+ }
+
+ proccount++;
+
+ if(strcasecmp(name2, "kB") == 0)
+ {
+ lvalue <<= 10;
+ }
+ else if(strcasecmp(name2, "mB") == 0)
+ {
+ lvalue <<= 20;
+ }
+ else if(strcasecmp(name2, "GB") == 0)
+ {
+ lvalue <<= 30;
+ }
+ else if(strcasecmp(name2, "TB") == 0)
{
- close(fd);
- closedir(dir);
- return SYSINFO_RET_FAIL;
+ lvalue <<= 40;
+ }
+
+ if(memsize < 0)
+ {
+ memsize = (double) lvalue;
}
else
{
- if(procname[0] != 0)
+ if(do_task == DO_MAX)
{
- if(strcmp(procname, psinfo.pr_fname) == 0)
- {
- proc_ok = 1;
- }
+ memsize = MAX(memsize, (double) lvalue);
}
- else
+ else if(do_task == DO_MIN)
{
- proc_ok = 1;
- }
-
- if(usrinfo != NULL)
- {
- /* uid_t pr_uid; real user id */
- if(usrinfo->pw_uid == psinfo.pr_uid)
- {
- usr_ok = 1;
- }
- }
- else
- {
- usr_ok = 1;
- }
-
- if(procstat[0] != 0)
- {
- /* char pr_state; numeric lwp state */
- if(psinfo.pr_lwp.pr_state == pr_state)
- {
- state_ok = 1;
- }
+ memsize = MIN(memsize, (double) lvalue);
}
else
{
- usr_ok = 1;
- }
-
- if(proc_ok && usr_ok && state_ok)
- {
- proccount++;
+ memsize += (double) lvalue;
}
}
- close (fd);
- }
- else
- {
- continue;
+
+ break;
}
}
+
+
+ fclose(f);
}
- closedir(dir);
- *value=(double)proccount;
- return SYSINFO_RET_OK;
-
-#elif defined(HAVE_PROC_1_STATUS)
+ }
+ closedir(dir);
+
+ if(memsize < 0)
+ {
+ /* incorrect process name */
+ memsize = 0;
+ }
+
+ if(do_task == DO_AVG)
+ {
+ memsize /= (double)proccount;
+ }
+
+ result->type |= AR_DOUBLE;
+ result->dbl = memsize;
+ return SYSINFO_RET_OK;
+#else
+ return SYSINFO_RET_FAIL;
+#endif
+}
+
+int PROC_NUM(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result)
+{
+#if defined(HAVE_PROC_1_STATUS)
DIR *dir;
struct dirent *entries;
@@ -339,6 +310,10 @@ int PROC_NUM(const char *cmd, const char *param,double *value, const char *msg,
FILE *f;
+ assert(result);
+
+ clean_result(result);
+
int proccount = 0;
if(num_param(param) > 3)
@@ -368,10 +343,15 @@ int PROC_NUM(const char *cmd, const char *param,double *value, const char *msg,
}
}
- if(get_param(param, 3, procstat, MAX_STRING_LEN) != 0)
- {
- strscpy(procstat,"all");
- }
+ if(get_param(param, 3, procstat, MAX_STRING_LEN) != 0)
+ {
+ procstat[0] = '\0';
+ }
+
+ if(procstat[0] == '\0')
+ {
+ strscpy(procstat, "all");
+ }
if(strcmp(procstat,"run") == 0)
{
@@ -514,10 +494,10 @@ int PROC_NUM(const char *cmd, const char *param,double *value, const char *msg,
}
closedir(dir);
- *value = (double) proccount;
+ result->type |= AR_DOUBLE;
+ result->dbl = (double) proccount;
return SYSINFO_RET_OK;
#else
return SYSINFO_RET_FAIL;
#endif
}
-