| 1 |
/* |
| 2 |
* i-scream central monitoring system |
| 3 |
* http://www.i-scream.org |
| 4 |
* Copyright (C) 2000-2004 i-scream |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 2.1 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this library; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 19 |
* 02111-1307 USA |
| 20 |
* |
| 21 |
* $Id$ |
| 22 |
*/ |
| 23 |
|
| 24 |
#ifdef HAVE_CONFIG_H |
| 25 |
#include "config.h" |
| 26 |
#endif |
| 27 |
|
| 28 |
#include "statgrab.h" |
| 29 |
#if defined(SOLARIS) || defined(LINUX) |
| 30 |
#include <stdio.h> |
| 31 |
#include <stdlib.h> |
| 32 |
#include <sys/types.h> |
| 33 |
#include <dirent.h> |
| 34 |
#include <string.h> |
| 35 |
#endif |
| 36 |
|
| 37 |
#include "tools.h" |
| 38 |
#ifdef SOLARIS |
| 39 |
#include <procfs.h> |
| 40 |
#include <limits.h> |
| 41 |
#define PROC_LOCATION "/proc" |
| 42 |
#define MAX_FILE_LENGTH PATH_MAX |
| 43 |
#endif |
| 44 |
#ifdef LINUX |
| 45 |
#include <limits.h> |
| 46 |
#define PROC_LOCATION "/proc" |
| 47 |
#define MAX_FILE_LENGTH PATH_MAX |
| 48 |
#endif |
| 49 |
#ifdef ALLBSD |
| 50 |
#include <kvm.h> |
| 51 |
#include <sys/param.h> |
| 52 |
#include <sys/sysctl.h> |
| 53 |
#endif |
| 54 |
#ifdef FREEBSD |
| 55 |
#include <sys/user.h> |
| 56 |
#endif |
| 57 |
|
| 58 |
process_stat_t *get_process_stats(){ |
| 59 |
|
| 60 |
static process_stat_t process_stat; |
| 61 |
|
| 62 |
#if defined(SOLARIS) || defined(LINUX) |
| 63 |
DIR *proc_dir; |
| 64 |
struct dirent *dir_entry; |
| 65 |
char filename[MAX_FILE_LENGTH]; |
| 66 |
FILE *f; |
| 67 |
#endif |
| 68 |
#ifdef LINUX |
| 69 |
char *line_ptr; |
| 70 |
#endif |
| 71 |
#ifdef SOLARIS |
| 72 |
psinfo_t process_info; |
| 73 |
#endif |
| 74 |
#ifdef ALLBSD |
| 75 |
struct kinfo_proc *kp_stats; |
| 76 |
kvm_t *kvmd; |
| 77 |
int procs; |
| 78 |
#endif |
| 79 |
|
| 80 |
process_stat.sleeping=0; |
| 81 |
process_stat.running=0; |
| 82 |
process_stat.zombie=0; |
| 83 |
process_stat.stopped=0; |
| 84 |
process_stat.total=0; |
| 85 |
|
| 86 |
#if defined(SOLARIS) || defined(LINUX) |
| 87 |
if((proc_dir=opendir(PROC_LOCATION))==NULL){ |
| 88 |
return NULL; |
| 89 |
} |
| 90 |
|
| 91 |
while((dir_entry=readdir(proc_dir))!=NULL){ |
| 92 |
if(atoi(dir_entry->d_name) == 0) continue; |
| 93 |
|
| 94 |
#ifdef SOLARIS |
| 95 |
snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/psinfo", dir_entry->d_name); |
| 96 |
#endif |
| 97 |
#ifdef LINUX |
| 98 |
snprintf(filename, MAX_FILE_LENGTH, "/proc/%s/status", dir_entry->d_name); |
| 99 |
#endif |
| 100 |
|
| 101 |
if((f=fopen(filename, "r"))==NULL){ |
| 102 |
/* Open failed.. Process since vanished, or the path was too long. |
| 103 |
* Ah well, move onwards to the next one */ |
| 104 |
continue; |
| 105 |
} |
| 106 |
|
| 107 |
#ifdef SOLARIS |
| 108 |
fread(&process_info, sizeof(psinfo_t), 1, f); |
| 109 |
if(process_info.pr_lwp.pr_state==1) process_stat.sleeping++; |
| 110 |
if(process_info.pr_lwp.pr_state==2) process_stat.running++; |
| 111 |
if(process_info.pr_lwp.pr_state==3) process_stat.zombie++; |
| 112 |
if(process_info.pr_lwp.pr_state==4) process_stat.stopped++; |
| 113 |
if(process_info.pr_lwp.pr_state==6) process_stat.running++; |
| 114 |
#endif |
| 115 |
#ifdef LINUX |
| 116 |
if((line_ptr=f_read_line(f, "State:"))==NULL){ |
| 117 |
fclose(f); |
| 118 |
continue; |
| 119 |
} |
| 120 |
if((line_ptr=strchr(line_ptr, '\t'))==NULL){ |
| 121 |
fclose(f); |
| 122 |
continue; |
| 123 |
} |
| 124 |
line_ptr++; |
| 125 |
if(line_ptr=='\0'){ |
| 126 |
fclose(f); |
| 127 |
continue; |
| 128 |
} |
| 129 |
|
| 130 |
if(*line_ptr=='S') process_stat.sleeping++; |
| 131 |
if(*line_ptr=='R') process_stat.running++; |
| 132 |
if(*line_ptr=='Z') process_stat.zombie++; |
| 133 |
if(*line_ptr=='T') process_stat.stopped++; |
| 134 |
if(*line_ptr=='D') process_stat.stopped++; |
| 135 |
#endif |
| 136 |
|
| 137 |
fclose(f); |
| 138 |
} |
| 139 |
closedir(proc_dir); |
| 140 |
#endif |
| 141 |
#ifdef ALLBSD |
| 142 |
if((kvmd = get_kvm()) == NULL){ |
| 143 |
return NULL; |
| 144 |
} |
| 145 |
|
| 146 |
kp_stats=kvm_getprocs(kvmd, KERN_PROC_ALL, 0, &procs); |
| 147 |
|
| 148 |
while(procs--){ |
| 149 |
#ifdef FREEBSD5 |
| 150 |
if (kp_stats[procs].ki_stat == SSLEEP) process_stat.sleeping++; |
| 151 |
if (kp_stats[procs].ki_stat == SWAIT) process_stat.sleeping++; |
| 152 |
if (kp_stats[procs].ki_stat == SLOCK) process_stat.sleeping++; |
| 153 |
if (kp_stats[procs].ki_stat == SRUN) process_stat.running++; |
| 154 |
if (kp_stats[procs].ki_stat == SIDL) process_stat.running++; |
| 155 |
if (kp_stats[procs].ki_stat == SZOMB) process_stat.zombie++; |
| 156 |
if (kp_stats[procs].ki_stat == SSTOP) process_stat.stopped++; |
| 157 |
|
| 158 |
#else |
| 159 |
if (kp_stats[procs].kp_proc.p_stat == SSLEEP) process_stat.sleeping++; |
| 160 |
if (kp_stats[procs].kp_proc.p_stat == SRUN) process_stat.running++; |
| 161 |
if (kp_stats[procs].kp_proc.p_stat == SIDL) process_stat.running++; |
| 162 |
if (kp_stats[procs].kp_proc.p_stat == SZOMB) process_stat.zombie++; |
| 163 |
if (kp_stats[procs].kp_proc.p_stat == SSTOP) process_stat.stopped++; |
| 164 |
#endif |
| 165 |
} |
| 166 |
#endif |
| 167 |
|
| 168 |
#ifdef CYGWIN |
| 169 |
return NULL; |
| 170 |
#endif |
| 171 |
|
| 172 |
process_stat.total=process_stat.sleeping+process_stat.running+process_stat.zombie+process_stat.stopped; |
| 173 |
|
| 174 |
return &process_stat; |
| 175 |
} |