~ubuntu-branches/ubuntu/trusty/net-snmp/trusty

« back to all changes in this revision

Viewing changes to agent/mibgroup/host/hr_proc.c

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2007-05-10 22:20:23 UTC
  • mto: (1.4.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 11.
  • Revision ID: james.westby@ubuntu.com-20070510222023-3fr07xb9i17xvq32
Tags: upstream-5.3.1
ImportĀ upstreamĀ versionĀ 5.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
 */
5
5
 
6
6
#include <net-snmp/net-snmp-config.h>
 
7
#include <net-snmp/net-snmp-includes.h>
 
8
#include <net-snmp/agent/net-snmp-agent-includes.h>
7
9
#if HAVE_STDLIB_H
8
10
#include <stdlib.h>
9
11
#endif
18
20
#include "hr_proc.h"
19
21
#include <net-snmp/agent/auto_nlist.h>
20
22
#include <net-snmp/agent/agent_read_config.h>
 
23
#include <net-snmp/agent/hardware/cpu.h>
21
24
#include "ucd-snmp/loadave.h"
22
25
 
23
26
#define HRPROC_MONOTONICALLY_INCREASING
57
60
extern void kstat_CPU(void);
58
61
int proc_status(int);
59
62
#else
60
 
#define MAX_NUM_HRPROC  32
 
63
#ifdef linux
 
64
static char **proc_descriptions;
 
65
#else
 
66
# define MAX_NUM_HRPROC  32
61
67
char proc_descriptions[MAX_NUM_HRPROC][BUFSIZ];
 
68
#endif
62
69
#endif  /*solaris 2*/
63
70
 
64
71
        /*********************
279
286
 
280
287
    }
281
288
#elif linux
282
 
    return (proc_descriptions[idx & HRDEV_TYPE_MASK]);
 
289
    netsnmp_cpu_info *cpu;
 
290
    cpu = netsnmp_cpu_get_byIdx( idx & HRDEV_TYPE_MASK, 0 );
 
291
    return (cpu ? cpu->descr : NULL );
283
292
#elif solaris2
284
293
    int cidx = idx & HRDEV_TYPE_MASK;
285
294
    snprintf(proc_description,sizeof(proc_description)-1, 
297
306
    int i;
298
307
    char tmpbuf[BUFSIZ], *cp;
299
308
    FILE *fp;
 
309
    int nrprocs;
300
310
 
301
 
    /*
302
 
     * Clear the buffers...
303
 
     */
304
 
    for ( i=0 ; i<MAX_NUM_HRPROC; i++ ) {
305
 
        memset( proc_descriptions[i], '\0', BUFSIZ);
306
 
    }
 
311
    DEBUGMSG(("hr_proc::detect_hrproc",""));
307
312
 
308
313
    /*
309
314
     * ... and try to interpret the CPU information
310
315
     */
311
316
    fp = fopen("/proc/cpuinfo", "r");
312
317
    if (!fp) {
313
 
        sprintf( proc_descriptions[0],
314
 
                 "An electronic chip that makes the computer work.");
 
318
        DEBUGMSG(("hr_proc::detect_hrproc","could not open /proc/cpuinfo"));
 
319
        nrprocs = 1;
 
320
        proc_descriptions = (char**)malloc(sizeof(char*));
 
321
        proc_descriptions[0] =
 
322
            strdup("An electronic chip that makes the computer work.");
315
323
        return;
316
324
    }
 
325
    nrprocs = 1;
 
326
    proc_descriptions = (char**)malloc(sizeof(char*)*nrprocs);
 
327
    if (!proc_descriptions) {
 
328
        fclose(fp);
 
329
        return;
 
330
    }
 
331
    proc_descriptions[0] =
 
332
        strdup("An electronic chip that makes the computer work.");
 
333
    i = -1;
 
334
    while (fgets(tmpbuf, sizeof(tmpbuf), fp)) {
 
335
        /* note that some older (eg 2.4) kernels don't have a processor line */
 
336
        if (!strncmp(tmpbuf,"processor\t",strlen("processor\t")))
 
337
                i++;
 
338
        if (!strncmp(tmpbuf,"processor ",strlen("processor ")))
 
339
                i++;
 
340
        if ((i!=-1) && (i >= nrprocs)) {
 
341
            nrprocs++;
 
342
            proc_descriptions = (char**)realloc(proc_descriptions, sizeof(char*)*nrprocs);
 
343
            if (!proc_descriptions) {
 
344
                fclose(fp);
 
345
                return;
 
346
            }
 
347
            proc_descriptions[nrprocs-1] = strdup("An electronic chip that makes the computer work."); /* will be overwritten */
 
348
        }
317
349
 
318
 
    i = 0;
319
 
    while (fgets(tmpbuf, BUFSIZ, fp)) {
 
350
#if defined(__i386__) || defined(__x86_64__)
320
351
        if ( !strncmp( tmpbuf, "vendor_id", 9)) {
321
352
            /* Stomp on trailing newline... */
322
353
            cp = &tmpbuf[strlen(tmpbuf)-1];
326
357
            cp++;
327
358
            while ( cp && isspace(*cp))
328
359
                cp++;
329
 
            snprintf( proc_descriptions[i], BUFSIZ, "%s", cp);
 
360
            if (proc_descriptions[nrprocs-1])
 
361
                free(proc_descriptions[nrprocs-1]);
 
362
            proc_descriptions[nrprocs-1] = strdup(cp);
330
363
        }
331
364
        if ( !strncmp( tmpbuf, "model name", 10)) {
 
365
           char *s;
332
366
            /* Stomp on trailing newline... */
333
367
            cp = &tmpbuf[strlen(tmpbuf)-1];
334
368
            *cp = 0;
337
371
            cp++;
338
372
            while ( cp && isspace(*cp))
339
373
                cp++;
340
 
            strncat( proc_descriptions[i], ": ",
341
 
                     BUFSIZ-strlen(proc_descriptions[i]));
342
 
            strncat( proc_descriptions[i], cp,
343
 
                     BUFSIZ-strlen(proc_descriptions[i]));
344
 
            i++;
345
 
            if (i >= MAX_NUM_HRPROC) {
346
 
                i--;
347
 
                break;
 
374
            if (!proc_descriptions[nrprocs-1]) {
 
375
                s = malloc(strlen(": ")+strlen(cp)+1);
 
376
                strcpy(s,": ");
 
377
                strcat(s,cp);
 
378
                proc_descriptions[nrprocs-1] = s;
 
379
            } else {
 
380
                s = malloc(strlen(proc_descriptions[nrprocs-1])+strlen(": ")+strlen(cp)+1);
 
381
                strcpy(s,proc_descriptions[nrprocs-1]);
 
382
                strcat(s,": ");
 
383
                strcat(s,cp);
 
384
                free(proc_descriptions[nrprocs-1]);
 
385
                proc_descriptions[nrprocs-1] = s;
348
386
            }
349
387
        }
 
388
#endif
 
389
#if defined(__powerpc__) || defined(__powerpc64__)
 
390
        if ( !strncmp( tmpbuf, "cpu\t", 4)) {
 
391
            char *s;
 
392
 
 
393
            /* Stomp on trailing newline... */
 
394
            cp = &tmpbuf[strlen(tmpbuf)-1];
 
395
            *cp = 0;
 
396
            /* ... and then extract the value */
 
397
            cp = index( tmpbuf, ':');
 
398
            cp++;
 
399
            while ( cp && isspace(*cp))
 
400
                cp++;
 
401
            if (proc_descriptions[nrprocs-1])
 
402
                free(proc_descriptions[nrprocs-1]);
 
403
            proc_descriptions[nrprocs-1] = strdup(cp);
 
404
        }
 
405
#endif
 
406
#if defined(__ia64__)
 
407
        /* since vendor is always Intel ... we don't parse vendor */
 
408
        if ( !strncmp( tmpbuf, "family\t", 6)) {
 
409
            char *s;
 
410
 
 
411
            /* Stomp on trailing newline... */
 
412
            cp = &tmpbuf[strlen(tmpbuf)-1];
 
413
            *cp = 0;
 
414
            /* ... and then extract the value */
 
415
            cp = index( tmpbuf, ':');
 
416
            cp++;
 
417
            while ( cp && isspace(*cp))
 
418
                cp++;
 
419
            if (proc_descriptions[nrprocs-1])
 
420
                free(proc_descriptions[nrprocs-1]);
 
421
            proc_descriptions[nrprocs-1] = strdup(cp);
 
422
        }
 
423
#endif
 
424
#if defined(__s390__) || defined(__s390x__)
 
425
        if (proc_descriptions[nrprocs-1])
 
426
            free(proc_descriptions[nrprocs-1]);
 
427
        proc_descriptions[nrprocs-1] = strdup("An S/390 CPU");
 
428
#endif
350
429
    }
351
 
    HRP_max_index = i;
 
430
    DEBUGMSG(("hr_proc::detect_hrproc","registered %d processors", nrprocs));
 
431
    HRP_max_index = nrprocs;
352
432
    fclose(fp);
353
433
    return;
354
434
}