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.9
Committed: Sat May 18 18:15:57 2002 UTC (23 years, 7 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.8: +19 -0 lines
Log Message:
i-scream is now licensed under the GPL. I've added the GPL headers to every
source file, and put a full copy of the license in the appropriate places.
I think I've covered everything. This is going to be a mad commit ;)

File Contents

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