summaryrefslogtreecommitdiffstats
path: root/src/libs/zbxsysinfo/aix/proc.c
diff options
context:
space:
mode:
authorosmiy <osmiy@97f52cf1-0a1b-0410-bd0e-c28be96e8082>2005-11-04 10:27:22 +0000
committerosmiy <osmiy@97f52cf1-0a1b-0410-bd0e-c28be96e8082>2005-11-04 10:27:22 +0000
commitfe983de869b773807b40060e7a08e4a7259765c1 (patch)
treef99267eb63593054e7ff441a822cfeb14f97aca5 /src/libs/zbxsysinfo/aix/proc.c
parent15bfeda02a7c9657638765a2789fe14ac0ec73df (diff)
downloadzabbix-fe983de869b773807b40060e7a08e4a7259765c1.tar.gz
zabbix-fe983de869b773807b40060e7a08e4a7259765c1.tar.xz
zabbix-fe983de869b773807b40060e7a08e4a7259765c1.zip
fixed 'system.swap.in' and 'system.swap.out' functions (Eugene)
git-svn-id: svn://svn.zabbix.com/trunk@2269 97f52cf1-0a1b-0410-bd0e-c28be96e8082
Diffstat (limited to 'src/libs/zbxsysinfo/aix/proc.c')
-rw-r--r--src/libs/zbxsysinfo/aix/proc.c494
1 files changed, 473 insertions, 21 deletions
diff --git a/src/libs/zbxsysinfo/aix/proc.c b/src/libs/zbxsysinfo/aix/proc.c
index 7ec184d7..5153605c 100644
--- a/src/libs/zbxsysinfo/aix/proc.c
+++ b/src/libs/zbxsysinfo/aix/proc.c
@@ -19,33 +19,485 @@
#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>
-
#include "common.h"
#include "sysinfo.h"
-/*
-#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)
+#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)
{
- /* in this moment this function for this platform unsupported */
- return SYSINFO_RET_FAIL;
+#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];
+
+ 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;
+
+
+ struct passwd *usrinfo = NULL;
+ long long int llvalue = 0;
+
+ FILE *f;
+
+ double memsize = -1;
+ int proccount = 0;
+
+ assert(result);
+
+ clean_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(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/");
+ 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)
+ {
+ continue;
+ }
+
+ if(stat(filename,&buf)==0)
+ {
+ f=fopen(filename,"r");
+ if(f==NULL)
+ {
+ 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)
+ {
+ 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;
+ }
+ }
+
+
+ fclose(f);
+ }
+ }
+ 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,double *value, const char *msg, int mlen_max)
+int PROC_NUM(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result)
{
- /* in this moment this function for this platform unsupported */
+#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];
+
+ char procname[MAX_STRING_LEN];
+ char usrname[MAX_STRING_LEN];
+ char procstat[MAX_STRING_LEN];
+
+ int proc_ok = 0;
+ int usr_ok = 0;
+ int stat_ok = 0;
+
+ struct passwd *usrinfo = NULL;
+ long int lvalue = 0;
+
+ FILE *f;
+ int proccount = 0;
+
+ assert(result);
+
+ clean_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(get_param(param, 3, procstat, MAX_STRING_LEN) != 0)
+ {
+ procstat[0] = '\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;
+ }
+
+ 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/");
+ strncat(filename,entries->d_name,MAX_STRING_LEN);
+ strncat(filename,"/status",MAX_STRING_LEN);
+
+ if(stat(filename,&buf)==0)
+ {
+ f=fopen(filename,"r");
+ if(f==NULL)
+ {
+ 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)
+ {
+ 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++;
+ }
+
+ fclose(f);
+ }
+ }
+ closedir(dir);
+
+ result->type |= AR_DOUBLE;
+ result->dbl = (double) proccount;
+ return SYSINFO_RET_OK;
+#else
return SYSINFO_RET_FAIL;
+#endif
}
-