| 1 |
tdb |
1.9 |
/* |
| 2 |
|
|
* i-scream central monitoring system |
| 3 |
tdb |
1.10 |
* http://www.i-scream.org.uk |
| 4 |
tdb |
1.9 |
* Copyright (C) 2000-2002 i-scream |
| 5 |
|
|
* |
| 6 |
|
|
* This program is free software; you can redistribute it and/or |
| 7 |
|
|
* modify it under the terms of the GNU General Public License |
| 8 |
|
|
* as published by the Free Software Foundation; either version 2 |
| 9 |
|
|
* of the License, or (at your option) any later version. |
| 10 |
|
|
* |
| 11 |
|
|
* This program is distributed in the hope that it will be useful, |
| 12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
|
|
* GNU General Public License for more details. |
| 15 |
|
|
* |
| 16 |
|
|
* You should have received a copy of the GNU General Public License |
| 17 |
|
|
* along with this program; if not, write to the Free Software |
| 18 |
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 |
|
|
*/ |
| 20 |
|
|
|
| 21 |
pajs |
1.1 |
#include <stdio.h> |
| 22 |
|
|
#include <sys/types.h> |
| 23 |
|
|
#include <sys/statvfs.h> |
| 24 |
|
|
#include <sys/mnttab.h> |
| 25 |
tdb |
1.3 |
#include "ukcprog.h" |
| 26 |
pajs |
1.2 |
#include <sys/utsname.h> |
| 27 |
|
|
#include <sys/loadavg.h> |
| 28 |
pajs |
1.4 |
#include <utmp.h> |
| 29 |
|
|
#include <kstat.h> |
| 30 |
|
|
#include <sys/sysinfo.h> |
| 31 |
|
|
#include <unistd.h> |
| 32 |
pajs |
1.5 |
#include <sys/stat.h> |
| 33 |
|
|
#include <sys/swap.h> |
| 34 |
|
|
#include <stdlib.h> |
| 35 |
|
|
#include <strings.h> |
| 36 |
pajs |
1.6 |
#include <time.h> |
| 37 |
pajs |
1.1 |
|
| 38 |
pajs |
1.4 |
void die(){ |
| 39 |
|
|
exit(1); |
| 40 |
pajs |
1.1 |
} |
| 41 |
|
|
|
| 42 |
|
|
|
| 43 |
pajs |
1.4 |
void diskStats(){ |
| 44 |
|
|
struct mnttab mp; |
| 45 |
|
|
struct statvfs df; |
| 46 |
|
|
FILE *f; |
| 47 |
|
|
int counter=0; |
| 48 |
|
|
|
| 49 |
|
|
if ((f=fopen("/etc/mnttab", "r" ))==NULL){ |
| 50 |
|
|
errf("Failed to open mounts (%m)"); |
| 51 |
|
|
die(); |
| 52 |
|
|
} |
| 53 |
pajs |
1.1 |
|
| 54 |
pajs |
1.4 |
while((getmntent(f, &mp)) == 0){ |
| 55 |
|
|
if ((statvfs(mp.mnt_mountp, &df)) !=0){ |
| 56 |
|
|
errf("Failed to gets fs stats (%m)"); |
| 57 |
pajs |
1.8 |
continue; |
| 58 |
pajs |
1.4 |
} |
| 59 |
pajs |
1.7 |
|
| 60 |
|
|
if((((strcmp(mp.mnt_fstype,"ufs"))==0) || (strcmp(mp.mnt_fstype,"tmpfs")) ==0)){ |
| 61 |
pajs |
1.1 |
|
| 62 |
pajs |
1.7 |
printf("packet.disk.p%d.attributes.mount %s\n", counter, mp.mnt_mountp); |
| 63 |
|
|
printf("packet.disk.p%d.attributes.name %s\n", counter, mp.mnt_special); |
| 64 |
|
|
printf("packet.disk.p%d.attributes.kbytes %lu\n",counter, ((df.f_frsize/1024) * df.f_blocks)); |
| 65 |
|
|
printf("packet.disk.p%d.attributes.used %lu\n",counter, (((df.f_frsize/1024) * df.f_blocks) -((df.f_frsize/1024) * df.f_bfree))); |
| 66 |
|
|
printf("packet.disk.p%d.attributes.avail %lu\n",counter, (df.f_frsize/1024) * df.f_bavail); |
| 67 |
|
|
printf("packet.disk.p%d.attributes.totalinodes %lu\n", counter, df.f_files); |
| 68 |
|
|
printf("packet.disk.p%d.attributes.freeinodes %lu\n",counter, df.f_ffree); |
| 69 |
pajs |
1.4 |
|
| 70 |
pajs |
1.7 |
counter++; |
| 71 |
|
|
} |
| 72 |
pajs |
1.4 |
} |
| 73 |
pajs |
1.1 |
} |
| 74 |
|
|
|
| 75 |
pajs |
1.4 |
void osStats(){ |
| 76 |
|
|
struct utsname os; |
| 77 |
|
|
|
| 78 |
|
|
if((uname(&os)) == -1){ |
| 79 |
|
|
errf("Failed to get os stats (%m)"); |
| 80 |
|
|
die(); |
| 81 |
|
|
} |
| 82 |
|
|
|
| 83 |
|
|
printf("packet.os.name %s\n", os.sysname); |
| 84 |
|
|
printf("packet.os.release %s\n" , os.release); |
| 85 |
|
|
printf("packet.os.version %s\n", os.version); |
| 86 |
|
|
printf("packet.os.sysname %s\n" , os.nodename); |
| 87 |
|
|
printf("packet.os.platform %s\n", os.machine); |
| 88 |
pajs |
1.2 |
|
| 89 |
|
|
} |
| 90 |
|
|
|
| 91 |
pajs |
1.4 |
void loadStats(){ |
| 92 |
pajs |
1.2 |
double loadav[3]; |
| 93 |
|
|
|
| 94 |
pajs |
1.4 |
if((getloadavg(loadav,3)) == -1){ |
| 95 |
|
|
errf("Failed to get load averages (%m)"); |
| 96 |
|
|
die(); |
| 97 |
pajs |
1.2 |
} |
| 98 |
|
|
|
| 99 |
|
|
printf("packet.load.load1 %.2f\n",loadav[0]); |
| 100 |
|
|
printf("packet.load.load5 %.2f\n",loadav[1]); |
| 101 |
|
|
printf("packet.load.load15 %.2f\n",loadav[2]); |
| 102 |
|
|
} |
| 103 |
pajs |
1.1 |
|
| 104 |
pajs |
1.4 |
void userStats(){ |
| 105 |
|
|
int nousers=0; |
| 106 |
|
|
struct utmp *entry; |
| 107 |
|
|
|
| 108 |
|
|
printf("packet.users.list"); |
| 109 |
|
|
|
| 110 |
|
|
while((entry=getutent()) != NULL){ |
| 111 |
|
|
if(entry->ut_type==USER_PROCESS) |
| 112 |
|
|
{ |
| 113 |
|
|
printf(" %s",entry->ut_user); |
| 114 |
|
|
nousers++; |
| 115 |
|
|
} |
| 116 |
|
|
} |
| 117 |
|
|
printf("\npacket.users.count %d\n", nousers); |
| 118 |
|
|
} |
| 119 |
|
|
|
| 120 |
|
|
void systemStats(){ |
| 121 |
|
|
kstat_ctl_t *kc; |
| 122 |
|
|
kstat_t *ksp; |
| 123 |
pajs |
1.5 |
kstat_named_t *kn; |
| 124 |
|
|
int pagesize; |
| 125 |
pajs |
1.4 |
cpu_stat_t cs; |
| 126 |
pajs |
1.6 |
uint_t cpustats[5][2]; |
| 127 |
pajs |
1.4 |
float usage; |
| 128 |
pajs |
1.6 |
uint_t user, kernel, idle, iowait, swap, total; |
| 129 |
pajs |
1.5 |
ulong totalmem, freemem; |
| 130 |
|
|
long swaptotal, swapused; |
| 131 |
pajs |
1.6 |
time_t uptime,curtime; |
| 132 |
pajs |
1.5 |
|
| 133 |
|
|
struct anoninfo ai; |
| 134 |
pajs |
1.4 |
|
| 135 |
pajs |
1.5 |
|
| 136 |
pajs |
1.4 |
if ((kc = kstat_open()) == NULL) { |
| 137 |
|
|
errf("kstat_open failure (%m)"); |
| 138 |
|
|
die(); |
| 139 |
|
|
} |
| 140 |
|
|
|
| 141 |
|
|
/* Borrowed from gkrellm ;) */ |
| 142 |
|
|
|
| 143 |
|
|
for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) { |
| 144 |
|
|
if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) |
| 145 |
|
|
continue; |
| 146 |
|
|
if (kstat_read(kc, ksp, &cs) == -1) { |
| 147 |
|
|
perror("kstat_read"); |
| 148 |
|
|
continue; |
| 149 |
|
|
} |
| 150 |
|
|
} |
| 151 |
|
|
|
| 152 |
|
|
cpustats[0][0]=cs.cpu_sysinfo.cpu[CPU_USER]; |
| 153 |
|
|
cpustats[1][0]=cs.cpu_sysinfo.cpu[CPU_WAIT]; |
| 154 |
|
|
cpustats[2][0]=cs.cpu_sysinfo.cpu[CPU_KERNEL]; |
| 155 |
|
|
cpustats[3][0]=cs.cpu_sysinfo.cpu[CPU_IDLE]; |
| 156 |
pajs |
1.6 |
cpustats[4][0]=cs.cpu_sysinfo.cpu[CPU_STATES]; |
| 157 |
pajs |
1.4 |
|
| 158 |
|
|
sleep(1); |
| 159 |
|
|
|
| 160 |
|
|
if (kstat_chain_update(kc) == -1) { |
| 161 |
|
|
errf("Kstat update failure (%m)"); |
| 162 |
|
|
die(); |
| 163 |
|
|
} |
| 164 |
|
|
|
| 165 |
|
|
for (ksp = kc->kc_chain; ksp!=NULL ; ksp = ksp->ks_next) { |
| 166 |
|
|
if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) |
| 167 |
|
|
continue; |
| 168 |
|
|
if (kstat_read(kc, ksp, &cs) == -1) { |
| 169 |
|
|
perror("kstat_read"); |
| 170 |
|
|
continue; |
| 171 |
|
|
} |
| 172 |
|
|
} |
| 173 |
|
|
|
| 174 |
|
|
cpustats[0][1]=cs.cpu_sysinfo.cpu[CPU_USER]; |
| 175 |
|
|
cpustats[1][1]=cs.cpu_sysinfo.cpu[CPU_WAIT]; |
| 176 |
|
|
cpustats[2][1]=cs.cpu_sysinfo.cpu[CPU_KERNEL]; |
| 177 |
|
|
cpustats[3][1]=cs.cpu_sysinfo.cpu[CPU_IDLE]; |
| 178 |
pajs |
1.6 |
cpustats[4][1]=cs.cpu_sysinfo.cpu[CPU_STATES]; |
| 179 |
pajs |
1.4 |
|
| 180 |
|
|
user=cpustats[0][1]-cpustats[0][0]; |
| 181 |
|
|
iowait=cpustats[1][1]-cpustats[1][0]; |
| 182 |
|
|
kernel=cpustats[2][1]-cpustats[2][0]; |
| 183 |
|
|
idle=cpustats[3][1]-cpustats[3][0]; |
| 184 |
pajs |
1.6 |
swap=cpustats[4][1]-cpustats[4][0]; |
| 185 |
pajs |
1.4 |
|
| 186 |
pajs |
1.6 |
total=user+kernel+idle+iowait+swap; |
| 187 |
pajs |
1.4 |
|
| 188 |
|
|
usage=((((float)user)/((float)total))*100.00); |
| 189 |
|
|
|
| 190 |
|
|
printf("packet.cpu.user %3.2f\n", usage); |
| 191 |
|
|
usage=((((float)kernel)/((float)total))*100.00); |
| 192 |
|
|
printf("packet.cpu.kernel %3.2f\n", usage); |
| 193 |
|
|
usage=((((float)idle)/((float)total))*100.00); |
| 194 |
|
|
printf("packet.cpu.idle %3.2f\n", usage); |
| 195 |
|
|
usage=((((float)iowait)/((float)total))*100.00); |
| 196 |
|
|
printf("packet.cpu.iowait %3.2f\n", usage); |
| 197 |
pajs |
1.6 |
usage=((((float)swap)/((float)total))*100.00); |
| 198 |
|
|
printf("packet.cpu.swap %3.2f\n", usage); |
| 199 |
|
|
|
| 200 |
pajs |
1.5 |
/* MEMORY STATS */ |
| 201 |
|
|
|
| 202 |
|
|
if((pagesize=sysconf(_SC_PAGESIZE)) == -1){ |
| 203 |
|
|
errf("Failed to get pagesize (%m)"); |
| 204 |
pajs |
1.4 |
die(); |
| 205 |
|
|
} |
| 206 |
pajs |
1.2 |
|
| 207 |
pajs |
1.5 |
if((totalmem=sysconf(_SC_PHYS_PAGES)) == -1){ |
| 208 |
|
|
errf("Failed to get total memory"); |
| 209 |
|
|
die(); |
| 210 |
|
|
} |
| 211 |
|
|
|
| 212 |
|
|
totalmem=((totalmem*pagesize)/1024)/1024; |
| 213 |
|
|
|
| 214 |
|
|
ksp = kstat_lookup(kc, "unix", 0, "system_pages"); |
| 215 |
|
|
if (kstat_read(kc, ksp, 0) == -1) { |
| 216 |
|
|
perror("kstat_read"); |
| 217 |
|
|
die(); |
| 218 |
|
|
} |
| 219 |
|
|
|
| 220 |
|
|
if((kn=kstat_data_lookup(ksp, "freemem")) == NULL){ |
| 221 |
|
|
errf("Failed to get free memory (%m)"); |
| 222 |
pajs |
1.4 |
die(); |
| 223 |
|
|
} |
| 224 |
|
|
|
| 225 |
pajs |
1.5 |
freemem=(((kn->value.ul)*pagesize)/1024)/1024; |
| 226 |
|
|
|
| 227 |
|
|
printf("packet.memory.total %lu\n",totalmem); |
| 228 |
|
|
printf("packet.memory.free %lu\n",freemem); |
| 229 |
|
|
printf("packet.memory.used %lu\n",(totalmem-freemem)); |
| 230 |
|
|
|
| 231 |
|
|
|
| 232 |
|
|
if (swapctl(SC_AINFO, &ai) == -1) { |
| 233 |
|
|
errf("Failed to get swap details (%m)"); |
| 234 |
|
|
die(); |
| 235 |
|
|
} |
| 236 |
|
|
|
| 237 |
|
|
swaptotal=((ai.ani_max/1024)*pagesize)/1024; |
| 238 |
|
|
swapused=((ai.ani_resv/1024)*pagesize)/1024; |
| 239 |
|
|
|
| 240 |
|
|
/* Ok this is actually for TOP combatability, but this is not |
| 241 |
|
|
total swap, cos it actually also seems to include memory stats. */ |
| 242 |
|
|
printf("packet.swap.total %ld\n",swaptotal); |
| 243 |
|
|
printf("packet.swap.used %ld\n", swapused); |
| 244 |
|
|
printf("packet.swap.free %lu\n", (swaptotal-swapused)); |
| 245 |
pajs |
1.6 |
|
| 246 |
|
|
ksp=kstat_lookup(kc, "unix", -1, "system_misc"); |
| 247 |
|
|
if (kstat_read(kc, ksp, 0) == -1) { |
| 248 |
|
|
errf("Failed to get uptime (%m)"); |
| 249 |
|
|
die(); |
| 250 |
|
|
} |
| 251 |
|
|
|
| 252 |
|
|
if((kn=kstat_data_lookup(ksp, "boot_time")) == NULL){ |
| 253 |
|
|
errf("Failed to get uptime (%m)"); |
| 254 |
|
|
die(); |
| 255 |
|
|
} |
| 256 |
|
|
|
| 257 |
|
|
uptime=(kn->value.ui32); |
| 258 |
|
|
time(&curtime); |
| 259 |
|
|
printf("packet.os.uptime %ld\n", (curtime-uptime)); |
| 260 |
pajs |
1.5 |
|
| 261 |
|
|
} |
| 262 |
|
|
|
| 263 |
|
|
void processStats() { |
| 264 |
|
|
int sleeping=-1; |
| 265 |
|
|
int zombie=0; |
| 266 |
|
|
int stopped=0; |
| 267 |
|
|
int running=0; |
| 268 |
|
|
char *line; |
| 269 |
|
|
char *line_p; |
| 270 |
|
|
|
| 271 |
|
|
|
| 272 |
|
|
FILE *f; |
| 273 |
|
|
|
| 274 |
|
|
if((f=popen("/bin/ps -Al" , "r")) == NULL) { |
| 275 |
|
|
errf("Failed to get process stats (%m)"); |
| 276 |
|
|
die(); |
| 277 |
|
|
} |
| 278 |
|
|
|
| 279 |
|
|
while((line=fpgetline(f)) != NULL) { |
| 280 |
|
|
line_p=line; |
| 281 |
|
|
for(; (*line_p == ' ') && (*line_p != '\0'); line_p++); |
| 282 |
|
|
line_p=strchr(line_p, ' '); |
| 283 |
|
|
for(; (*line_p == ' ') && (*line_p != '\0'); line_p++); |
| 284 |
|
|
if (line_p==NULL) abort(); |
| 285 |
|
|
/* Ok, we should now be at the state :) .. */ |
| 286 |
|
|
if (*line_p=='S') sleeping++; |
| 287 |
|
|
if (*line_p=='R') running++; |
| 288 |
|
|
if (*line_p=='Z') zombie++; |
| 289 |
|
|
if (*line_p=='T') stopped++; |
| 290 |
|
|
} |
| 291 |
|
|
|
| 292 |
|
|
if((pclose(f)) == -1) { |
| 293 |
|
|
errf("Failed to close process stats (%m)"); |
| 294 |
|
|
die(); |
| 295 |
|
|
} |
| 296 |
|
|
|
| 297 |
|
|
printf("packet.processes.sleeping %d\n",sleeping); |
| 298 |
|
|
printf("packet.processes.cpu %d\n",running); |
| 299 |
|
|
printf("packet.processes.zombie %d\n",zombie); |
| 300 |
|
|
printf("packet.processes.stopped %d\n", stopped); |
| 301 |
|
|
printf("packet.processes.total %d\n", (sleeping+running+zombie+stopped)); |
| 302 |
|
|
|
| 303 |
pajs |
1.4 |
} |
| 304 |
pajs |
1.5 |
|
| 305 |
pajs |
1.4 |
|
| 306 |
|
|
int main(){ |
| 307 |
pajs |
1.2 |
diskStats(); |
| 308 |
|
|
osStats(); |
| 309 |
|
|
loadStats(); |
| 310 |
pajs |
1.4 |
userStats(); |
| 311 |
|
|
systemStats(); |
| 312 |
pajs |
1.5 |
processStats(); |
| 313 |
pajs |
1.2 |
return 0; |
| 314 |
pajs |
1.1 |
} |
| 315 |
|
|
|