| 13 |
|
#include <sys/swap.h> |
| 14 |
|
#include <stdlib.h> |
| 15 |
|
#include <strings.h> |
| 16 |
+ |
#include <time.h> |
| 17 |
|
|
| 18 |
|
void die(){ |
| 19 |
|
exit(1); |
| 100 |
|
kstat_named_t *kn; |
| 101 |
|
int pagesize; |
| 102 |
|
cpu_stat_t cs; |
| 103 |
< |
uint_t cpustats[4][2]; |
| 103 |
> |
uint_t cpustats[5][2]; |
| 104 |
|
float usage; |
| 105 |
< |
uint_t user, kernel, idle, iowait, total; |
| 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 |
|
|
| 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 |
|
|
| 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; |
| 163 |
> |
total=user+kernel+idle+iowait+swap; |
| 164 |
|
|
| 165 |
|
usage=((((float)user)/((float)total))*100.00); |
| 166 |
|
|
| 162 |
– |
printf("user : %u\n",user); |
| 163 |
– |
printf("total : %u\n",total); |
| 164 |
– |
|
| 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); |
| 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 |
< |
|
| 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){ |
| 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 |
|
|