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
Revision: 1.8
Committed: Tue Mar 19 14:55:42 2002 UTC (23 years, 9 months ago) by pajs
Content type: text/plain
Branch: MAIN
Changes since 1.7: +1 -1 lines
Log Message:
Fix to allow it to continue gettings stats, even if it fails to get some disk
stats (mainly useful if iscream user doesn't have permission to stat the fs)

File Contents

# Content
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/statvfs.h>
4 #include <sys/mnttab.h>
5 #include "ukcprog.h"
6 #include <sys/utsname.h>
7 #include <sys/loadavg.h>
8 #include <utmp.h>
9 #include <kstat.h>
10 #include <sys/sysinfo.h>
11 #include <unistd.h>
12 #include <sys/stat.h>
13 #include <sys/swap.h>
14 #include <stdlib.h>
15 #include <strings.h>
16 #include <time.h>
17
18 void die(){
19 exit(1);
20 }
21
22
23 void diskStats(){
24 struct mnttab mp;
25 struct statvfs df;
26 FILE *f;
27 int counter=0;
28
29 if ((f=fopen("/etc/mnttab", "r" ))==NULL){
30 errf("Failed to open mounts (%m)");
31 die();
32 }
33
34 while((getmntent(f, &mp)) == 0){
35 if ((statvfs(mp.mnt_mountp, &df)) !=0){
36 errf("Failed to gets fs stats (%m)");
37 continue;
38 }
39
40 if((((strcmp(mp.mnt_fstype,"ufs"))==0) || (strcmp(mp.mnt_fstype,"tmpfs")) ==0)){
41
42 printf("packet.disk.p%d.attributes.mount %s\n", counter, mp.mnt_mountp);
43 printf("packet.disk.p%d.attributes.name %s\n", counter, mp.mnt_special);
44 printf("packet.disk.p%d.attributes.kbytes %lu\n",counter, ((df.f_frsize/1024) * df.f_blocks));
45 printf("packet.disk.p%d.attributes.used %lu\n",counter, (((df.f_frsize/1024) * df.f_blocks) -((df.f_frsize/1024) * df.f_bfree)));
46 printf("packet.disk.p%d.attributes.avail %lu\n",counter, (df.f_frsize/1024) * df.f_bavail);
47 printf("packet.disk.p%d.attributes.totalinodes %lu\n", counter, df.f_files);
48 printf("packet.disk.p%d.attributes.freeinodes %lu\n",counter, df.f_ffree);
49
50 counter++;
51 }
52 }
53 }
54
55 void osStats(){
56 struct utsname os;
57
58 if((uname(&os)) == -1){
59 errf("Failed to get os stats (%m)");
60 die();
61 }
62
63 printf("packet.os.name %s\n", os.sysname);
64 printf("packet.os.release %s\n" , os.release);
65 printf("packet.os.version %s\n", os.version);
66 printf("packet.os.sysname %s\n" , os.nodename);
67 printf("packet.os.platform %s\n", os.machine);
68
69 }
70
71 void loadStats(){
72 double loadav[3];
73
74 if((getloadavg(loadav,3)) == -1){
75 errf("Failed to get load averages (%m)");
76 die();
77 }
78
79 printf("packet.load.load1 %.2f\n",loadav[0]);
80 printf("packet.load.load5 %.2f\n",loadav[1]);
81 printf("packet.load.load15 %.2f\n",loadav[2]);
82 }
83
84 void userStats(){
85 int nousers=0;
86 struct utmp *entry;
87
88 printf("packet.users.list");
89
90 while((entry=getutent()) != NULL){
91 if(entry->ut_type==USER_PROCESS)
92 {
93 printf(" %s",entry->ut_user);
94 nousers++;
95 }
96 }
97 printf("\npacket.users.count %d\n", nousers);
98 }
99
100 void systemStats(){
101 kstat_ctl_t *kc;
102 kstat_t *ksp;
103 kstat_named_t *kn;
104 int pagesize;
105 cpu_stat_t cs;
106 uint_t cpustats[5][2];
107 float usage;
108 uint_t user, kernel, idle, iowait, swap, total;
109 ulong totalmem, freemem;
110 long swaptotal, swapused;
111 time_t uptime,curtime;
112
113 struct anoninfo ai;
114
115
116 if ((kc = kstat_open()) == NULL) {
117 errf("kstat_open failure (%m)");
118 die();
119 }
120
121 /* Borrowed from gkrellm ;) */
122
123 for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) {
124 if ((strcmp(ksp->ks_module, "cpu_stat")) != 0)
125 continue;
126 if (kstat_read(kc, ksp, &cs) == -1) {
127 perror("kstat_read");
128 continue;
129 }
130 }
131
132 cpustats[0][0]=cs.cpu_sysinfo.cpu[CPU_USER];
133 cpustats[1][0]=cs.cpu_sysinfo.cpu[CPU_WAIT];
134 cpustats[2][0]=cs.cpu_sysinfo.cpu[CPU_KERNEL];
135 cpustats[3][0]=cs.cpu_sysinfo.cpu[CPU_IDLE];
136 cpustats[4][0]=cs.cpu_sysinfo.cpu[CPU_STATES];
137
138 sleep(1);
139
140 if (kstat_chain_update(kc) == -1) {
141 errf("Kstat update failure (%m)");
142 die();
143 }
144
145 for (ksp = kc->kc_chain; ksp!=NULL ; ksp = ksp->ks_next) {
146 if ((strcmp(ksp->ks_module, "cpu_stat")) != 0)
147 continue;
148 if (kstat_read(kc, ksp, &cs) == -1) {
149 perror("kstat_read");
150 continue;
151 }
152 }
153
154 cpustats[0][1]=cs.cpu_sysinfo.cpu[CPU_USER];
155 cpustats[1][1]=cs.cpu_sysinfo.cpu[CPU_WAIT];
156 cpustats[2][1]=cs.cpu_sysinfo.cpu[CPU_KERNEL];
157 cpustats[3][1]=cs.cpu_sysinfo.cpu[CPU_IDLE];
158 cpustats[4][1]=cs.cpu_sysinfo.cpu[CPU_STATES];
159
160 user=cpustats[0][1]-cpustats[0][0];
161 iowait=cpustats[1][1]-cpustats[1][0];
162 kernel=cpustats[2][1]-cpustats[2][0];
163 idle=cpustats[3][1]-cpustats[3][0];
164 swap=cpustats[4][1]-cpustats[4][0];
165
166 total=user+kernel+idle+iowait+swap;
167
168 usage=((((float)user)/((float)total))*100.00);
169
170 printf("packet.cpu.user %3.2f\n", usage);
171 usage=((((float)kernel)/((float)total))*100.00);
172 printf("packet.cpu.kernel %3.2f\n", usage);
173 usage=((((float)idle)/((float)total))*100.00);
174 printf("packet.cpu.idle %3.2f\n", usage);
175 usage=((((float)iowait)/((float)total))*100.00);
176 printf("packet.cpu.iowait %3.2f\n", usage);
177 usage=((((float)swap)/((float)total))*100.00);
178 printf("packet.cpu.swap %3.2f\n", usage);
179
180 /* MEMORY STATS */
181
182 if((pagesize=sysconf(_SC_PAGESIZE)) == -1){
183 errf("Failed to get pagesize (%m)");
184 die();
185 }
186
187 if((totalmem=sysconf(_SC_PHYS_PAGES)) == -1){
188 errf("Failed to get total memory");
189 die();
190 }
191
192 totalmem=((totalmem*pagesize)/1024)/1024;
193
194 ksp = kstat_lookup(kc, "unix", 0, "system_pages");
195 if (kstat_read(kc, ksp, 0) == -1) {
196 perror("kstat_read");
197 die();
198 }
199
200 if((kn=kstat_data_lookup(ksp, "freemem")) == NULL){
201 errf("Failed to get free memory (%m)");
202 die();
203 }
204
205 freemem=(((kn->value.ul)*pagesize)/1024)/1024;
206
207 printf("packet.memory.total %lu\n",totalmem);
208 printf("packet.memory.free %lu\n",freemem);
209 printf("packet.memory.used %lu\n",(totalmem-freemem));
210
211
212 if (swapctl(SC_AINFO, &ai) == -1) {
213 errf("Failed to get swap details (%m)");
214 die();
215 }
216
217 swaptotal=((ai.ani_max/1024)*pagesize)/1024;
218 swapused=((ai.ani_resv/1024)*pagesize)/1024;
219
220 /* Ok this is actually for TOP combatability, but this is not
221 total swap, cos it actually also seems to include memory stats. */
222 printf("packet.swap.total %ld\n",swaptotal);
223 printf("packet.swap.used %ld\n", swapused);
224 printf("packet.swap.free %lu\n", (swaptotal-swapused));
225
226 ksp=kstat_lookup(kc, "unix", -1, "system_misc");
227 if (kstat_read(kc, ksp, 0) == -1) {
228 errf("Failed to get uptime (%m)");
229 die();
230 }
231
232 if((kn=kstat_data_lookup(ksp, "boot_time")) == NULL){
233 errf("Failed to get uptime (%m)");
234 die();
235 }
236
237 uptime=(kn->value.ui32);
238 time(&curtime);
239 printf("packet.os.uptime %ld\n", (curtime-uptime));
240
241 }
242
243 void processStats() {
244 int sleeping=-1;
245 int zombie=0;
246 int stopped=0;
247 int running=0;
248 char *line;
249 char *line_p;
250
251
252 FILE *f;
253
254 if((f=popen("/bin/ps -Al" , "r")) == NULL) {
255 errf("Failed to get process stats (%m)");
256 die();
257 }
258
259 while((line=fpgetline(f)) != NULL) {
260 line_p=line;
261 for(; (*line_p == ' ') && (*line_p != '\0'); line_p++);
262 line_p=strchr(line_p, ' ');
263 for(; (*line_p == ' ') && (*line_p != '\0'); line_p++);
264 if (line_p==NULL) abort();
265 /* Ok, we should now be at the state :) .. */
266 if (*line_p=='S') sleeping++;
267 if (*line_p=='R') running++;
268 if (*line_p=='Z') zombie++;
269 if (*line_p=='T') stopped++;
270 }
271
272 if((pclose(f)) == -1) {
273 errf("Failed to close process stats (%m)");
274 die();
275 }
276
277 printf("packet.processes.sleeping %d\n",sleeping);
278 printf("packet.processes.cpu %d\n",running);
279 printf("packet.processes.zombie %d\n",zombie);
280 printf("packet.processes.stopped %d\n", stopped);
281 printf("packet.processes.total %d\n", (sleeping+running+zombie+stopped));
282
283 }
284
285
286 int main(){
287 diskStats();
288 osStats();
289 loadStats();
290 userStats();
291 systemStats();
292 processStats();
293 return 0;
294 }
295