示例#1
0
int main(int argc, char *argv[])
{
    malloc_perf();

    free_perf();

    return 0;
}
示例#2
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;
}