Beispiel #1
0
int snmptop(int argc, char **argv)
{
    netsnmp_session session, *ss;
    int             arg;
    struct hrSWRunTable *oproc;
    int             ocount = 0;
    int             show_idle = 1;
    int             show_os = 1;
    char            ch;
    struct cpuStats oldCpu;
    struct memStats mem;
    int             has_cpu, has_mem; 

    /*
     * get the common command line arguments 
     */
    switch (arg = snmp_parse_args(argc, argv, &session, "C:", optProc)) {
    case NETSNMP_PARSE_ARGS_ERROR:
        exit(1);
    case NETSNMP_PARSE_ARGS_SUCCESS_EXIT:
        exit(0);
    case NETSNMP_PARSE_ARGS_ERROR_USAGE:
        usage();
        exit(1);
    default:
        break;
    }

    if (arg != argc) {
        fprintf(stderr, "snmptop: extra argument: %s\n", argv[arg]);
        exit(1);
    }

    SOCK_STARTUP;

    /*
     * Open an SNMP session.
     */
    ss = snmp_open(&session);
    if (ss == NULL) {
        /*
         * diagnose snmp_open errors with the input netsnmp_session pointer 
         */
        snmp_sess_perror("snmptop", &session);
        SOCK_CLEANUP;
        exit(1);
    }

    ocount = collect_perf(ss, &oproc);
    if (ocount == 0) {
        fprintf(stderr, "snmptop: no processes found\n");
        exit(1);
    }

    collect_cpu(ss, &oldCpu);

    signal(SIGINT, endtop);
    initscr();
    cbreak();
    noecho();
    nonl();
    halfdelay(50);

    while ((ch = getch()) != 'q') {
        int ncount;
        struct hrSWRunTable *nproc;
        int oinx = 0, ninx = 0, line = 0;
        netsnmp_pdu    *pdu;
        netsnmp_pdu    *response = NULL;
        int status;
        time_t clock;
        struct tm *ptm;
        char uptime[40];
        char timestr[40];
        char b1[15], b2[15], b3[15], b4[15];
        struct cpuStats newCpu;

        if (ch == 'c' || ch == 'm' || ch == 'n' || ch == 't') topsort = ch;
        if (ch == 'i') show_idle = !show_idle;
        if (ch == 'o') show_os = !show_os;
        if (ch == 'a') command_args = !command_args;
        if (ch == 'p') command_path = !command_path;

        ncount = collect_perf(ss, &nproc);

        while (oinx < ocount && ninx < ncount) {
            if (oproc[oinx].hrSWRunIndex == nproc[ninx].hrSWRunIndex) {
                nproc[ninx].hrSWRunPerfCPUInc = nproc[ninx].hrSWRunPerfCPU-oproc[oinx].hrSWRunPerfCPU;
                ninx++;
                oinx++;
            }
            else if (nproc[oinx].hrSWRunIndex < oproc[ninx].hrSWRunIndex)
                oinx++;
            else {
                nproc[ninx].hrSWRunPerfCPUInc = nproc[ninx].hrSWRunPerfCPU;
                ninx++;
            }
        }
        while (ninx < ncount) {
            nproc[ninx].hrSWRunPerfCPUInc = nproc[ninx].hrSWRunPerfCPU;
            ninx++;
        }

        switch (topsort) {
        case 'c':
            qsort(nproc, ncount, sizeof(nproc[0]), cpucomp);
            break;
        case 'm':
            qsort(nproc, ncount, sizeof(nproc[0]), memcomp);
            break;
        case 't':
            qsort(nproc, ncount, sizeof(nproc[0]), totcomp);
            break;
        }

        has_cpu = collect_cpu(ss, &newCpu);
        has_mem = collect_mem(ss, &mem);

        pdu = snmp_pdu_create(SNMP_MSG_GET);
        add(pdu, "HOST-RESOURCES-MIB:hrSystemUptime.0", NULL, 0);
        status = snmp_synch_response(ss, pdu, &response);
        if (status != STAT_SUCCESS || !response ||
                response->errstat != SNMP_ERR_NOERROR) {
            uptime[0] = '\0';
        }
        else {
            netsnmp_variable_list *vlp = response->variables;
            if (vlp->type == SNMP_NOSUCHINSTANCE) abort();
            uptime_string_n(*vlp->val.integer, uptime, sizeof(uptime));
        }
        snmp_free_pdu(response);

        clock = time(NULL);
        ptm = localtime(&clock);
        strftime(timestr, sizeof(timestr), "%H:%M:%S", ptm);

        clear();
        move(0, 0);
        printw("%s %s%s", session.peername, uptime[0] ? "up " : "", uptime);
        move(0, COLS-strlen(timestr)-1);
        printw("%s", timestr);
        if (has_cpu) {
            struct cpuStats deltaCpu;
            u_long sumCpu;

            deltaCpu.user = newCpu.user - oldCpu.user;
            deltaCpu.nice = newCpu.nice - oldCpu.nice;
            deltaCpu.system = newCpu.system - oldCpu.system;
            deltaCpu.idle = newCpu.idle - oldCpu.idle;
            deltaCpu.wait = newCpu.wait - oldCpu.wait;
            deltaCpu.kernel = newCpu.kernel - oldCpu.kernel;
            deltaCpu.intr = newCpu.intr - oldCpu.intr;
            deltaCpu.softintr = newCpu.softintr - oldCpu.softintr;
            deltaCpu.steal = newCpu.steal - oldCpu.steal;
            deltaCpu.guest = newCpu.guest - oldCpu.guest;
            deltaCpu.guestnice = newCpu.guestnice - oldCpu.guestnice;
            oldCpu = newCpu;
            sumCpu = deltaCpu.user + deltaCpu.nice
                + deltaCpu.system + deltaCpu.idle
                + deltaCpu.wait + deltaCpu.kernel + deltaCpu.steal
                + deltaCpu.intr + deltaCpu.softintr
                + deltaCpu.guest + deltaCpu.guestnice;

            printw("\nCPU%%: %4.1fUs %4.1fSy %4.1fId %3.1fWa %3.1fNi %3.1fKe %3.1fHi %3.1fSi %3.1fSt %3.1fGu %3.1fGN",
                (float)deltaCpu.user*100/sumCpu,
                (float)deltaCpu.system*100/sumCpu,
                (float)deltaCpu.idle*100/sumCpu,
                (float)deltaCpu.wait*100/sumCpu,
                (float)deltaCpu.nice*100/sumCpu,
                (float)deltaCpu.kernel*100/sumCpu,
                (float)deltaCpu.intr*100/sumCpu,
                (float)deltaCpu.softintr*100/sumCpu,
                (float)deltaCpu.steal*100/sumCpu,
                (float)deltaCpu.guest*100/sumCpu,
                (float)deltaCpu.guestnice*100/sumCpu);
            line++;
        }

        if (has_mem) {
            printw("\nMem:  %10s Total %10s Used %10s Free %10s Buffer",
                format_humanmem(b1, sizeof b1, mem.totalReal),
                format_humanmem(b2, sizeof b2, mem.totalReal-mem.availReal),
                format_humanmem(b3, sizeof b3, mem.availReal),
                format_humanmem(b4, sizeof b4, mem.buffer));
            line++;
            printw("\nSwap: %10s Total %10s Used %10s Free %10s Cached",
                format_humanmem(b1, sizeof b1, mem.totalSwap),
                format_humanmem(b2, sizeof b2, mem.totalSwap-mem.availSwap),
                format_humanmem(b3, sizeof b3, mem.availSwap),
                format_humanmem(b4, sizeof b4, mem.cached));
            line++;
        }

        printw("\n%7s %4s %6s %10s %11s %5s %-10s",
            "Index", "Type", "Status", "Memory", "Total CPU", "%CPU", "Command");
        line++;
        ninx = 0;
        while (line < LINES && ninx < ncount) {
            struct hrSWRunTable *proc = nproc+ninx;
            const char *hr_status, *hr_type;

            ninx++;
            if (proc->hrSWRunPerfCPUInc == 0 && !show_idle)
                continue;
            if (proc->hrSWRunType != 4 && !show_os)
                continue;

            line++;

            switch (proc->hrSWRunType) {
            case 1: hr_type = "Unkn"; break;
            case 2: hr_type = "Os"; break;
            case 3: hr_type = "Drvr"; break;
            case 4: hr_type = "Appl"; break;
            default: hr_type = "?"; break;
            }

            switch (proc->hrSWRunStatus) {
            case 1: hr_status = "Run"; break;
            case 2: hr_status = "Wait"; break;
            case 3: hr_status = "Event"; break;
            case 4: hr_status = "Inval"; break;
            default: hr_status = "?"; break;
            }

            printw("\n%7lu %4s %6s %10s %11s %5.1f %s %s",
                   proc->hrSWRunIndex,
                   hr_type,
                   hr_status,
                   format_humanmem(b1, sizeof b1, proc->hrSWRunPerfMem),
                   format_sec(b2,sizeof b2, proc->hrSWRunPerfCPU),
                   (float)proc->hrSWRunPerfCPUInc/5,
                   command_path && proc->hrSWRunPath[0] ? proc->hrSWRunPath : proc->hrSWRunName,
                   command_args ? proc->hrSWRunParameters : "");
        }
        refresh();

        qsort(nproc, ncount, sizeof(nproc[0]), pidcomp);
        free_perf(oproc, ocount);
        oproc = nproc;
        ocount = ncount;
    }
    endwin();

    snmp_close(ss);
    SOCK_CLEANUP;
    return 0;
}
Beispiel #2
0
int parse_para(int argc, char **argv)
{
        // parse the monitor agent parameters
        int c, index;
        char *optstring = "i:l:m:n:s:p:dtvh";

        char *version, *help;
        char *disk, *cpu_seq, *interface, *loadlevel, *processid;
        char *mproc, *mused, *mpri, *mvir, *mpri_ipcs;
        char *mswap_used, *mswap_total, *pgpgin;
        char *pgpgout, *pswpin, *pswpout, *svctm, *await, *aveq, *avgrq_sz, *util;
        char *net_byte_in, *net_byte_out, *net_pack_in, *net_pack_out;
        char *passiveopens, *currestab, *indatagrams, *outdatagrams;
        char *hard_proc, *hard_mem, *hard_temp, *hard_disk,
                *hard_pwr, *hard_batt, *hard_nics, *hard_fans;


        char * para[40] ;

        int i = 0;
        for ( i = 0; i<40; i++ ) { para[i] = NULL; }


        struct option longopts[] = {
/* 0 */         {"help", no_argument, NULL, 'h'},
/* 1 */         {"version", no_argument, NULL, 'v'},
/* 2 */         {"daemon", no_argument, NULL, 'd'},

/* 3 */         {"url_post", required_argument, NULL, 'd'},
/* 4 */         {"rul_server", required_argument, NULL, 0},
/* 5 */         {"url_config", required_argument, NULL, 0},
/* 6 */         {"url_report", required_argument, NULL, 0},
/* 7 */         {"processid", required_argument, NULL, 0},


/* 8 */         {"mproc", no_argument, NULL, 0},
/* 9 */         {"mused", no_argument, NULL, 0},
/* 10 */        {"mpri", no_argument, NULL, 0},
/* 11 */        {"mvir", no_argument, NULL, 0},
/* 12 */        {"mpri_ipcs", no_argument, NULL, 0},

/* 13 */        {"mswap_used", no_argument, NULL, 0},
/* 14 */        {"mswap_total", no_argument, NULL, 0},

/* 15 */        {"pgpgin", no_argument, NULL, 0},
/* 16 */        {"pgpgout", no_argument, NULL, 0},
/* 17 */        {"pswpin", no_argument, NULL, 0},
/* 18 */        {"pswpout", no_argument, NULL, 0},

/* 19 */        {"svctm", no_argument, NULL, 0},
/* 20 */        {"await", no_argument, NULL, 0},
/* 21 */        {"aveq", no_argument, NULL, 0},
/* 22 */        {"avgrq_sz", no_argument, NULL, 0},
/* 23 */        {"util", no_argument, NULL, 0},

/* 24 */        {"net_byte_in", no_argument, NULL, 0},
/* 25 */        {"net_byte_out", no_argument, NULL, 0},
/* 26 */        {"net_pack_in", no_argument, NULL, 0},
/* 27 */        {"net_pack_out", no_argument, NULL, 0},

/* 28 */        {"passiveopens", no_argument, NULL, 0},
/* 29 */        {"currestab", no_argument, NULL, 0},
/* 30 */        {"indatagrams", no_argument, NULL, 0},
/* 31 */        {"outdatagrams", no_argument, NULL, 0},

/* 32 */        {"hard_proc", no_argument, NULL, 0},
/* 33 */        {"hard_mem", no_argument, NULL, 0},
/* 34 */        {"hard_temps", no_argument, NULL, 0},
/* 35 */        {"hard_disk", no_argument, NULL, 0},
/* 36 */        {"hard_pwr", no_argument, NULL, 0},
/* 37 */        {"hard_batt", no_argument, NULL, 0},
/* 38 */        {"hard_nics", no_argument, NULL, 0},
/* 39 */        {"hard_fans", no_argument, NULL, 0},


/* end */       {NULL, no_argument, NULL, 0},

        };


        while ((c = getopt_long(argc, argv, optstring, longopts, &index)) != -1) {
                switch (c) {


                case 'h':
                        para[0]="";
                        break;

                case 'v':
                        para[1]="";
                        break;

                case 'd':
                        para[2]="";
                        break;

                case 'i':
                        para[5]=optarg;
                        break;

                case 'l':
                        loadlevel = optarg;
                        break;

                case '?':
                        c = (char)optopt;
                        printf("parameters is not correct! \n");
                        break;

                case ':':
                        printf("1111111111111111111\n");
                        break;
                }


                switch(index){
                case 0:
                        para[0] = "";
                        break;

                case 1:
                        para[1] = "";
                        break;

                case 2:
                        para[2] = "";
                        break;

                case 3:
                        para[3] = optarg;
                        break;

                case 4:
                        para[4] = optarg;
                        break;

                case 5:
                        para[5] = optarg;
                        break;

                case 6:
                        para[6] = optarg;
                        break;

                case 7:
                        para[7] = optarg;
                        break;

                case 8:
                        para[8] = "";
                        break;

                case 9:
                        para[9] = "";
                        break;

                case 10:
                        para[10] = "";
                        break;

                case 11:
                        para[11] = "";
                        break;

                case 12:
                        para[12] = "";
                        break;

                case 13:
                        para[13] = "";
                        break;

                case 14:
                        para[14] = "";
                        break;

                case 15:
                        para[15] = "";
                        break;

                case 16:
                        para[16] = "";
                        break;

                case 17:
                        para[17] = "";
                        break;

                case 18:
                        para[18] = "";
                        break;

                case 19:
                        para[19] = "";
                        break;

                case 20:
                        para[20] = "";
                        break;

                case 21:
                        para[21] = "";
                        break;

                case 22:
                        para[22] = "";
                        break;

                case 23:
                        para[23] = "";
                        break;

                case 24:
                        para[24] = "";
                        break;

                case 25:
                        para[25] = "";
                        break;

                case 26:
                        para[26] = "";
                        break;

                case 27:
                        para[27] = "";
                        break;

                case 28:
                        para[28] = "";
                        break;

                case 29:
                        para[29] = "";
                        break;

                case 30:
                        para[30] = "";
                        break;

                case 31:
                        para[31] = "";
                        break;

                case 32:
                        para[32] = "";
                        break;

                case 33:
                        para[33] = "";
                        break;

                case 34:
                        para[34] = "";
                        break;

                case 35:
                        para[35] = "";
                        break;

                case 36:
                        para[36] = "";
                        break;

                case 37:
                        para[37] = "";
                        break;

                case 38:
                        para[38] = "";
                        break;

                case 39:
                        para[39] = "";
                        break;

                case 40:
                        para[40] = "";
                        break;

                }

        //      printf("the swich finished!\n");

        }


/* 0 */         if( para[0]      != NULL ) { print_help(); }
/* 1 */         if( para[1]      != NULL ) { print_version(); }
/* 2 */         if( para[2]      != NULL ) { run_daemon(); }
/* 3 */         if( para[3]      != NULL ) { disk       = para[3]; }
/* 4 */         if( para[4]      != NULL ) { cpu_seq    = para[4];      collect_cpu(cpu_seq);   }
/* 5 */         if( para[5]      != NULL ) { interface  = para[5]; }
/* 6 */         if( para[6]      != NULL ) { loadlevel  = para[6];      cpu_load(loadlevel);    }
/* 7 */         if( para[7]      != NULL ) { processid  = para[7]; }
/* 8 */         if( para[8]      != NULL ) { collect_mem_proc( "mem_proc" ); }
/* 9 */         if( para[9]      != NULL ) { collect_mem_proc( "mem_used" ); }
/* 10 */        if( para[10]     != NULL ) { collect_mem_pri(); }
/* 11 */        if( para[11]     != NULL ) { collect_mem_vir(); }
/* 12 */        if( para[12]     != NULL ) { collect_mem_pri_ipcs(); }
/* 13 */        if( para[13]     != NULL ) { collect_mem_proc( "mswap_used" ); }
/* 14 */        if( para[14]     != NULL ) { collect_mem_proc( "mswap_total" ); }
/* 15 */        if( para[15]     != NULL ) { collect_disk_vmstat( "pgpgin"); }
/* 16 */        if( para[16]     != NULL ) { collect_disk_vmstat( "pgpgout"); }
/* 17 */        if( para[17]     != NULL ) { collect_disk_vmstat( "pswpin"); }
/* 18 */        if( para[18]     != NULL ) { collect_disk_vmstat( "pswpout"); }
/* 19 */        if( para[19]     != NULL ) { collect_diskstats( disk, "svctm" ); }
/* 20 */        if( para[20]     != NULL ) { collect_diskstats( disk, "await" ); }
/* 21 */        if( para[21]     != NULL ) { collect_diskstats( disk, "aveq" ); }
/* 22 */        if( para[22]     != NULL ) { collect_diskstats( disk, "avgrq_sz" ); }
/* 23 */        if( para[23]     != NULL ) { collect_diskstats( disk, "util" ); }
/* 24 */        if( para[24]     != NULL ) { collect_net_dev ( interface, "net_byte_in" ); }
/* 25 */        if( para[25]     != NULL ) { collect_net_dev ( interface, "net_byte_out" ); }
/* 26 */        if( para[26]     != NULL ) { collect_net_dev ( interface, "net_pack_in" ); }
/* 27 */        if( para[27]     != NULL ) { collect_net_dev ( interface, "net_pack_out" ); }
/* 28 */        if( para[28]     != NULL ) { collect_net_snmp( "passiveopens" ); }
/* 29 */        if( para[29]     != NULL ) { collect_net_snmp( "currestab" ); }
/* 30 */        if( para[30]     != NULL ) { collect_net_snmp( "indatagrams" ); }
/* 31 */        if( para[31]     != NULL ) { collect_net_snmp( "outdatagrams" ); }
/* 32 */        if( para[32]     != NULL ) { collect_hardinfo ( "hard_proc" ); }
/* 33 */        if( para[33]     != NULL ) { collect_hardinfo ( "hard_mem"  ); }
/* 34 */        if( para[34]     != NULL ) { collect_hardinfo ( "hard_temp" ); }
/* 35 */        if( para[35]     != NULL ) { collect_hardinfo ( "hard_disk" ); }
/* 36 */        if( para[36]     != NULL ) { collect_hardinfo ( "hard_pwr"  ); }
/* 37 */        if( para[37]     != NULL ) { collect_hardinfo ( "hard_batt" ); }
/* 38 */        if( para[38]     != NULL ) { collect_hardinfo ( "hard_nics" ); }
/* 39 */        if( para[39]     != NULL ) { collect_hardinfo ( "hard_fans" ); }

//      printf("the while loop finished\n");

        return 0;
}