Debian Codegraph @

agent/mibgroup/hardware/cpu/cpu_pcp.c

raw
1 /*
2 * pcp interface
3 * e.g. IRIX
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 <pcp/pmapi.h>
12
13 /*
14 * Performance Metrics Name Space Map
15 * Built by pmgenmap from the file irix_kernstats.pcp
16 */
17 char *kernstats[] = {
18 #define NCPU 0
19 "hinv.ncpu",
20 #define CPUTYPE 1
21 "hinv.cputype",
22 #define CPUIDLE 2
23 "kernel.all.cpu.idle",
24 #define CPUINTR 3
25 "kernel.all.cpu.intr",
26 #define CPUSYS 4
27 "kernel.all.cpu.sys",
28 #define CPUUSER 5
29 "kernel.all.cpu.user",
30 #define CPUWAIT 6
31 "kernel.all.cpu.wait.total",
32 #define PAGESIN 7
33 "swap.pagesin",
34 #define PAGESOUT 8
35 "swap.pagesout",
36 #define SWAPIN 9
37 "swap.in",
38 #define SWAPOUT 10
39 "swap.out",
40 #define INTR 11
41 "kernel.all.intr.non_vme",
42 #define CTXT 12
43 "kernel.all.kswitch"
44 };
45
46 # define MAX_MID 17
47
48 pmResult *resp;
49 pmID pmidlist[MAX_MID];
50 int numpmid;
51 int pmInitDone = 0;
52
53 /* initialize pcp if necessary */
54 void init_pcp () {
55 int err;
56
57 if (pmInitDone == 1) {
58 return;
59 }
60
61 snmp_log_perror("Initializing pcp");
62 numpmid = sizeof(kernstats)/sizeof(kernstats[0]);
63
64 /* Load default namespace */
65 if ((err=pmLoadNameSpace(PM_NS_DEFAULT)) < 0) {
66 snmp_log_perror("pmLoadNameSpace returned an error.");
67 snmp_log_perror(pmErrStr(err));
68 exit (1);
69 }
70
71 /* get mappings between internal IDs and external IDs */
72 if ((err=pmLookupName(numpmid, kernstats, pmidlist)) < 0) {
73 snmp_log_perror("pmLookupName returned an error.");
74 snmp_log_perror(pmErrStr(err));
75 exit (1);
76 }
77
78 /* specify a context to use */
79 /* a type of PM_CONTEXT_HOST lets you specify a hostname */
80 /* a type of PM_CONTEXT_LOCAL should ignore the string param */
81 if ((err=pmNewContext(PM_CONTEXT_LOCAL,"localhost")) < 0) {
82 snmp_log_perror("pmNewContext returned error opening a LOCAL Context");
83 snmp_log_perror(pmErrStr(err));
84
85 if ((err=pmNewContext(PM_CONTEXT_HOST,"localhost")) < 0) {
86 snmp_log_perror("pmNewContext returned error opening a HOST Context");
87 snmp_log_perror(pmErrStr(err));
88 exit(1);
89 }
90 }
91 snmp_log_perror ("done initializing pcp");
92 pmInitDone = 1;
93 }
94
95 /*
96 * Initialise the list of CPUs on the system
97 * (including descriptions)
98 */
99 void init_cpu_pcp( void ) {
100 int i, n = 0;
101 char tstr[1024];
102 int err;
103 netsnmp_cpu_info *cpu;
104
105 init_pcp();
106
107 /* At this stage, pmidlist contains the PMID for my metrics of interest */
108
109 cpu = netsnmp_cpu_get_byIdx( -1, 1 );
110 strcpy(cpu->name, "Overall CPU statistics");
111
112 if ((err=pmFetch(numpmid, pmidlist, &resp)) < 0) {
113 snmp_log_perror ("init_cpu_pcp: pmFetch returned error");
114 snmp_log_perror (pmErrStr(err));
115 exit (1);
116 }
117 cpu_num = resp->vset[NCPU]->vlist[0].value.lval;
118 pmFreeResult(resp);
119
120 for (i=0; i<cpu_num ; i++) {
121 cpu = netsnmp_cpu_get_byIdx( i, 1 );
122 sprintf(tstr, "cpu%d",i);
123 strlcpy(cpu->name, tstr, sizeof(cpu->name));
124 strcpy(cpu->descr, "An electronic chip that makes the computer work");
125 }
126 }
127
128 /*void _cpu_load_swap_etc( char *buff, netsnmp_cpu_info *cpu );*/
129
130 /*
131 * Load the latest CPU usage statistics
132 */
133 int netsnmp_cpu_arch_load( netsnmp_cache *cache, void *magic ) {
134 int err;
135 /*static char *buff = NULL;*/
136 static int first = 1;
137 netsnmp_cpu_info* cpu;
138
139 init_pcp();
140
141 /*
142 * CPU statistics (overall and per-CPU)
143 */
144 if ((err=pmFetch(numpmid, pmidlist, &resp)) < 0) {
145 snmp_log_perror ("netsnmp_cpu_arch_load: pmFetch returned an error.");
146 snmp_log_perror (pmErrStr(err));
147 exit (1);
148 }
149
150 cpu = netsnmp_cpu_get_byIdx( -1, 0 );
151 if (!cpu) {
152 snmp_log_perror ("netsnmp_cpu_arch_load: netsnmp_cpu_get_byIdx failed!");
153 exit(1);
154 }
155
156 cpu->wait_ticks = (unsigned long long)resp->vset[CPUWAIT]->vlist[0].value.lval / 10;
157 cpu->intrpt_ticks = (unsigned long long)resp->vset[CPUINTR]->vlist[0].value.lval / 10;
158 /*cpu->sirq_ticks = (unsigned long)csoftll / 10;*/
159 cpu->user_ticks = (unsigned long long)resp->vset[CPUUSER]->vlist[0].value.lval / 10;
160 /*cpu->nice_ticks = (unsigned long)cicell / 10;*/
161 cpu->sys_ticks = (unsigned long long)resp->vset[CPUSYS]->vlist[0].value.lval / 10;
162 cpu->idle_ticks = (unsigned long long)resp->vset[CPUIDLE]->vlist[0].value.lval / 10;
163
164
165 /*
166 * Interrupt/Context Switch statistics
167 * XXX - Do these really belong here ?
168 */
169 /*cpu = netsnmp_cpu_get_byIdx( -1, 0 );*/
170 /*_cpu_load_swap_etc( buff, cpu );*/
171 cpu->pageIn = (unsigned long long)resp->vset[PAGESIN]->vlist[0].value.lval;
172 cpu->pageOut = (unsigned long long)resp->vset[PAGESOUT]->vlist[0].value.lval;
173 cpu->swapIn = (unsigned long long)resp->vset[SWAPIN]->vlist[0].value.lval;
174 cpu->swapOut = (unsigned long long)resp->vset[SWAPOUT]->vlist[0].value.lval;
175 cpu->nInterrupts = (unsigned long long)resp->vset[INTR]->vlist[0].value.lval;
176 cpu->nCtxSwitches = (unsigned long long)resp->vset[CTXT]->vlist[0].value.lval;
177
178 /*
179 * XXX - TODO: extract per-CPU statistics
180 * (Into separate netsnmp_cpu_info data structures)
181 */
182
183 /* free pcp response */
184 pmFreeResult(resp);
185
186 first = 0;
187 return 0;
188 }
189
190
191