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.6
Committed: Mon Mar 18 15:34:42 2002 UTC (23 years, 9 months ago) by pajs
Content type: text/plain
Branch: MAIN
Changes since 1.5: +26 -7 lines
Log Message:
Added the forgotten function of uptime, and removed 2 debug lines.

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