summaryrefslogtreecommitdiff
path: root/agent/mibgroup/hardware/cpu/cpu_sysinfo.c
diff options
context:
space:
mode:
authorHideki Yamane <henrich@debian.org>2014-03-30 19:38:48 +0900
committerHideki Yamane <henrich@debian.org>2014-03-30 19:38:48 +0900
commit7769a9595c3da9a35f31b42451b1f6c3ed4004fa (patch)
tree009bf8fd68af6bb1129e07dd8c1ed205010d81f8 /agent/mibgroup/hardware/cpu/cpu_sysinfo.c
parent2e7891b0311204e0ecd5dc4a4334df01f3a6a1b4 (diff)
downloadpkg-net-snmp-7769a9595c3da9a35f31b42451b1f6c3ed4004fa.tar.gz
Imported Upstream version 5.7.2~dfsg
Diffstat (limited to 'agent/mibgroup/hardware/cpu/cpu_sysinfo.c')
-rw-r--r--agent/mibgroup/hardware/cpu/cpu_sysinfo.c132
1 files changed, 132 insertions, 0 deletions
diff --git a/agent/mibgroup/hardware/cpu/cpu_sysinfo.c b/agent/mibgroup/hardware/cpu/cpu_sysinfo.c
new file mode 100644
index 0000000..ed1f357
--- /dev/null
+++ b/agent/mibgroup/hardware/cpu/cpu_sysinfo.c
@@ -0,0 +1,132 @@
+/*
+ * sysinfo (sysget/sysmp) interface
+ * e.g. IRIX 6.x
+ */
+#include <net-snmp/net-snmp-config.h>
+#include <net-snmp/net-snmp-includes.h>
+#include <net-snmp/agent/net-snmp-agent-includes.h>
+#include <net-snmp/agent/hardware/cpu.h>
+
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/sysmp.h>
+#include <sys/sysinfo.h>
+#include <sys/sysget.h>
+
+int cpu_count;
+int sinfo_ksize;
+struct sysinfo* sinfo_cpus; /* individual cpu stats */
+struct sysinfo* sinfo_gen; /* global stats */
+
+/*
+* Initialise the list of CPUs on the system
+* (including descriptions)
+*/
+void init_cpu_sysinfo( void )
+{
+ int i;
+ netsnmp_cpu_info *cpu;
+ char tstr[1024];
+
+ cpu_count = sysmp(MP_NPROCS);
+
+ /* fetch struct sysinfo size in kernel */
+
+ if ((sinfo_ksize = sysmp(MP_SASZ, MPSA_SINFO)) < 0) {
+ snmp_log_perror("init_cpu_sysinfo: sysinfo scall interface not supported");
+ exit(1);
+ }
+
+ if (sizeof(struct sysinfo) != sinfo_ksize)
+ {
+ snmp_log_perror("init_cpu_sysinfo: size mismatch between userland and kernel sysinfo struct");
+ exit(1);
+ }
+
+ /* allocate sysinfo for all cpus + global stats */
+
+ sinfo_cpus = (struct sysinfo *) calloc(cpu_count, sinfo_ksize);
+ if (!sinfo_cpus) {
+ snmp_log_perror("init_cpu_sysinfo: Couldn't allocate memory bytes for sinfo_cpus");
+ exit(1);
+ }
+
+ sinfo_gen = (struct sysinfo *) calloc(1, sinfo_ksize);
+ if (!sinfo_gen) {
+ snmp_log_perror("init_cpu_sysinfo: Couldn't allocate memory bytes for sinfo_gen");
+ exit(1);
+ }
+
+ /* register cpus */
+
+ cpu = netsnmp_cpu_get_byIdx(-1, 1);
+ strcpy(cpu->name, "Overall CPU statistics");
+
+ for (i = 0; i < cpu_count ; ++i)
+ {
+ cpu = netsnmp_cpu_get_byIdx(i, 1);
+ sprintf(tstr, "cpu%d",i);
+ strcpy(cpu->name, tstr);
+ strcpy(cpu->descr, "Central Processing Unit");
+ }
+}
+
+/*
+* Load the latest CPU usage statistics
+*/
+int netsnmp_cpu_arch_load( netsnmp_cache *cache, void *magic )
+{
+ int i;
+ netsnmp_cpu_info* cpu;
+ sgt_cookie_t ck;
+
+ /* fetch global stats */
+
+ cpu = netsnmp_cpu_get_byIdx(-1, 0);
+
+ if (sysmp(MP_SAGET, MPSA_SINFO, (char *) sinfo_gen, sinfo_ksize) < 0)
+ {
+ snmp_log_perror("netsnmp_cpu_arch_load: sysmp() failed");
+ exit(1);
+ }
+
+ DEBUGMSGTL(("cpu_sysinfo", "total cpu kernel: %lu\n", sinfo_gen->cpu[CPU_KERNEL]));
+ 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];
+ cpu->kern_ticks = (unsigned long long) sinfo_gen->cpu[CPU_KERNEL];
+ cpu->intrpt_ticks = (unsigned long long) sinfo_gen->cpu[CPU_INTR];
+ cpu->user_ticks = (unsigned long long) sinfo_gen->cpu[CPU_USER];
+ cpu->wait_ticks = (unsigned long long) sinfo_gen->cpu[CPU_WAIT];
+ cpu->idle_ticks = (unsigned long long) sinfo_gen->cpu[CPU_IDLE];
+
+ /* XXX - Do these really belong here ? */
+ cpu->pageIn = (unsigned long long)0;
+ cpu->pageOut = (unsigned long long)0;
+ cpu->swapIn = (unsigned long long)sinfo_gen->swapin;
+ cpu->swapOut = (unsigned long long)sinfo_gen->swapout;
+ cpu->nInterrupts = (unsigned long long)sinfo_gen->intr_svcd;
+ cpu->nCtxSwitches = (unsigned long long)sinfo_gen->pswitch;
+
+ /* fetch individual cpu stats */
+
+ SGT_COOKIE_INIT(&ck);
+ if (sysget(MPSA_SINFO, (char *) sinfo_cpus, sinfo_ksize * cpu_count, SGT_READ | SGT_CPUS | SGT_SUM, &ck) < 0)
+ {
+ snmp_log_perror("netsnmp_cpu_arch_load: sysget() failed");
+ exit(1);
+ }
+
+ for (i = 0; i < cpu_count ; ++i)
+ {
+ cpu = netsnmp_cpu_get_byIdx(i, 0);
+
+ DEBUGMSGTL(("cpu_sysinfo", "cpu %u kernel: %lu\n", i, sinfo_cpus[i].cpu[CPU_KERNEL]));
+ 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];
+ cpu->intrpt_ticks = (unsigned long long)sinfo_cpus[i].cpu[CPU_INTR];
+ cpu->user_ticks = (unsigned long long)sinfo_cpus[i].cpu[CPU_USER];
+ cpu->wait_ticks = (unsigned long long)sinfo_cpus[i].cpu[CPU_WAIT];
+ cpu->idle_ticks = (unsigned long long)sinfo_cpus[i].cpu[CPU_IDLE];
+ }
+
+ return 0;
+}
+