Debian Codegraph @

agent/mibgroup/hardware/cpu/cpu_nlist.c

raw
1 /*
2 * nlist() interface
3 * e.g. FreeBSD
4 */
5 #include <net-snmp/net-snmp-config.h>
6 #include <net-snmp/net-snmp-features.h>
7 #include <net-snmp/net-snmp-includes.h>
8 #include <net-snmp/agent/net-snmp-agent-includes.h>
9 #include <net-snmp/agent/hardware/cpu.h>
10
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <sys/types.h>
14 #include <sys/resource.h>
15
16 #ifdef HAVE_SYS_DKSTAT_H
17 #include <sys/dkstat.h>
18 #endif
19 #ifdef HAVE_SYS_SYSCTL_H
20 #include <sys/sysctl.h>
21 #endif
22 #ifdef HAVE_SYS_VMMETER_H
23 #include <sys/vmmeter.h>
24 #endif
25 #ifdef HAVE_VM_VM_PARAM_H
26 #include <vm/vm_param.h>
27 #endif
28 #ifdef HAVE_VM_VM_EXTERN_H
29 #include <vm/vm_extern.h>
30 #endif
31
32 #define CPU_SYMBOL "cp_time"
33 #define MEM_SYMBOL "cnt"
34
35 netsnmp_feature_require(hardware_cpu_copy_stats);
36 void _cpu_copy_stats( netsnmp_cpu_info *cpu );
37
38 /*
39 * Initialise the list of CPUs on the system
40 * (including descriptions)
41 */
42 void init_cpu_nlist( void ) {
43 int n;
44 size_t i;
45 int ncpu_mib[] = { CTL_HW, HW_NCPU };
46 int model_mib[] = { CTL_HW, HW_MODEL };
47 char descr[ SNMP_MAXBUF ];
48 netsnmp_cpu_info *cpu = netsnmp_cpu_get_byIdx( -1, 1 );
49 strcpy(cpu->name, "Overall CPU statistics");
50
51 i = sizeof(n);
52 sysctl(ncpu_mib, 2, &n, &i, NULL, 0);
53 i = sizeof(descr);
54 sysctl(model_mib, 2, descr, &i, NULL, 0);
55
56 if ( n <= 0 )
57 n = 1; /* Single CPU system */
58 for ( i=0; i<n; i++ ) {
59 cpu = netsnmp_cpu_get_byIdx( i, 1 );
60 cpu->status = 2; /* running */
61 sprintf(cpu->name, "cpu%d", i);
62 sprintf(cpu->descr, "%s", descr);
63 }
64 cpu_num = n;
65 }
66
67
68 /*
69 * Load the latest CPU usage statistics
70 */
71 int netsnmp_cpu_arch_load( netsnmp_cache *cache, void *magic ) {
72 long cpu_stats[CPUSTATES];
73 struct vmmeter mem_stats;
74 netsnmp_cpu_info *cpu = netsnmp_cpu_get_byIdx( -1, 0 );
75
76 auto_nlist( CPU_SYMBOL, (char *) cpu_stats, sizeof(cpu_stats));
77 auto_nlist( MEM_SYMBOL, (char *)&mem_stats, sizeof(mem_stats));
78
79 cpu->user_ticks = (unsigned long long)cpu_stats[CP_USER];
80 cpu->nice_ticks = (unsigned long long)cpu_stats[CP_NICE];
81 cpu->sys2_ticks = (unsigned long long)cpu_stats[CP_SYS]+cpu_stats[CP_INTR];
82 cpu->idle_ticks = (unsigned long long)cpu_stats[CP_IDLE];
83 cpu->kern_ticks = (unsigned long long)cpu_stats[CP_SYS];
84 cpu->intrpt_ticks = (unsigned long long)cpu_stats[CP_INTR];
85 /* wait_ticks, sirq_ticks unused */
86
87 /*
88 * Interrupt/Context Switch statistics
89 * XXX - Do these really belong here ?
90 */
91 #if defined(openbsd2) || defined(darwin)
92 cpu->swapIn = (unsigned long long)mem_stats.v_swpin;
93 cpu->swapOut = (unsigned long long)mem_stats.v_swpout;
94 #else
95 cpu->swapIn = (unsigned long long)mem_stats.v_swappgsin+mem_stats.v_vnodepgsin;
96 cpu->swapOut = (unsigned long long)mem_stats.v_swappgsout+mem_stats.v_vnodepgsout;
97 #endif
98 cpu->nInterrupts = (unsigned long long)mem_stats.v_intr;
99 cpu->nCtxSwitches = (unsigned long long)mem_stats.v_swtch;
100
101 #ifdef PER_CPU_INFO
102 for ( i = 0; i < n; i++ ) {
103 cpu = netsnmp_cpu_get_byIdx( i, 0 );
104 /* XXX - per-CPU statistics */
105 }
106 #else
107 /* Copy "overall" figures to cpu0 entry */
108 _cpu_copy_stats( cpu );
109 #endif
110
111 return 0;
112 }