| 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; |
| 114 |
|
long cp_time[CPUSTATES]; |
| 115 |
|
long total, user, idle, kernel, nice; |
| 116 |
|
long totalmem, freemem, swaptotal, swapused; |
| 117 |
+ |
float stat; |
| 118 |
|
|
| 119 |
+ |
static char *uptimename = "kern.boottime"; |
| 120 |
|
static char *cpname = "kern.cp_time"; |
| 121 |
|
static char *tmemname = "hw.physmem"; |
| 122 |
|
static char *fmemname = "vm.stats.vm.v_free_count"; |
| 126 |
|
kvm_t *kvmd = NULL; |
| 127 |
|
struct kvm_swap swapinfo; |
| 128 |
|
char errbuf[_POSIX2_LINE_MAX]; |
| 129 |
+ |
time_t current; |
| 130 |
+ |
struct timeval uptime; |
| 131 |
+ |
|
| 132 |
+ |
time(¤t); |
| 133 |
|
|
| 134 |
+ |
if (sysctlbyname(uptimename, NULL, &size, NULL, NULL) < 0){ |
| 135 |
+ |
errf("sysctlbyname (%m)"); |
| 136 |
+ |
die(); |
| 137 |
+ |
} |
| 138 |
+ |
|
| 139 |
+ |
|
| 140 |
+ |
if (sysctlbyname(uptimename, &uptime, &size, NULL, NULL) < 0){ |
| 141 |
+ |
errf("Failed to get cpu stats (%m)"); |
| 142 |
+ |
die(); |
| 143 |
+ |
} |
| 144 |
+ |
|
| 145 |
+ |
printf("packet.os.uptime %ld\n",(current-(uptime.tv_sec))); |
| 146 |
+ |
|
| 147 |
+ |
|
| 148 |
|
if (sysctlbyname(cpname, NULL, &size, NULL, NULL) < 0){ |
| 149 |
|
errf("sysctlbyname (%m)"); |
| 150 |
|
die(); |
| 172 |
|
die(); |
| 173 |
|
} |
| 174 |
|
|
| 175 |
< |
user-=cp_time[CP_USER]; |
| 176 |
< |
nice-=cp_time[CP_NICE]; |
| 177 |
< |
kernel-=cp_time[CP_SYS]; |
| 178 |
< |
idle-=cp_time[CP_IDLE]; |
| 175 |
> |
user=cp_time[CP_USER]-user; |
| 176 |
> |
nice=cp_time[CP_NICE]-nice; |
| 177 |
> |
kernel=cp_time[CP_SYS]-kernel; |
| 178 |
> |
idle=cp_time[CP_IDLE]-idle; |
| 179 |
|
|
| 180 |
|
total=user+nice+kernel+idle; |
| 181 |
< |
|
| 182 |
< |
printf("packet.cpu.user %ld\n",((user+nice)*100)/total); |
| 183 |
< |
printf("packet.cpu.kernel %ld\n",(kernel*100)/total); |
| 184 |
< |
printf("packet.cpu.idle %ld\n",(idle*100)/total); |
| 181 |
> |
stat=((float)(user+nice)/(float)total)*100.0; |
| 182 |
> |
printf("packet.cpu.user %.2f\n",stat); |
| 183 |
> |
stat=((float)(kernel)/(float)total)*100.0; |
| 184 |
> |
printf("packet.cpu.kernel %.2f\n",stat); |
| 185 |
> |
stat=((float)(idle)/(float)total)*100.0; |
| 186 |
> |
printf("packet.cpu.idle %.2f\n",stat); |
| 187 |
|
|
| 188 |
|
/* Cos i-scream's expects this to be sent :/ */ |
| 189 |
|
printf("packet.cpu.iowait 0\n"); |
| 240 |
|
die(); |
| 241 |
|
} |
| 242 |
|
|
| 243 |
< |
/* Lose are setgid'ness */ |
| 220 |
< |
setegid(gid); |
| 243 |
> |
/* Lose our setgid'ness */ |
| 244 |
|
|
| 245 |
+ |
if ((setegid(gid)) != 0){ |
| 246 |
+ |
errf("Failed to release permissions, refusing to keep setgid. (%m)"); |
| 247 |
+ |
die(); |
| 248 |
+ |
} |
| 249 |
+ |
|
| 250 |
+ |
|
| 251 |
|
/* ok, just for proof of concept atm, ideally this will need to handle more |
| 252 |
|
than one swap device */ |
| 253 |
|
|
| 265 |
|
|
| 266 |
|
} |
| 267 |
|
|
| 268 |
+ |
void processStats(){ |
| 269 |
+ |
int sleeping=-1; |
| 270 |
+ |
int zombie=0; |
| 271 |
+ |
int stopped=0; |
| 272 |
+ |
int running=0; |
| 273 |
+ |
char *line; |
| 274 |
+ |
char *line_p; |
| 275 |
+ |
|
| 276 |
+ |
FILE *f; |
| 277 |
+ |
|
| 278 |
+ |
if((f=popen("/bin/ps -ax" , "r")) == NULL) { |
| 279 |
+ |
errf("Failed to get process stats (%m)"); |
| 280 |
+ |
die(); |
| 281 |
+ |
} |
| 282 |
+ |
|
| 283 |
+ |
while((line=fpgetline(f)) != NULL) { |
| 284 |
+ |
line_p=line; |
| 285 |
+ |
for(; (*line_p == ' ') && (*line_p != '\0'); line_p++); |
| 286 |
+ |
line_p=strchr(line_p, ' '); |
| 287 |
+ |
for(; (*line_p == ' ') && (*line_p != '\0'); line_p++); |
| 288 |
+ |
line_p=strchr(line_p, ' '); |
| 289 |
+ |
for(; (*line_p == ' ') && (*line_p != '\0'); line_p++); |
| 290 |
+ |
if (line_p==NULL) abort(); |
| 291 |
+ |
|
| 292 |
+ |
if (*line_p=='S') sleeping++; |
| 293 |
+ |
if (*line_p=='I') sleeping++; |
| 294 |
+ |
if (*line_p=='R') running++; |
| 295 |
+ |
if (*line_p=='D') running++; |
| 296 |
+ |
if (*line_p=='Z') zombie++; |
| 297 |
+ |
if (*line_p=='T') stopped++; |
| 298 |
+ |
if (*line_p=='J') running++; |
| 299 |
+ |
|
| 300 |
+ |
} |
| 301 |
+ |
|
| 302 |
+ |
if((pclose(f)) != 0) { |
| 303 |
+ |
errf("Failed to close process stats (%m)"); |
| 304 |
+ |
die(); |
| 305 |
+ |
} |
| 306 |
+ |
|
| 307 |
+ |
printf("packet.processes.sleeping %d\n",sleeping); |
| 308 |
+ |
printf("packet.processes.cpu %d\n",running); |
| 309 |
+ |
printf("packet.processes.zombie %d\n",zombie); |
| 310 |
+ |
printf("packet.processes.stopped %d\n", stopped); |
| 311 |
+ |
printf("packet.processes.total %d\n", (sleeping+running+zombie+stopped)); |
| 312 |
+ |
|
| 313 |
+ |
|
| 314 |
+ |
} |
| 315 |
+ |
|
| 316 |
+ |
|
| 317 |
|
int main(){ |
| 318 |
|
uid=getuid(); |
| 319 |
|
euid=geteuid(); |
| 321 |
|
egid=getegid(); |
| 322 |
|
|
| 323 |
|
/* We dont want to run with more permissions than we need, until we need em */ |
| 324 |
< |
seteuid(uid); |
| 325 |
< |
setegid(gid); |
| 324 |
> |
if ((setegid(gid)) != 0){ |
| 325 |
> |
errf("Failed to release permissions, refusing to keep setgid. (%m)"); |
| 326 |
> |
die(); |
| 327 |
> |
} |
| 328 |
|
|
| 329 |
+ |
if ((seteuid(uid)) != 0){ |
| 330 |
+ |
errf("Failed to release permissions, refusing to keep setuid. (%m)"); |
| 331 |
+ |
die(); |
| 332 |
+ |
} |
| 333 |
+ |
|
| 334 |
|
diskStats(); |
| 335 |
|
osStats(); |
| 336 |
|
loadStats(); |
| 337 |
|
userStats(); |
| 338 |
|
systemStats(); |
| 339 |
+ |
processStats(); |
| 340 |
|
exit(0); |
| 341 |
|
} |