| die letzten Änderungen * Seitenstruktur * Stichwortsuche :


logo_puschin.jpg

Startseite

Knowledge base



Kontakt

Impressum

Knowledge base - C

Knowledge base

10 Benutzer online Druckversion




zurück



C
trmon.c


#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>
#include <sys/sysinfo.h>
#include <sys/time.h>
#include <string.h>

int debug=0;
char ethdev[255];
FILE *fd;
int a=1;
char buffer[1024];
long txPackets_mem;
long txPackets;
long rxPackets;
long diffPackets;
char ifname[8];
struct timeval diffPitch;
struct timeval tv_mem;
struct timeval tv_now;
int zaehler1=0,zaehler2=0,zaehler3=0,zaehler4=0;
int traffic1=0,traffic2=0,traffic3=0,traffic4=0;
float rate1=0,rate2=0,rate3=0,rate4=0;
int report=1;
char html[2048];

int main(int argc, char *argv[]) {

        printf("============================================\n");
        printf("trmon - Traffic Monitor Plugin for darkstat3\n");
        printf("              26.01.2007 -  fp\n");
        printf("============================================\n");

        if (argc<2) {
                printf("%s Interface [debug]\n", argv[0]);
                return 1;
        }

        // Interface wurde uerbegeben
        //
        sprintf(ethdev,argv[1]);

        // Debug-Modus aktivieren?
        //
        if (argc>2) {
                if (strncmp(argv[2],"debug",5)==0) {
                        debug=1;
                }
        }

        // Diese Schleife laeuft ziemlich lange
        //
        while (a=1) {
                fd=fopen("/proc/net/dev","r");
                fscanf(fd, "%*[\n] %*[\n] ");

                while(feof(fd) == 0) {
                        fscanf(fd, "%7[:]:%llu %*u %*u %*u %*u %*u %*u %*u" "%llu %*[\n] ", &ifname, &rxPackets, &txPackets);
                        if (debug==1) { printf("debug (ifname) : %s\n", ifname); }
                        if (debug==1) { printf("debug (ethdev) : %s\n", ethdev); }
                        //if (strncmp(ifname, ethdev, 7) == 0) {
                        if (strcmp(ifname, ethdev) == 0) {
                                break;
                        }
                }
                fclose(fd);

                if (debug==1) { printf("debug (txPackets) : %d\n", txPackets); }
                if (debug==1) { printf("debug (txPackets_mem) : %d\n", txPackets_mem); }

                if (txPackets_mem != 0) {
                        // Differenz der Pakete
                        //
                        diffPackets=txPackets-txPackets_mem;
                        if (debug==1) { printf("debug (diffPackets) : %d\n", diffPackets); }

                        // Zeit *jetzt* bestimmen + Pitch
                        //
                        gettimeofday(&tv_now, NULL);
                        if (debug==1) { printf("debug (tv_mem) : %d\n", tv_mem); }
                        if (debug==1) { printf("debug (tv_now) : %d\n", tv_now); }
                        diffPitch.tv_sec=tv_now.tv_sec-tv_mem.tv_sec;
                        if (debug==1) { printf("debug (diffPitch) : %d\n", diffPitch); }

                        // Zeiten hochzaehlen (5 verschiedene)
                        //
                        zaehler1+=diffPitch.tv_sec;
                        zaehler2+=diffPitch.tv_sec;
                        zaehler3+=diffPitch.tv_sec;
                        zaehler4+=diffPitch.tv_sec;

                        // Traffic summieren (5 mal)
                        //
                        traffic1+=diffPackets;
                        traffic2+=diffPackets;
                        traffic3+=diffPackets;
                        traffic4+=diffPackets;

                        if (debug==1) { printf("debug (1) : %d %d\n", traffic1, zaehler1); }
                        if (debug==1) { printf("debug (2) : %d %d\n", traffic2, zaehler2); }
                        if (debug==1) { printf("debug (3) : %d %d\n", traffic3, zaehler3); }
                        if (debug==1) { printf("debug (4) : %d %d\n", traffic4, zaehler4); }

                        // Wenn zaehler1 ueber 5 ist, dann errechnen (NOW)
                        //
                        if (zaehler1>5) {
                                rate1=traffic1/zaehler1/1024;
                                printf("debug (rate1) : %f\n", rate1);
                                traffic1=0;
                                zaehler1=0;
                                report=1;
                        }

                        // Wenn zaehler2 ueber 300 (5 Minuten)
                        //
                        if (zaehler2>300) {
                                rate2=traffic2/zaehler2/1024;
                                printf("debug (rate2) : %f\n", rate2);
                                traffic2=0;
                                zaehler2=0;
                                report=1;
                        }

                        // Wenn zaehler3 ueber 3600 (1 Stunde)
                        //
                        if (zaehler3>3600) {
                                rate3=traffic3/zaehler3/1024;
                                printf("debug (rate3) : %f\n", rate3);
                                traffic3=0;
                                zaehler3=0;
                                report=1;
                        }

                        // Wenn zaehler4 ueber 86400 (1 Tag)
                        //
                        if (zaehler4>86400) {
                                rate4=traffic4/zaehler4/1024;
                                printf("debug (rate4) : %f\n", rate4);
                                traffic4=0;
                                zaehler4=0;
                                report=1;
                        }

                        if (report==1) {
                                if (debug==1) { printf("debug (report) : HTML-Report\n"); }
                                sprintf(html, "<table>\
                                <tr><td><b>Traffic der letzten 5 Sekunden&nbsp;&nbsp;</td><td>%f kbytes/s</td></tr> \
                                <tr><td><b>Traffic der letzten 5 Minuten</td><td>%f kbytes/s</td></tr> \
                                <tr><td><b>Traffic der letzten Stunde</td><td>%f kbytes/s</td></tr> \
                                <tr><td><b>Traffic des letzten Tages</td><td>%f kbytes/s</td></tr> \
                                </table>", rate1, rate2, rate3, rate4);

                                fd=fopen("trmon.dat", "w");
                                fwrite(html, strlen(html), 1, fd);
                                fclose(fd);

                                report=0;
                        }
                }

                // Vor dem naechsten Durchgang die Werte merken
                //
                txPackets_mem=txPackets;
                gettimeofday(&tv_mem, NULL);
                sleep(1);
        }
}



zurück



Knowledge base wurde zuletzt bearbeitet am 12.07.13 durch Frank

www.puschin.de
login

<body bgcolor='#FFFFFF' link='#000000' vlink='#000000' alink='#000000' text='#000000'><font face='Verdana' size='2'><strong>www.puschin.de</strong><br>Die Webseite http://www.puschin.de bietet eine interessante Webseite zu vielen Themen aus dem Bereich Linux und Windows. Man findet hier Tipps und Tricks zu cms php apache postfix openxchange tomcat windows linux firewall <br><br><font face='Verdana' size='2'><li><a href='cms.php?print=&aktion=thema_anzeigen&menue_id=191'>Startseite</a><font face='Verdana' size='2'><li><a href='cms.php?print=&aktion=thema_anzeigen&menue_id=63'>Knowledge base</a><font face='Verdana' size='2'><li><a href='cms.php?print=&aktion=thema_anzeigen&menue_id=57'>Kontakt</a><font face='Verdana' size='2'><li><a href='cms.php?print=&aktion=thema_anzeigen&menue_id=9'>Impressum</a></body>