| 1 |
#include <net-snmp/net-snmp-config.h> |
| 2 |
#include <net-snmp/net-snmp-includes.h> |
| 3 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 4 |
#include <net-snmp/agent/hardware/cpu.h> |
| 5 |
#include "cpu_linux.h" |
| 6 |
|
| 7 |
#include <unistd.h> |
| 8 |
#include <fcntl.h> |
| 9 |
|
| 10 |
#define CPU_FILE "/proc/cpuinfo" |
| 11 |
#define STAT_FILE "/proc/stat" |
| 12 |
#define VMSTAT_FILE "/proc/vmstat" |
| 13 |
|
| 14 |
|
| 15 |
/* Which field(s) describe the type of CPU */ |
| 16 |
#if defined(__i386__) || defined(__x86_64__) |
| 17 |
#define DESCR_FIELD "vendor_id" |
| 18 |
#define DESCR2_FIELD "model name" |
| 19 |
#endif |
| 20 |
#if defined(__powerpc__) || defined(__powerpc64__) |
| 21 |
#define DESCR_FIELD "cpu\t" |
| 22 |
#endif |
| 23 |
#if defined(__ia64__) |
| 24 |
/* since vendor is always Intel ... we don't parse vendor */ |
| 25 |
#define DESCR_FIELD "family" |
| 26 |
#endif |
| 27 |
|
| 28 |
|
| 29 |
/* |
| 30 |
* Initialise the list of CPUs on the system |
| 31 |
* (including descriptions) |
| 32 |
* |
| 33 |
* XXX - Assumes x86-style /proc/cpuinfo format |
| 34 |
* See CPUinfo database at |
| 35 |
* http://www.rush3d.com/gcc/ |
| 36 |
* for info on alternative styles |
| 37 |
*/ |
| 38 |
void init_cpu_linux( void ) { |
| 39 |
FILE *fp; |
| 40 |
char buf[1024]; |
| 41 |
int i, n = 0; |
| 42 |
netsnmp_cpu_info *cpu = netsnmp_cpu_get_byIdx( -1, 1 ); |
| 43 |
strcpy(cpu->name, "Overall CPU statistics"); |
| 44 |
|
| 45 |
fp = fopen( CPU_FILE, "r" ); |
| 46 |
if (!fp) { |
| 47 |
snmp_log(LOG_ERR, "Can't open procinfo file %s\n", CPU_FILE); |
| 48 |
return; |
| 49 |
} |
| 50 |
while ( fgets( buf, sizeof(buf), fp)) { |
| 51 |
if ( sscanf( buf, "processor : %d", &i ) == 1) { |
| 52 |
n++; |
| 53 |
cpu = netsnmp_cpu_get_byIdx( i, 1 ); |
| 54 |
cpu->status = 2; /* running */ |
| 55 |
sprintf( cpu->name, "cpu%d", i ); |
| 56 |
#if defined(__s390__) || defined(__s390x__) |
| 57 |
strlcat(cpu->descr, "An S/390 CPU", sizeof(cpu->descr)); |
| 58 |
#endif |
| 59 |
} |
| 60 |
#if defined(__s390__) || defined(__s390x__) |
| 61 |
/* s390 may have different format of CPU_FILE */ |
| 62 |
else { |
| 63 |
if (sscanf( buf, "processor %d:", &i ) == 1) { |
| 64 |
n++; |
| 65 |
cpu = netsnmp_cpu_get_byIdx( i, 1 ); |
| 66 |
cpu->status = 2; /* running */ |
| 67 |
sprintf(cpu->name, "cpu%d", i); |
| 68 |
strlcat(cpu->descr, "An S/390 CPU", sizeof(cpu->descr)); |
| 69 |
} |
| 70 |
} |
| 71 |
#endif |
| 72 |
|
| 73 |
#ifdef DESCR_FIELD |
| 74 |
if (!strncmp( buf, DESCR_FIELD, strlen(DESCR_FIELD))) { |
| 75 |
char *cp = strchr( buf, ':' ); |
| 76 |
if (cp) { |
| 77 |
strlcpy(cpu->descr, cp + 2, sizeof(cpu->descr)); |
| 78 |
cp = strchr(cpu->descr, '\n'); |
| 79 |
if (cp) |
| 80 |
*cp = 0; |
| 81 |
} |
| 82 |
} |
| 83 |
#endif |
| 84 |
#ifdef DESCR2_FIELD |
| 85 |
if (!strncmp( buf, DESCR2_FIELD, strlen(DESCR2_FIELD))) { |
| 86 |
char *cp = strchr( buf, ':' ); |
| 87 |
if (cp) { |
| 88 |
strlcat(cpu->descr, cp, sizeof(cpu->descr)); |
| 89 |
cp = strchr(cpu->descr, '\n'); |
| 90 |
if (cp) |
| 91 |
*cp = 0; |
| 92 |
} |
| 93 |
} |
| 94 |
#endif |
| 95 |
} |
| 96 |
fclose(fp); |
| 97 |
cpu_num = n; |
| 98 |
} |
| 99 |
|
| 100 |
void _cpu_load_swap_etc( char *buff, netsnmp_cpu_info *cpu ); |
| 101 |
|
| 102 |
/* |
| 103 |
* Load the latest CPU usage statistics |
| 104 |
*/ |
| 105 |
int netsnmp_cpu_arch_load( netsnmp_cache *cache, void *magic ) { |
| 106 |
static char *buff = NULL; |
| 107 |
static int bsize = 0; |
| 108 |
static int first = 1; |
| 109 |
static int num_cpuline_elem = 0; |
| 110 |
int bytes_read, statfd, i; |
| 111 |
char *b1, *b2; |
| 112 |
unsigned long long cusell = 0, cicell = 0, csysll = 0, cidell = 0, |
| 113 |
ciowll = 0, cirqll = 0, csoftll = 0, cstealll = 0, |
| 114 |
cguestll = 0, cguest_nicell = 0; |
| 115 |
netsnmp_cpu_info* cpu; |
| 116 |
|
| 117 |
if ((statfd = open(STAT_FILE, O_RDONLY, 0)) == -1) { |
| 118 |
snmp_log_perror(STAT_FILE); |
| 119 |
return -1; |
| 120 |
} |
| 121 |
if (bsize == 0) { |
| 122 |
bsize = getpagesize()-1; |
| 123 |
buff = (char*)malloc(bsize+1); |
| 124 |
if (buff == NULL) { |
| 125 |
close(statfd); |
| 126 |
return -1; |
| 127 |
} |
| 128 |
} |
| 129 |
while ((bytes_read = read(statfd, buff, bsize)) == bsize) { |
| 130 |
bsize += BUFSIZ; |
| 131 |
buff = (char*)realloc(buff, bsize+1); |
| 132 |
DEBUGMSGTL(("cpu", "/proc/stat buffer increased to %d\n", bsize)); |
| 133 |
close(statfd); |
| 134 |
statfd = open(STAT_FILE, O_RDONLY, 0); |
| 135 |
if (statfd == -1) { |
| 136 |
snmp_log_perror(STAT_FILE); |
| 137 |
return -1; |
| 138 |
} |
| 139 |
} |
| 140 |
close(statfd); |
| 141 |
|
| 142 |
if ( bytes_read < 0 ) { |
| 143 |
snmp_log_perror(STAT_FILE "read error"); |
| 144 |
return -1; |
| 145 |
} |
| 146 |
buff[bytes_read] = '\0'; |
| 147 |
|
| 148 |
/* |
| 149 |
* CPU statistics (overall and per-CPU) |
| 150 |
*/ |
| 151 |
b1 = buff; |
| 152 |
while ((b2 = strstr( b1, "cpu" ))) { |
| 153 |
if (b2[3] == ' ') { |
| 154 |
cpu = netsnmp_cpu_get_byIdx( -1, 0 ); |
| 155 |
if (!cpu) { |
| 156 |
snmp_log_perror("No (overall) CPU info entry"); |
| 157 |
return -1; |
| 158 |
} |
| 159 |
b1 = b2+4; /* Skip "cpu " */ |
| 160 |
} else { |
| 161 |
sscanf( b2, "cpu%d", &i ); |
| 162 |
/* Create on the fly to support non-x86 systems - see init */ |
| 163 |
cpu = netsnmp_cpu_get_byIdx( i, 1 ); |
| 164 |
if (!cpu) { |
| 165 |
snmp_log_perror("Missing CPU info entry"); |
| 166 |
break; |
| 167 |
} |
| 168 |
b1 = b2; /* Skip "cpuN " */ |
| 169 |
while(*b1 != ' ') b1++; |
| 170 |
b1++; |
| 171 |
} |
| 172 |
|
| 173 |
num_cpuline_elem = sscanf(b1, "%llu %llu %llu %llu %llu %llu %llu %llu %llu %llu", |
| 174 |
&cusell, &cicell, &csysll, &cidell, &ciowll, &cirqll, &csoftll, &cstealll, &cguestll, &cguest_nicell); |
| 175 |
DEBUGMSGTL(("cpu", "/proc/stat cpu line number of elements: %i\n", num_cpuline_elem)); |
| 176 |
|
| 177 |
/* kernel 2.6.33 and above */ |
| 178 |
if (num_cpuline_elem == 10) { |
| 179 |
cpu->guestnice_ticks = cguest_nicell; |
| 180 |
} |
| 181 |
/* kernel 2.6.24 and above */ |
| 182 |
if (num_cpuline_elem >= 9) { |
| 183 |
cpu->guest_ticks = cguestll; |
| 184 |
} |
| 185 |
/* kernel 2.6.11 and above */ |
| 186 |
if (num_cpuline_elem >= 8) { |
| 187 |
cpu->steal_ticks = cstealll; |
| 188 |
} |
| 189 |
/* kernel 2.6 */ |
| 190 |
if (num_cpuline_elem >= 5) { |
| 191 |
cpu->wait_ticks = ciowll; |
| 192 |
cpu->intrpt_ticks = cirqll; |
| 193 |
cpu->sirq_ticks = csoftll; |
| 194 |
} |
| 195 |
/* rest */ |
| 196 |
cpu->user_ticks = cusell; |
| 197 |
cpu->nice_ticks = cicell; |
| 198 |
cpu->sys_ticks = csysll; |
| 199 |
cpu->idle_ticks = cidell; |
| 200 |
} |
| 201 |
if ( b1 == buff ) { |
| 202 |
if (first) |
| 203 |
snmp_log(LOG_ERR, "No cpu line in %s\n", STAT_FILE); |
| 204 |
} |
| 205 |
|
| 206 |
/* |
| 207 |
* Interrupt/Context Switch statistics |
| 208 |
* XXX - Do these really belong here ? |
| 209 |
*/ |
| 210 |
cpu = netsnmp_cpu_get_byIdx( -1, 0 ); |
| 211 |
_cpu_load_swap_etc( buff, cpu ); |
| 212 |
|
| 213 |
/* |
| 214 |
* XXX - TODO: extract per-CPU statistics |
| 215 |
* (Into separate netsnmp_cpu_info data structures) |
| 216 |
*/ |
| 217 |
|
| 218 |
first = 0; |
| 219 |
return 0; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
/* |
| 224 |
* Interrupt/Context Switch statistics |
| 225 |
* XXX - Do these really belong here ? |
| 226 |
*/ |
| 227 |
void _cpu_load_swap_etc( char *buff, netsnmp_cpu_info *cpu ) { |
| 228 |
static int has_vmstat = 1; |
| 229 |
static char *vmbuff = NULL; |
| 230 |
static int vmbsize = 0; |
| 231 |
static int first = 1; |
| 232 |
int bytes_read, vmstatfd; |
| 233 |
char *b; |
| 234 |
unsigned long long pin, pout, swpin, swpout; |
| 235 |
unsigned long long itot, iticks, ctx; |
| 236 |
|
| 237 |
if (has_vmstat) { |
| 238 |
vmstatfd = open(VMSTAT_FILE, O_RDONLY, 0); |
| 239 |
if (vmstatfd == -1 ) { |
| 240 |
snmp_log(LOG_ERR, "cannot open %s\n", VMSTAT_FILE); |
| 241 |
has_vmstat = 0; |
| 242 |
} else { |
| 243 |
if (vmbsize == 0) { |
| 244 |
vmbsize = getpagesize()-1; |
| 245 |
vmbuff = (char*)malloc(vmbsize+1); |
| 246 |
} |
| 247 |
while ((bytes_read = read(vmstatfd, vmbuff, vmbsize)) == vmbsize) { |
| 248 |
vmbsize += BUFSIZ; |
| 249 |
vmbuff = (char*)realloc(vmbuff, vmbsize+1); |
| 250 |
close(vmstatfd); |
| 251 |
vmstatfd = open(VMSTAT_FILE, O_RDONLY, 0); |
| 252 |
if (vmstatfd == -1) { |
| 253 |
snmp_log_perror("cannot open " VMSTAT_FILE); |
| 254 |
return; |
| 255 |
} |
| 256 |
} |
| 257 |
close(vmstatfd); |
| 258 |
if ( bytes_read < 0 ) { |
| 259 |
snmp_log_perror(VMSTAT_FILE "read error"); |
| 260 |
return; |
| 261 |
} |
| 262 |
vmbuff[bytes_read] = '\0'; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
if (has_vmstat) { |
| 267 |
b = strstr(vmbuff, "pgpgin "); |
| 268 |
if (b) { |
| 269 |
sscanf(b, "pgpgin %llu", &pin); |
| 270 |
cpu->pageIn = (unsigned long long)pin*2; /* ??? */ |
| 271 |
} else { |
| 272 |
if (first) |
| 273 |
snmp_log(LOG_ERR, "No pgpgin line in %s\n", VMSTAT_FILE); |
| 274 |
cpu->pageIn = 0; |
| 275 |
} |
| 276 |
b = strstr(vmbuff, "pgpgout "); |
| 277 |
if (b) { |
| 278 |
sscanf(b, "pgpgout %llu", &pout); |
| 279 |
cpu->pageOut = (unsigned long long)pout*2; /* ??? */ |
| 280 |
} else { |
| 281 |
if (first) |
| 282 |
snmp_log(LOG_ERR, "No pgpgout line in %s\n", VMSTAT_FILE); |
| 283 |
cpu->pageOut = 0; |
| 284 |
} |
| 285 |
b = strstr(vmbuff, "pswpin "); |
| 286 |
if (b) { |
| 287 |
sscanf(b, "pswpin %llu", &swpin); |
| 288 |
cpu->swapIn = (unsigned long long)swpin; |
| 289 |
} else { |
| 290 |
if (first) |
| 291 |
snmp_log(LOG_ERR, "No pswpin line in %s\n", VMSTAT_FILE); |
| 292 |
cpu->swapIn = 0; |
| 293 |
} |
| 294 |
b = strstr(vmbuff, "pswpout "); |
| 295 |
if (b) { |
| 296 |
sscanf(b, "pswpout %llu", &swpout); |
| 297 |
cpu->swapOut = (unsigned long long)swpout; |
| 298 |
} else { |
| 299 |
if (first) |
| 300 |
snmp_log(LOG_ERR, "No pswpout line in %s\n", VMSTAT_FILE); |
| 301 |
cpu->swapOut = 0; |
| 302 |
} |
| 303 |
} |
| 304 |
else { |
| 305 |
b = strstr(buff, "page "); |
| 306 |
if (b) { |
| 307 |
sscanf(b, "page %llu %llu", &pin, &pout); |
| 308 |
cpu->pageIn = (unsigned long long)pin; |
| 309 |
cpu->pageOut = (unsigned long long)pout; |
| 310 |
} else { |
| 311 |
if (first) |
| 312 |
snmp_log(LOG_ERR, "No page line in %s\n", STAT_FILE); |
| 313 |
cpu->pageIn = cpu->pageOut = 0; |
| 314 |
} |
| 315 |
b = strstr(buff, "swap "); |
| 316 |
if (b) { |
| 317 |
sscanf(b, "swap %llu %llu", &swpin, &swpout); |
| 318 |
cpu->swapIn = (unsigned long long)swpin; |
| 319 |
cpu->swapOut = (unsigned long long)swpout; |
| 320 |
} else { |
| 321 |
if (first) |
| 322 |
snmp_log(LOG_ERR, "No swap line in %s\n", STAT_FILE); |
| 323 |
cpu->swapIn = cpu->swapOut = 0; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
b = strstr(buff, "intr "); |
| 328 |
if (b) { |
| 329 |
sscanf(b, "intr %llu %llu", &itot, &iticks); |
| 330 |
cpu->nInterrupts = (unsigned long long)itot; |
| 331 |
/* iticks not used? */ |
| 332 |
} else { |
| 333 |
if (first) |
| 334 |
snmp_log(LOG_ERR, "No intr line in %s\n", STAT_FILE); |
| 335 |
} |
| 336 |
b = strstr(buff, "ctxt "); |
| 337 |
if (b) { |
| 338 |
sscanf(b, "ctxt %llu", &ctx); |
| 339 |
cpu->nCtxSwitches = (unsigned long long)ctx; |
| 340 |
} else { |
| 341 |
if (first) |
| 342 |
snmp_log(LOG_ERR, "No ctxt line in %s\n", STAT_FILE); |
| 343 |
} |
| 344 |
first = 0; |
| 345 |
} |