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.3 by pajs, Thu Mar 21 16:40:11 2002 UTC vs.
Revision 1.6 by pajs, Thu Mar 28 18:16:21 2002 UTC

# Line 8 | Line 8
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  
18 + uid_t uid;
19 + uid_t euid;
20 + gid_t gid;
21 + gid_t egid;
22 +
23   void die(){
24    exit(1);
25   }
# Line 94 | Line 105 | void userStats(){
105  
106    printf("\npacket.users.count %d\n",numusers);
107  
108 + }
109 +
110 + void systemStats(){
111 +
112 +  long cp_time[CPUSTATES];
113 +  long total, user, idle, kernel, nice;
114 +  long totalmem, freemem, swaptotal, swapused;
115 +
116 +  static char *cpname = "kern.cp_time";
117 +  static char *tmemname = "hw.physmem";
118 +  static char *fmemname = "vm.stats.vm.v_free_count";
119 +  int pagesize=-1;
120 +  size_t size;
121 +
122 +  kvm_t *kvmd = NULL;
123 +  struct kvm_swap swapinfo;
124 +  char errbuf[_POSIX2_LINE_MAX];
125    
126 +  if (sysctlbyname(cpname, NULL, &size, NULL, NULL) < 0){
127 +    errf("sysctlbyname (%m)");
128 +    die();
129 +  }
130  
131 +  if (size != sizeof cp_time){
132 +    errf("bailing out, trying to write into something to small");
133 +    die();
134 +  }
135 +
136 +  if (sysctlbyname(cpname, &cp_time, &size, NULL, NULL) < 0){
137 +    errf("Failed to get cpu stats (%m)");
138 +    die();
139 +  }
140 +
141 +  user=cp_time[CP_USER];
142 +  nice=cp_time[CP_NICE];
143 +  kernel=cp_time[CP_SYS];
144 +  idle=cp_time[CP_IDLE];
145 +
146 +  sleep(1);
147 +
148 +  if (sysctlbyname(cpname, &cp_time, &size, NULL, NULL) < 0){
149 +    errf("Failed to get cpu stats (%m)");
150 +    die();
151 +  }
152 +  
153 +  user-=cp_time[CP_USER];
154 +  nice-=cp_time[CP_NICE];
155 +  kernel-=cp_time[CP_SYS];
156 +  idle-=cp_time[CP_IDLE];
157 +  
158 +  total=user+nice+kernel+idle;
159 +
160 +  printf("packet.cpu.user %ld\n",((user+nice)*100)/total);
161 +  printf("packet.cpu.kernel %ld\n",(kernel*100)/total);
162 +  printf("packet.cpu.idle %ld\n",(idle*100)/total);
163 +
164 +   /* Cos i-scream's expects this to be sent :/ */
165 +  printf("packet.cpu.iowait 0\n");
166 +  printf("packet.cpu.swap 0\n");
167 +  
168 +  /* MEMORY STATS */
169 +  pagesize=getpagesize();
170 +  if (pagesize==-1){
171 +    errf("Failed to get pagesize (%m)");
172 +    die();
173 +  }
174 +
175 +  if (sysctlbyname(tmemname, NULL, &size, NULL, NULL) < 0){
176 +      errf("sysctlbyname (%m)");
177 +      die();
178 +    }
179 +
180 +  if (sysctlbyname(tmemname, &totalmem, &size, NULL, NULL) < 0){
181 +    errf("Failed to get memory stats (%m)");
182 +    die();
183 +  }
184 +
185 +   if (sysctlbyname(fmemname, NULL, &size, NULL, NULL) < 0){
186 +      errf("sysctlbyname (%m)");
187 +      die();
188 +    }
189 +
190 +  if (sysctlbyname(fmemname, &freemem, &size, NULL, NULL) < 0){
191 +    errf("Failed to get memory stats (%m)");
192 +    die();
193 +  }
194 +  
195 +  totalmem/=(1024*1024);
196 +  freemem=(freemem*pagesize)/(1024*1024);
197 +  
198 +  printf("packet.memory.total %ld\n", totalmem);
199 +  printf("packet.memory.free %ld\n", freemem);
200 +  printf("packet.memory.used %ld\n", (totalmem-freemem));
201 +
202 +  /* Swap stats */
203 +
204 +  /* Get sufficent privilages to do this */
205 +  if (gid!=egid){
206 +    /* Means we are setgid something, hopefully kmem :) */
207 +    if ((setegid(egid)) != 0){
208 +      errf("Failed to gain sufficent privilages (%m)");
209 +      die();
210 +    }
211 +  }
212 +
213 +  kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
214 +  if (kvmd == NULL){
215 +    errf("Failed to open kvm info to get swap stats (%m)");
216 +    die();
217 +  }
218 +  
219 +  /* Lose are setgid'ness */
220 +
221 +  if ((setegid(gid)) != 0){
222 +    errf("Failed to release permissions, refusing to keep setgid. (%m)");
223 +    die();
224 +  }
225 +
226 +
227 +  /* ok, just for proof of concept atm, ideally this will need to handle more
228 +     than one swap device */
229 +
230 +  if ((kvm_getswapinfo(kvmd, &swapinfo, 1,0)) == -1){
231 +    errf("Failed to get swap info (%m)");
232 +    die();
233 +  }
234 +  
235 +  swaptotal=((((long)swapinfo.ksw_total)*pagesize)/1024)/1024;
236 +  swapused=((((long)swapinfo.ksw_used)*pagesize)/1024)/1024;
237 +
238 +  printf("packet.swap.total %ld\n" , swaptotal);
239 +  printf("packet.swap.used %ld\n", swapused);
240 +  printf("packet.swap.free %lu\n", (swaptotal-swapused));
241 +  
242   }
243  
244 + void processStats(){
245 +  int sleeping=-1;
246 +  int zombie=0;
247 +  int stopped=0;
248 +  int running=0;
249 +  char *line;
250 +  char *line_p;
251 +  
252 +  FILE *f;
253 +
254 +  if((f=popen("/bin/ps -ax" , "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 +    line_p=strchr(line_p, ' ');
265 +    for(; (*line_p == ' ') && (*line_p != '\0'); line_p++);
266 +    if (line_p==NULL) abort();
267 +
268 +    if (*line_p=='S') sleeping++;
269 +    if (*line_p=='I') sleeping++;
270 +    if (*line_p=='R') running++;
271 +    if (*line_p=='D') running++;
272 +    if (*line_p=='Z') zombie++;
273 +    if (*line_p=='T') stopped++;
274 +    if (*line_p=='J') running++;
275 +
276 +  }
277 +
278 +  if((pclose(f)) != 0) {
279 +    errf("Failed to close process stats (%m)");
280 +    die();
281 +  }
282 +  
283 +  printf("packet.processes.sleeping %d\n",sleeping);
284 +  printf("packet.processes.cpu %d\n",running);
285 +  printf("packet.processes.zombie %d\n",zombie);
286 +  printf("packet.processes.stopped %d\n", stopped);
287 +  printf("packet.processes.total %d\n", (sleeping+running+zombie+stopped));
288 +
289 +
290 + }
291 +
292 +
293   int main(){
294 +  uid=getuid();
295 +  euid=geteuid();
296 +  gid=getgid();
297 +  egid=getegid();
298 +
299 +  /* We dont want to run with more permissions than we need, until we need em */
300 +  if ((setegid(gid)) != 0){
301 +    errf("Failed to release permissions, refusing to keep setgid. (%m)");
302 +    die();
303 +  }
304 +
305 + if ((seteuid(uid)) != 0){
306 +    errf("Failed to release permissions, refusing to keep setuid. (%m)");
307 +    die();
308 +  }
309 +
310    diskStats();
311    osStats();
312    loadStats();
313    userStats();
314 +  systemStats();
315 +  processStats();
316    exit(0);
317   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines