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
(Generate patch)

Comparing projects/cms/source/host/ihost-perl/plugins/freebsd/freebsd.c (file contents):
Revision 1.4 by pajs, Thu Mar 28 17:42:05 2002 UTC vs.
Revision 1.9 by tdb, Sat May 18 18:15:56 2002 UTC

# Line 1 | Line 1
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 "ukcprog.h"
22   #include <sys/param.h>
# Line 14 | Line 33
33   #include <sys/sysctl.h>
34   #include <fcntl.h>
35   #include <limits.h>
36 + #include <time.h>
37 + #include <sys/time.h>
38  
39   uid_t uid;
40   uid_t euid;
# Line 112 | Line 133 | void systemStats(){
133    long cp_time[CPUSTATES];
134    long total, user, idle, kernel, nice;
135    long totalmem, freemem, swaptotal, swapused;
136 +  float stat;
137  
138 +  static char *uptimename = "kern.boottime";
139    static char *cpname = "kern.cp_time";
140    static char *tmemname = "hw.physmem";
141    static char *fmemname = "vm.stats.vm.v_free_count";
# Line 122 | Line 145 | void systemStats(){
145    kvm_t *kvmd = NULL;
146    struct kvm_swap swapinfo;
147    char errbuf[_POSIX2_LINE_MAX];
148 +  time_t current;
149 +  struct timeval uptime;
150 +
151 +  time(&current);
152    
153 +  if (sysctlbyname(uptimename, NULL, &size, NULL, NULL) < 0){
154 +    errf("sysctlbyname (%m)");
155 +    die();
156 +  }
157 +
158 +
159 +  if (sysctlbyname(uptimename, &uptime, &size, NULL, NULL) < 0){
160 +    errf("Failed to get cpu stats (%m)");
161 +    die();
162 +  }
163 +  
164 +  printf("packet.os.uptime %ld\n",(current-(uptime.tv_sec)));
165 +
166 +
167    if (sysctlbyname(cpname, NULL, &size, NULL, NULL) < 0){
168      errf("sysctlbyname (%m)");
169      die();
# Line 150 | Line 191 | void systemStats(){
191      die();
192    }
193    
194 <  user-=cp_time[CP_USER];
195 <  nice-=cp_time[CP_NICE];
196 <  kernel-=cp_time[CP_SYS];
197 <  idle-=cp_time[CP_IDLE];
194 >  user=cp_time[CP_USER]-user;
195 >  nice=cp_time[CP_NICE]-nice;
196 >  kernel=cp_time[CP_SYS]-kernel;
197 >  idle=cp_time[CP_IDLE]-idle;
198    
199    total=user+nice+kernel+idle;
200 <
201 <  printf("packet.cpu.user %ld\n",((user+nice)*100)/total);
202 <  printf("packet.cpu.kernel %ld\n",(kernel*100)/total);
203 <  printf("packet.cpu.idle %ld\n",(idle*100)/total);
200 >  stat=((float)(user+nice)/(float)total)*100.0;
201 >  printf("packet.cpu.user %.2f\n",stat);
202 >  stat=((float)(kernel)/(float)total)*100.0;
203 >  printf("packet.cpu.kernel %.2f\n",stat);
204 >  stat=((float)(idle)/(float)total)*100.0;
205 >  printf("packet.cpu.idle %.2f\n",stat);
206  
207     /* Cos i-scream's expects this to be sent :/ */
208    printf("packet.cpu.iowait 0\n");
# Line 216 | Line 259 | void systemStats(){
259      die();
260    }
261    
262 <  /* Lose are setgid'ness */
220 <  setegid(gid);
262 >  /* Lose our setgid'ness */
263  
264 +  if ((setegid(gid)) != 0){
265 +    errf("Failed to release permissions, refusing to keep setgid. (%m)");
266 +    die();
267 +  }
268 +
269 +
270    /* ok, just for proof of concept atm, ideally this will need to handle more
271       than one swap device */
272  
# Line 236 | Line 284 | void systemStats(){
284    
285   }
286  
287 + void processStats(){
288 +  int sleeping=-1;
289 +  int zombie=0;
290 +  int stopped=0;
291 +  int running=0;
292 +  char *line;
293 +  char *line_p;
294 +  
295 +  FILE *f;
296 +
297 +  if((f=popen("/bin/ps -ax" , "r")) == NULL) {
298 +    errf("Failed to get process stats (%m)");
299 +    die();
300 +  }
301 +
302 +  while((line=fpgetline(f)) != NULL) {
303 +    line_p=line;
304 +    for(; (*line_p == ' ') && (*line_p != '\0'); line_p++);
305 +    line_p=strchr(line_p, ' ');
306 +    for(; (*line_p == ' ') && (*line_p != '\0'); line_p++);
307 +    line_p=strchr(line_p, ' ');
308 +    for(; (*line_p == ' ') && (*line_p != '\0'); line_p++);
309 +    if (line_p==NULL) abort();
310 +
311 +    if (*line_p=='S') sleeping++;
312 +    if (*line_p=='I') sleeping++;
313 +    if (*line_p=='R') running++;
314 +    if (*line_p=='D') running++;
315 +    if (*line_p=='Z') zombie++;
316 +    if (*line_p=='T') stopped++;
317 +    if (*line_p=='J') running++;
318 +
319 +  }
320 +
321 +  if((pclose(f)) != 0) {
322 +    errf("Failed to close process stats (%m)");
323 +    die();
324 +  }
325 +  
326 +  printf("packet.processes.sleeping %d\n",sleeping);
327 +  printf("packet.processes.cpu %d\n",running);
328 +  printf("packet.processes.zombie %d\n",zombie);
329 +  printf("packet.processes.stopped %d\n", stopped);
330 +  printf("packet.processes.total %d\n", (sleeping+running+zombie+stopped));
331 +
332 +
333 + }
334 +
335 +
336   int main(){
337    uid=getuid();
338    euid=geteuid();
# Line 243 | Line 340 | int main(){
340    egid=getegid();
341  
342    /* We dont want to run with more permissions than we need, until we need em */
343 <  seteuid(uid);
344 <  setegid(gid);
343 >  if ((setegid(gid)) != 0){
344 >    errf("Failed to release permissions, refusing to keep setgid. (%m)");
345 >    die();
346 >  }
347  
348 + if ((seteuid(uid)) != 0){
349 +    errf("Failed to release permissions, refusing to keep setuid. (%m)");
350 +    die();
351 +  }
352 +
353    diskStats();
354    osStats();
355    loadStats();
356    userStats();
357    systemStats();
358 +  processStats();
359    exit(0);
360   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines