Debian Codegraph @

agent/mibgroup/hardware/cpu/cpu_kinfo.c

raw
1 /*
2 * DragonFly kinfo interface
3 */
4 #include <net-snmp/net-snmp-config.h>
5 #include <net-snmp/net-snmp-features.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 <sys/param.h>
11 #include <sys/vmmeter.h>
12 #include <kinfo.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <sys/sysctl.h>
16
17 netsnmp_feature_require(hardware_cpu_copy_stats);
18 void _cpu_copy_stats( netsnmp_cpu_info *cpu );
19
20 /*
21 * Initialise the list of CPUs on the system
22 * (including descriptions)
23 */
24 void init_cpu_kinfo( void ) {
25 netsnmp_cpu_info *cpu;
26 int i;
27 size_t len;
28 char descr[ SNMP_MAXBUF ];
29
30 cpu = netsnmp_cpu_get_byIdx( -1, 1 );
31 strcpy(cpu->name, "Overall CPU statistics");
32
33 kinfo_get_cpus(&cpu_num);
34 len = sizeof(descr);
35 sysctlbyname("hw.model", descr, &len, NULL, 0);
36 for ( i = 0; i < cpu_num; i++ ) {
37 cpu = netsnmp_cpu_get_byIdx( i, 1 );
38 cpu->status = 2; /* running */
39 sprintf(cpu->name, "cpu%d", i);
40 sprintf(cpu->descr, "%s", descr);
41 }
42 }
43
44
45 /*
46 * Load the latest CPU usage statistics
47 */
48 int netsnmp_cpu_arch_load( netsnmp_cache *cache, void *magic ) {
49 struct vmmeter vmm;
50 size_t len;
51 struct kinfo_cputime cp_time;
52 netsnmp_cpu_info *cpu = netsnmp_cpu_get_byIdx( -1, 0 );
53
54 kinfo_get_sched_cputime(&cp_time);
55 len = sizeof(vmm);
56 sysctlbyname("vm.vmmeter", &vmm, &len, NULL, 0);
57
58 cpu->user_ticks = cp_time.cp_user;
59 cpu->nice_ticks = cp_time.cp_nice;
60 cpu->sys2_ticks = cp_time.cp_sys + cp_time.cp_intr;
61 cpu->idle_ticks = cp_time.cp_idle;
62 cpu->kern_ticks = cp_time.cp_sys;
63 cpu->intrpt_ticks = cp_time.cp_intr;
64
65 cpu->swapIn = vmm.v_swappgsin + vmm.v_vnodepgsin;
66 cpu->swapOut = vmm.v_swappgsout + vmm.v_vnodepgsout;
67 cpu->nInterrupts = vmm.v_intr;
68 cpu->nCtxSwitches = vmm.v_swtch;
69
70 /* Copy "overall" figures to cpu0 entry */
71 _cpu_copy_stats( cpu );
72
73 return 0;
74 }
75