ifstats.c

来自「一个很好用的linux 下的流量监控软件」· C语言 代码 · 共 1,292 行 · 第 1/3 页

C
1,292
字号
    int fd;    FILE *logfile = NULL;    int br;    char ifname[10];    int ch;    struct timeval tv;    unsigned long starttime = 0;    unsigned long statbegin = 0;    unsigned long now = 0;    unsigned long long unow = 0;    unsigned long startlog = 0;    unsigned long updtime = 0;    unsigned long long updtime_usec = 0;    struct promisc_states *promisc_list;    if (!facility_active(GSTATIDFILE, ""))        mark_facility(GSTATIDFILE, "general interface statistics", "");    else {        write_error            ("General interface stats already active in another process",             daemonized);        return;    }    initiflist(&(table.head));    if (table.head == NULL) {        no_ifaces_error();        unmark_facility(GSTATIDFILE, "");        return;    }    initiftab(&table);    open_socket(&fd);    if (fd < 0) {        unmark_facility(GSTATIDFILE, "");        return;    }    if ((first_active_facility()) && (options->promisc)) {        init_promisc_list(&promisc_list);        save_promisc_list(promisc_list);        srpromisc(1, promisc_list);        destroy_promisc_list(&promisc_list);    }    adjust_instance_count(PROCCOUNTFILE, 1);    active_facility_countfile[0] = '\0';    if (logging) {        if (strcmp(current_logfile, "") == 0) {            strcpy(current_logfile, GSTATLOG);            if (!daemonized)                input_logfile(current_logfile, &logging);        }    }    if (logging) {        opentlog(&logfile, GSTATLOG);        if (logfile == NULL)            logging = 0;    }    if (logging)        signal(SIGUSR1, rotate_gstat_log);    rotate_flag = 0;    writelog(logging, logfile,             "******** General interface statistics started ********");    if (table.head != NULL) {        preparescreen(&table);        update_panels();        doupdate();        isdnfd = -1;        exitloop = 0;        gettimeofday(&tv, NULL);        starttime = startlog = statbegin = tv.tv_sec;        while (!exitloop) {            gettimeofday(&tv, NULL);            now = tv.tv_sec;            unow = tv.tv_sec * 1e+6 + tv.tv_usec;            if ((now - starttime) >= 5) {                updaterates(&table, options->actmode, starttime, now, idx);                printelapsedtime(statbegin, now, LINES - 3, 1,                                 table.borderwin);                starttime = now;            }            if (((now - startlog) >= options->logspan) && (logging)) {                writegstatlog(&table, options->actmode,                              time((time_t *) NULL) - statbegin, logfile);                startlog = now;            }            if (((options->updrate != 0)                 && (now - updtime >= options->updrate))                || ((options->updrate == 0)                    && (unow - updtime_usec >= DEFAULT_UPDATE_DELAY))) {                update_panels();                doupdate();                updtime = now;                updtime_usec = unow;            }            check_rotate_flag(&logfile, logging);            if ((facilitytime != 0)                && (((now - statbegin) / 60) >= facilitytime))                exitloop = 1;            getpacket(fd, buf, &fromaddr, &ch, &br, ifname, table.statwin);            if (ch != ERR) {                switch (ch) {                case KEY_UP:                    scrollgstatwin(&table, SCROLLDOWN, &idx);                    break;                case KEY_DOWN:                    scrollgstatwin(&table, SCROLLUP, &idx);                    break;                case KEY_PPAGE:                case '-':                    pagegstatwin(&table, SCROLLDOWN, &idx);                    break;                case KEY_NPAGE:                case ' ':                    pagegstatwin(&table, SCROLLUP, &idx);                    break;                case 12:                case 'l':                case 'L':                    tx_refresh_screen();                    break;                case 'Q':                case 'q':                case 'X':                case 'x':                case 27:                case 24:                    exitloop = 1;                    break;                }            }            if (br > 0) {                pkt_result =                    processpacket(buf, &packet, &br, NULL, NULL, NULL,                                  &fromaddr, &linktype, ofilter,                                  MATCH_OPPOSITE_USECONFIG, ifname, NULL);                if (pkt_result != PACKET_OK                    && pkt_result != MORE_FRAGMENTS)                    continue;                positionptr(&table, &ptmp, ifname);                ptmp->total++;                ptmp->spanbr += br;                ptmp->br += br;                if (fromaddr.sll_protocol == ETH_P_IP) {                    ptmp->iptotal++;                    if (pkt_result == CHECKSUM_ERROR) {                        (ptmp->badtotal)++;                        continue;                    }                } else {                    (ptmp->noniptotal)++;                }                printifentry(ptmp, table.statwin, idx);            }        }        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);    del_panel(table.statpanel);    delwin(table.statwin);    del_panel(table.borderpanel);    delwin(table.borderwin);    update_panels();    doupdate();    if (logging) {        signal(SIGUSR1, SIG_DFL);        writegstatlog(&table, options->actmode,                      time((time_t *) NULL) - statbegin, logfile);        writelog(logging, logfile,                 "******** General interface statistics stopped ********");        fclose(logfile);    }    destroyiflist(table.head);    pkt_cleanup();    unmark_facility(GSTATIDFILE, "");    strcpy(current_logfile, "");}void printdetlabels(WINDOW * win, struct iftotals *totals){    wattrset(win, BOXATTR);    mvwprintw(win, 2, 14,              "  Total      Total    Incoming   Incoming    Outgoing   Outgoing");    mvwprintw(win, 3, 14,              "Packets      Bytes     Packets      Bytes     Packets      Bytes");    wattrset(win, STDATTR);    mvwprintw(win, 4, 2, "Total:");    mvwprintw(win, 5, 2, "IP:");    mvwprintw(win, 6, 2, "TCP:");    mvwprintw(win, 7, 2, "UDP:");    mvwprintw(win, 8, 2, "ICMP:");    mvwprintw(win, 9, 2, "Other IP:");    mvwprintw(win, 10, 2, "Non-IP:");    mvwprintw(win, 13, 2, "Total rates:");    mvwprintw(win, 16, 2, "Incoming rates:");    mvwprintw(win, 19, 2, "Outgoing rates:");    mvwprintw(win, 13, 45, "Broadcast packets:");    mvwprintw(win, 14, 45, "Broadcast bytes:");    mvwprintw(win, 18, 45, "IP checksum errors:");    update_panels();    doupdate();}void printstatrow(WINDOW * win, int row,                  unsigned long long total, unsigned long long btotal,                  unsigned long long total_in,                  unsigned long long btotal_in,                  unsigned long long total_out,                  unsigned long long btotal_out){    wmove(win, row, 12);    printlargenum(total, win);    wmove(win, row, 23);    printlargenum(btotal, win);    wmove(win, row, 35);    printlargenum(total_in, win);    wmove(win, row, 46);    printlargenum(btotal_in, win);    wmove(win, row, 58);    printlargenum(total_out, win);    wmove(win, row, 69);    printlargenum(btotal_out, win);}void printdetails(struct iftotals *totals, WINDOW * win){    wattrset(win, HIGHATTR);    /* Print totals on the IP protocols */    printstatrow(win, 4, totals->total, totals->bytestotal,                 totals->total_in, totals->bytestotal_in,                 totals->total_out, totals->bytestotal_out);    printstatrow(win, 5, totals->iptotal, totals->ipbtotal,                 totals->iptotal_in, totals->ipbtotal_in,                 totals->iptotal_out, totals->ipbtotal_out);    printstatrow(win, 6, totals->tcptotal, totals->tcpbtotal,                 totals->tcptotal_in, totals->tcpbtotal_in,                 totals->tcptotal_out, totals->tcpbtotal_out);    printstatrow(win, 7, totals->udptotal, totals->udpbtotal,                 totals->udptotal_in, totals->udpbtotal_in,                 totals->udptotal_out, totals->udpbtotal_out);    printstatrow(win, 8, totals->icmptotal, totals->icmpbtotal,                 totals->icmptotal_in, totals->icmpbtotal_in,                 totals->icmptotal_out, totals->icmpbtotal_out);    printstatrow(win, 9, totals->othtotal, totals->othbtotal,                 totals->othtotal_in, totals->othbtotal_in,                 totals->othtotal_out, totals->othbtotal_out);    /* Print non-IP totals */    printstatrow(win, 10, totals->noniptotal, totals->nonipbtotal,                 totals->noniptotal_in, totals->nonipbtotal_in,                 totals->noniptotal_out, totals->nonipbtotal_out);    /* Broadcast totals */    wmove(win, 13, 67);    printlargenum(totals->bcast, win);    wmove(win, 14, 67);    printlargenum(totals->bcastbytes, win);    /* Bad packet count */    mvwprintw(win, 18, 68, "%8lu", totals->badtotal);}/* * The detailed interface statistics function */void detstats(char *iface, const struct OPTIONS *options, int facilitytime,              struct filterstate *ofilter){    int logging = options->logging;    WINDOW *statwin;    PANEL *statpanel;    char buf[MAX_PACKET_SIZE];    char *packet;    struct iphdr *ipacket = NULL;    char *tpacket;    unsigned int iphlen;    char ifname[10];    struct sockaddr_ll fromaddr;    unsigned short linktype;    int fd;    int br;    int framelen = 0;    int pkt_result = 0;    FILE *logfile = NULL;    unsigned int iplen = 0;    struct iftotals totals;    int ch;    struct timeval tv;    unsigned long updtime = 0;    unsigned long long updtime_usec = 0;    unsigned long starttime, now;    unsigned long statbegin, startlog;    unsigned long rate_interval;    unsigned long long unow;    float spanbr = 0;    float spanpkt = 0;    float spanbr_in = 0;    float spanbr_out = 0;    float spanpkt_in = 0;    float spanpkt_out = 0;    float activity = 0;    float activity_in = 0;    float activity_out = 0;    float peakactivity = 0;    float peakactivity_in = 0;    float peakactivity_out = 0;    float pps = 0;    float peakpps = 0;    float pps_in = 0;    float pps_out = 0;    float peakpps_in = 0;    float peakpps_out = 0;    char unitstring[7];    struct promisc_states *promisc_list;    char err_msg[80];#ifdef ACTIVATE_GRAPHING    FILE *graphing_fd;    unsigned long last_graph_time;    unsigned long graph_interval;    float graph_span_pkts = 0;    float graph_span_bytes = 0;    float graph_span_pkts_in = 0;    float graph_span_bytes_in = 0;    float graph_span_pkts_out = 0;    float graph_span_bytes_out = 0;#endif    /*     * Mark this facility     */    if (!facility_active(DSTATIDFILE, iface))        mark_facility(DSTATIDFILE, "detailed interface statistics", iface);    else {        snprintf(err_msg, 80,                 "Detailed interface stats already monitoring %s", iface);        write_error(err_msg, daemonized);        return;    }    open_socket(&fd);    if (fd < 0) {        unmark_facility(DSTATIDFILE, iface);        return;    }    if (!iface_supported(iface)) {        err_iface_unsupported();        unmark_facility(DSTATIDFILE, iface);        return;    }    if (!iface_up(iface)) {        err_iface_down();        unmark_facility(DSTATIDFILE, iface);        return;    }    if ((first_active_facility()) && (options->promisc)) {        init_promisc_list(&promisc_list);        save_promisc_list(promisc_list);        srpromisc(1, promisc_list);        destroy_promisc_list(&promisc_list);    }

⌨️ 快捷键说明

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