| 1 |
/* |
| 2 |
* kstat() interface |
| 3 |
* e.g. Solaris |
| 4 |
*/ |
| 5 |
#include <net-snmp/net-snmp-config.h> |
| 6 |
#include <net-snmp/net-snmp-includes.h> |
| 7 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 8 |
#include <net-snmp/agent/hardware/cpu.h> |
| 9 |
|
| 10 |
#include <stdlib.h> |
| 11 |
#include <unistd.h> |
| 12 |
#include <sys/types.h> |
| 13 |
|
| 14 |
#include <kstat.h> |
| 15 |
#include <sys/sysinfo.h> |
| 16 |
|
| 17 |
extern kstat_ctl_t *kstat_fd; |
| 18 |
extern int cpu_num; |
| 19 |
int _cpu_status(char *state); |
| 20 |
|
| 21 |
/* |
| 22 |
* Initialise the list of CPUs on the system |
| 23 |
* (including descriptions) |
| 24 |
*/ |
| 25 |
void init_cpu_kstat(void) |
| 26 |
{ |
| 27 |
int i = 0, n = 0, clock; |
| 28 |
char ctype[16], ftype[16], state[10]; |
| 29 |
kstat_t *ksp; |
| 30 |
kstat_named_t *ks_data; |
| 31 |
netsnmp_cpu_info *cpu = netsnmp_cpu_get_byIdx( -1, 1 ); |
| 32 |
strcpy(cpu->name, "Overall CPU statistics"); |
| 33 |
|
| 34 |
if (kstat_fd == NULL) |
| 35 |
kstat_fd = kstat_open(); |
| 36 |
kstat_chain_update( kstat_fd ); |
| 37 |
|
| 38 |
DEBUGMSGTL(("cpu", "cpu_kstat init\n ")); |
| 39 |
for (ksp = kstat_fd->kc_chain; ksp != NULL; ksp = ksp->ks_next) { |
| 40 |
if (ksp->ks_flags & KSTAT_FLAG_INVALID) |
| 41 |
continue; |
| 42 |
if ((strcmp(ksp->ks_module, "cpu_info") == 0) && |
| 43 |
(strcmp(ksp->ks_class, "misc" ) == 0)) { |
| 44 |
kstat_read(kstat_fd, ksp, NULL ); |
| 45 |
n++; |
| 46 |
clock = 999999; |
| 47 |
memset(ctype, 0, sizeof(ctype)); |
| 48 |
memset(ftype, 0, sizeof(ftype)); |
| 49 |
memset(state, 0, sizeof(state)); |
| 50 |
for (i=0, ks_data = ksp->ks_data; i < ksp->ks_ndata; i++, ks_data++) { |
| 51 |
if ( strcmp( ks_data->name, "state" ) == 0 ) { |
| 52 |
strlcpy(state, ks_data->value.c, sizeof(state)); |
| 53 |
} else if ( strcmp( ks_data->name, "cpu_type" ) == 0 ) { |
| 54 |
strlcpy(ctype, ks_data->value.c, sizeof(ctype)); |
| 55 |
} else if ( strcmp( ks_data->name, "fpu_type" ) == 0 ) { |
| 56 |
strlcpy(ftype, ks_data->value.c, sizeof(ftype)); |
| 57 |
} else if ( strcmp( ks_data->name, "clock_MHz" ) == 0 ) { |
| 58 |
clock = ks_data->value.i32; |
| 59 |
} |
| 60 |
} |
| 61 |
i = ksp->ks_instance; |
| 62 |
cpu = netsnmp_cpu_get_byIdx( i, 1 ); |
| 63 |
sprintf( cpu->name, "cpu%d", i ); |
| 64 |
sprintf( cpu->descr, "CPU %d Sun %d MHz %s with %s FPU %s", |
| 65 |
i, clock, ctype, ftype, state ); |
| 66 |
cpu->status = _cpu_status(state); /* XXX - or in 'n_c_a_load' ? */ |
| 67 |
} |
| 68 |
} |
| 69 |
cpu_num = i; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
/* |
| 74 |
* Load the latest CPU usage statistics |
| 75 |
*/ |
| 76 |
int netsnmp_cpu_arch_load( netsnmp_cache *cache, void *magic ) { |
| 77 |
int i=1; |
| 78 |
kstat_t *ksp; |
| 79 |
cpu_stat_t cs; |
| 80 |
netsnmp_cpu_info *cpu = netsnmp_cpu_get_byIdx( -1, 0 ); |
| 81 |
netsnmp_cpu_info *cpu2; |
| 82 |
|
| 83 |
/* Clear overall stats, ready for summing individual CPUs */ |
| 84 |
cpu->user_ticks = 0; |
| 85 |
cpu->idle_ticks = 0; |
| 86 |
cpu->kern_ticks = 0; |
| 87 |
cpu->wait_ticks = 0; |
| 88 |
cpu->sys2_ticks = 0; |
| 89 |
cpu->swapIn = 0; |
| 90 |
cpu->swapOut = 0; |
| 91 |
cpu->nInterrupts = 0; |
| 92 |
cpu->nCtxSwitches = 0; |
| 93 |
|
| 94 |
kstat_chain_update( kstat_fd ); |
| 95 |
DEBUGMSGTL(("cpu", "cpu_kstat load\n ")); |
| 96 |
for (ksp = kstat_fd->kc_chain; ksp != NULL; ksp = ksp->ks_next) { |
| 97 |
if (ksp->ks_flags & KSTAT_FLAG_INVALID) |
| 98 |
continue; |
| 99 |
if (strcmp(ksp->ks_module, "cpu_stat") == 0) { |
| 100 |
i = ksp->ks_instance; |
| 101 |
cpu2 = netsnmp_cpu_get_byIdx( i, 0 ); |
| 102 |
if ( !cpu2 ) |
| 103 |
break; /* or continue ? */ /* Skip new CPUs */ |
| 104 |
if ((ksp->ks_type != KSTAT_TYPE_RAW) || |
| 105 |
(ksp->ks_data_size != sizeof(cs))|| |
| 106 |
(kstat_read(kstat_fd, ksp, &cs) == -1)) { |
| 107 |
DEBUGMSGTL(("cpu", "cpu_kstat load failed (%d)\n ", i)); |
| 108 |
break; /* or continue ? */ |
| 109 |
} |
| 110 |
|
| 111 |
cpu2->user_ticks = (unsigned long long)cs.cpu_sysinfo.cpu[CPU_USER]; |
| 112 |
cpu2->idle_ticks = (unsigned long long)cs.cpu_sysinfo.cpu[CPU_IDLE]; |
| 113 |
cpu2->kern_ticks = (unsigned long long)cs.cpu_sysinfo.cpu[CPU_KERNEL]; |
| 114 |
cpu2->wait_ticks = (unsigned long long)cs.cpu_sysinfo.cpu[CPU_WAIT]; |
| 115 |
/* or cs.cpu_sysinfo.wait[W_IO]+cs.cpu_sysinfo.wait[W_PIO] */ |
| 116 |
cpu2->sys2_ticks = (unsigned long long)cpu2->kern_ticks+cpu2->wait_ticks; |
| 117 |
/* nice_ticks, intrpt_ticks, sirq_ticks unused */ |
| 118 |
|
| 119 |
/* sum these for the overall stats */ |
| 120 |
cpu->user_ticks += (unsigned long long)cs.cpu_sysinfo.cpu[CPU_USER]; |
| 121 |
cpu->idle_ticks += (unsigned long long)cs.cpu_sysinfo.cpu[CPU_IDLE]; |
| 122 |
cpu->kern_ticks += (unsigned long long)cs.cpu_sysinfo.cpu[CPU_KERNEL]; |
| 123 |
cpu->wait_ticks += (unsigned long long)cs.cpu_sysinfo.cpu[CPU_WAIT]; |
| 124 |
/* or cs.cpu_sysinfo.wait[W_IO]+cs.cpu_sysinfo.wait[W_PIO] */ |
| 125 |
cpu->sys2_ticks += (unsigned long long)cpu2->kern_ticks+cpu2->wait_ticks; |
| 126 |
|
| 127 |
/* |
| 128 |
* Interrupt/Context Switch statistics |
| 129 |
* XXX - Do these really belong here ? |
| 130 |
*/ |
| 131 |
cpu->swapIn += (unsigned long long)cs.cpu_vminfo.swapin; |
| 132 |
cpu->swapOut += (unsigned long long)cs.cpu_vminfo.swapout; |
| 133 |
cpu->pageIn += (unsigned long long)cs.cpu_sysinfo.bread; |
| 134 |
cpu->pageOut += (unsigned long long)cs.cpu_sysinfo.bwrite; |
| 135 |
cpu->nInterrupts += (unsigned long long)cs.cpu_sysinfo.intr; |
| 136 |
cpu->nCtxSwitches += (unsigned long long)cs.cpu_sysinfo.pswitch; |
| 137 |
} |
| 138 |
} |
| 139 |
return 0; |
| 140 |
} |
| 141 |
|
| 142 |
int |
| 143 |
_cpu_status( char *state) |
| 144 |
{ |
| 145 |
/* |
| 146 |
* hrDeviceStatus OBJECT-TYPE |
| 147 |
* SYNTAX INTEGER { |
| 148 |
* unknown(1), running(2), warning(3), testing(4), down(5) |
| 149 |
* } |
| 150 |
*/ |
| 151 |
if (strcmp(state,"on-line")==0) |
| 152 |
{ return 2; /* running */ } |
| 153 |
else if (strcmp(state,"off-line")==0) |
| 154 |
{ return 5; /* down */ } |
| 155 |
else if (strcmp(state,"missing")==0) |
| 156 |
{ return 3; /* warning, went missing */ } |
| 157 |
else if (strcmp(state,"testing")==0) |
| 158 |
{ return 4; /* somebody must be testing code somewhere */ } |
| 159 |
else |
| 160 |
{ return 1; /* unknown */ } |
| 161 |
} |
| 162 |
|