| 1 |
/* |
| 2 |
* sysinfo (sysget/sysmp) interface |
| 3 |
* e.g. IRIX 6.x |
| 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 <unistd.h> |
| 11 |
#include <sys/types.h> |
| 12 |
#include <sys/sysmp.h> |
| 13 |
#include <sys/sysinfo.h> |
| 14 |
#include <sys/sysget.h> |
| 15 |
|
| 16 |
int cpu_count; |
| 17 |
int sinfo_ksize; |
| 18 |
struct sysinfo* sinfo_cpus; /* individual cpu stats */ |
| 19 |
struct sysinfo* sinfo_gen; /* global stats */ |
| 20 |
|
| 21 |
/* |
| 22 |
* Initialise the list of CPUs on the system |
| 23 |
* (including descriptions) |
| 24 |
*/ |
| 25 |
void init_cpu_sysinfo( void ) |
| 26 |
{ |
| 27 |
int i; |
| 28 |
netsnmp_cpu_info *cpu; |
| 29 |
char tstr[1024]; |
| 30 |
|
| 31 |
cpu_count = sysmp(MP_NPROCS); |
| 32 |
|
| 33 |
/* fetch struct sysinfo size in kernel */ |
| 34 |
|
| 35 |
if ((sinfo_ksize = sysmp(MP_SASZ, MPSA_SINFO)) < 0) { |
| 36 |
snmp_log_perror("init_cpu_sysinfo: sysinfo scall interface not supported"); |
| 37 |
exit(1); |
| 38 |
} |
| 39 |
|
| 40 |
if (sizeof(struct sysinfo) != sinfo_ksize) |
| 41 |
{ |
| 42 |
snmp_log_perror("init_cpu_sysinfo: size mismatch between userland and kernel sysinfo struct"); |
| 43 |
exit(1); |
| 44 |
} |
| 45 |
|
| 46 |
/* allocate sysinfo for all cpus + global stats */ |
| 47 |
|
| 48 |
sinfo_cpus = (struct sysinfo *) calloc(cpu_count, sinfo_ksize); |
| 49 |
if (!sinfo_cpus) { |
| 50 |
snmp_log_perror("init_cpu_sysinfo: Couldn't allocate memory bytes for sinfo_cpus"); |
| 51 |
exit(1); |
| 52 |
} |
| 53 |
|
| 54 |
sinfo_gen = (struct sysinfo *) calloc(1, sinfo_ksize); |
| 55 |
if (!sinfo_gen) { |
| 56 |
snmp_log_perror("init_cpu_sysinfo: Couldn't allocate memory bytes for sinfo_gen"); |
| 57 |
exit(1); |
| 58 |
} |
| 59 |
|
| 60 |
/* register cpus */ |
| 61 |
|
| 62 |
cpu = netsnmp_cpu_get_byIdx(-1, 1); |
| 63 |
strcpy(cpu->name, "Overall CPU statistics"); |
| 64 |
|
| 65 |
for (i = 0; i < cpu_count ; ++i) |
| 66 |
{ |
| 67 |
cpu = netsnmp_cpu_get_byIdx(i, 1); |
| 68 |
sprintf(tstr, "cpu%d",i); |
| 69 |
strlcpy(cpu->name, tstr, sizeof(cpu->name)); |
| 70 |
strcpy(cpu->descr, "Central Processing Unit"); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
/* |
| 75 |
* Load the latest CPU usage statistics |
| 76 |
*/ |
| 77 |
int netsnmp_cpu_arch_load( netsnmp_cache *cache, void *magic ) |
| 78 |
{ |
| 79 |
int i; |
| 80 |
netsnmp_cpu_info* cpu; |
| 81 |
sgt_cookie_t ck; |
| 82 |
|
| 83 |
/* fetch global stats */ |
| 84 |
|
| 85 |
cpu = netsnmp_cpu_get_byIdx(-1, 0); |
| 86 |
|
| 87 |
if (sysmp(MP_SAGET, MPSA_SINFO, (char *) sinfo_gen, sinfo_ksize) < 0) |
| 88 |
{ |
| 89 |
snmp_log_perror("netsnmp_cpu_arch_load: sysmp() failed"); |
| 90 |
exit(1); |
| 91 |
} |
| 92 |
|
| 93 |
DEBUGMSGTL(("cpu_sysinfo", "total cpu kernel: %lu\n", sinfo_gen->cpu[CPU_KERNEL])); |
| 94 |
cpu->sys2_ticks = (unsigned long long) sinfo_gen->cpu[CPU_KERNEL] + (unsigned long long) sinfo_gen->cpu[CPU_SXBRK] + (unsigned long long) sinfo_gen->cpu[CPU_INTR]; |
| 95 |
cpu->kern_ticks = (unsigned long long) sinfo_gen->cpu[CPU_KERNEL]; |
| 96 |
cpu->intrpt_ticks = (unsigned long long) sinfo_gen->cpu[CPU_INTR]; |
| 97 |
cpu->user_ticks = (unsigned long long) sinfo_gen->cpu[CPU_USER]; |
| 98 |
cpu->wait_ticks = (unsigned long long) sinfo_gen->cpu[CPU_WAIT]; |
| 99 |
cpu->idle_ticks = (unsigned long long) sinfo_gen->cpu[CPU_IDLE]; |
| 100 |
|
| 101 |
/* XXX - Do these really belong here ? */ |
| 102 |
cpu->pageIn = (unsigned long long)0; |
| 103 |
cpu->pageOut = (unsigned long long)0; |
| 104 |
cpu->swapIn = (unsigned long long)sinfo_gen->swapin; |
| 105 |
cpu->swapOut = (unsigned long long)sinfo_gen->swapout; |
| 106 |
cpu->nInterrupts = (unsigned long long)sinfo_gen->intr_svcd; |
| 107 |
cpu->nCtxSwitches = (unsigned long long)sinfo_gen->pswitch; |
| 108 |
|
| 109 |
/* fetch individual cpu stats */ |
| 110 |
|
| 111 |
SGT_COOKIE_INIT(&ck); |
| 112 |
if (sysget(MPSA_SINFO, (char *) sinfo_cpus, sinfo_ksize * cpu_count, SGT_READ | SGT_CPUS | SGT_SUM, &ck) < 0) |
| 113 |
{ |
| 114 |
snmp_log_perror("netsnmp_cpu_arch_load: sysget() failed"); |
| 115 |
exit(1); |
| 116 |
} |
| 117 |
|
| 118 |
for (i = 0; i < cpu_count ; ++i) |
| 119 |
{ |
| 120 |
cpu = netsnmp_cpu_get_byIdx(i, 0); |
| 121 |
|
| 122 |
DEBUGMSGTL(("cpu_sysinfo", "cpu %u kernel: %lu\n", i, sinfo_cpus[i].cpu[CPU_KERNEL])); |
| 123 |
cpu->sys2_ticks = (unsigned long long)sinfo_cpus[i].cpu[CPU_KERNEL] + (unsigned long long) sinfo_cpus[i].cpu[CPU_SXBRK] + (unsigned long long)sinfo_cpus[i].cpu[CPU_INTR]; |
| 124 |
cpu->intrpt_ticks = (unsigned long long)sinfo_cpus[i].cpu[CPU_INTR]; |
| 125 |
cpu->user_ticks = (unsigned long long)sinfo_cpus[i].cpu[CPU_USER]; |
| 126 |
cpu->wait_ticks = (unsigned long long)sinfo_cpus[i].cpu[CPU_WAIT]; |
| 127 |
cpu->idle_ticks = (unsigned long long)sinfo_cpus[i].cpu[CPU_IDLE]; |
| 128 |
} |
| 129 |
|
| 130 |
return 0; |
| 131 |
} |
| 132 |
|