| 9 |
|
#include <kstat.h> |
| 10 |
|
#include <sys/sysinfo.h> |
| 11 |
|
#include <unistd.h> |
| 12 |
+ |
#include <sys/stat.h> |
| 13 |
+ |
#include <sys/swap.h> |
| 14 |
+ |
#include <stdlib.h> |
| 15 |
+ |
#include <strings.h> |
| 16 |
|
|
| 17 |
|
void die(){ |
| 18 |
|
exit(1); |
| 96 |
|
void systemStats(){ |
| 97 |
|
kstat_ctl_t *kc; |
| 98 |
|
kstat_t *ksp; |
| 99 |
< |
kstat_named_t *knp; |
| 99 |
> |
kstat_named_t *kn; |
| 100 |
> |
int pagesize; |
| 101 |
|
cpu_stat_t cs; |
| 102 |
|
uint_t cpustats[4][2]; |
| 103 |
|
float usage; |
| 104 |
|
uint_t user, kernel, idle, iowait, total; |
| 105 |
+ |
ulong totalmem, freemem; |
| 106 |
+ |
long swaptotal, swapused; |
| 107 |
+ |
|
| 108 |
+ |
struct anoninfo ai; |
| 109 |
|
|
| 110 |
+ |
|
| 111 |
|
if ((kc = kstat_open()) == NULL) { |
| 112 |
|
errf("kstat_open failure (%m)"); |
| 113 |
|
die(); |
| 170 |
|
usage=((((float)iowait)/((float)total))*100.00); |
| 171 |
|
printf("packet.cpu.iowait %3.2f\n", usage); |
| 172 |
|
|
| 173 |
< |
/* if((ksp = kstat_lookup(kc, "unix", -1, "system_pages")) == NULL){ |
| 174 |
< |
errf("Mem lookup failed (%m)"); |
| 173 |
> |
/* MEMORY STATS */ |
| 174 |
> |
|
| 175 |
> |
if((pagesize=sysconf(_SC_PAGESIZE)) == -1){ |
| 176 |
> |
errf("Failed to get pagesize (%m)"); |
| 177 |
|
die(); |
| 178 |
|
} |
| 179 |
|
|
| 180 |
< |
if((knp = (kstat_named_t *)kstat_data_lookup(ksp, "pagesfree")) == NULL){ |
| 181 |
< |
errf("Mem lookup failed (%m)"); |
| 180 |
> |
if((totalmem=sysconf(_SC_PHYS_PAGES)) == -1){ |
| 181 |
> |
errf("Failed to get total memory"); |
| 182 |
|
die(); |
| 183 |
|
} |
| 184 |
|
|
| 185 |
< |
printf("%ui32\n", knp->value.ui32); |
| 174 |
< |
*/ |
| 175 |
< |
} |
| 176 |
< |
|
| 185 |
> |
totalmem=((totalmem*pagesize)/1024)/1024; |
| 186 |
|
|
| 187 |
+ |
ksp = kstat_lookup(kc, "unix", 0, "system_pages"); |
| 188 |
+ |
if (kstat_read(kc, ksp, 0) == -1) { |
| 189 |
+ |
perror("kstat_read"); |
| 190 |
+ |
die(); |
| 191 |
+ |
} |
| 192 |
|
|
| 193 |
+ |
if((kn=kstat_data_lookup(ksp, "freemem")) == NULL){ |
| 194 |
+ |
errf("Failed to get free memory (%m)"); |
| 195 |
+ |
die(); |
| 196 |
+ |
} |
| 197 |
+ |
|
| 198 |
+ |
freemem=(((kn->value.ul)*pagesize)/1024)/1024; |
| 199 |
+ |
|
| 200 |
+ |
printf("packet.memory.total %lu\n",totalmem); |
| 201 |
+ |
printf("packet.memory.free %lu\n",freemem); |
| 202 |
+ |
printf("packet.memory.used %lu\n",(totalmem-freemem)); |
| 203 |
+ |
|
| 204 |
+ |
|
| 205 |
+ |
if (swapctl(SC_AINFO, &ai) == -1) { |
| 206 |
+ |
errf("Failed to get swap details (%m)"); |
| 207 |
+ |
die(); |
| 208 |
+ |
} |
| 209 |
+ |
|
| 210 |
+ |
swaptotal=((ai.ani_max/1024)*pagesize)/1024; |
| 211 |
+ |
swapused=((ai.ani_resv/1024)*pagesize)/1024; |
| 212 |
+ |
|
| 213 |
+ |
/* Ok this is actually for TOP combatability, but this is not |
| 214 |
+ |
total swap, cos it actually also seems to include memory stats. */ |
| 215 |
+ |
printf("packet.swap.total %ld\n",swaptotal); |
| 216 |
+ |
printf("packet.swap.used %ld\n", swapused); |
| 217 |
+ |
printf("packet.swap.free %lu\n", (swaptotal-swapused)); |
| 218 |
+ |
|
| 219 |
+ |
} |
| 220 |
+ |
|
| 221 |
+ |
void processStats() { |
| 222 |
+ |
int sleeping=-1; |
| 223 |
+ |
int zombie=0; |
| 224 |
+ |
int stopped=0; |
| 225 |
+ |
int running=0; |
| 226 |
+ |
char *line; |
| 227 |
+ |
char *line_p; |
| 228 |
+ |
|
| 229 |
+ |
|
| 230 |
+ |
FILE *f; |
| 231 |
+ |
|
| 232 |
+ |
if((f=popen("/bin/ps -Al" , "r")) == NULL) { |
| 233 |
+ |
errf("Failed to get process stats (%m)"); |
| 234 |
+ |
die(); |
| 235 |
+ |
} |
| 236 |
+ |
|
| 237 |
+ |
while((line=fpgetline(f)) != NULL) { |
| 238 |
+ |
line_p=line; |
| 239 |
+ |
for(; (*line_p == ' ') && (*line_p != '\0'); line_p++); |
| 240 |
+ |
line_p=strchr(line_p, ' '); |
| 241 |
+ |
for(; (*line_p == ' ') && (*line_p != '\0'); line_p++); |
| 242 |
+ |
if (line_p==NULL) abort(); |
| 243 |
+ |
/* Ok, we should now be at the state :) .. */ |
| 244 |
+ |
if (*line_p=='S') sleeping++; |
| 245 |
+ |
if (*line_p=='R') running++; |
| 246 |
+ |
if (*line_p=='Z') zombie++; |
| 247 |
+ |
if (*line_p=='T') stopped++; |
| 248 |
+ |
} |
| 249 |
+ |
|
| 250 |
+ |
if((pclose(f)) == -1) { |
| 251 |
+ |
errf("Failed to close process stats (%m)"); |
| 252 |
+ |
die(); |
| 253 |
+ |
} |
| 254 |
+ |
|
| 255 |
+ |
printf("packet.processes.sleeping %d\n",sleeping); |
| 256 |
+ |
printf("packet.processes.cpu %d\n",running); |
| 257 |
+ |
printf("packet.processes.zombie %d\n",zombie); |
| 258 |
+ |
printf("packet.processes.stopped %d\n", stopped); |
| 259 |
+ |
printf("packet.processes.total %d\n", (sleeping+running+zombie+stopped)); |
| 260 |
+ |
|
| 261 |
+ |
} |
| 262 |
+ |
|
| 263 |
+ |
|
| 264 |
|
int main(){ |
| 265 |
|
diskStats(); |
| 266 |
|
osStats(); |
| 267 |
|
loadStats(); |
| 268 |
|
userStats(); |
| 269 |
|
systemStats(); |
| 270 |
< |
|
| 270 |
> |
processStats(); |
| 271 |
|
return 0; |
| 272 |
|
} |
| 273 |
|
|