crabs_firewall-3.0.1 ©2005-2011 - Christophe CAZAJUS (crabs-mettre_le_signe_at-crabs-world.com)
1 /* 2 **============================================================================= 3 ** crabs_firewall-3.0.1 : Firewall-Masquerade avec interface http 4 ** Copyright (C) 2005-2011 : Christophe CAZAJUS (crabs-mettre_le_signe_at-crabs-world.com) 5 ** 6 ** Ce source fait partie d'un projet logiciel libre. Vous pouvez le distribuer 7 ** et/ou le modifier en respectant les termes de la GNU General Public License 8 ** version 2 ou (suite a votre propre choix) une version ulterieure. 9 ** 10 ** Ce programme est distribue dans l'espoir qu'il puisse etre utile, mais 11 ** sans aucune garantie, meme si il est associe a un produit qui vous en 12 ** propose une. Conformez-vous a la GNU General Public License pour avoir 13 ** plus de precisions. 14 ** 15 ** L'auteur ne peut etre tenu responsable de l'utilisation faite des 16 ** composantes associees a ce projet (en partie ou dans leur totalite). 17 ** 18 ** Une copie du fichier de la GNU GPL est fournie dans le repertoire DOC 19 ** de ce projet sous le nom gnu_gpl.txt 20 ** 21 **============================================================================= 22 */ 23 // firewall_g vmin vmax debut fin date w h prefix 24 // 25 #include <firewall.h> 26 #include <stdio.h> 27 #include <string.h> 28 #include <unistd.h> 29 #include <stdlib.h> 30 #include <time.h> 31 #include <fcntl.h> 32 #include <sys/stat.h> 33 #include <graph.h> 34 #define DEBUG_INFO 0 35 bool heure_valide( const char* pt, time_t &t, time_t debut ) ; 36 37 int main( int argc, char** argv ) 38 { 39 char buff [1024] ; 40 if ( argc < 9 ) 41 { 42 fprintf( stderr, "%s vmin vmax debut fin date w h prefix\n", *argv ) ; 43 exit(1) ; 44 } 45 46 if (DEBUG_INFO) 47 fprintf( stderr,"DBG: %s %s %s %s %s %s %s %s %s\n", 48 argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], 49 argv[7], argv[8] ) ; 50 51 strcpy( buff, argv[5] ) ; char fichier [1024] ; 52 sprintf( fichier, "%s/var/%s.dat", getenv( "FW_HOME" ), buff ) ; 53 if (DEBUG_INFO) fprintf( stderr, "DBG: fichier=%s\n", fichier ) ; 54 55 struct stat st ; 56 if ( stat( fichier, &st ) ==-1 ) { perror("stat"); return 1; } 57 int nb = st.st_size / sizeof( StatEthCpu ) ; 58 if (DEBUG_INFO) fprintf( stderr, "DBG: nb=%d\n", nb ) ; 59 60 61 CGserieTimeStamp* temps = new CGserieTimeStamp( nb ) ; 62 temps->setNom("Heure") ; temps->setTextFormat( "%H:%M" ) ; 63 64 char gname[128] ; CGserie *sa[5] ; 65 66 CGserie* r0 = new CGserie( nb ) ; sa[0] = r0 ; 67 sprintf( gname, "%s : RECU", getenv("FW_I0_NET" ) ) ; 68 r0->setNom( gname ) ; r0->setTextFormat("%.1f") ; 69 70 CGserie* s0 = new CGserie( nb ) ; sa[1] = s0 ; 71 sprintf( gname, "%s : ENVOYE", getenv("FW_I0_NET" ) ) ; 72 s0->setNom( gname ) ; s0->setTextFormat("%.1f") ; 73 74 CGserie* r1 = new CGserie( nb ) ; sa[2] = r1 ; 75 sprintf( gname, "%s : RECU", getenv("FW_I1_NET" ) ) ; 76 r1->setNom( gname ) ; r1->setTextFormat("%.1f") ; 77 78 CGserie* s1 = new CGserie( nb ) ; sa[3] = s1 ; 79 sprintf( gname, "%s : ENVOYE", getenv("FW_I1_NET" ) ) ; 80 s1->setNom( gname ) ; s1->setTextFormat("%.1f") ; 81 82 // LE CPU A UN MODE DE GESTION DIFFERENT... 83 CGserie* cpu = new CGserie( nb ) ; sa[4] = cpu ; 84 cpu->setNom( "CPU" ) ; cpu->setTextFormat("%.1f") ; 85 86 int in = open( fichier, O_RDONLY ) ; 87 if ( in==-1 ) { perror("open"); return 1; } 88 time_t debut ; 89 for ( int i=0; i<nb; i++ ) 90 { 91 time_t t1, t2 ; double vr0, vs0, vr1, vs1, ratio ; 92 StatEthCpu d ; read( in, &d, sizeof(StatEthCpu) ) ; 93 switch(i) 94 { 95 case 0 : 96 temps->setValeur( 0, d.t ) ; 97 cpu->setValeur( 0, d.cpu ) ; 98 vr0 = ((double)d.eth0.recv_bytes) ; 99 vs0 = ((double)d.eth0.trns_bytes) ; 100 vr1 = ((double)d.eth1.recv_bytes) ; 101 vs1 = ((double)d.eth1.trns_bytes) ; 102 debut=d.t ; t1=d.t ; 103 break ; 104 case 1 : 105 t2=d.t ; ratio=(double)(t2-t1)*1024.0 ; 106 r0->setValeur( 0, vr0/ratio ) ; 107 s0->setValeur( 0, vs0/ratio ) ; 108 r1->setValeur( 0, vr1/ratio ) ; 109 s1->setValeur( 0, vs1/ratio ) ; 110 default : 111 t2=d.t ; ratio=(double)(t2-t1)*1024.0 ; 112 temps->setValeur( i, d.t ) ; 113 cpu->setValeur( i, d.cpu ) ; 114 r0->setValeur( i, ((double)d.eth0.recv_bytes)/ratio ) ; 115 s0->setValeur( i, ((double)d.eth0.trns_bytes)/ratio ) ; 116 r1->setValeur( i, ((double)d.eth1.recv_bytes)/ratio ) ; 117 s1->setValeur( i, ((double)d.eth1.trns_bytes)/ratio ) ; 118 t1=d.t ; t1 = t2 ; 119 } 120 } 121 close(in) ; 122 struct tm* lt = localtime(&debut) ; char titre[128] ; 123 124 time_t t ; bool t_set = false ; 125 if ( heure_valide( argv[3], t, debut ) ) { temps->setMin(t); t_set=true; } 126 if ( heure_valide( argv[4], t, debut ) ) { temps->setMax(t); t_set=true; } 127 128 if ( t_set ) // Le CPU EST IMPACTE 129 { 130 for( int i=0; i<5; i++ ) sa[i]->setMinMax( temps ) ; 131 } 132 if ( strcmp( argv[1], "auto" ) ) // Le CPU N'EST PAS IMPACTE 133 { 134 float min = atof( argv[1] ) ; 135 for( int i=0; i<4; i++ ) sa[i]->setMin( min ) ; 136 } 137 if ( strcmp( argv[2], "auto" ) ) // Le CPU N'EST PAS IMPACTE 138 { 139 float max = atof( argv[2] ) ; 140 for( int i=0; i<4; i++ ) sa[i]->setMax( max ) ; 141 } 142 cpu->setMinMax( 0, 100 ) ; 143 144 const char *filename[5] = { "r0", "s0", "r1", "s1", "cpu" } ; 145 int the_w = atoi( argv[6] ), the_h = atoi( argv[7] ) ; 146 time_t h0 ; heure_valide( "0", h0, debut ) ; 147 for( int i=0; i<5; i++ ) 148 { 149 CGgraph* gr = new CGgraph() ; 150 gr->setWidthHeight( the_w, the_h ) ; 151 sa[i]->getNom( gname ) ; 152 sprintf( titre, "%s (%02d/%02d/%04d)", gname, 153 lt->tm_mday, lt->tm_mon+1, lt->tm_year+1900 ) ; 154 gr->setTitre( titre ) ; 155 gr->setAxeX( temps ) ; 156 int eth_axe = gr->ajouteAxeY( sa[i] ) ; 157 int R,G,B ; 158 switch( i ) 159 { 160 case 0 : R=0 ; G=0; B=255 ; break ; 161 case 1 : R=0 ; G=156; B=0 ; break ; 162 case 2 : R=0 ; G=0; B=255 ; break ; 163 case 3 : R=0 ; G=156; B=0 ; break ; 164 case 4 : R=255 ; G=0; B=0 ; break ; 165 } 166 gr->ajouteCourbe( sa[i], eth_axe, R,G,B , "A voir") ; 167 char fn[1024] ; sprintf( fn, "%s_%s.png", argv[8], filename[i] ) ; 168 if (DEBUG_INFO) 169 fprintf( stderr,"%s : %s\n", titre, fn ) ; 170 gr->setFichier( fn ) ; gr->creerGraph() ; 171 172 int x0,y0, x1,y1; double v0,v1, vd=(double)h0 ; 173 gr->zoneCourbe( x0, y0, x1, y1 ) ; temps->getMinMax( v0, v1 ) ; 174 printf( "%s;%s;%d;%d;%d;%d;%f;%f<#>", 175 filename[i], fn ,x0, x1, y0, y1, v0-vd, v1-vd ) ; 176 delete gr ; 177 } 178 179 for( int i=0; i<5; i++ ) delete sa[i] ; 180 delete temps ; 181 fflush( stdout ) ; 182 return 0 ; 183 } 184 185 bool heure_valide( const char* cdate, time_t& v, time_t debut ) 186 { 187 if ( strcmp( cdate, "auto" ) == 0 ) return false ; 188 char date[64] ; char* pt ; 189 memcpy( date, cdate, sizeof(date) ) ; date[sizeof(date)-1] = 0 ; 190 int heure, min, sec ; heure=min=sec=0 ; 191 pt = strtok( date, ":" ) ; 192 if (pt) heure=atoi(pt) ; 193 if (pt) pt=strtok( 0, ":" ) ; 194 if (pt) min=atoi(pt) ; 195 if (pt) pt=strtok( 0, ":" ) ; 196 if (pt) sec=atoi(pt) ; 197 struct tm* lt=localtime( &debut ) ; 198 struct tm ntm ; memcpy( &ntm, lt, sizeof( struct tm ) ) ; 199 ntm.tm_hour = heure; ntm.tm_min = min ; ntm.tm_sec = sec ; 200 v = mktime( &ntm ) ; 201 return true ; 202 } 203
Date de génération : 22/09/2011 21:48