--- projects/cms/source/ihost/libstatgrab/cpu_stats.c 2002/05/13 17:10:09 1.1 +++ projects/cms/source/ihost/libstatgrab/cpu_stats.c 2002/05/21 16:47:12 1.4 @@ -1,15 +1,60 @@ +/* + * i-scream central monitoring system + * http://www.i-scream.org.uk + * Copyright (C) 2000-2002 i-scream + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + #include -#include +#include #include - +#ifdef SOLARIS +#include +#include +#endif +#ifdef FREEBSD +#include +#include +#endif #define WAIT_TIME_IN_SECS 1 char *get_cpu_stats(){ - double u_p, i_p, k_p; + char *xml_cpu_stats; + double u_p=0.00; + double i_p=0.00; + double k_p=0.00; + double io_p=0.00; + double s_p=0.00; #ifdef LINUX long total, user, idle, kernel, nice; long cpu_states[4][2]; FILE *f; #endif +#ifdef SOLARIS + kstat_ctl_t *kc; + kstat_t *ksp; + uint_t cpu_states[5][2]; + uint_t user, kernel, idle, iowait, swap, total; + cpu_stat_t cs; + +#endif +#ifdef FREEBSD + long cp_time[CPUSTATES]; + long total, user, idle, kernel, nice; + size_t size; +#endif #ifdef LINUX if ((f=fopen("/proc/stat", "r" ))==NULL) { @@ -54,11 +99,105 @@ char *get_cpu_stats(){ i_p=((double)idle/(double)total)*100.00; #endif +#ifdef SOLARIS + if ((kc = kstat_open()) == NULL) { + errf("kstat_open failure (%m)"); + return NULL; + } + for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) { + if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue; + if (kstat_read(kc, ksp, &cs) == -1) { + errf("kstat read failure"); + continue; + } + } + cpu_states[0][0]=cs.cpu_sysinfo.cpu[CPU_USER]; + cpu_states[1][0]=cs.cpu_sysinfo.cpu[CPU_WAIT]; + cpu_states[2][0]=cs.cpu_sysinfo.cpu[CPU_KERNEL]; + cpu_states[3][0]=cs.cpu_sysinfo.cpu[CPU_IDLE]; + cpu_states[4][0]=cs.cpu_sysinfo.cpu[CPU_STATES]; + + sleep(WAIT_TIME_IN_SECS); - return "jibble"; -} + if (kstat_chain_update(kc) == -1) { + errf("Kstat update failure (%m)"); + return NULL; + } + for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) { + if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue; + if (kstat_read(kc, ksp, &cs) == -1) { + errf("kstat read failure"); + continue; + } + } + cpu_states[0][1]=cs.cpu_sysinfo.cpu[CPU_USER]; + cpu_states[1][1]=cs.cpu_sysinfo.cpu[CPU_WAIT]; + cpu_states[2][1]=cs.cpu_sysinfo.cpu[CPU_KERNEL]; + cpu_states[3][1]=cs.cpu_sysinfo.cpu[CPU_IDLE]; + cpu_states[4][1]=cs.cpu_sysinfo.cpu[CPU_STATES]; -int main(){ - get_cpu_stats(); - exit(0); + if((kstat_close(kc)) != 0){ + errf("Failed to close kstat control structure (%m)"); + return NULL; + } + + user=cpu_states[0][1]-cpu_states[0][0]; + iowait=cpu_states[1][1]-cpu_states[1][0]; + kernel=cpu_states[2][1]-cpu_states[2][0]; + idle=cpu_states[3][1]-cpu_states[3][0]; + swap=cpu_states[4][1]-cpu_states[4][0]; + total=user+kernel+idle+iowait+swap; + + u_p=((double)user/(double)total)*100.00; + k_p=((double)kernel/(double)total)*100.00; + i_p=((double)idle/(double)total)*100.00; + io_p=((double)iowait/(double)total)*100.00; + s_p=((double)swap/(double)total)*100.00; + +#endif +#ifdef FREEBSD + +#define CPU_STAT_NAME "kern.cp_time" + + if (sysctlbyname(CPU_STAT_NAME, NULL, &size, NULL, NULL) < 0){ + errf("sysctrlbyname failed (%m)"); + return NULL; + } + if (sysctlbyname(CPU_STAT_NAME, &cp_time, &size, NULL, NULL) < 0){ + errf("Failed to get cpu stats (%m)"); + return NULL; + } + + user=cp_time[CP_USER]; + nice=cp_time[CP_NICE]; + kernel=cp_time[CP_SYS]; + idle=cp_time[CP_IDLE]; + + sleep(WAIT_TIME_IN_SECS); + + if (sysctlbyname(CPU_STAT_NAME, &cp_time, &size, NULL, NULL) < 0){ + errf("Failed to get cpu stats (%m)"); + return NULL; + } + + user=cp_time[CP_USER]-user; + nice=cp_time[CP_NICE]-nice; + kernel=cp_time[CP_SYS]-kernel; + idle=cp_time[CP_IDLE]-idle; + total=user+nice+kernel+idle; + + u_p=((float)(user+nice)/(float)total)*100.0; + k_p=((float)(kernel)/(float)total)*100.0; + i_p=((float)(idle)/(float)total)*100.0; + +#endif + + + if((xml_cpu_stats=strf("%3.2f%3.2f%3.2f%3.2f%3.2f", u_p, k_p, i_p, io_p, s_p)) == NULL){ + errf("strf failed (%m)"); + return NULL; + } + + return xml_cpu_stats; } +