⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ifstats.c

📁 一个很好用的linux 下的流量监控软件
💻 C
📖 第 1 页 / 共 3 页
字号:
    adjust_instance_count(PROCCOUNTFILE, 1);    active_facility_countfile[0] = '\0';    move(LINES - 1, 1);    stdexitkeyhelp();    statwin = newwin(LINES - 2, COLS, 1, 0);    statpanel = new_panel(statwin);    tx_stdwinset(statwin);    wtimeout(statwin, -1);    wattrset(statwin, BOXATTR);    tx_colorwin(statwin);    tx_box(statwin, ACS_VLINE, ACS_HLINE);    wmove(statwin, 0, 1);    wprintw(statwin, " Statistics for %s ", iface);    wattrset(statwin, STDATTR);    update_panels();    doupdate();    bzero(&totals, sizeof(struct iftotals));    if (logging) {        if (strcmp(current_logfile, "") == 0) {            snprintf(current_logfile, 64, "%s-%s.log", DSTATLOG, iface);            if (!daemonized)                input_logfile(current_logfile, &logging);        }    }    if (logging) {        opentlog(&logfile, current_logfile);        if (logfile == NULL)            logging = 0;    }    if (logging)        signal(SIGUSR1, rotate_dstat_log);    rotate_flag = 0;    writelog(logging, logfile,             "******** Detailed interface statistics started ********");    printdetlabels(statwin, &totals);    printdetails(&totals, statwin);    update_panels();    doupdate();    spanbr = 0;    gettimeofday(&tv, NULL);    starttime = startlog = statbegin = tv.tv_sec;#ifdef ACTIVATE_GRAPHING    last_graph_time = starttime;#endif    leaveok(statwin, TRUE);    isdnfd = -1;    exitloop = 0;    dispmode(options->actmode, unitstring);    /*     * Data-gathering loop     */    while (!exitloop) {        gettimeofday(&tv, NULL);        now = tv.tv_sec;        unow = tv.tv_sec * 1e+6 + tv.tv_usec;        rate_interval = now - starttime;        if (rate_interval >= 5) {            wattrset(statwin, BOXATTR);            printelapsedtime(statbegin, now, LINES - 3, 1, statwin);            if (options->actmode == KBITS) {                activity =                    (float) (spanbr * 8 / 1000) / (float) rate_interval;                activity_in =                    (float) (spanbr_in * 8 / 1000) / (float) rate_interval;                activity_out =                    (float) (spanbr_out * 8 / 1000) /                    (float) rate_interval;            } else {                activity = (float) (spanbr / 1024) / (float) rate_interval;                activity_in =                    (float) (spanbr_in / 1024) / (float) rate_interval;                activity_out =                    (float) (spanbr_out / 1024) / (float) rate_interval;            }            pps = (float) (spanpkt) / (float) (now - starttime);            pps_in = (float) (spanpkt_in) / (float) (now - starttime);            pps_out = (float) (spanpkt_out) / (float) (now - starttime);            spanbr = spanbr_in = spanbr_out = 0;            spanpkt = spanpkt_in = spanpkt_out = 0;            starttime = now;            wattrset(statwin, HIGHATTR);            mvwprintw(statwin, 13, 19, "%8.1f %s/sec", activity,                      unitstring);            mvwprintw(statwin, 14, 19, "%8.1f packets/sec", pps);            mvwprintw(statwin, 16, 19, "%8.1f %s/sec", activity_in,                      unitstring);            mvwprintw(statwin, 17, 19, "%8.1f packets/sec", pps_in);            mvwprintw(statwin, 19, 19, "%8.1f %s/sec", activity_out,                      unitstring);            mvwprintw(statwin, 20, 19, "%8.1f packets/sec", pps_out);            if (activity > peakactivity)                peakactivity = activity;            if (activity_in > peakactivity_in)                peakactivity_in = activity_in;            if (activity_out > peakactivity_out)                peakactivity_out = activity_out;            if (pps > peakpps)                peakpps = pps;            if (pps_in > peakpps_in)                peakpps_in = pps_in;            if (pps_out > peakpps_out)                peakpps_out = pps_out;        }        if ((now - startlog) >= options->logspan && logging) {            writedstatlog(iface, options->actmode, activity, pps,                          peakactivity, peakpps,                          peakactivity_in, peakpps_in,                          peakactivity_out, peakpps_out, &totals,                          time((time_t *) NULL) - statbegin, logfile);            startlog = now;        }#ifdef ACTIVATE_GRAPHING        graph_interval = now - last_graph_time;        if (daemonized && graph_interval >= 60            && graphing_logfile[0] != '\0') {            graphing_fd = fopen(graphing_logfile, "w");            if (graphing_fd == NULL) {                write_error                    ("Unable to open raw logfile, raw logging diabled", 1);                graphing_logfile[0] = '\0';            } else {                fprintf(graphing_fd, "%lu %8.2f %8.2f %8.2f %8.2f\n",                        now,                        (float) graph_span_pkts_out /                        (float) graph_interval,                        (float) (graph_span_bytes_out * 8 / 1000) /                        (float) graph_interval,                        (float) graph_span_pkts_in /                        (float) graph_interval,                        (float) (graph_span_bytes_in * 8 / 1000) /                        (float) graph_interval);                fclose(graphing_fd);                last_graph_time = now;                graph_span_pkts_out = 0;                graph_span_bytes_out = 0;                graph_span_pkts_in = 0;                graph_span_bytes_in = 0;            }        }#endif        if (((options->updrate == 0)             && (unow - updtime_usec >= DEFAULT_UPDATE_DELAY))            || ((options->updrate != 0)                && (now - updtime >= options->updrate))) {            printdetails(&totals, statwin);            update_panels();            doupdate();            updtime_usec = unow;            updtime = now;        }        check_rotate_flag(&logfile, logging);        if ((facilitytime != 0)            && (((now - statbegin) / 60) >= facilitytime))            exitloop = 1;        getpacket(fd, buf, &fromaddr, &ch, &br, ifname, statwin);        if (ch != ERR) {            switch (ch) {            case 12:            case 'l':            case 'L':                tx_refresh_screen();                break;            case 'Q':            case 'q':            case 'X':            case 'x':            case 24:            case 27:                exitloop = 1;                break;            }        }        if (br > 0) {            framelen = br;            pkt_result = processpacket(buf, &packet, &br, NULL,                                       NULL, NULL, &fromaddr,                                       &linktype, ofilter,                                       MATCH_OPPOSITE_USECONFIG, ifname,                                       iface);            if (pkt_result != PACKET_OK && pkt_result != MORE_FRAGMENTS)                continue;            totals.total++;            totals.bytestotal += framelen;            if (fromaddr.sll_pkttype == PACKET_OUTGOING) {                totals.total_out++;                totals.bytestotal_out += framelen;                spanbr_out += framelen;                spanpkt_out++;            } else {                totals.total_in++;                totals.bytestotal_in += framelen;                spanbr_in += framelen;                spanpkt_in++;            }            if (fromaddr.sll_pkttype == PACKET_BROADCAST) {                totals.bcast++;                totals.bcastbytes += framelen;            }            spanbr += framelen;            spanpkt++;            if (fromaddr.sll_protocol == ETH_P_IP) {                if (pkt_result == CHECKSUM_ERROR) {                    totals.badtotal++;                    continue;                }                ipacket = (struct iphdr *) packet;                iphlen = ipacket->ihl * 4;                tpacket = packet + iphlen;                iplen = ntohs(ipacket->tot_len);                totals.iptotal++;                totals.ipbtotal += iplen;#ifdef ACTIVATE_GRAPHING                graph_span_pkts++;                graph_span_bytes += framelen;#endif                if (fromaddr.sll_pkttype == PACKET_OUTGOING) {                    totals.iptotal_out++;                    totals.ipbtotal_out += iplen;#ifdef ACTIVATE_GRAPHING                    graph_span_pkts_out++;                    graph_span_bytes_out += framelen;#endif                } else {                    totals.iptotal_in++;                    totals.ipbtotal_in += iplen;#ifdef ACTIVATE_GRAPHING                    graph_span_pkts_in++;                    graph_span_bytes_in += framelen;#endif                }                switch (ipacket->protocol) {                case IPPROTO_TCP:                    totals.tcptotal++;                    totals.tcpbtotal += iplen;                    if (fromaddr.sll_pkttype == PACKET_OUTGOING) {                        totals.tcptotal_out++;                        totals.tcpbtotal_out += iplen;                    } else {                        totals.tcptotal_in++;                        totals.tcpbtotal_in += iplen;                    }                    break;                case IPPROTO_UDP:                    totals.udptotal++;                    totals.udpbtotal += iplen;                    if (fromaddr.sll_pkttype == PACKET_OUTGOING) {                        totals.udptotal_out++;                        totals.udpbtotal_out += iplen;                    } else {                        totals.udptotal_in++;                        totals.udpbtotal_in += iplen;                    }                    break;                case IPPROTO_ICMP:                    totals.icmptotal++;                    totals.icmpbtotal += iplen;                    if (fromaddr.sll_pkttype == PACKET_OUTGOING) {                        totals.icmptotal_out++;                        totals.icmpbtotal_out += iplen;                    } else {                        totals.icmptotal_in++;                        totals.icmpbtotal_in += iplen;                    }                    break;                default:                    totals.othtotal++;                    totals.othbtotal += iplen;                    if (fromaddr.sll_pkttype == PACKET_OUTGOING) {                        totals.othtotal_out++;                        totals.othbtotal_out += iplen;                    } else {                        totals.othtotal_in++;                        totals.othbtotal_in += iplen;                    }                    break;                }            } else {                totals.noniptotal++;                totals.nonipbtotal += br;                if (fromaddr.sll_pkttype == PACKET_OUTGOING) {                    totals.noniptotal_out++;                    totals.nonipbtotal_out += br;                } else {                    totals.noniptotal_in++;                    totals.nonipbtotal_in += br;                }            }        }    }    close(fd);    if ((options->promisc) && (is_last_instance())) {        load_promisc_list(&promisc_list);        srpromisc(0, promisc_list);        destroy_promisc_list(&promisc_list);    }    adjust_instance_count(PROCCOUNTFILE, -1);    if (logging) {        signal(SIGUSR1, SIG_DFL);        writedstatlog(iface, options->actmode, activity, pps,                      peakactivity, peakpps,                      peakactivity_in, peakpps_in,                      peakactivity_out, peakpps_out, &totals,                      time((time_t *) NULL) - statbegin, logfile);        writelog(logging, logfile,                 "******** Detailed interface statistics stopped ********");        fclose(logfile);    }    del_panel(statpanel);    delwin(statwin);    unmark_facility(DSTATIDFILE, iface);    strcpy(current_logfile, "");    pkt_cleanup();    update_panels();    doupdate();}void selectiface(char *ifname, int withall, int *aborted){    int ch;    struct iflist *list;    struct iflist *ptmp;    struct scroll_list scrolllist;    initiflist(&list);    if (list == NULL) {        no_ifaces_error();        *aborted = 1;        return;    }    if ((withall) && (list != NULL)) {        ptmp = malloc(sizeof(struct iflist));        strcpy(ptmp->ifname, "All interfaces");        ptmp->prev_entry = NULL;        list->prev_entry = ptmp;        ptmp->next_entry = list;        list = ptmp;    }    tx_listkeyhelp(STDATTR, HIGHATTR);    ptmp = list;    tx_init_listbox(&scrolllist, 24, 14, (COLS - 24) / 2 - 9,                    (LINES - 14) / 2, STDATTR, BOXATTR, BARSTDATTR,                    HIGHATTR);    tx_set_listbox_title(&scrolllist, "Select Interface", 1);    while (ptmp != NULL) {        tx_add_list_entry(&scrolllist, (char *) ptmp, ptmp->ifname);        ptmp = ptmp->next_entry;    }    tx_show_listbox(&scrolllist);    tx_operate_listbox(&scrolllist, &ch, aborted);    tx_close_listbox(&scrolllist);    if (!(*aborted) && (list != NULL)) {        ptmp = (struct iflist *) scrolllist.textptr->nodeptr;        if ((withall) && (ptmp->prev_entry == NULL))    /* All Interfaces */            strcpy(ifname, "");        else            strcpy(ifname, ptmp->ifname);    }    tx_destroy_list(&scrolllist);    destroyiflist(list);    update_panels();    doupdate();}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -