| 1 |
pajs |
1.1 |
#include <stdio.h> |
| 2 |
|
|
#include <sys/types.h> |
| 3 |
|
|
#include <sys/statvfs.h> |
| 4 |
|
|
#include <sys/mnttab.h> |
| 5 |
tdb |
1.3 |
#include "ukcprog.h" |
| 6 |
pajs |
1.2 |
#include <sys/utsname.h> |
| 7 |
|
|
#include <sys/loadavg.h> |
| 8 |
pajs |
1.4 |
#include <utmp.h> |
| 9 |
|
|
#include <kstat.h> |
| 10 |
|
|
#include <sys/sysinfo.h> |
| 11 |
|
|
#include <unistd.h> |
| 12 |
pajs |
1.1 |
|
| 13 |
pajs |
1.4 |
void die(){ |
| 14 |
|
|
exit(1); |
| 15 |
pajs |
1.1 |
} |
| 16 |
|
|
|
| 17 |
|
|
|
| 18 |
pajs |
1.4 |
void diskStats(){ |
| 19 |
|
|
struct mnttab mp; |
| 20 |
|
|
struct statvfs df; |
| 21 |
|
|
FILE *f; |
| 22 |
|
|
int counter=0; |
| 23 |
|
|
|
| 24 |
|
|
if ((f=fopen("/etc/mnttab", "r" ))==NULL){ |
| 25 |
|
|
errf("Failed to open mounts (%m)"); |
| 26 |
|
|
die(); |
| 27 |
|
|
} |
| 28 |
pajs |
1.1 |
|
| 29 |
pajs |
1.4 |
while((getmntent(f, &mp)) == 0){ |
| 30 |
|
|
if ((statvfs(mp.mnt_mountp, &df)) !=0){ |
| 31 |
|
|
errf("Failed to gets fs stats (%m)"); |
| 32 |
|
|
die(); |
| 33 |
|
|
} |
| 34 |
pajs |
1.1 |
|
| 35 |
pajs |
1.4 |
printf("packet.disk.p%d.attributes.mount %s\n", counter, mp.mnt_mountp); |
| 36 |
|
|
printf("packet.disk.p%d.attributes.name %s\n", counter, mp.mnt_special); |
| 37 |
|
|
printf("packet.disk.p%d.attributes.kbytes %lu\n",counter, ((df.f_frsize/1024) * df.f_blocks)); |
| 38 |
|
|
printf("packet.disk.p%d.attributes.used %lu\n",counter, (((df.f_frsize/1024) * df.f_blocks) -((df.f_frsize/1024) * df.f_bfree))); |
| 39 |
|
|
printf("packet.disk.p%d.attributes.avail %lu\n",counter, (df.f_frsize/1024) * df.f_bavail); |
| 40 |
|
|
printf("packet.disk.p%d.attributes.totalinodes %lu\n", counter, df.f_files); |
| 41 |
|
|
printf("packet.disk.p%d.attributes.freeinodes %lu\n",counter, df.f_ffree); |
| 42 |
|
|
|
| 43 |
|
|
counter++; |
| 44 |
|
|
} |
| 45 |
pajs |
1.1 |
} |
| 46 |
|
|
|
| 47 |
pajs |
1.4 |
void osStats(){ |
| 48 |
|
|
struct utsname os; |
| 49 |
|
|
|
| 50 |
|
|
if((uname(&os)) == -1){ |
| 51 |
|
|
errf("Failed to get os stats (%m)"); |
| 52 |
|
|
die(); |
| 53 |
|
|
} |
| 54 |
|
|
|
| 55 |
|
|
printf("packet.os.name %s\n", os.sysname); |
| 56 |
|
|
printf("packet.os.release %s\n" , os.release); |
| 57 |
|
|
printf("packet.os.version %s\n", os.version); |
| 58 |
|
|
printf("packet.os.sysname %s\n" , os.nodename); |
| 59 |
|
|
printf("packet.os.platform %s\n", os.machine); |
| 60 |
pajs |
1.2 |
|
| 61 |
|
|
} |
| 62 |
|
|
|
| 63 |
pajs |
1.4 |
void loadStats(){ |
| 64 |
pajs |
1.2 |
double loadav[3]; |
| 65 |
|
|
|
| 66 |
pajs |
1.4 |
if((getloadavg(loadav,3)) == -1){ |
| 67 |
|
|
errf("Failed to get load averages (%m)"); |
| 68 |
|
|
die(); |
| 69 |
pajs |
1.2 |
} |
| 70 |
|
|
|
| 71 |
|
|
printf("packet.load.load1 %.2f\n",loadav[0]); |
| 72 |
|
|
printf("packet.load.load5 %.2f\n",loadav[1]); |
| 73 |
|
|
printf("packet.load.load15 %.2f\n",loadav[2]); |
| 74 |
|
|
} |
| 75 |
pajs |
1.1 |
|
| 76 |
pajs |
1.4 |
void userStats(){ |
| 77 |
|
|
int nousers=0; |
| 78 |
|
|
struct utmp *entry; |
| 79 |
|
|
|
| 80 |
|
|
printf("packet.users.list"); |
| 81 |
|
|
|
| 82 |
|
|
while((entry=getutent()) != NULL){ |
| 83 |
|
|
if(entry->ut_type==USER_PROCESS) |
| 84 |
|
|
{ |
| 85 |
|
|
printf(" %s",entry->ut_user); |
| 86 |
|
|
nousers++; |
| 87 |
|
|
} |
| 88 |
|
|
} |
| 89 |
|
|
printf("\npacket.users.count %d\n", nousers); |
| 90 |
|
|
} |
| 91 |
|
|
|
| 92 |
|
|
void systemStats(){ |
| 93 |
|
|
kstat_ctl_t *kc; |
| 94 |
|
|
kstat_t *ksp; |
| 95 |
|
|
kstat_named_t *knp; |
| 96 |
|
|
cpu_stat_t cs; |
| 97 |
|
|
uint_t cpustats[4][2]; |
| 98 |
|
|
float usage; |
| 99 |
|
|
uint_t user, kernel, idle, iowait, total; |
| 100 |
|
|
|
| 101 |
|
|
if ((kc = kstat_open()) == NULL) { |
| 102 |
|
|
errf("kstat_open failure (%m)"); |
| 103 |
|
|
die(); |
| 104 |
|
|
} |
| 105 |
|
|
|
| 106 |
|
|
/* Borrowed from gkrellm ;) */ |
| 107 |
|
|
|
| 108 |
|
|
for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) { |
| 109 |
|
|
if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) |
| 110 |
|
|
continue; |
| 111 |
|
|
if (kstat_read(kc, ksp, &cs) == -1) { |
| 112 |
|
|
perror("kstat_read"); |
| 113 |
|
|
continue; |
| 114 |
|
|
} |
| 115 |
|
|
} |
| 116 |
|
|
|
| 117 |
|
|
cpustats[0][0]=cs.cpu_sysinfo.cpu[CPU_USER]; |
| 118 |
|
|
cpustats[1][0]=cs.cpu_sysinfo.cpu[CPU_WAIT]; |
| 119 |
|
|
cpustats[2][0]=cs.cpu_sysinfo.cpu[CPU_KERNEL]; |
| 120 |
|
|
cpustats[3][0]=cs.cpu_sysinfo.cpu[CPU_IDLE]; |
| 121 |
|
|
|
| 122 |
|
|
sleep(1); |
| 123 |
|
|
|
| 124 |
|
|
if (kstat_chain_update(kc) == -1) { |
| 125 |
|
|
errf("Kstat update failure (%m)"); |
| 126 |
|
|
die(); |
| 127 |
|
|
} |
| 128 |
|
|
|
| 129 |
|
|
for (ksp = kc->kc_chain; ksp!=NULL ; ksp = ksp->ks_next) { |
| 130 |
|
|
if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) |
| 131 |
|
|
continue; |
| 132 |
|
|
if (kstat_read(kc, ksp, &cs) == -1) { |
| 133 |
|
|
perror("kstat_read"); |
| 134 |
|
|
continue; |
| 135 |
|
|
} |
| 136 |
|
|
} |
| 137 |
|
|
|
| 138 |
|
|
cpustats[0][1]=cs.cpu_sysinfo.cpu[CPU_USER]; |
| 139 |
|
|
cpustats[1][1]=cs.cpu_sysinfo.cpu[CPU_WAIT]; |
| 140 |
|
|
cpustats[2][1]=cs.cpu_sysinfo.cpu[CPU_KERNEL]; |
| 141 |
|
|
cpustats[3][1]=cs.cpu_sysinfo.cpu[CPU_IDLE]; |
| 142 |
|
|
|
| 143 |
|
|
user=cpustats[0][1]-cpustats[0][0]; |
| 144 |
|
|
iowait=cpustats[1][1]-cpustats[1][0]; |
| 145 |
|
|
kernel=cpustats[2][1]-cpustats[2][0]; |
| 146 |
|
|
idle=cpustats[3][1]-cpustats[3][0]; |
| 147 |
|
|
|
| 148 |
|
|
total=user+kernel+idle+iowait; |
| 149 |
|
|
|
| 150 |
|
|
usage=((((float)user)/((float)total))*100.00); |
| 151 |
|
|
|
| 152 |
|
|
printf("user : %u\n",user); |
| 153 |
|
|
printf("total : %u\n",total); |
| 154 |
|
|
|
| 155 |
|
|
printf("packet.cpu.user %3.2f\n", usage); |
| 156 |
|
|
usage=((((float)kernel)/((float)total))*100.00); |
| 157 |
|
|
printf("packet.cpu.kernel %3.2f\n", usage); |
| 158 |
|
|
usage=((((float)idle)/((float)total))*100.00); |
| 159 |
|
|
printf("packet.cpu.idle %3.2f\n", usage); |
| 160 |
|
|
usage=((((float)iowait)/((float)total))*100.00); |
| 161 |
|
|
printf("packet.cpu.iowait %3.2f\n", usage); |
| 162 |
|
|
|
| 163 |
|
|
/* if((ksp = kstat_lookup(kc, "unix", -1, "system_pages")) == NULL){ |
| 164 |
|
|
errf("Mem lookup failed (%m)"); |
| 165 |
|
|
die(); |
| 166 |
|
|
} |
| 167 |
pajs |
1.2 |
|
| 168 |
pajs |
1.4 |
if((knp = (kstat_named_t *)kstat_data_lookup(ksp, "pagesfree")) == NULL){ |
| 169 |
|
|
errf("Mem lookup failed (%m)"); |
| 170 |
|
|
die(); |
| 171 |
|
|
} |
| 172 |
|
|
|
| 173 |
|
|
printf("%ui32\n", knp->value.ui32); |
| 174 |
|
|
*/ |
| 175 |
|
|
} |
| 176 |
|
|
|
| 177 |
|
|
|
| 178 |
|
|
|
| 179 |
|
|
int main(){ |
| 180 |
pajs |
1.2 |
diskStats(); |
| 181 |
|
|
osStats(); |
| 182 |
|
|
loadStats(); |
| 183 |
pajs |
1.4 |
userStats(); |
| 184 |
|
|
systemStats(); |
| 185 |
|
|
|
| 186 |
pajs |
1.2 |
return 0; |
| 187 |
pajs |
1.1 |
} |
| 188 |
|
|
|