ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/host/ihost-perl/plugins/solaris/solaris.c
(Generate patch)

Comparing projects/cms/source/host/ihost-perl/plugins/solaris/solaris.c (file contents):
Revision 1.4 by pajs, Thu Mar 14 17:05:53 2002 UTC vs.
Revision 1.10 by tdb, Tue May 21 16:47:12 2002 UTC

# Line 1 | Line 1
1 + /*
2 + * i-scream central monitoring system
3 + * http://www.i-scream.org.uk
4 + * Copyright (C) 2000-2002 i-scream
5 + *
6 + * This program is free software; you can redistribute it and/or
7 + * modify it under the terms of the GNU General Public License
8 + * as published by the Free Software Foundation; either version 2
9 + * of the License, or (at your option) any later version.
10 + *
11 + * This program 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
14 + * GNU General Public License for more details.
15 + *
16 + * You should have received a copy of the GNU General Public License
17 + * along with this program; if not, write to the Free Software
18 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 + */
20 +
21   #include <stdio.h>
22   #include <sys/types.h>
23   #include <sys/statvfs.h>
# Line 9 | Line 29
29   #include <kstat.h>
30   #include <sys/sysinfo.h>
31   #include <unistd.h>
32 + #include <sys/stat.h>
33 + #include <sys/swap.h>
34 + #include <stdlib.h>
35 + #include <strings.h>
36 + #include <time.h>
37  
38   void die(){
39    exit(1);
# Line 29 | Line 54 | void diskStats(){
54    while((getmntent(f, &mp)) == 0){      
55      if ((statvfs(mp.mnt_mountp, &df)) !=0){
56        errf("Failed to gets fs stats (%m)");
57 <      die();
57 >      continue;
58      }
59 +
60 +    if((((strcmp(mp.mnt_fstype,"ufs"))==0) || (strcmp(mp.mnt_fstype,"tmpfs")) ==0)){
61          
62 <    printf("packet.disk.p%d.attributes.mount %s\n", counter, mp.mnt_mountp);
63 <    printf("packet.disk.p%d.attributes.name %s\n", counter, mp.mnt_special);
64 <    printf("packet.disk.p%d.attributes.kbytes %lu\n",counter, ((df.f_frsize/1024) * df.f_blocks));
65 <    printf("packet.disk.p%d.attributes.used %lu\n",counter, (((df.f_frsize/1024) * df.f_blocks) -((df.f_frsize/1024) * df.f_bfree)));
66 <    printf("packet.disk.p%d.attributes.avail %lu\n",counter, (df.f_frsize/1024) * df.f_bavail);
67 <    printf("packet.disk.p%d.attributes.totalinodes %lu\n", counter, df.f_files);
68 <    printf("packet.disk.p%d.attributes.freeinodes %lu\n",counter, df.f_ffree);
62 >      printf("packet.disk.p%d.attributes.mount %s\n", counter, mp.mnt_mountp);
63 >      printf("packet.disk.p%d.attributes.name %s\n", counter, mp.mnt_special);
64 >      printf("packet.disk.p%d.attributes.kbytes %lu\n",counter, ((df.f_frsize/1024) * df.f_blocks));
65 >      printf("packet.disk.p%d.attributes.used %lu\n",counter, (((df.f_frsize/1024) * df.f_blocks) -((df.f_frsize/1024) * df.f_bfree)));
66 >      printf("packet.disk.p%d.attributes.avail %lu\n",counter, (df.f_frsize/1024) * df.f_bavail);
67 >      printf("packet.disk.p%d.attributes.totalinodes %lu\n", counter, df.f_files);
68 >      printf("packet.disk.p%d.attributes.freeinodes %lu\n",counter, df.f_ffree);
69      
70 <    counter++;
70 >      counter++;
71 >    }
72    }
73   }
74  
# Line 92 | Line 120 | void userStats(){
120   void systemStats(){
121    kstat_ctl_t *kc;
122    kstat_t *ksp;
123 <  kstat_named_t *knp;
123 >  kstat_named_t *kn;
124 >  int pagesize;
125    cpu_stat_t cs;
126 <  uint_t cpustats[4][2];
126 >  uint_t cpustats[5][2];
127    float usage;
128 <  uint_t user, kernel, idle, iowait, total;
128 >  uint_t user, kernel, idle, iowait, swap, total;
129 >  ulong totalmem, freemem;
130 >  long swaptotal, swapused;
131 >  time_t uptime,curtime;
132 >  
133 >  struct anoninfo ai;
134  
135 +
136    if ((kc = kstat_open()) == NULL) {
137      errf("kstat_open failure (%m)");
138      die();
# Line 118 | Line 153 | void systemStats(){
153    cpustats[1][0]=cs.cpu_sysinfo.cpu[CPU_WAIT];
154    cpustats[2][0]=cs.cpu_sysinfo.cpu[CPU_KERNEL];
155    cpustats[3][0]=cs.cpu_sysinfo.cpu[CPU_IDLE];
156 +  cpustats[4][0]=cs.cpu_sysinfo.cpu[CPU_STATES];
157    
158    sleep(1);
159    
# Line 139 | Line 175 | void systemStats(){
175    cpustats[1][1]=cs.cpu_sysinfo.cpu[CPU_WAIT];
176    cpustats[2][1]=cs.cpu_sysinfo.cpu[CPU_KERNEL];
177    cpustats[3][1]=cs.cpu_sysinfo.cpu[CPU_IDLE];
178 +  cpustats[4][1]=cs.cpu_sysinfo.cpu[CPU_STATES];
179  
180    user=cpustats[0][1]-cpustats[0][0];
181    iowait=cpustats[1][1]-cpustats[1][0];
182    kernel=cpustats[2][1]-cpustats[2][0];
183    idle=cpustats[3][1]-cpustats[3][0];
184 +  swap=cpustats[4][1]-cpustats[4][0];
185  
186 <  total=user+kernel+idle+iowait;
186 >  total=user+kernel+idle+iowait+swap;
187    
188    usage=((((float)user)/((float)total))*100.00);
189  
152  printf("user : %u\n",user);
153  printf("total : %u\n",total);
154
190    printf("packet.cpu.user %3.2f\n", usage);
191    usage=((((float)kernel)/((float)total))*100.00);
192    printf("packet.cpu.kernel %3.2f\n", usage);
# Line 159 | Line 194 | void systemStats(){
194    printf("packet.cpu.idle %3.2f\n", usage);
195    usage=((((float)iowait)/((float)total))*100.00);
196    printf("packet.cpu.iowait %3.2f\n", usage);
197 +  usage=((((float)swap)/((float)total))*100.00);
198 +  printf("packet.cpu.swap %3.2f\n", usage);
199 +  
200 +  /* MEMORY STATS */
201 +  
202 +  if((pagesize=sysconf(_SC_PAGESIZE)) == -1){
203 +    errf("Failed to get pagesize (%m)");
204 +    die();
205 +  }
206  
207 <  /* if((ksp = kstat_lookup(kc, "unix", -1, "system_pages")) == NULL){
208 <    errf("Mem lookup failed (%m)");
207 >  if((totalmem=sysconf(_SC_PHYS_PAGES)) == -1){
208 >    errf("Failed to get total memory");
209      die();
210    }
211 +  
212 +  totalmem=((totalmem*pagesize)/1024)/1024;
213 +  
214 +  ksp = kstat_lookup(kc, "unix", 0, "system_pages");
215 +  if (kstat_read(kc, ksp, 0) == -1) {
216 +    perror("kstat_read");
217 +    die();
218 +  }  
219  
220 <  if((knp = (kstat_named_t *)kstat_data_lookup(ksp, "pagesfree")) == NULL){
221 <    errf("Mem lookup failed (%m)");
220 >  if((kn=kstat_data_lookup(ksp, "freemem")) == NULL){
221 >    errf("Failed to get free memory (%m)");
222      die();
223    }
224    
225 <  printf("%ui32\n",  knp->value.ui32);
226 <  */
227 < }
228 <    
225 >  freemem=(((kn->value.ul)*pagesize)/1024)/1024;
226 >
227 >  printf("packet.memory.total %lu\n",totalmem);
228 >  printf("packet.memory.free %lu\n",freemem);
229 >  printf("packet.memory.used %lu\n",(totalmem-freemem));
230 >
231 >
232 >  if (swapctl(SC_AINFO, &ai) == -1) {
233 >    errf("Failed to get swap details (%m)");
234 >    die();
235 >  }
236 >
237 >  swaptotal=((ai.ani_max/1024)*pagesize)/1024;
238 >  swapused=((ai.ani_resv/1024)*pagesize)/1024;
239 >
240 >  /* Ok this is actually for TOP combatability, but this is not
241 >     total swap, cos it actually also seems to include memory stats. */
242 >  printf("packet.swap.total %ld\n",swaptotal);
243 >  printf("packet.swap.used %ld\n", swapused);
244 >  printf("packet.swap.free %lu\n", (swaptotal-swapused));
245 >
246 >  ksp=kstat_lookup(kc, "unix", -1, "system_misc");
247 >  if (kstat_read(kc, ksp, 0) == -1) {
248 >    errf("Failed to get uptime (%m)");
249 >    die();
250 >  }
251 >
252 >  if((kn=kstat_data_lookup(ksp, "boot_time")) == NULL){
253 >    errf("Failed to get uptime (%m)");
254 >    die();
255 >  }
256 >
257 >  uptime=(kn->value.ui32);
258 >  time(&curtime);
259 >  printf("packet.os.uptime %ld\n", (curtime-uptime));
260 >
261 > }    
262    
263 + void processStats() {
264 +    int sleeping=-1;
265 +    int zombie=0;
266 +    int stopped=0;
267 +    int running=0;
268 +    char *line;
269 +    char *line_p;
270  
271 +
272 +    FILE *f;
273 +
274 +    if((f=popen("/bin/ps -Al" , "r")) == NULL) {
275 +        errf("Failed to get process stats (%m)");
276 +        die();
277 +    }
278 +
279 +    while((line=fpgetline(f)) != NULL) {
280 +      line_p=line;
281 +      for(; (*line_p == ' ') && (*line_p != '\0'); line_p++);
282 +      line_p=strchr(line_p, ' ');
283 +      for(; (*line_p == ' ') && (*line_p != '\0'); line_p++);
284 +      if (line_p==NULL) abort();
285 +      /* Ok, we should now be at the state :) .. */
286 +      if (*line_p=='S') sleeping++;
287 +      if (*line_p=='R') running++;
288 +      if (*line_p=='Z') zombie++;
289 +      if (*line_p=='T') stopped++;
290 +    }
291 +
292 +    if((pclose(f)) == -1) {
293 +        errf("Failed to close process stats (%m)");
294 +        die();
295 +    }
296 +
297 +    printf("packet.processes.sleeping %d\n",sleeping);
298 +    printf("packet.processes.cpu %d\n",running);
299 +    printf("packet.processes.zombie %d\n",zombie);
300 +    printf("packet.processes.stopped %d\n", stopped);
301 +    printf("packet.processes.total %d\n", (sleeping+running+zombie+stopped));
302 +
303 + }
304 +
305 +
306   int main(){
307    diskStats();
308    osStats();
309    loadStats();
310    userStats();
311    systemStats();
312 <
312 >  processStats();
313    return 0;
314   }
315  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines