ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/host/ihost-perl/plugins/freebsd/freebsd.c
Revision: 1.7
Committed: Tue Apr 2 11:25:37 2002 UTC (23 years, 8 months ago) by pajs
Content type: text/plain
Branch: MAIN
Changes since 1.6: +21 -0 lines
Log Message:
Added a forgoten bit getting uptime.

File Contents

# Content
1 #include <stdio.h>
2 #include "ukcprog.h"
3 #include <sys/param.h>
4 #include <sys/ucred.h>
5 #include <sys/mount.h>
6 #include <string.h>
7 #include <stdlib.h>
8 #include <sys/utsname.h>
9 #include <sys/types.h>
10 #include <utmp.h>
11 #include <sys/dkstat.h>
12 #include <kvm.h>
13 #include <unistd.h>
14 #include <sys/sysctl.h>
15 #include <fcntl.h>
16 #include <limits.h>
17 #include <time.h>
18 #include <sys/time.h>
19
20 uid_t uid;
21 uid_t euid;
22 gid_t gid;
23 gid_t egid;
24
25 void die(){
26 exit(1);
27 }
28
29 void diskStats(){
30 struct statfs *mp;
31 int nummnt;
32 int counter=0;
33 char *mntpnt;
34 char *devicename;
35
36 nummnt=getmntinfo(&mp , MNT_LOCAL);
37 if (nummnt<=0){
38 errf("Failed to get disk stats (%m)");
39 die();
40 }
41
42 mntpnt=malloc(MNAMELEN);
43 devicename=malloc(MNAMELEN);
44
45 for(;counter<nummnt;counter++){
46 strncpy(mntpnt, mp->f_mntonname ,MNAMELEN);
47 strncpy(devicename, mp->f_mntfromname ,MNAMELEN);
48 printf("packet.disk.p%d.attributes.mount %s\n", counter, mntpnt);
49 printf("packet.disk.p%d.attributes.name %s\n", counter, devicename);
50 printf("packet.disk.p%d.attributes.kbytes %lu\n",counter, ((mp->f_bsize/1024) * mp->f_blocks));
51 printf("packet.disk.p%d.attributes.used %lu\n",counter, (((mp->f_bsize/1024) * mp->f_blocks) -((mp->f_bsize/1024) * mp->f_bfree)));
52 printf("packet.disk.p%d.attributes.avail %lu\n",counter, (mp->f_bsize/1024) * mp->f_bavail);
53 printf("packet.disk.p%d.attributes.totalinodes %lu\n", counter, mp->f_files);
54 printf("packet.disk.p%d.attributes.freeinodes %lu\n",counter, mp->f_ffree);
55
56 mp++;
57 }
58
59
60 }
61
62 void osStats(){
63 struct utsname os;
64
65 if((uname(&os)) != 0){
66 errf("Failed to get os stats (%m)");
67 die();
68 }
69
70 printf("packet.os.name %s\n", os.sysname);
71 printf("packet.os.release %s\n" , os.release);
72 printf("packet.os.version %s\n", os.version);
73 printf("packet.os.sysname %s\n" , os.nodename);
74 printf("packet.os.platform %s\n", os.machine);
75
76 }
77 void loadStats(){
78 double loadav[3];
79
80 if((getloadavg(loadav,3)) == -1){
81 errf("Failed to get load averages (%m)");
82 die();
83 }
84
85 printf("packet.load.load1 %.2f\n",loadav[0]);
86 printf("packet.load.load5 %.2f\n",loadav[1]);
87 printf("packet.load.load15 %.2f\n",loadav[2]);
88 }
89
90 void userStats(){
91 struct utmp users;
92 FILE *f;
93 int numusers=0;
94
95 if ((f=fopen(_PATH_UTMP, "r")) == NULL){
96 errf("Failed to get user stats(%m)");
97 die();
98 }
99
100 printf("packet.users.list");
101
102 while((fread(&users, sizeof(users),1,f)) != 0){
103 if (users.ut_name[0] == '\0') continue;
104 printf(" %s",users.ut_name);
105 numusers++;
106 }
107
108 printf("\npacket.users.count %d\n",numusers);
109
110 }
111
112 void systemStats(){
113
114 long cp_time[CPUSTATES];
115 long total, user, idle, kernel, nice;
116 long totalmem, freemem, swaptotal, swapused;
117
118 static char *uptimename = "kern.boottime";
119 static char *cpname = "kern.cp_time";
120 static char *tmemname = "hw.physmem";
121 static char *fmemname = "vm.stats.vm.v_free_count";
122 int pagesize=-1;
123 size_t size;
124
125 kvm_t *kvmd = NULL;
126 struct kvm_swap swapinfo;
127 char errbuf[_POSIX2_LINE_MAX];
128 time_t current;
129 struct timeval uptime;
130
131 time(&current);
132
133 if (sysctlbyname(uptimename, NULL, &size, NULL, NULL) < 0){
134 errf("sysctlbyname (%m)");
135 die();
136 }
137
138
139 if (sysctlbyname(uptimename, &uptime, &size, NULL, NULL) < 0){
140 errf("Failed to get cpu stats (%m)");
141 die();
142 }
143
144 printf("packet.os.uptime %ld\n",(current-(uptime.tv_sec)));
145
146
147 if (sysctlbyname(cpname, NULL, &size, NULL, NULL) < 0){
148 errf("sysctlbyname (%m)");
149 die();
150 }
151
152 if (size != sizeof cp_time){
153 errf("bailing out, trying to write into something to small");
154 die();
155 }
156
157 if (sysctlbyname(cpname, &cp_time, &size, NULL, NULL) < 0){
158 errf("Failed to get cpu stats (%m)");
159 die();
160 }
161
162 user=cp_time[CP_USER];
163 nice=cp_time[CP_NICE];
164 kernel=cp_time[CP_SYS];
165 idle=cp_time[CP_IDLE];
166
167 sleep(1);
168
169 if (sysctlbyname(cpname, &cp_time, &size, NULL, NULL) < 0){
170 errf("Failed to get cpu stats (%m)");
171 die();
172 }
173
174 user-=cp_time[CP_USER];
175 nice-=cp_time[CP_NICE];
176 kernel-=cp_time[CP_SYS];
177 idle-=cp_time[CP_IDLE];
178
179 total=user+nice+kernel+idle;
180
181 printf("packet.cpu.user %ld\n",((user+nice)*100)/total);
182 printf("packet.cpu.kernel %ld\n",(kernel*100)/total);
183 printf("packet.cpu.idle %ld\n",(idle*100)/total);
184
185 /* Cos i-scream's expects this to be sent :/ */
186 printf("packet.cpu.iowait 0\n");
187 printf("packet.cpu.swap 0\n");
188
189 /* MEMORY STATS */
190 pagesize=getpagesize();
191 if (pagesize==-1){
192 errf("Failed to get pagesize (%m)");
193 die();
194 }
195
196 if (sysctlbyname(tmemname, NULL, &size, NULL, NULL) < 0){
197 errf("sysctlbyname (%m)");
198 die();
199 }
200
201 if (sysctlbyname(tmemname, &totalmem, &size, NULL, NULL) < 0){
202 errf("Failed to get memory stats (%m)");
203 die();
204 }
205
206 if (sysctlbyname(fmemname, NULL, &size, NULL, NULL) < 0){
207 errf("sysctlbyname (%m)");
208 die();
209 }
210
211 if (sysctlbyname(fmemname, &freemem, &size, NULL, NULL) < 0){
212 errf("Failed to get memory stats (%m)");
213 die();
214 }
215
216 totalmem/=(1024*1024);
217 freemem=(freemem*pagesize)/(1024*1024);
218
219 printf("packet.memory.total %ld\n", totalmem);
220 printf("packet.memory.free %ld\n", freemem);
221 printf("packet.memory.used %ld\n", (totalmem-freemem));
222
223 /* Swap stats */
224
225 /* Get sufficent privilages to do this */
226 if (gid!=egid){
227 /* Means we are setgid something, hopefully kmem :) */
228 if ((setegid(egid)) != 0){
229 errf("Failed to gain sufficent privilages (%m)");
230 die();
231 }
232 }
233
234 kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
235 if (kvmd == NULL){
236 errf("Failed to open kvm info to get swap stats (%m)");
237 die();
238 }
239
240 /* Lose are setgid'ness */
241
242 if ((setegid(gid)) != 0){
243 errf("Failed to release permissions, refusing to keep setgid. (%m)");
244 die();
245 }
246
247
248 /* ok, just for proof of concept atm, ideally this will need to handle more
249 than one swap device */
250
251 if ((kvm_getswapinfo(kvmd, &swapinfo, 1,0)) == -1){
252 errf("Failed to get swap info (%m)");
253 die();
254 }
255
256 swaptotal=((((long)swapinfo.ksw_total)*pagesize)/1024)/1024;
257 swapused=((((long)swapinfo.ksw_used)*pagesize)/1024)/1024;
258
259 printf("packet.swap.total %ld\n" , swaptotal);
260 printf("packet.swap.used %ld\n", swapused);
261 printf("packet.swap.free %lu\n", (swaptotal-swapused));
262
263 }
264
265 void processStats(){
266 int sleeping=-1;
267 int zombie=0;
268 int stopped=0;
269 int running=0;
270 char *line;
271 char *line_p;
272
273 FILE *f;
274
275 if((f=popen("/bin/ps -ax" , "r")) == NULL) {
276 errf("Failed to get process stats (%m)");
277 die();
278 }
279
280 while((line=fpgetline(f)) != NULL) {
281 line_p=line;
282 for(; (*line_p == ' ') && (*line_p != '\0'); line_p++);
283 line_p=strchr(line_p, ' ');
284 for(; (*line_p == ' ') && (*line_p != '\0'); line_p++);
285 line_p=strchr(line_p, ' ');
286 for(; (*line_p == ' ') && (*line_p != '\0'); line_p++);
287 if (line_p==NULL) abort();
288
289 if (*line_p=='S') sleeping++;
290 if (*line_p=='I') sleeping++;
291 if (*line_p=='R') running++;
292 if (*line_p=='D') running++;
293 if (*line_p=='Z') zombie++;
294 if (*line_p=='T') stopped++;
295 if (*line_p=='J') running++;
296
297 }
298
299 if((pclose(f)) != 0) {
300 errf("Failed to close process stats (%m)");
301 die();
302 }
303
304 printf("packet.processes.sleeping %d\n",sleeping);
305 printf("packet.processes.cpu %d\n",running);
306 printf("packet.processes.zombie %d\n",zombie);
307 printf("packet.processes.stopped %d\n", stopped);
308 printf("packet.processes.total %d\n", (sleeping+running+zombie+stopped));
309
310
311 }
312
313
314 int main(){
315 uid=getuid();
316 euid=geteuid();
317 gid=getgid();
318 egid=getegid();
319
320 /* We dont want to run with more permissions than we need, until we need em */
321 if ((setegid(gid)) != 0){
322 errf("Failed to release permissions, refusing to keep setgid. (%m)");
323 die();
324 }
325
326 if ((seteuid(uid)) != 0){
327 errf("Failed to release permissions, refusing to keep setuid. (%m)");
328 die();
329 }
330
331 diskStats();
332 osStats();
333 loadStats();
334 userStats();
335 systemStats();
336 processStats();
337 exit(0);
338 }