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 // graph_para vmin vmax debut fin date r0|s0|r1|s1|cpu 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 enum GraphNom { GNerr=-1, GNr0, GNs0, GNr1, GNs1, GNcpu } ; 37 int main( int argc, char** argv ) 38 { 39 char buff [1024] ; int R, G, B ; 40 if ( argc < 7 ) 41 { 42 fprintf( stderr, "%s vmin vmax debut fin date what\n", *argv ) ; 43 exit(1) ; 44 } 45 46 if (DEBUG_INFO) fprintf( stderr,"DBG: %s %s %s %s %s %s %s\n", argv[0], argv[1], 47 argv[2], argv[3], argv[4], argv[5], argv[6] ) ; 48 49 strcpy( buff, argv[5] ) ; char fichier [1024] ; 50 sprintf( fichier, "%s/var/%s.dat", getenv( "FW_HOME" ), buff ) ; 51 if (DEBUG_INFO) fprintf( stderr, "DBG: fichier=%s\n", fichier ) ; 52 53 struct stat st ; 54 if ( stat( fichier, &st ) ==-1 ) { perror("stat"); return 1; } 55 int nb = st.st_size / sizeof( StatEthCpu ) ; 56 if (DEBUG_INFO) fprintf( stderr, "DBG: nb=%d\n", nb ) ; 57 58 59 CGserie* y ; 60 CGserieTimeStamp* temps = new CGserieTimeStamp( nb ) ; 61 temps->setNom("Heure") ; temps->setTextFormat( "%H:%M" ) ; 62 63 GraphNom gn = GNerr ; y = new CGserie( nb ) ; char gname[128] ; 64 if ( strcmp( argv[6], "cpu" ) == 0 ) 65 { 66 gn = GNcpu ; R=255; G=0; B=0 ; strcpy( gname, "CPU" ) ; 67 y->setNom( argv[6] ) ; y->setTextFormat("%.0f") ; 68 } 69 else if ( strcmp( argv[6], "r0" ) == 0 ) 70 { 71 gn = GNr0 ; R=0; G=0; B=255 ; 72 sprintf( gname, "%s (reçu)", getenv("FW_I0_NET" ) ) ; 73 y->setNom( argv[6] ) ; y->setTextFormat("%.1f") ; 74 } 75 else if ( strcmp( argv[6], "r1" ) == 0 ) 76 { 77 gn = GNr1 ; R=0; G=0; B=255 ; 78 sprintf( gname, "%s (reçu)", getenv("FW_I1_NET" ) ) ; 79 y->setNom( argv[6] ) ; y->setTextFormat("%.1f") ; 80 } 81 else if ( strcmp( argv[6], "s0" ) == 0 ) 82 { 83 gn = GNs0 ; R=0; G=156; B=0 ; 84 sprintf( gname, "%s (envoyé)", getenv("FW_I0_NET" ) ) ; 85 y->setNom( argv[6] ) ; y->setTextFormat("%.1f") ; 86 } 87 else if ( strcmp( argv[6], "s1" ) == 0 ) 88 { 89 gn = GNs1 ; R=0; G=156; B=0 ; 90 sprintf( gname, "%s (envoyé)", getenv("FW_I1_NET" ) ) ; 91 y->setNom( argv[6] ) ; y->setTextFormat("%.1f") ; 92 } 93 else 94 { fprintf(stderr,"%s: %s unknow graph\n",*argv,argv[6]); return 1; } 95 96 97 int in = open( fichier, O_RDONLY ) ; 98 if ( in==-1 ) { perror("open"); return 1; } 99 time_t debut ; 100 for ( int i=0; i<nb; i++ ) 101 { 102 time_t t1, t2 ; double v, ratio ; 103 StatEthCpu d ; read( in, &d, sizeof(StatEthCpu) ) ; 104 switch(i) 105 { 106 case 0 : 107 temps->setValeur( 0, d.t ) ; 108 switch( gn ) 109 { 110 case GNcpu : y->setValeur( 0, d.cpu ) ; break ; 111 case GNr0 : v = ((double)d.eth0.recv_bytes) ; break ; 112 case GNs0 : v = ((double)d.eth0.trns_bytes) ; break ; 113 case GNr1 : v = ((double)d.eth1.recv_bytes) ; break ; 114 case GNs1 : v = ((double)d.eth1.trns_bytes) ; break ; 115 } 116 debut=d.t ; t1=d.t ; 117 break ; 118 case 1 : 119 t2=d.t ; ratio=(double)(t2-t1)*1024.0 ; 120 if ( gn != GNcpu ) y->setValeur( 0, v/ratio ) ; 121 default : 122 t2=d.t ; ratio=(double)(t2-t1)*1024.0 ; 123 temps->setValeur( i, d.t ) ; 124 switch( gn ) 125 { 126 case GNcpu : v=d.cpu ; break ; 127 case GNr0 : v=((double)d.eth0.recv_bytes)/ratio ; break ; 128 case GNs0 : v=((double)d.eth0.trns_bytes)/ratio ; break ; 129 case GNr1 : v=((double)d.eth1.recv_bytes)/ratio ; break ; 130 case GNs1 : v=((double)d.eth1.trns_bytes)/ratio ; break ; 131 } 132 debut=d.t ; t1=d.t ; t1 = t2 ; 133 y->setValeur( i, v ) ; 134 } 135 } 136 close(in) ; 137 struct tm* lt = localtime(&debut) ; char titre[128] ; 138 139 time_t t ; bool t_set = false ; 140 if ( heure_valide( argv[3], t, debut ) ) { temps->setMin(t); t_set=true; } 141 if ( heure_valide( argv[4], t, debut ) ) { temps->setMax(t); t_set=true; } 142 if ( gn == GNcpu ) 143 y->setMinMax( 0, 100 ) ; 144 else 145 { 146 if ( t_set ) y->setMinMax( temps ) ; 147 if ( strcmp( argv[1], "auto" ) ) y->setMin( atof( argv[1] ) ) ; 148 if ( strcmp( argv[2], "auto" ) ) y->setMax( atof( argv[2] ) ) ; 149 } 150 151 CGgraph* gr = new CGgraph() ; 152 gr->setWidthHeight( atoi( getenv("FW_W") ) , atoi( getenv("FW_H") ) ) ; 153 sprintf( titre, "%s (%02d/%02d/%04d)", gname, 154 lt->tm_mday, lt->tm_mon+1, lt->tm_year+1900 ) ; 155 gr->setTitre( titre ) ; 156 gr->setAxeX( temps ) ; 157 int eth_axe = gr->ajouteAxeY( y ) ; 158 gr->ajouteCourbe( y, eth_axe, R,G,B , "A voir") ; 159 gr->creerGraph() ; 160 delete gr ; 161 162 delete y ; delete temps ; 163 return 0 ; 164 } 165 166 bool heure_valide( const char* cdate, time_t& v, time_t debut ) 167 { 168 if ( strcmp( cdate, "auto" ) == 0 ) return false ; 169 char date[64] ; char* pt ; 170 memcpy( date, cdate, sizeof(date) ) ; date[sizeof(date)-1] = 0 ; 171 int heure, min, sec ; heure=min=sec=0 ; 172 pt = strtok( date, ":" ) ; 173 if (pt) heure=atoi(pt) ; 174 if (pt) pt=strtok( 0, ":" ) ; 175 if (pt) min=atoi(pt) ; 176 if (pt) pt=strtok( 0, ":" ) ; 177 if (pt) sec=atoi(pt) ; 178 struct tm* lt=localtime( &debut ) ; 179 struct tm ntm ; memcpy( &ntm, lt, sizeof( struct tm ) ) ; 180 ntm.tm_hour = heure; ntm.tm_min = min ; ntm.tm_sec = sec ; 181 v = mktime( &ntm ) ; 182 return true ; 183 } 184
Date de génération : 22/09/2011 21:48