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.2 by pajs, Fri Mar 8 14:57:28 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>
24   #include <sys/mnttab.h>
25 < #include <ukcprog.h>
25 > #include "ukcprog.h"
26   #include <sys/utsname.h>
27   #include <sys/loadavg.h>
28 + #include <utmp.h>
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 < {
11 <   exit(1);
38 > void die(){
39 >  exit(1);
40   }
41  
42  
43 < void diskStats()
44 < {
45 <   struct mnttab mp;
46 <   struct statvfs df;
47 <   FILE *f;
48 <   int counter=0;
43 > void diskStats(){
44 >  struct mnttab mp;
45 >  struct statvfs df;
46 >  FILE *f;
47 >  int counter=0;
48 >  
49 >  if ((f=fopen("/etc/mnttab", "r" ))==NULL){
50 >      errf("Failed to open mounts (%m)");
51 >      die();
52 >  }
53    
54 <    if ((f=fopen("/etc/mnttab", "r" ))==NULL)
55 <     {
56 <        errf("Failed to open mounts (%m)");
57 <        die();
58 <     }
59 <  
60 <   while((getmntent(f, &mp)) == 0)
29 <     {      
30 <        if ((statvfs(mp.mnt_mountp, &df)) !=0)
31 <             {
32 <                errf("Failed to gets fs stats (%m)");
33 <                die();
34 <             }
54 >  while((getmntent(f, &mp)) == 0){      
55 >    if ((statvfs(mp.mnt_mountp, &df)) !=0){
56 >      errf("Failed to gets fs stats (%m)");
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);
69 <        
70 <        counter++;
71 <     }
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++;
71 >    }
72 >  }
73   }
74  
75 < void osStats()
76 < {
77 <   struct utsname os;
75 > void osStats(){
76 >  struct utsname os;
77 >  
78 >  if((uname(&os)) == -1){
79 >    errf("Failed to get os stats (%m)");
80 >    die();
81 >  }
82  
83 <   if((uname(&os)) == -1)
84 <     {
85 <        errf("Failed to get os stats (%m)");
86 <        die();
87 <     }
83 >  printf("packet.os.name %s\n", os.sysname);
84 >  printf("packet.os.release %s\n" , os.release);
85 >  printf("packet.os.version %s\n", os.version);
86 >  printf("packet.os.sysname %s\n" , os.nodename);
87 >  printf("packet.os.platform %s\n", os.machine);
88  
58   printf("packet.os.name %s\n", os.sysname);
59   printf("packet.os.release %s\n" , os.release);
60   printf("packet.os.version %s\n", os.version);
61   printf("packet.os.sysname %s\n" , os.nodename);
62   printf("packet.os.platform %s\n", os.machine);
63
89   }  
90    
91 < void loadStats()
67 < {
91 > void loadStats(){
92    double loadav[3];
93    
94 <  if((getloadavg(loadav,3)) == -1)
95 <    {
96 <      errf("Failed to get load averages (%m)");
73 <      die();
74 <  
94 >  if((getloadavg(loadav,3)) == -1){
95 >    errf("Failed to get load averages (%m)");
96 >    die();    
97    }
98    
99    printf("packet.load.load1 %.2f\n",loadav[0]);
# Line 79 | Line 101 | void loadStats()
101    printf("packet.load.load15 %.2f\n",loadav[2]);
102   }
103  
104 < int main()
105 < {
104 > void userStats(){
105 >  int nousers=0;
106 >  struct utmp *entry;
107 >  
108 >  printf("packet.users.list");
109 >  
110 >  while((entry=getutent()) != NULL){
111 >    if(entry->ut_type==USER_PROCESS)
112 >      {
113 >        printf(" %s",entry->ut_user);
114 >        nousers++;
115 >      }
116 >  }
117 >  printf("\npacket.users.count %d\n", nousers);
118 > }
119  
120 + void systemStats(){
121 +  kstat_ctl_t *kc;
122 +  kstat_t *ksp;
123 +  kstat_named_t *kn;
124 +  int pagesize;
125 +  cpu_stat_t cs;
126 +  uint_t cpustats[5][2];
127 +  float usage;
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();
139 +  }
140 +
141 +  /* Borrowed from gkrellm ;) */
142 +
143 +  for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) {
144 +    if ((strcmp(ksp->ks_module, "cpu_stat")) != 0)
145 +      continue;
146 +    if (kstat_read(kc, ksp, &cs) == -1) {
147 +      perror("kstat_read");
148 +      continue;
149 +    }
150 +  }
151 +
152 +  cpustats[0][0]=cs.cpu_sysinfo.cpu[CPU_USER];
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 +  
160 +  if (kstat_chain_update(kc) == -1) {
161 +    errf("Kstat update failure (%m)");
162 +    die();
163 +  }
164 +  
165 +  for (ksp = kc->kc_chain; ksp!=NULL ; ksp = ksp->ks_next) {
166 +    if ((strcmp(ksp->ks_module, "cpu_stat")) != 0)
167 +      continue;
168 +    if (kstat_read(kc, ksp, &cs) == -1) {
169 +      perror("kstat_read");
170 +      continue;
171 +      }
172 +  }
173 +
174 +  cpustats[0][1]=cs.cpu_sysinfo.cpu[CPU_USER];
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+swap;
187 +  
188 +  usage=((((float)user)/((float)total))*100.00);
189 +
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);
193 +  usage=((((float)idle)/((float)total))*100.00);
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((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((kn=kstat_data_lookup(ksp, "freemem")) == NULL){
221 +    errf("Failed to get free memory (%m)");
222 +    die();
223 +  }
224 +  
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 +  processStats();
313    return 0;
314   }
315  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines