diff options
author | osmiy <osmiy@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2005-11-01 15:52:10 +0000 |
---|---|---|
committer | osmiy <osmiy@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2005-11-01 15:52:10 +0000 |
commit | 2c9ca1001703a1ba0ae7b6d4cd238ebd44148fdc (patch) | |
tree | fc0cf11a6f9aff4e59bcef0f20986dc08fed12f3 /src/libs/zbxsysinfo/osf/proc.c | |
parent | 12b9d9858224301d1abb19f08956964c6fabd9a0 (diff) | |
download | zabbix-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/osf/proc.c')
-rw-r--r-- | src/libs/zbxsysinfo/osf/proc.c | 493 |
1 files changed, 473 insertions, 20 deletions
diff --git a/src/libs/zbxsysinfo/osf/proc.c b/src/libs/zbxsysinfo/osf/proc.c index 7f5c596b..d80dac13 100644 --- a/src/libs/zbxsysinfo/osf/proc.c +++ b/src/libs/zbxsysinfo/osf/proc.c @@ -19,32 +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 int lvalue = 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%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) + { + lvalue <<= 40; + } + + if(memsize < 0) + { + memsize = (double) lvalue; + } + else + { + if(do_task == DO_MAX) + { + memsize = MAX(memsize, (double) lvalue); + } + else if(do_task == DO_MIN) + { + memsize = MIN(memsize, (double) lvalue); + } + else + { + memsize += (double) lvalue; + } + } + + 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; + + assert(result); + + clean_result(result); + + int proccount = 0; + + 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 } - |