Firewall-Masquerade avec interface http

crabs_firewall-3.0.1 ©2005-2011 - Christophe CAZAJUS (crabs-mettre_le_signe_at-crabs-world.com)

firewall_col~~ / STAT / COL / main.c
Makefile firewall.h main.c daemon.c rc.firewall_col
    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 #include <unistd.h>
   24 #include "firewall.h"
   25 #include <string.h>
   26 #include <stdio.h>
   27 #include <stdlib.h>
   28 #include <fcntl.h>
   29 #include <syslog.h>
   30 #include <signal.h>
   31 
   32 extern int be_a_daemon( char *name, char *err, size_t sz ) ;
   33 int stop ;
   34 void sigINT( int sig ) { stop = 1 ; syslog( LOG_ERR, "SIG_INT recu" ) ; }
   35 
   36 #define ATTENTE_DEFAUT 15
   37 
   38 typedef struct StatCpu { uint64_t total, use ; } StatCpu ;
   39 typedef struct
   40     {
   41     StatInter   eth0    ;
   42     StatInter   eth1    ;
   43     StatCpu     cpu     ;
   44     } Stat  ;
   45 Stat    actuel, prec, aff   ;
   46 
   47 void affecte( StatInter* si, uint64_t rb, uint64_t rp, uint64_t tb,
   48         uint64_t tp ) ;
   49 void difference( StatInter* res, StatInter a, StatInter p ) ;
   50 int pourcentage( StatCpu a, StatCpu p ) ;
   51 void store( StatEthCpu* sec, char* fichier ) ;
   52 
   53 void get_stat() ;
   54 char        fichier [128]   ;
   55 
   56 int main( int argc, char** argv )
   57     {
   58     char        err     [1024]  ;
   59     char        fmt     [128]   ;
   60     struct tm * lt              ;
   61     int         attente         ;
   62     int         nb              ;
   63 
   64     stop = attente=0 ;
   65     if ( argc > 1 ) attente = atoi( argv[1] ) ;
   66     if ( attente <= 0 ) attente = ATTENTE_DEFAUT ;
   67     if ( be_a_daemon( "firewall_col", err, sizeof(err) ) == -1 )
   68         {
   69         fprintf( stderr, "ERREUR: %s\n", err ) ;
   70         syslog( LOG_ERR, "ERREUR: %s\n", err ) ;
   71         exit(1) ;
   72         }
   73     sigset( SIGINT, sigINT ) ;
   74 
   75     strcpy( fmt, getenv("HOME") ) ; strcat( fmt, "/var/%04d-%02d-%02d.dat" ) ;
   76     store( 0, fmt ) ;
   77     memset( &prec, 0, sizeof( Stat ) ) ;
   78     get_stat() ; sleep(attente) ;
   79     memcpy( &prec, &actuel, sizeof( Stat ) ) ;
   80     for( nb=(3600/attente); !stop; nb++ )
   81         {
   82         StatEthCpu sec ; time_t t ; int out ;
   83         get_stat() ; time(&t) ;
   84 
   85         if ( nb == (3600/attente) )
   86             { syslog( LOG_INFO, "OK (%s)", fichier ) ; nb=0; }
   87 
   88         sec.t = t ;
   89         difference( &(sec.eth0), actuel.eth0, prec.eth0 ) ;
   90         difference( &(sec.eth1), actuel.eth1, prec.eth1 ) ;
   91         sec.cpu = pourcentage( actuel.cpu, prec.cpu ) ;
   92         store( &sec, fmt ) ;
   93 
   94         memcpy( &prec, &actuel, sizeof( Stat ) ) ;
   95         sleep( attente ) ;
   96         }
   97     store( 0, fmt ) ;
   98     return 0 ;
   99     }
  100 
  101 void get_stat()
  102     {
  103     char        interface   [16]    ;
  104     char        line        [1024]  ;
  105     char    *   pt                  ;
  106     uint64_t    rb, rp, re, rd, rf, rfr, rc, rm ;
  107     uint64_t    tb, tp, te, td, tf, tcol, tca, tc ;
  108     uint64_t    f0, f1, f2, f3, f4, f5, f6, t, u ;
  109 
  110     memcpy( &actuel, &prec, sizeof( Stat ) ) ;
  111     FILE* in = fopen("/proc/net/dev", "r");
  112     while ( 1 )
  113         {
  114         char* ret = fgets( line, 1024, in ) ;
  115         if ( !ret ) break ;
  116         for( pt=line;*pt;pt++ ) if ( *pt==':' ) *pt=' ';
  117         for( pt=line;*pt==' ';pt++ ) ;
  118         sscanf( pt,
  119           "%s %llu %llu %llu %llu %llu %llu %llu %llu"
  120              "%llu %llu %llu %llu %llu %llu %llu %llu",
  121             interface,
  122             &rb, &rp, &re, &rd, &rf, &rfr, &rc, &rm,
  123             &tb, &tp, &te, &td, &tf, &tcol, &tca, &tc  );
  124         if ( strcmp( interface, "eth0" ) == 0 )
  125             affecte( &(actuel.eth0), rb, rp, tb, tp ) ;
  126         if ( strcmp( interface, "eth1" ) == 0 )
  127             affecte( &(actuel.eth1), rb, rp, tb, tp ) ;
  128         }
  129     fclose( in ) ;
  130     in = fopen("/proc/stat", "r");
  131     fscanf( in, "%*s %llu %llu %llu %llu %llu %llu %llu",
  132         &f0, &f1, &f2, &f3, &f4, &f5, &f6);
  133     fclose( in ) ;
  134     t = f0 + f1 + f2 + f3 + f4 + f5 + f6 ;
  135     u = t - f3 ;
  136     actuel.cpu.use = u ; actuel.cpu.total = t ;
  137     }
  138 
  139 void affecte( StatInter* si, uint64_t rb, uint64_t rp, uint64_t tb,
  140         uint64_t tp )
  141     {
  142     si->recv_bytes = rb ; si->recv_pack = rp ;
  143     si->trns_bytes = tb ; si->trns_pack = tp ;
  144     }
  145 
  146 uint64_t U64diff( uint64_t a, uint64_t b )
  147     {
  148     uint64_t max, res ;
  149     if ( a >= b ) return a - b ;
  150     max = 0 ; max-- ;
  151     res = max - b ;
  152     res = res + a + 1 ;
  153     return res ;
  154     }
  155 void difference( StatInter* res, StatInter a, StatInter p )
  156     {
  157     res->recv_bytes = U64diff( a.recv_bytes, p.recv_bytes ) ;
  158     res->recv_pack  = U64diff( a.recv_pack , p.recv_pack  ) ;
  159     res->trns_bytes = U64diff( a.trns_bytes, p.trns_bytes ) ;
  160     res->trns_pack  = U64diff( a.trns_pack , p.trns_pack  ) ;
  161     }
  162 
  163 int pourcentage( StatCpu a, StatCpu p )
  164     {
  165     double tt = (double)( U64diff(a.total, p.total) ) ;
  166     double tu = (double)( U64diff(a.use, p.use) ) ;
  167     double use = tu / tt * 100 ;
  168     return (int) use ;
  169     }
  170 
  171 void store( StatEthCpu* sec, char* fmt )
  172     {
  173     struct tm * lt              ;
  174     StatEthCpu  zero            ;
  175     time_t      t               ;
  176     int         out             ;
  177 
  178     time(&t) ; lt = localtime( &t ) ;
  179     sprintf( fichier, fmt, lt->tm_year+1900, lt->tm_mon+1, lt->tm_mday ) ;
  180 
  181     if (!sec)
  182         {
  183         memset( &zero, 0, sizeof( StatEthCpu ) ) ;
  184         zero.t = t ;
  185         sec = &zero ;
  186         }
  187 
  188     out = open( fichier, O_WRONLY|O_CREAT|O_APPEND, 0640 ) ;
  189     lockf( out, F_LOCK, 0 ) ;
  190     write( out, sec, sizeof( StatEthCpu ) ) ;
  191     lockf( out, F_ULOCK, 0 ) ;
  192     close( out ) ;
  193     }
  194 
Makefile firewall.h main.c daemon.c rc.firewall_col
firewall_col~~ / STAT / COL / main.c

Haut de page

Contacter crabs

Date de génération : 22/09/2011 21:48