Inizializzazione
This commit is contained in:
2
script/.htaccess
Normal file
2
script/.htaccess
Normal file
@@ -0,0 +1,2 @@
|
||||
Order deny,allow
|
||||
Deny from all
|
||||
216
script/check_ip.pl
Executable file
216
script/check_ip.pl
Executable file
@@ -0,0 +1,216 @@
|
||||
#!/usr/bin/perl
|
||||
$|=1;
|
||||
|
||||
sub check_ip {
|
||||
$OID_ifTable = '1.3.6.1.2.1.4.20.1.1';
|
||||
my $comunity = "esseweb";
|
||||
|
||||
%tabella;
|
||||
|
||||
$risultato = "result";
|
||||
|
||||
my $fw_query = "SELECT id, nome FROM firewall WHERE attivo = 1";
|
||||
my $fw_sts = $dbmysql->prepare($fw_query);
|
||||
$fw_sts->execute ();
|
||||
|
||||
while (my $fw_dato = $fw_sts->fetchrow_hashref) {
|
||||
my $fw_id = $$fw_dato{'id'};
|
||||
my $fw_nome = $$fw_dato{'nome'};
|
||||
|
||||
$nomeres = $risultato.$fw_id;
|
||||
|
||||
my ($sessione, $errore) = Net::SNMP->session(
|
||||
-hostname => shift || $fw_nome,
|
||||
-community => shift || $comunity,
|
||||
-nonblocking => 1,
|
||||
-translate => [-octetstring => 0],
|
||||
-version => 'snmpv2c',
|
||||
);
|
||||
|
||||
if (!defined $sessione) {
|
||||
printf "ERROR: %s.\n", $errore;
|
||||
return 0;
|
||||
}
|
||||
|
||||
my $nomehash = "temphash$fw_id";
|
||||
%$nomehash;
|
||||
|
||||
$$nomeres = $sessione->get_bulk_request(
|
||||
-varbindlist => [ $OID_ifTable ],
|
||||
-callback => [ \&table_callback, \%$nomehash ],
|
||||
-maxrepetitions => 10,
|
||||
);
|
||||
|
||||
if (!defined $$nomeres) {
|
||||
printf "ERROR: %s \n", $sessione->error();
|
||||
$sessione->close();
|
||||
return 0;
|
||||
}
|
||||
|
||||
snmp_dispatcher();
|
||||
$sessione->close();
|
||||
$tabella{$fw_id} = \%$nomehash;
|
||||
my %hasttmp = \%$nomehash;
|
||||
}
|
||||
|
||||
my $ip_query = "SELECT id, ip, idfirewall, attuale, errore FROM indirizzi WHERE attivo = 1 AND vf = 0";
|
||||
my $ip_sts = $dbmysql->prepare($ip_query);
|
||||
$ip_sts->execute ();
|
||||
|
||||
my @query;
|
||||
my @oggetti;
|
||||
my @testi;
|
||||
|
||||
while (my $ip_dato = $ip_sts->fetchrow_hashref) {
|
||||
my $ip_id = $$ip_dato{'id'};
|
||||
my $ip_ip = $$ip_dato{'ip'};
|
||||
my $ip_idfirewall = $$ip_dato{'idfirewall'};
|
||||
my $ip_attuale = $$ip_dato{'attuale'};
|
||||
my $ip_errore = $$ip_dato{'errore'};
|
||||
|
||||
my @listafw;
|
||||
while (($fwid, %tabellaip) = each(%tabella)){
|
||||
my $oid = "1.3.6.1.2.1.4.20.1.1.$ip_ip";
|
||||
if (defined $tabella{$fwid}{$oid} ) {
|
||||
push @listafw, $fwid;
|
||||
}
|
||||
}
|
||||
|
||||
if (scalar(@listafw) == 1) {
|
||||
if ($listafw[0] != $ip_attuale) {
|
||||
#print "L'IP $ip_ip e' migrato dal FW $ip_attuale al FW $listafw[0]\n";
|
||||
if ($listafw[0] != $ip_idfirewall) {
|
||||
push @query, "UPDATE indirizzi SET attuale = ".$listafw[0].", errore=1 WHERE id = $ip_id";
|
||||
$testo = "L\\'IP $ip_ip non e\\' sul FW corretto ed e\\' migrato sul FW ".$listafw[0];
|
||||
} else {
|
||||
push @query, "UPDATE indirizzi SET attuale = ".$listafw[0].", errore=0 WHERE id = $ip_id";
|
||||
$testo = "L\\'IP $ip_ip e\\' migrato sul FW corretto ".$listafw[0];
|
||||
}
|
||||
push @oggetti, "Firewall Warning"; push @testi, $testo;
|
||||
push @query, "INSERT INTO log_server (fw, data, idazione, stato, testo) VALUES (".$listafw[0].", NOW(), 0, 1, '$testo')";
|
||||
}
|
||||
} elsif ((scalar(@listafw) > 1) && ($ip_errore < 2)) {
|
||||
push @query, "UPDATE indirizzi SET attuale=0, errore=2 WHERE id = $ip_id";
|
||||
push @query, "INSERT INTO log_server (fw, data, idazione, stato, testo) VALUES (0, NOW(), 0, 2, 'L\\'IP $ip_ip e\\' presente su piu\\' firewall')";
|
||||
push @oggetti, "Firewall Error"; push @testi, "L'IP $ip_ip e' presente su piu' firewall";
|
||||
} elsif ((scalar(@listafw) == 0) && ($ip_errore < 2)) {
|
||||
push @query, "UPDATE indirizzi SET attuale=0, errore=2 WHERE id = $ip_id";
|
||||
push @query, "INSERT INTO log_server (fw, data, idazione, stato, testo) VALUES (0, NOW(), 0, 2, 'L\\'IP $ip_ip non e\\' presente su nessun firewall')";
|
||||
push @oggetti, "Firewall Error"; push @testi, "L'IP $ip_ip non e' presente su nessun firewall";
|
||||
}
|
||||
}
|
||||
|
||||
while (my $invia = pop @query) {
|
||||
#print "$invia\n";
|
||||
my $ip_sts = $dbmysql->prepare($invia);
|
||||
$ip_sts->execute ();
|
||||
}
|
||||
|
||||
while (my $subject = pop @oggetti) {
|
||||
my $body = pop @testi;
|
||||
#print "$subject -- $body\n";
|
||||
invio ($subject, $body);
|
||||
}
|
||||
}
|
||||
|
||||
sub table_callback {
|
||||
my ($session, $table) = @_;
|
||||
my $list = $session->var_bind_list();
|
||||
|
||||
if (!defined $list) {
|
||||
printf "ERROR: %s\n", $session->error();
|
||||
return;
|
||||
}
|
||||
|
||||
my @names = $session->var_bind_names();
|
||||
my $next = undef;
|
||||
|
||||
while (@names) {
|
||||
$next = shift @names;
|
||||
if (!oid_base_match($OID_ifTable, $next)) {
|
||||
return; # Table is done.
|
||||
}
|
||||
$table->{$next} = $list->{$next};
|
||||
}
|
||||
|
||||
my $result = $session->get_bulk_request(
|
||||
-varbindlist => [ $next ],
|
||||
-maxrepetitions => 10,
|
||||
);
|
||||
|
||||
if (!defined $result) {
|
||||
printf "ERROR: %s.\n", $session->error();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
sub invio {
|
||||
my ($oggetto, $testo) = @_;
|
||||
|
||||
my $mittente = 'firewall@esseweb.eu';
|
||||
my @destinatario;
|
||||
push @destinatario, 'cmaffioletti@esseweb.eu';
|
||||
push @destinatario, 'fmeini@esseweb.eu';
|
||||
|
||||
my $sasl = Authen::SASL->new(
|
||||
mechanism => 'CRAM-MD5 PLAIN ANONYMOUS',
|
||||
callback => {
|
||||
pass => 'l30nard0',
|
||||
user => 'cmaffio@bmm.it',
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
my $smtp;
|
||||
if (not $smtp = Net::SMTP::SSL->new (
|
||||
'smtp.mi.esseweb.intra',
|
||||
Port => 465,
|
||||
Debug => 0))
|
||||
{
|
||||
die "Could not connect to server\n";
|
||||
}
|
||||
|
||||
$smtp->auth($sasl) || die "Authentication failed!\n";
|
||||
|
||||
$smtp->mail($mittente . "\n");
|
||||
my $to = "";
|
||||
foreach my $recp (@destinatario) {
|
||||
$smtp->to($recp . "\n");
|
||||
$to .= "$recp, ";
|
||||
}
|
||||
|
||||
$smtp->data();
|
||||
$smtp->datasend("From: " . $mittente . "\n");
|
||||
$smtp->datasend("To: " . $to . "\n");
|
||||
$smtp->datasend("Subject: " . $oggetto . "\n");
|
||||
$smtp->datasend("Date: " . date_r() . "\n");
|
||||
$smtp->datasend("\n");
|
||||
$smtp->datasend($testo . "\n");
|
||||
$smtp->dataend();
|
||||
$smtp->quit;
|
||||
|
||||
}
|
||||
|
||||
sub date_r {
|
||||
my ($day, $mon, $str);
|
||||
my (@lt) = ();
|
||||
|
||||
@lt = localtime();
|
||||
$day = $lt[6];
|
||||
$mon = $lt[4];
|
||||
|
||||
my @DAYS = ('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun');
|
||||
my @MON = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Lug', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
|
||||
|
||||
$str = $DAYS[$day] . ", " .
|
||||
sprintf ("%2d", $lt[3]) . " " .
|
||||
$MON[$mon] . " " .
|
||||
($lt[5]+1900)
|
||||
. " " . sprintf("%02d:%02d:%02d", $lt[2], $lt[1], $lt[0] )
|
||||
. " " . sprintf("%03d%02d", (tz_offset() / 3600), 0);
|
||||
|
||||
return $str;
|
||||
}
|
||||
|
||||
return 1
|
||||
276
script/check_traffico.pl
Executable file
276
script/check_traffico.pl
Executable file
@@ -0,0 +1,276 @@
|
||||
#!/usr/bin/perl
|
||||
$|=1;
|
||||
|
||||
use DBI;
|
||||
use Switch;
|
||||
use FindBin qw($Bin);
|
||||
use POSIX;
|
||||
use Net::SNMP qw(:snmp);
|
||||
use Net::SMTP;
|
||||
use Net::SMTP::SSL;
|
||||
use Authen::SASL;
|
||||
use Time::Zone;
|
||||
require "$Bin/check_ip.pl";
|
||||
|
||||
our $force = 0;
|
||||
while( $_ = shift @ARGV ) {
|
||||
$force = 1 if /^\-f$/;
|
||||
}
|
||||
|
||||
open CONFFILE, "< $Bin/../php/config.php" or die ("Manca file di configurazione\n");
|
||||
while (<CONFFILE>) {
|
||||
if(/^\$db_data_server = "(\S+)"/ ) {
|
||||
$db_host = $1;
|
||||
}
|
||||
if(/^\$db_data_name = "(\S+)"/ ) {
|
||||
$db_name = $1;
|
||||
}
|
||||
if(/^\$db_data_user = "(\S+)"/ ) {
|
||||
$db_user = $1;
|
||||
}
|
||||
if(/^\$db_data_pwd = "(\S+)"/ ) {
|
||||
$db_pass = $1;
|
||||
}
|
||||
}
|
||||
close CONFFILE;
|
||||
|
||||
my $sys_error = 0;
|
||||
if (!lockpid ("lock")) {
|
||||
print "Prg in esecuzione";
|
||||
exit 0;
|
||||
}
|
||||
|
||||
$dbmysql = DBI->connect("DBI:mysql:;host=$db_host", "$db_user", "$db_pass") or die ($DBI::errstr);
|
||||
$sts = $dbmysql->prepare("use $db_name");
|
||||
$sts->execute ();
|
||||
|
||||
my $conf_query = "SELECT variabile, valore FROM conf ORDER BY ordine";
|
||||
my $conf_sts = $dbmysql->prepare($conf_query);
|
||||
$conf_sts->execute ();
|
||||
%conf = ();
|
||||
while (my $conf_dato = $conf_sts->fetchrow_hashref) {
|
||||
$nomevar = $$conf_dato{'variabile'};
|
||||
$conf{$nomevar} = $$conf_dato{'valore'};
|
||||
}
|
||||
|
||||
#check_ip ();
|
||||
@tempo = localtime();
|
||||
dati_ora() if ($tempo[1] == 5);
|
||||
#dati_ora();
|
||||
dati_giorno() if ($tempo[1] == 10 && $tempo[2] == 0);
|
||||
dati_giorno() if ($force);;
|
||||
dati_settimana() if ($tempo[1] == 15 && $tempo[2] == 0 && $tempo[6] == 1);
|
||||
dati_mese() if ($tempo[1] == 20 && $tempo[2] == 0 && $tempo[3] == 0);
|
||||
|
||||
my $fw_query = "SELECT id, TIMESTAMPDIFF(MINUTE,ultimoconn, NOW()) AS tempo_conn, errore FROM firewall WHERE attivo = 1";
|
||||
my $fw_sts = $dbmysql->prepare($fw_query);
|
||||
$fw_sts->execute ();
|
||||
while (my $fw_dato = $fw_sts->fetchrow_hashref) {
|
||||
$fw_id = $$fw_dato{'id'};
|
||||
$fw_tempo = $$fw_dato{'tempo_conn'};
|
||||
$fw_errore = $$fw_dato{'errore'};
|
||||
if (($fw_errore < 2) && ($fw_tempo > $conf{'timeout_server'})) {
|
||||
my $fwup_query = "UPDATE firewall SET errore = 2, msg_err = 'Mancata connessione dal server' WHERE id = $fw_id";
|
||||
my $fwup_sts = $dbmysql->prepare($fwup_query);
|
||||
$fwup_sts->execute ();
|
||||
|
||||
my $login_query = "INSERT INTO log_server SET fw=$fw_id, idazione=0, data=NOW(), stato=2, testo='Mancata connessione dal server'";
|
||||
my $login_sts = $dbmysql->prepare($login_query);
|
||||
$login_sts->execute ();
|
||||
}
|
||||
}
|
||||
|
||||
my $noerr_query = "SELECT id, rate, ceil FROM defrule WHERE stato = 4";
|
||||
my $noerr_sts = $dbmysql->prepare($noerr_query);
|
||||
$noerr_sts->execute ();
|
||||
while (my $noerr_dato = $noerr_sts->fetchrow_hashref) {
|
||||
my $id = $$noerr_dato{'id'};
|
||||
my $rate = $$noerr_dato{'rate'};
|
||||
my $ceil = $$noerr_dato{'ceil'};
|
||||
if ($rate > 0 && $ceil >= $rate) {
|
||||
my $defrule_query = "UPDATE defrule SET stato = 0 WHERE id = $id";
|
||||
my $defrule_sts = $dbmysql->prepare($defrule_query);
|
||||
$defrule_sts->execute ();
|
||||
|
||||
$nomeregola = creanome ($id);
|
||||
my $login_query = "INSERT INTO log_server SET fw=0, idazione=0, data=NOW(), stato=0, testo='Configurazione regola $nomeregola corretta'";
|
||||
my $login_sts = $dbmysql->prepare($login_query);
|
||||
$login_sts->execute ();
|
||||
}
|
||||
}
|
||||
|
||||
my $err_query = "SELECT id, stato FROM `defrule` WHERE (rate = 0 OR ceil < rate) AND stato != 3";
|
||||
my $err_sts = $dbmysql->prepare($err_query);
|
||||
$err_sts->execute ();
|
||||
my $presenza_errori = 0;
|
||||
while (my $err_dato = $err_sts->fetchrow_hashref) {
|
||||
my $err_id = $$err_dato{'id'};
|
||||
my $err_stato = $$err_dato{'stato'};
|
||||
$presenza_errori = 1;
|
||||
if ($conf{'errore'} != 3) {
|
||||
my $conferr_query = "UPDATE conf SET valore = 3 WHERE variabile = 'errore'";
|
||||
my $confmsg_query = "UPDATE conf SET valore = 'Errore nei dati di sistema' WHERE variabile = 'errore_msg'";
|
||||
my $conferr_sts = $dbmysql->prepare($conferr_query);
|
||||
my $confmsg_sts = $dbmysql->prepare($confmsg_query);
|
||||
$conferr_sts->execute ();
|
||||
$confmsg_sts->execute ();
|
||||
}
|
||||
if ($err_stato != 4) {
|
||||
my $defrule_query = "UPDATE defrule SET stato = 4 WHERE id = $err_id";
|
||||
my $defrule_sts = $dbmysql->prepare($defrule_query);
|
||||
$defrule_sts->execute ();
|
||||
|
||||
$nomeregola = creanome ($err_id);
|
||||
my $login_query = "INSERT INTO log_server SET fw=0, idazione=0, data=NOW(), stato=4, testo='Configurazione regola $nomeregola errata'";
|
||||
my $login_sts = $dbmysql->prepare($login_query);
|
||||
$login_sts->execute ();
|
||||
}
|
||||
}
|
||||
|
||||
if (!$presenza_errori && $conf{'errore'} == 3) {
|
||||
my $conferr_query = "UPDATE conf SET valore = 0 WHERE variabile = 'errore'";
|
||||
my $confmsg_query = "UPDATE conf SET valore = '' WHERE variabile = 'errore_msg'";
|
||||
my $conferr_sts = $dbmysql->prepare($conferr_query);
|
||||
my $confmsg_sts = $dbmysql->prepare($confmsg_query);
|
||||
$conferr_sts->execute ();
|
||||
$confmsg_sts->execute ();
|
||||
}
|
||||
|
||||
|
||||
lockpid ("unlock");
|
||||
exit 0;
|
||||
|
||||
sub azione_conf {
|
||||
my $id = shift;
|
||||
my $azioniup_query = "UPDATE azioni SET dataexec= NOW() WHERE id = $id";
|
||||
my $azioniup_sts = $dbmysql->prepare($azioniup_query);
|
||||
$azioniup_sts->execute ();
|
||||
}
|
||||
|
||||
sub lockpid {
|
||||
my $op = shift;
|
||||
|
||||
my $pidfile = "/tmp/check_traffico.pid";
|
||||
if ($op eq "lock") {
|
||||
if (open PIDFILE, "< $pidfile") {
|
||||
read PIDFILE, $oldpid, 256;
|
||||
my $exists = kill 0, $oldpid;
|
||||
if ( $exists ) {
|
||||
return 0;
|
||||
} else {
|
||||
close PIDFILE;
|
||||
}
|
||||
} else {
|
||||
close PIDFILE;
|
||||
}
|
||||
open PIDFILE, "> $pidfile" || return 0;
|
||||
print PIDFILE $$;
|
||||
close PIDFILE;
|
||||
} elsif ($op eq "unlock") {
|
||||
unlink $pidfile;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
sub creanome {
|
||||
my $idparent = shift;
|
||||
|
||||
my $query = "SELECT interfacce.device, defrule.idparent, defrule.priorita FROM defrule JOIN interfacce ON defrule.idinterfacce = interfacce.id WHERE defrule.id = '$idparent'";
|
||||
my $sts = $dbmysql->prepare($query);
|
||||
$sts->execute ();
|
||||
my $dato = $sts->fetchrow_hashref;
|
||||
|
||||
if ($$dato{'idparent'} == 0) {
|
||||
$ritorno = $$dato{'device'}."-2:".$$dato{'priorita'};
|
||||
return $ritorno;
|
||||
} else {
|
||||
$ritorno = creanome($$dato{'idparent'}).":".$$dato{'priorita'};
|
||||
return $ritorno;
|
||||
}
|
||||
}
|
||||
|
||||
sub dati_ora {
|
||||
|
||||
my $query1 = "UPDATE dati_traffico SET h=1 WHERE dataunix BETWEEN UNIX_TIMESTAMP(DATE_ADD(CURDATE(), INTERVAL -1 DAY)) AND UNIX_TIMESTAMP(SUBSTR(NOW(), 1,13)) AND h<2";
|
||||
my $query2 = "INSERT INTO dati_ora (data,idfirewall,idinterfacce,iddefrule,max,media,deviazione,traffico) SELECT SUBSTR(data, 1,13), idfirewall, idinterfacce, iddefrule, MAX(rate), AVG(rate), STDDEV(rate), SUM(diff) FROM dati_traffico WHERE dataunix BETWEEN UNIX_TIMESTAMP(DATE_ADD(CURDATE(), INTERVAL -1 DAY)) AND UNIX_TIMESTAMP(SUBSTR(NOW(), 1,13)) AND h=1 GROUP BY idfirewall, idinterfacce, iddefrule, SUBSTR(data, 1,13)";
|
||||
my $query3 = "UPDATE dati_traffico SET h=2 WHERE dataunix BETWEEN UNIX_TIMESTAMP(DATE_ADD(CURDATE(), INTERVAL -1 DAY)) AND UNIX_TIMESTAMP(SUBSTR(NOW(), 1,13)) AND h=1";
|
||||
|
||||
my $sts1 = $dbmysql->prepare($query1);
|
||||
my $sts2 = $dbmysql->prepare($query2);
|
||||
my $sts3 = $dbmysql->prepare($query3);
|
||||
|
||||
$sts1->execute ();
|
||||
$sts2->execute ();
|
||||
$sts3->execute ();
|
||||
}
|
||||
|
||||
sub dati_giorno {
|
||||
my $giorno = sprintf "%4d%02d%02d",$tempo[5]+1900,$tempo[4]+1,$tempo[3];
|
||||
my $giornout = mktime(59,59,23,$tempo[3],$tempo[4],$tempo[5]);
|
||||
|
||||
my $giornodomut = $giornout + 86400;
|
||||
@tempodom = localtime($giornodomut);
|
||||
my $giornodom = sprintf "%4d%02d%02d",$tempodom[5]+1900,$tempodom[4]+1,$tempodom[3];
|
||||
|
||||
my $giornoel = $giornout - (86400 * $conf{'giorni'});
|
||||
@tempoel = localtime($giornoel);
|
||||
$giornoel = sprintf "%4d%02d%02d",$tempoel[5]+1900,$tempoel[4]+1,$tempoel[3];
|
||||
|
||||
my %tabelle = ('dati_traffico', 'dati_ora');
|
||||
|
||||
foreach $worktable (%tabelle) {
|
||||
my $query5 = "alter table $worktable DROP PARTITION p$giornoel";
|
||||
my $query7 = "alter table $worktable ADD PARTITION (PARTITION p$giornodom VALUES LESS THAN ($giornodomut))";
|
||||
|
||||
my $sts5 = $dbmysql->prepare($query5);
|
||||
my $sts7 = $dbmysql->prepare($query7);
|
||||
|
||||
$sts5->execute ();
|
||||
$sts7->execute ();
|
||||
}
|
||||
|
||||
my $query1 = "UPDATE dati_traffico SET g=1 WHERE dataunix BETWEEN UNIX_TIMESTAMP(DATE_ADD(CURDATE(), INTERVAL -1 DAY)) AND UNIX_TIMESTAMP(SUBSTR(NOW(), 1,10)) AND g<2";
|
||||
my $query2 = "INSERT INTO dati_giorno (data,idfirewall,idinterfacce,iddefrule,max,media,deviazione,traffico) SELECT SUBSTR(data, 1,10), idfirewall, idinterfacce, iddefrule, MAX(rate), AVG(rate), STDDEV(rate), SUM(diff) FROM dati_traffico WHERE dataunix BETWEEN UNIX_TIMESTAMP(DATE_ADD(CURDATE(), INTERVAL -1 DAY)) AND UNIX_TIMESTAMP(SUBSTR(NOW(), 1,10)) AND g=1 GROUP BY idfirewall, idinterfacce, iddefrule, SUBSTR(data, 1,10)";
|
||||
my $query3 = "UPDATE dati_traffico SET g=2 WHERE dataunix BETWEEN UNIX_TIMESTAMP(DATE_ADD(CURDATE(), INTERVAL -1 DAY)) AND UNIX_TIMESTAMP(SUBSTR(NOW(), 1,10)) AND g=1";
|
||||
|
||||
my $query9 = "OPTIMIZE TABLE dati_traffico";
|
||||
|
||||
my $sts1 = $dbmysql->prepare($query1);
|
||||
my $sts2 = $dbmysql->prepare($query2);
|
||||
my $sts3 = $dbmysql->prepare($query3);
|
||||
my $sts9 = $dbmysql->prepare($query9);
|
||||
|
||||
$sts1->execute ();
|
||||
$sts2->execute ();
|
||||
$sts3->execute ();
|
||||
$sts9->execute ();
|
||||
}
|
||||
|
||||
sub dati_settimana {
|
||||
my $query1 = "UPDATE dati_traffico SET s=1 WHERE WEEK(data) < WEEK(NOW()) OR YEAR(data) < YEAR(NOW()) AND s<2";
|
||||
my $query2 = "INSERT INTO dati_settimana (anno,settimana,idfirewall,idinterfacce,iddefrule,max,media,deviazione,traffico) SELECT YEAR(data), WEEK(data), idfirewall, idinterfacce, iddefrule, MAX(rate), AVG(rate), STDDEV(rate), SUM(diff) FROM dati_traffico WHERE s=1 GROUP BY idfirewall, idinterfacce, iddefrule, YEAR(data), WEEK(data)";
|
||||
my $query3 = "UPDATE dati_traffico SET s=2 WHERE s=1";
|
||||
|
||||
my $sts1 = $dbmysql->prepare($query1);
|
||||
my $sts2 = $dbmysql->prepare($query2);
|
||||
my $sts3 = $dbmysql->prepare($query3);
|
||||
|
||||
$sts1->execute ();
|
||||
$sts2->execute ();
|
||||
$sts3->execute ();
|
||||
}
|
||||
|
||||
sub dati_mese {
|
||||
my $query1 = "UPDATE dati_traffico SET m=1 WHERE MONTH(data) < MONT(NOW()) OR YEAR(data) < YEAR(NOW()) AND m<2";
|
||||
my $query2 = "INSERT INTO dati_mese (anno,mese,idfirewall,idinterfacce,iddefrule,max,media,deviazione,traffico) SELECT YEAR(data), MONTH(data), idfirewall, idinterfacce, iddefrule, MAX(rate), AVG(rate), STDDEV(rate), SUM(diff) FROM dati_traffico WHERE m=1 GROUP BY idfirewall, idinterfacce, iddefrule, YEAR(data), MONTH(data)";
|
||||
my $query3 = "UPDATE dati_traffico SET s=2 WHERE s=1";
|
||||
|
||||
my $sts1 = $dbmysql->prepare($query1);
|
||||
my $sts2 = $dbmysql->prepare($query2);
|
||||
my $sts3 = $dbmysql->prepare($query3);
|
||||
|
||||
$sts1->execute ();
|
||||
$sts2->execute ();
|
||||
$sts3->execute ();
|
||||
}
|
||||
|
||||
7
script/installaperl.sh
Executable file
7
script/installaperl.sh
Executable file
@@ -0,0 +1,7 @@
|
||||
#!/bin/sh
|
||||
|
||||
zypper in make perl-DBI perl-Switch perl-Net-SMTP-SSL perl-Authen-SASL perl-DBD-mysql
|
||||
|
||||
cpan install YAML
|
||||
cpan install CPAN
|
||||
cpan install Time::Zone
|
||||
63
script/partitioning.pl
Executable file
63
script/partitioning.pl
Executable file
@@ -0,0 +1,63 @@
|
||||
#!/usr/bin/perl
|
||||
$|=1;
|
||||
|
||||
use DBI;
|
||||
use Switch;
|
||||
use FindBin qw($Bin);
|
||||
use POSIX qw/strftime/;
|
||||
use Time::Local;
|
||||
|
||||
our $messaggi = 0;
|
||||
while( $_ = shift @ARGV ) {
|
||||
$messaggi = 1 if /^\-v$/;
|
||||
}
|
||||
|
||||
open CONFFILE, "< $Bin/../php/config.php" or die ("Manca file di configurazione\n");
|
||||
while (<CONFFILE>) {
|
||||
if(/^\$db_data_server = "(\S+)"/ ) {
|
||||
$db_host = $1;
|
||||
}
|
||||
if(/^\$db_data_name = "(\S+)"/ ) {
|
||||
$db_name = $1;
|
||||
}
|
||||
if(/^\$db_data_user = "(\S+)"/ ) {
|
||||
$db_user = $1;
|
||||
}
|
||||
if(/^\$db_data_pwd = "(\S+)"/ ) {
|
||||
$db_pass = $1;
|
||||
}
|
||||
}
|
||||
close CONFFILE;
|
||||
|
||||
printf "Inizio attivita' %s\n",strftime('%d-%m-%Y %H:%M:%S',localtime) if ($messaggi);
|
||||
my $dbmysql = DBI->connect("DBI:mysql:;host=$db_host", "$db_user", "$db_pass") or die ($DBI::errstr);
|
||||
$sts = $dbmysql->prepare("use $db_name");
|
||||
$sts->execute ();
|
||||
|
||||
$part = "ALTER TABLE dati_traffico PARTITION BY RANGE ( dataunix ) (\n";
|
||||
|
||||
#$query = "SELECT DATE_FORMAT(data, \"%Y%m%d\") AS giorno, DATE_FORMAT(data, \"%Y-%m-%d\") AS giorno2 FROM dati_traffico GROUP BY giorno ORDER BY giorno";
|
||||
$query = "SELECT DATE_FORMAT(data, \"%Y%m%d\") AS giorno, DATE_FORMAT(data, \"%Y\") AS a, DATE_FORMAT(data, \"%m\") AS m, DATE_FORMAT(data, \"%d\") AS g FROM dati_traffico GROUP BY giorno ORDER BY giorno";
|
||||
$sts = $dbmysql->prepare($query);
|
||||
$sts->execute ();
|
||||
while (my $dato = $sts->fetchrow_hashref) {
|
||||
my $giorno = $$dato{'giorno'};
|
||||
my $a = $$dato{'a'};
|
||||
my $m = $$dato{'m'};
|
||||
my $g = $$dato{'g'};
|
||||
# my $giorno2 = $$dato{'giorno2'};
|
||||
# print "$giorno\n";
|
||||
my $timest = timelocal (59, 59, 23, $g, $m-1, $a);
|
||||
# $part .= "PARTITION p$giorno VALUES LESS THAN ( UNIX_TIMESTAMP('$giorno2 23:59:59') ),\n";
|
||||
$part .= "PARTITION p$giorno VALUES LESS THAN ( $timest ),\n";
|
||||
}
|
||||
$part .= "PARTITION p99999999 VALUES LESS THAN (MAXVALUE)\n";
|
||||
$part .= ");\n";
|
||||
|
||||
print $part;
|
||||
|
||||
printf "Inizio partizionamento %s\n",strftime('%d-%m-%Y %H:%M:%S',localtime) if ($messaggi);
|
||||
$sts = $dbmysql->prepare($part);
|
||||
$sts->execute ();
|
||||
|
||||
printf "Termine attivita' %s\n",strftime('%d-%m-%Y %H:%M:%S',localtime) if ($messaggi);
|
||||
219
script/sw_traffico.sql
Normal file
219
script/sw_traffico.sql
Normal file
@@ -0,0 +1,219 @@
|
||||
-- phpMyAdmin SQL Dump
|
||||
-- version 3.4.3.1
|
||||
-- http://www.phpmyadmin.net
|
||||
--
|
||||
-- Host: db.mi.esseweb.intra
|
||||
-- Generato il: Feb 05, 2013 alle 16:07
|
||||
-- Versione del server: 5.1.46
|
||||
-- Versione PHP: 5.2.8
|
||||
|
||||
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
|
||||
SET time_zone = "+00:00";
|
||||
|
||||
|
||||
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
|
||||
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
|
||||
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
|
||||
/*!40101 SET NAMES utf8 */;
|
||||
|
||||
--
|
||||
-- Database: `sw_traffico`
|
||||
--
|
||||
|
||||
-- --------------------------------------------------------
|
||||
|
||||
--
|
||||
-- Struttura della tabella `accessi_utenti`
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `accessi_utenti`;
|
||||
CREATE TABLE IF NOT EXISTS `accessi_utenti` (
|
||||
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID operazione',
|
||||
`utente` varchar(256) NOT NULL COMMENT 'Nome utente',
|
||||
`quando` datetime NOT NULL COMMENT 'Data operazione',
|
||||
`ip` varchar(15) NOT NULL COMMENT 'IP connessione',
|
||||
`cosa` varchar(512) NOT NULL COMMENT 'operazione effettuata',
|
||||
`sessione` varchar(50) NOT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=88203 ;
|
||||
|
||||
-- --------------------------------------------------------
|
||||
|
||||
--
|
||||
-- Struttura della tabella `azioni`
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `azioni`;
|
||||
CREATE TABLE IF NOT EXISTS `azioni` (
|
||||
`id` bigint(20) NOT NULL AUTO_INCREMENT,
|
||||
`fw` int(11) NOT NULL,
|
||||
`tabella` varchar(16) COLLATE utf8_unicode_ci NOT NULL,
|
||||
`idtabella` bigint(20) NOT NULL,
|
||||
`stato` int(11) NOT NULL,
|
||||
`datains` datetime NOT NULL,
|
||||
`dataexec` datetime DEFAULT NULL,
|
||||
`ignora` int(11) NOT NULL DEFAULT '0',
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=2351 ;
|
||||
|
||||
-- --------------------------------------------------------
|
||||
|
||||
--
|
||||
-- Struttura della tabella `conf`
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `conf`;
|
||||
CREATE TABLE IF NOT EXISTS `conf` (
|
||||
`ordine` int(11) NOT NULL,
|
||||
`variabile` varchar(16) NOT NULL,
|
||||
`valore` varchar(254) NOT NULL,
|
||||
`Commento` varchar(256) DEFAULT NULL,
|
||||
`vis` int(11) NOT NULL DEFAULT '0',
|
||||
UNIQUE KEY `copia` (`variabile`,`valore`)
|
||||
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT='Configirazioni di default';
|
||||
|
||||
--
|
||||
-- Dump dei dati per la tabella `conf`
|
||||
--
|
||||
|
||||
INSERT INTO `conf` (`ordine`, `variabile`, `valore`, `Commento`, `vis`) VALUES(10, 'Versione', 'SpazioFirewall ver. 0.8.3', 'Versione DB', 1);
|
||||
INSERT INTO `conf` (`ordine`, `variabile`, `valore`, `Commento`, `vis`) VALUES(20, 'base_url', 'https://web.mi.esseweb.eu/firewall', 'Url di base su cui comporre le url', 1);
|
||||
INSERT INTO `conf` (`ordine`, `variabile`, `valore`, `Commento`, `vis`) VALUES(50, 'temposessione', '30', 'Tempo limite sessione in minuti', 1);
|
||||
INSERT INTO `conf` (`ordine`, `variabile`, `valore`, `Commento`, `vis`) VALUES(30, 'timeout_server', '5', 'Minuti dopo i quali un server viene considerato non funzionante', 1);
|
||||
INSERT INTO `conf` (`ordine`, `variabile`, `valore`, `Commento`, `vis`) VALUES(100, 'errore', '0', 'Indica la presenza di errori di sistema', 0);
|
||||
INSERT INTO `conf` (`ordine`, `variabile`, `valore`, `Commento`, `vis`) VALUES(100, 'errore_msg', '', '', 0);
|
||||
INSERT INTO `conf` (`ordine`, `variabile`, `valore`, `Commento`, `vis`) VALUES(40, 'giorni', '60', 'Numero di giorni memorizzati', 1);
|
||||
|
||||
-- --------------------------------------------------------
|
||||
|
||||
--
|
||||
-- Struttura della tabella `dati_giorno`
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `dati_giorno`;
|
||||
CREATE TABLE IF NOT EXISTS `dati_giorno` (
|
||||
`idfirewall` bigint(20) NOT NULL,
|
||||
`idinterfacce` bigint(20) NOT NULL,
|
||||
`iddefrule` bigint(20) NOT NULL,
|
||||
`max` decimal(20,3) NOT NULL,
|
||||
`media` decimal(20,3) NOT NULL,
|
||||
`deviazione` decimal(20,3) NOT NULL,
|
||||
`traffico` decimal(20,3) NOT NULL,
|
||||
`data` date NOT NULL,
|
||||
UNIQUE KEY `idfirewall` (`idfirewall`,`idinterfacce`,`data`,`iddefrule`)
|
||||
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
|
||||
|
||||
-- --------------------------------------------------------
|
||||
|
||||
--
|
||||
-- Struttura della tabella `dati_mese`
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `dati_mese`;
|
||||
CREATE TABLE IF NOT EXISTS `dati_mese` (
|
||||
`anno` int(11) NOT NULL,
|
||||
`mese` int(11) NOT NULL,
|
||||
`idfirewall` bigint(20) NOT NULL,
|
||||
`idinterfacce` bigint(20) NOT NULL,
|
||||
`iddefrule` bigint(20) NOT NULL,
|
||||
`max` decimal(20,3) NOT NULL,
|
||||
`media` decimal(20,3) NOT NULL,
|
||||
`deviazione` decimal(20,3) NOT NULL,
|
||||
`traffico` decimal(20,3) NOT NULL,
|
||||
UNIQUE KEY `idfirewall` (`idfirewall`,`idinterfacce`,`iddefrule`)
|
||||
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
|
||||
|
||||
-- --------------------------------------------------------
|
||||
|
||||
--
|
||||
-- Struttura della tabella `dati_ora`
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `dati_ora`;
|
||||
CREATE TABLE IF NOT EXISTS `dati_ora` (
|
||||
`idfirewall` bigint(20) NOT NULL,
|
||||
`idinterfacce` bigint(20) NOT NULL,
|
||||
`iddefrule` bigint(20) NOT NULL,
|
||||
`max` decimal(20,3) NOT NULL,
|
||||
`media` decimal(20,3) NOT NULL,
|
||||
`deviazione` decimal(20,3) NOT NULL,
|
||||
`traffico` decimal(20,3) NOT NULL,
|
||||
`data` datetime NOT NULL,
|
||||
`dataunix` bigint(20) NOT NULL DEFAULT '0',
|
||||
UNIQUE KEY `idfirewall` (`idfirewall`,`idinterfacce`,`data`,`iddefrule`,`dataunix`)
|
||||
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
|
||||
/*!50100 PARTITION BY RANGE ( dataunix)
|
||||
(PARTITION p20130129 VALUES LESS THAN (1359500399) ENGINE = MyISAM,
|
||||
PARTITION p20130130 VALUES LESS THAN (1359586799) ENGINE = MyISAM,
|
||||
PARTITION p20130131 VALUES LESS THAN (1359673199) ENGINE = MyISAM,
|
||||
PARTITION p20130201 VALUES LESS THAN (1359759599) ENGINE = MyISAM,
|
||||
PARTITION p20130202 VALUES LESS THAN (1359845999) ENGINE = MyISAM,
|
||||
PARTITION p20130203 VALUES LESS THAN (1359932399) ENGINE = MyISAM,
|
||||
PARTITION p20130204 VALUES LESS THAN (1360018799) ENGINE = MyISAM,
|
||||
PARTITION p20130205 VALUES LESS THAN (1360105199) ENGINE = MyISAM,
|
||||
PARTITION p20130206 VALUES LESS THAN (1360191599) ENGINE = MyISAM) */;
|
||||
|
||||
--
|
||||
-- Triggers `dati_ora`
|
||||
--
|
||||
DROP TRIGGER IF EXISTS `dati_ora_insert`;
|
||||
DELIMITER //
|
||||
CREATE TRIGGER `dati_ora_insert` BEFORE INSERT ON `dati_ora`
|
||||
FOR EACH ROW BEGIN
|
||||
SET NEW.dataunix = UNIX_TIMESTAMP(NEW.data);
|
||||
END
|
||||
//
|
||||
DELIMITER ;
|
||||
|
||||
-- --------------------------------------------------------
|
||||
|
||||
--
|
||||
-- Struttura della tabella `dati_settimana`
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `dati_settimana`;
|
||||
CREATE TABLE IF NOT EXISTS `dati_settimana` (
|
||||
`anno` int(11) NOT NULL,
|
||||
`settimana` int(11) NOT NULL,
|
||||
`idfirewall` bigint(20) NOT NULL,
|
||||
`idinterfacce` bigint(20) NOT NULL,
|
||||
`iddefrule` bigint(20) NOT NULL,
|
||||
`max` decimal(20,3) NOT NULL,
|
||||
`media` decimal(20,3) NOT NULL,
|
||||
`deviazione` decimal(20,3) NOT NULL,
|
||||
`traffico` decimal(20,3) NOT NULL,
|
||||
UNIQUE KEY `idfirewall` (`idfirewall`,`idinterfacce`,`iddefrule`)
|
||||
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
|
||||
|
||||
-- --------------------------------------------------------
|
||||
|
||||
--
|
||||
-- Struttura della tabella `dati_traffico`
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `dati_traffico`;
|
||||
CREATE TABLE IF NOT EXISTS `dati_traffico` (
|
||||
`id` bigint(20) NOT NULL AUTO_INCREMENT,
|
||||
`idfirewall` bigint(20) NOT NULL,
|
||||
`idinterfacce` bigint(20) NOT NULL,
|
||||
`iddefrule` bigint(20) NOT NULL,
|
||||
`sent` bigint(20) NOT NULL,
|
||||
`rate` bigint(20) NOT NULL,
|
||||
`data` datetime NOT NULL,
|
||||
`dataunix` bigint(20) NOT NULL DEFAULT '0',
|
||||
`calc` decimal(11,3) NOT NULL,
|
||||
`diff` float NOT NULL,
|
||||
`h` bit(2) NOT NULL DEFAULT b'0',
|
||||
`g` bit(2) NOT NULL DEFAULT b'0',
|
||||
`s` bit(2) NOT NULL DEFAULT b'0',
|
||||
`m` bit(2) NOT NULL DEFAULT b'0',
|
||||
PRIMARY KEY (`id`,`dataunix`),
|
||||
KEY `ricerca` (`idinterfacce`,`iddefrule`,`idfirewall`)
|
||||
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
|
||||
/*!50100 PARTITION BY RANGE ( dataunix)
|
||||
(PARTITION p20130202 VALUES LESS THAN (1359845999) ENGINE = MyISAM,
|
||||
PARTITION p20130203 VALUES LESS THAN (1359932399) ENGINE = MyISAM,
|
||||
PARTITION p20130204 VALUES LESS THAN (1360018799) ENGINE = MyISAM,
|
||||
PARTITION p20130205 VALUES LESS THAN (1360105199) ENGINE = MyISAM,
|
||||
PARTITION p20130206 VALUES LESS THAN (1360191599) ENGINE = MyISAM) */ AUTO_INCREMENT=47722790 ;
|
||||
|
||||
5
script/traffico.conf
Normal file
5
script/traffico.conf
Normal file
@@ -0,0 +1,5 @@
|
||||
$fw_name = "sole";
|
||||
$db_host = "localhost";
|
||||
$db_user = "traffico";
|
||||
$db_name = "traffico";
|
||||
$db_pass = "4BhxS4vTWSnVSDV8";
|
||||
469
script/traffico.pl
Executable file
469
script/traffico.pl
Executable file
@@ -0,0 +1,469 @@
|
||||
#!/usr/bin/perl
|
||||
$|=1;
|
||||
|
||||
use DBI;
|
||||
use Switch;
|
||||
use FindBin qw($Bin);
|
||||
use POSIX qw/strftime/;
|
||||
|
||||
our $messaggi = 0;
|
||||
while( $_ = shift @ARGV ) {
|
||||
$messaggi = 1 if /^\-v$/;
|
||||
}
|
||||
|
||||
open CONFFILE, "< $Bin/traffico.conf" or die ("Manca file di configurazione\n");
|
||||
while (<CONFFILE>) {
|
||||
if(/^\$fw_name = "(\S+)"/ ) {
|
||||
$fw_name = $1;
|
||||
}
|
||||
if(/^\$db_host = "(\S+)"/ ) {
|
||||
$db_host = $1;
|
||||
}
|
||||
if(/^\$db_user = "(\S+)"/ ) {
|
||||
$db_user = $1;
|
||||
}
|
||||
if(/^\$db_name = "(\S+)"/ ) {
|
||||
$db_name = $1;
|
||||
}
|
||||
if(/^\$db_pass = "(\S+)"/ ) {
|
||||
$db_pass = $1;
|
||||
}
|
||||
}
|
||||
close CONFFILE;
|
||||
|
||||
my $sys_error = 0;
|
||||
|
||||
if (!lockpid ("lock")) {
|
||||
print "Prg in esecuzione";
|
||||
exit 0;
|
||||
}
|
||||
|
||||
printf "Inizio attivita' %s\n",strftime('%d-%m-%Y %H:%M:%S',localtime) if ($messaggi);
|
||||
my $dbmysql = DBI->connect("DBI:mysql:;host=$db_host", "$db_user", "$db_pass") or die ($DBI::errstr);
|
||||
$sts = $dbmysql->prepare("use $db_name");
|
||||
$sts->execute ();
|
||||
|
||||
my $fw_query = "SELECT id, attivo, aggiornamenti, path, errore FROM firewall WHERE nome = '$fw_name'";
|
||||
my $fw_sts = $dbmysql->prepare($fw_query);
|
||||
$fw_sts->execute ();
|
||||
my $fw_dato = $fw_sts->fetchrow_hashref;
|
||||
my $fw_id = $$fw_dato{'id'};
|
||||
my $fw_aggiornamenti = $$fw_dato{'aggiornamenti'};
|
||||
my $fw_attivo = $$fw_dato{'attivo'};
|
||||
my $fw_path = $$fw_dato{'path'};
|
||||
my $fw_errore = $$fw_dato{'errore'};
|
||||
|
||||
if ($fw_errore == 2) {
|
||||
fw_update ($fw_id, 0, -1, 0, NULL);
|
||||
log_server($fw_id, 0, 0, 'Connessione ripristinata');
|
||||
}
|
||||
|
||||
@path_stat = stat ($fw_path);
|
||||
$permessi = sprintf "%04o", $path_stat[2] & 00700;
|
||||
if (!defined ($path_stat[2])) {
|
||||
$sys_error = 1;
|
||||
fw_update ($fw_id, 0, -1, 1, 'Path configurazioni non disponibile');
|
||||
log_server($fw_id, 0, 2, 'Path configurazioni non disponibile');
|
||||
}
|
||||
if (($permessi ne '0700') && (!$sys_error)) {
|
||||
$sys_error = 1;
|
||||
fw_update ($fw_id, 0, -1, 1, 'Path configurazioni non scrivibile');
|
||||
log_server($fw_id, 0, 2, 'Path configurazioni non scrivibile');
|
||||
}
|
||||
|
||||
if (!$sys_error) {
|
||||
fw_update ($fw_id, 0, -1, 0, NULL);
|
||||
}
|
||||
|
||||
if (($fw_aggiornamenti == 1) && $fw_attivo && !$sys_error) {
|
||||
printf "Inizio gestione aggiornamenti %s\n",strftime('%d-%m-%Y %H:%M:%S',localtime) if ($messaggi);
|
||||
fw_update ($fw_id, 0, 2, 0, NULL);
|
||||
log_server($fw_id, 0, 0, 'Rilevati aggiornamenti');
|
||||
|
||||
$azioni_query = "SELECT id, tabella, idtabella, stato FROM azioni WHERE fw = $fw_id AND dataexec IS NULL and ignora = 0 AND stato != 5 ORDER BY id";
|
||||
my $azioni_sts = $dbmysql->prepare($azioni_query);
|
||||
$azioni_sts->execute ();
|
||||
|
||||
my $file_ok = 1;
|
||||
while ((my $azioni_dato = $azioni_sts->fetchrow_hashref) && $file_ok) {
|
||||
my $azioni_id = $$azioni_dato{'id'};
|
||||
my $azioni_tabella = $$azioni_dato{'tabella'};
|
||||
my $azioni_idtabella = $$azioni_dato{'idtabella'};
|
||||
my $azioni_stato = $$azioni_dato{'stato'};
|
||||
|
||||
switch ($azioni_tabella) {
|
||||
case "interfacce" {
|
||||
if ($azioni_stato == 1 || $azioni_stato == 2) {
|
||||
my $interfacce_query = "SELECT device, descrizione, rate, ceil FROM interfacce WHERE id = $azioni_idtabella";
|
||||
my $interfacce_sts = $dbmysql->prepare($interfacce_query);
|
||||
$interfacce_sts->execute ();
|
||||
my $interfacce_dato = $interfacce_sts->fetchrow_hashref;
|
||||
my $nome_interfaccia = $$interfacce_dato{'device'};
|
||||
my $nomefile1 = "$fw_path/".$$interfacce_dato{'device'};
|
||||
my $nomefile2 = "$fw_path/".$$interfacce_dato{'device'}."-2.root";
|
||||
open (OUTFILE1, ">", $nomefile1);
|
||||
open (OUTFILE2, ">", $nomefile2);
|
||||
print OUTFILE1 "#".$$interfacce_dato{'descrizione'}."\n";
|
||||
print OUTFILE1 "DEFAULT=1000\n";
|
||||
print OUTFILE1 "R2Q=10\n";
|
||||
print OUTFILE2 "#".$$interfacce_dato{'descrizione'}."\n";
|
||||
print OUTFILE2 "RATE=".$$interfacce_dato{'rate'}."Mbit\n";
|
||||
print OUTFILE2 "CEIL=".$$interfacce_dato{'ceil'}."Mbit\n";
|
||||
print OUTFILE2 "BURST=15k\n";
|
||||
close OUTFILE1;
|
||||
close OUTFILE2;
|
||||
azione_conf ($azioni_id);
|
||||
log_server($fw_id, $azioni_id, 0, "Inserita interfaccia $nome_interfaccia") if ($azioni_stato == 1);
|
||||
log_server($fw_id, $azioni_id, 0, "Modificata interfaccia $nome_interfaccia") if ($azioni_stato == 2);
|
||||
} elsif ($azioni_stato == 3) {
|
||||
my $interfacce_query = "SELECT device FROM interfacce WHERE id = $azioni_idtabella";
|
||||
my $interfacce_sts = $dbmysql->prepare($interfacce_query);
|
||||
$interfacce_sts->execute ();
|
||||
my $interfacce_dato = $interfacce_sts->fetchrow_hashref;
|
||||
my $nomefile1 = "$fw_path/".$$interfacce_dato{'device'};
|
||||
my $nomefile2 = "$fw_path/".$$interfacce_dato{'device'}."-2.root";
|
||||
unlink ($nomefile1);
|
||||
unlink ($nomefile2);
|
||||
azione_conf ($azioni_id);
|
||||
log_server($fw_id, $azioni_id, 0, "Eliminata interfaccia $nome_interfaccia");
|
||||
} else {
|
||||
fw_update ($fw_id, 0, 0, 1, 'Richiesta azione non prevista');
|
||||
azione_conf ($azioni_id);
|
||||
log_server($fw_id, $azioni_id, 2, "Richiesta operazione $azioni_stato non esistente su device $nome_interfaccia");
|
||||
}
|
||||
}
|
||||
case "defrule" {
|
||||
if ($azioni_stato == 1 || $azioni_stato == 2) {
|
||||
my $defrule_query = "SELECT descrizione, priorita, rate, ceil FROM defrule WHERE id = $azioni_idtabella";
|
||||
my $defrule_sts = $dbmysql->prepare($defrule_query);
|
||||
$defrule_sts->execute ();
|
||||
my $defrule_dato = $defrule_sts->fetchrow_hashref;
|
||||
my $nomeregola = creanome ($azioni_idtabella);
|
||||
my $nomefile = "$fw_path/$nomeregola";
|
||||
open (OUTFILE, ">", $nomefile);
|
||||
print OUTFILE "#".$$defrule_dato{'descrizione'}."\n";
|
||||
print OUTFILE "RATE=".$$defrule_dato{'rate'}."Mbit\n";
|
||||
print OUTFILE "CEIL=".$$defrule_dato{'ceil'}."Mbit\n";
|
||||
print OUTFILE "BURST=15k\n";
|
||||
print OUTFILE "LEAF=sfq\n";
|
||||
my $rule_query = "SELECT ipin, portin, ipout, portout FROM rule WHERE iddefrule = $azioni_idtabella";
|
||||
my $rule_sts = $dbmysql->prepare($rule_query);
|
||||
$rule_sts->execute ();
|
||||
while (my $rule_dato = $rule_sts->fetchrow_hashref) {
|
||||
my $ipin = $$rule_dato{'ipin'};
|
||||
my $portin = $$rule_dato{'portin'};
|
||||
if ($portin == 0) {
|
||||
$portin = "";
|
||||
} else {
|
||||
$portin = ":$portin";
|
||||
}
|
||||
my $ipout = $$rule_dato{'ipout'};
|
||||
my $portout = $$rule_dato{'portout'};
|
||||
if ($portout == 0) {
|
||||
$portout = "";
|
||||
} else {
|
||||
$portout = ":$portout";
|
||||
}
|
||||
print OUTFILE "RULE=$ipin$portin,$ipout$portout\n";
|
||||
}
|
||||
print OUTFILE "PRIO=".$$defrule_dato{'priorita'}."\n";
|
||||
close OUTFILE;
|
||||
azione_conf ($azioni_id);
|
||||
log_server($fw_id, $azioni_id, 0, "Inserita regola $nomeregola") if ($azioni_stato == 1);
|
||||
log_server($fw_id, $azioni_id, 0, "Modificata regola $nomeregola") if ($azioni_stato == 2);
|
||||
} elsif ($azioni_stato == 3) {
|
||||
my $nomeregola = creanome ($azioni_idtabella);
|
||||
my $nomefile = "$fw_path/$nomeregola";
|
||||
unlink ($nomefile);
|
||||
azione_conf ($azioni_id);
|
||||
log_server($fw_id, $azioni_id, 0, "Eliminata regola $nomeregola");
|
||||
} else {
|
||||
my $nomeregola = creanome ($azioni_idtabella);
|
||||
fw_update ($fw_id, 0, 0, 1, 'Richiesta azione non prevista');
|
||||
azione_conf ($azioni_id);
|
||||
log_server($fw_id, $azioni_id, 2, "Richiesta operazione $azioni_stato non esistente su regola $nome_regola");
|
||||
}
|
||||
}
|
||||
case "RESET" {
|
||||
reset_file ($fw_path);
|
||||
azione_conf ($azioni_id);
|
||||
log_server($fw_id, $azioni_id, 0, "Richiesta operazione di RESET");
|
||||
}
|
||||
case "FINE APPLICA" {
|
||||
$file_ok = 0;
|
||||
azione_conf ($azioni_id);
|
||||
}
|
||||
else {
|
||||
}
|
||||
}
|
||||
}
|
||||
`/etc/init.d/htbinit restart`;
|
||||
fw_update ($fw_id, 1, 0, 0, NULL);
|
||||
printf "Termine gestione aggiornamenti %s\n",strftime('%d-%m-%Y %H:%M:%S',localtime) if ($messaggi);
|
||||
} elsif (($fw_aggiornamenti == 1) && $fw_attivo && $sys_error) {
|
||||
$azioni_query = "SELECT id, tabella FROM azioni WHERE fw = $fw_id AND dataexec IS NULL and ignora = 0 AND stato = 5";
|
||||
my $azioni_sts = $dbmysql->prepare($azioni_query);
|
||||
$azioni_sts->execute ();
|
||||
|
||||
my $azioni_ok = 1;
|
||||
while ((my $azioni_dato = $azioni_sts->fetchrow_hashref) && ($azioni_ok)) {
|
||||
my $azioni_id = $$azioni_dato{'id'};
|
||||
my $azioni_tabella = $$azioni_dato{'tabella'};
|
||||
|
||||
my $azioni_risop = 0;
|
||||
switch ($azioni_tabella) {
|
||||
case "CREADIR" {
|
||||
if (mkdir $fw_path, 0755) {
|
||||
fw_update ($fw_id, 1, 0, 0, NULL);
|
||||
azione_conf ($azioni_id);
|
||||
log_server($fw_id, $azioni_id, 0, "Creato path dati $fw_path");
|
||||
} else {
|
||||
fw_update ($fw_id, 1, 0, 1, $!);
|
||||
azione_conf ($azioni_id);
|
||||
$azioni_ok = 0;
|
||||
log_server($fw_id, $azioni_id, 2, "Non e' stato possibile creare il path dati $fw_path");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
estraidati ();
|
||||
|
||||
lockpid ("unlock");
|
||||
printf "Termine attivita' %s\n",strftime('%d-%m-%Y %H:%M:%S',localtime) if ($messaggi);
|
||||
exit 0;
|
||||
|
||||
sub azione_conf {
|
||||
my $id = shift;
|
||||
my $azioniup_query = "UPDATE azioni SET dataexec= NOW() WHERE id = $id";
|
||||
my $azioniup_sts = $dbmysql->prepare($azioniup_query);
|
||||
$azioniup_sts->execute ();
|
||||
}
|
||||
|
||||
sub fw_update {
|
||||
my $id = shift;
|
||||
my $syn = shift;
|
||||
my $agg = shift;
|
||||
my $err = shift;
|
||||
my $msg = shift;
|
||||
|
||||
if ($syn) {
|
||||
$syn = ", ultimosync = NOW()";
|
||||
} else {
|
||||
$syn = "";
|
||||
}
|
||||
|
||||
if ($agg == -1) {
|
||||
$agg = "";
|
||||
} else {
|
||||
$agg = ", aggiornamenti = $agg";
|
||||
}
|
||||
|
||||
my $fwup_query = "UPDATE firewall SET ultimoconn = NOW(), errore = $err, msg_err = '$msg' $syn $agg WHERE id = $id";
|
||||
my $fwup_sts = $dbmysql->prepare($fwup_query);
|
||||
$fwup_sts->execute ();
|
||||
}
|
||||
|
||||
sub reset_file {
|
||||
my $dir = shift;
|
||||
|
||||
opendir (DIR, $dir);
|
||||
my @files = grep { /^[^\.]/ } readdir(DIR);
|
||||
closedir DIR;
|
||||
|
||||
foreach $file (@files) {
|
||||
unlink ("$dir/$file");
|
||||
}
|
||||
}
|
||||
|
||||
sub creanome {
|
||||
my $idparent = shift;
|
||||
|
||||
my $query = "SELECT interfacce.device, defrule.idparent, defrule.priorita FROM defrule JOIN interfacce ON defrule.idinterfacce = interfacce.id WHERE defrule.id = '$idparent'";
|
||||
my $sts = $dbmysql->prepare($query);
|
||||
$sts->execute ();
|
||||
my $dato = $sts->fetchrow_hashref;
|
||||
|
||||
if ($$dato{'idparent'} == 0) {
|
||||
$ritorno = $$dato{'device'}."-2:".$$dato{'priorita'};
|
||||
return $ritorno;
|
||||
} else {
|
||||
$ritorno = creanome($$dato{'idparent'}).":".$$dato{'priorita'};
|
||||
return $ritorno;
|
||||
}
|
||||
}
|
||||
|
||||
sub log_server {
|
||||
my $fw = shift;
|
||||
my $idazione = shift;
|
||||
my $stato = shift;
|
||||
my $testo = shift;
|
||||
|
||||
my $query = "INSERT log_server SET fw = $fw, idazione = $idazione, data= NOW(), stato = $stato, testo = '$testo'";
|
||||
my $sts = $dbmysql->prepare($query);
|
||||
$sts->execute ();
|
||||
}
|
||||
|
||||
sub lockpid {
|
||||
my $op = shift;
|
||||
|
||||
my $pidfile = "/tmp/traffico.pid";
|
||||
if ($op eq "lock") {
|
||||
if (open PIDFILE, "< $pidfile") {
|
||||
read PIDFILE, $oldpid, 256;
|
||||
my $exists = kill 0, $oldpid;
|
||||
if ( $exists ) {
|
||||
return 0;
|
||||
} else {
|
||||
close PIDFILE;
|
||||
}
|
||||
} else {
|
||||
close PIDFILE;
|
||||
}
|
||||
open PIDFILE, "> $pidfile" || return 0;
|
||||
print PIDFILE $$;
|
||||
close PIDFILE;
|
||||
} elsif ($op eq "unlock") {
|
||||
unlink $pidfile;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
sub estraidati {
|
||||
my $class;
|
||||
my $classid;
|
||||
my $parent;
|
||||
my $rate;
|
||||
my $ceil;
|
||||
my $crate;
|
||||
my $sent;
|
||||
my $range;
|
||||
|
||||
$campi = "INSERT INTO dati_traffico (idfirewall, idinterfacce, iddefrule, sent, rate, data, calc, diff) VALUES ";
|
||||
$campi_tmp = "INSERT INTO dati_traffico_tmp (idfirewall, idinterfacce, iddefrule, sent, rate, data, calc, diff, dataunix) VALUES ";
|
||||
|
||||
printf "Inizio estrazione dati %s\n",strftime('%d-%m-%Y %H:%M:%S',localtime) if ($messaggi);
|
||||
my $query = "SELECT id, device FROM interfacce WHERE attivo = 1 AND stato != 3";
|
||||
my $sts = $dbmysql->prepare($query);
|
||||
$sts->execute ();
|
||||
while (my $dato = $sts->fetchrow_hashref) {
|
||||
my $device_id = $$dato{'id'};
|
||||
my $device = $$dato{'device'};
|
||||
my $outcompleto;
|
||||
my @outriga;
|
||||
|
||||
my $contarighe = 0;
|
||||
my @righequery;
|
||||
|
||||
$outcompleto = qx/\/usr\/sbin\/tc -s class show dev $device/;
|
||||
@outriga = split /\n/, $outcompleto;
|
||||
|
||||
for ($i=0;$i< scalar(@outriga);$i++) {
|
||||
my $rigaatt = $outriga[$i];
|
||||
if($rigaatt =~ /^ lended: / ) {
|
||||
$range="$rate $ceil";
|
||||
$crate=sprintf "%8.1lf",$crate;
|
||||
$sent=sprintf "%10.0lf",$sent/1024;
|
||||
$$device{$classid}[0]=$crate if (($conta eq 0) || ($crate > $$device{$classid}[0]));
|
||||
if ($conta eq 0) {
|
||||
$$device{$classid}[4]=$crate;
|
||||
$$device{$classid}[5]=$sent;
|
||||
} else {
|
||||
$$device{$classid}[4] += $crate;
|
||||
}
|
||||
$$device{$classid}[6]=$sent;
|
||||
$$device{$classid}[1]=$crate;
|
||||
$$device{$classid}[2]=$ceil;
|
||||
|
||||
my @pezzi = split ':', $classid;
|
||||
my $classe = $pezzi[1];
|
||||
my $defrule_id;
|
||||
if ($classe == 2) {
|
||||
$defrule_id = 0;
|
||||
} else {
|
||||
my $pri_query = "SELECT id FROM defrule WHERE idinterfacce = $device_id AND priorita = $classe AND attivo = 1 AND stato != 3 ";
|
||||
my $pri_sts = $dbmysql->prepare($pri_query);
|
||||
$pri_sts->execute ();
|
||||
my $pri_dato = $pri_sts->fetchrow_hashref;
|
||||
$defrule_id = $$pri_dato{'id'};
|
||||
}
|
||||
printf "Inizio SELECT iddefrule $defrule_id %s\n",strftime('%d-%m-%Y %H:%M:%S',localtime) if ($messaggi);
|
||||
my $prec_query = "SELECT ($sent-sent) AS inviati, (UNIX_TIMESTAMP(NOW()) - dataunix) AS tempo FROM dati_traffico_tmp WHERE id = (SELECT MAX(id) FROM dati_traffico_tmp WHERE idfirewall = $fw_id AND idinterfacce = $device_id AND iddefrule = $defrule_id)";
|
||||
my $prec_sts = $dbmysql->prepare($prec_query);
|
||||
$prec_sts->execute ();
|
||||
printf "Fine SELECT iddefrule $defrule_id %s\n",strftime('%d-%m-%Y %H:%M:%S',localtime) if ($messaggi);
|
||||
my $media;
|
||||
my $tempo;
|
||||
my $inviati;
|
||||
if (my $prec_dato = $prec_sts->fetchrow_hashref) {
|
||||
$tempo = $$prec_dato{'tempo'};
|
||||
$inviati = $$prec_dato{'inviati'};
|
||||
$inviati = $sent if ($inviati < 0);
|
||||
if ($tempo > 0) {
|
||||
$media = $inviati/$tempo;
|
||||
} else {
|
||||
$media = 0;
|
||||
}
|
||||
} else {
|
||||
$media = 0;
|
||||
$inviati = 0;
|
||||
}
|
||||
$righequery[$contarighe++] = "($fw_id, $device_id, $defrule_id, $sent, $crate, NOW(), $media, $inviati";
|
||||
next;
|
||||
}
|
||||
if($rigaatt =~ /^ Sent (\d+) bytes /) {
|
||||
$sent=$1;
|
||||
next;
|
||||
}
|
||||
if($rigaatt =~ /^ rate (\S+)(bit|bps) /) {
|
||||
# print "BPS rate!\n" if $2 eq "bps";
|
||||
($crate=$1);
|
||||
$crate=~s/K/*1024/;
|
||||
$crate=~s/M/*1024*1024/;
|
||||
$crate=eval $crate;
|
||||
next;
|
||||
}
|
||||
if($rigaatt =~ /^class (\S+) (\S+:\S+) (root|parent (\S+:\S+)) .*rate (\S+)bit ceil (\S+)bit/) {
|
||||
$class=$1;
|
||||
$classid=$2;
|
||||
$rate=$5;
|
||||
$ceil=$6;
|
||||
($parent=$3)=~s/parent //;
|
||||
printf "Inizio elaborazione classe $classid %s\n",strftime('%d-%m-%Y %H:%M:%S',localtime) if ($messaggi);
|
||||
|
||||
$rate=~s/K/*1024/;
|
||||
$rate=~s/M/*1024*1024/;
|
||||
$rate=eval $rate;
|
||||
$rate/=1024;
|
||||
|
||||
$ceil=~s/K/*1024/;
|
||||
$ceil=~s/M/*1024*1024/;
|
||||
$ceil=eval $ceil;
|
||||
$ceil/=1024;
|
||||
next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
my $querycrt = "$campi ".$righequery[0].")";
|
||||
my $querytmp = "$campi_tmp ".$righequery[0].", UNIX_TIMESTAMP(NOW()))";
|
||||
for (my $i=1;$i<scalar(@righequery);$i++) {
|
||||
$querycrt .= ", ".$righequery[$i].")";
|
||||
$querytmp .= ", ".$righequery[$i].", UNIX_TIMESTAMP(NOW()))";
|
||||
}
|
||||
# print "$querycrt\n";
|
||||
# print "$querytmp\n";
|
||||
my $ins_sts = $dbmysql->prepare($querycrt);
|
||||
$ins_sts->execute ();
|
||||
$ins_sts = $dbmysql->prepare($querytmp);
|
||||
$ins_sts->execute ();
|
||||
|
||||
printf "Termine estrazione dati %s\n",strftime('%d-%m-%Y %H:%M:%S',localtime) if ($messaggi);
|
||||
}
|
||||
my $querydel = "DELETE FROM dati_traffico_tmp WHERE data < DATE_SUB(now(), INTERVAL 60 MINUTE)";
|
||||
my $ins_sts = $dbmysql->prepare($querydel);
|
||||
$ins_sts->execute ();
|
||||
|
||||
}
|
||||
|
||||
447
script/traffico.pl.20120525
Executable file
447
script/traffico.pl.20120525
Executable file
@@ -0,0 +1,447 @@
|
||||
#!/usr/bin/perl
|
||||
$|=1;
|
||||
|
||||
use DBI;
|
||||
use Switch;
|
||||
use FindBin qw($Bin);
|
||||
use POSIX qw/strftime/;
|
||||
|
||||
our $messaggi = 0;
|
||||
while( $_ = shift @ARGV ) {
|
||||
$messaggi = 1 if /^\-v$/;
|
||||
}
|
||||
|
||||
open CONFFILE, "< $Bin/traffico.conf" or die ("Manca file di configurazione\n");
|
||||
while (<CONFFILE>) {
|
||||
if(/^\$fw_name = "(\S+)"/ ) {
|
||||
$fw_name = $1;
|
||||
}
|
||||
if(/^\$db_host = "(\S+)"/ ) {
|
||||
$db_host = $1;
|
||||
}
|
||||
if(/^\$db_user = "(\S+)"/ ) {
|
||||
$db_user = $1;
|
||||
}
|
||||
if(/^\$db_name = "(\S+)"/ ) {
|
||||
$db_name = $1;
|
||||
}
|
||||
if(/^\$db_pass = "(\S+)"/ ) {
|
||||
$db_pass = $1;
|
||||
}
|
||||
}
|
||||
close CONFFILE;
|
||||
|
||||
my $sys_error = 0;
|
||||
|
||||
if (!lockpid ("lock")) {
|
||||
print "Prg in esecuzione";
|
||||
exit 0;
|
||||
}
|
||||
|
||||
printf "Inizio attivita' %s\n",strftime('%d-%m-%Y %H:%M:%S',localtime) if ($messaggi);
|
||||
my $dbmysql = DBI->connect("DBI:mysql:;host=$db_host", "$db_user", "$db_pass") or die ($DBI::errstr);
|
||||
$sts = $dbmysql->prepare("use $db_name");
|
||||
$sts->execute ();
|
||||
|
||||
my $fw_query = "SELECT id, attivo, aggiornamenti, path, errore FROM firewall WHERE nome = '$fw_name'";
|
||||
my $fw_sts = $dbmysql->prepare($fw_query);
|
||||
$fw_sts->execute ();
|
||||
my $fw_dato = $fw_sts->fetchrow_hashref;
|
||||
my $fw_id = $$fw_dato{'id'};
|
||||
my $fw_aggiornamenti = $$fw_dato{'aggiornamenti'};
|
||||
my $fw_attivo = $$fw_dato{'attivo'};
|
||||
my $fw_path = $$fw_dato{'path'};
|
||||
my $fw_errore = $$fw_dato{'errore'};
|
||||
|
||||
if ($fw_errore == 2) {
|
||||
fw_update ($fw_id, 0, -1, 0, NULL);
|
||||
log_server($fw_id, 0, 0, 'Connessione ripristinata');
|
||||
}
|
||||
|
||||
@path_stat = stat ($fw_path);
|
||||
$permessi = sprintf "%04o", $path_stat[2] & 00700;
|
||||
if (!defined ($path_stat[2])) {
|
||||
$sys_error = 1;
|
||||
fw_update ($fw_id, 0, -1, 1, 'Path configurazioni non disponibile');
|
||||
log_server($fw_id, 0, 2, 'Path configurazioni non disponibile');
|
||||
}
|
||||
if (($permessi ne '0700') && (!$sys_error)) {
|
||||
$sys_error = 1;
|
||||
fw_update ($fw_id, 0, -1, 1, 'Path configurazioni non scrivibile');
|
||||
log_server($fw_id, 0, 2, 'Path configurazioni non scrivibile');
|
||||
}
|
||||
|
||||
if (!$sys_error) {
|
||||
fw_update ($fw_id, 0, -1, 0, NULL);
|
||||
}
|
||||
|
||||
if (($fw_aggiornamenti == 1) && $fw_attivo && !$sys_error) {
|
||||
printf "Inizio gestione aggiornamenti %s\n",strftime('%d-%m-%Y %H:%M:%S',localtime) if ($messaggi);
|
||||
fw_update ($fw_id, 0, 2, 0, NULL);
|
||||
log_server($fw_id, 0, 0, 'Rilevati aggiornamenti');
|
||||
|
||||
$azioni_query = "SELECT id, tabella, idtabella, stato FROM azioni WHERE fw = $fw_id AND dataexec IS NULL and ignora = 0 AND stato != 5 ORDER BY id";
|
||||
my $azioni_sts = $dbmysql->prepare($azioni_query);
|
||||
$azioni_sts->execute ();
|
||||
|
||||
my $file_ok = 1;
|
||||
while ((my $azioni_dato = $azioni_sts->fetchrow_hashref) && $file_ok) {
|
||||
my $azioni_id = $$azioni_dato{'id'};
|
||||
my $azioni_tabella = $$azioni_dato{'tabella'};
|
||||
my $azioni_idtabella = $$azioni_dato{'idtabella'};
|
||||
my $azioni_stato = $$azioni_dato{'stato'};
|
||||
|
||||
switch ($azioni_tabella) {
|
||||
case "interfacce" {
|
||||
if ($azioni_stato == 1 || $azioni_stato == 2) {
|
||||
my $interfacce_query = "SELECT device, descrizione, rate, ceil FROM interfacce WHERE id = $azioni_idtabella";
|
||||
my $interfacce_sts = $dbmysql->prepare($interfacce_query);
|
||||
$interfacce_sts->execute ();
|
||||
my $interfacce_dato = $interfacce_sts->fetchrow_hashref;
|
||||
my $nome_interfaccia = $$interfacce_dato{'device'};
|
||||
my $nomefile1 = "$fw_path/".$$interfacce_dato{'device'};
|
||||
my $nomefile2 = "$fw_path/".$$interfacce_dato{'device'}."-2.root";
|
||||
open (OUTFILE1, ">", $nomefile1);
|
||||
open (OUTFILE2, ">", $nomefile2);
|
||||
print OUTFILE1 "#".$$interfacce_dato{'descrizione'}."\n";
|
||||
print OUTFILE1 "DEFAULT=1000\n";
|
||||
print OUTFILE1 "R2Q=10\n";
|
||||
print OUTFILE2 "#".$$interfacce_dato{'descrizione'}."\n";
|
||||
print OUTFILE2 "RATE=".$$interfacce_dato{'rate'}."Mbit\n";
|
||||
print OUTFILE2 "CEIL=".$$interfacce_dato{'ceil'}."Mbit\n";
|
||||
print OUTFILE2 "BURST=15k\n";
|
||||
close OUTFILE1;
|
||||
close OUTFILE2;
|
||||
azione_conf ($azioni_id);
|
||||
log_server($fw_id, $azioni_id, 0, "Inserita interfaccia $nome_interfaccia") if ($azioni_stato == 1);
|
||||
log_server($fw_id, $azioni_id, 0, "Modificata interfaccia $nome_interfaccia") if ($azioni_stato == 2);
|
||||
} elsif ($azioni_stato == 3) {
|
||||
my $interfacce_query = "SELECT device FROM interfacce WHERE id = $azioni_idtabella";
|
||||
my $interfacce_sts = $dbmysql->prepare($interfacce_query);
|
||||
$interfacce_sts->execute ();
|
||||
my $interfacce_dato = $interfacce_sts->fetchrow_hashref;
|
||||
my $nomefile1 = "$fw_path/".$$interfacce_dato{'device'};
|
||||
my $nomefile2 = "$fw_path/".$$interfacce_dato{'device'}."-2.root";
|
||||
unlink ($nomefile1);
|
||||
unlink ($nomefile2);
|
||||
azione_conf ($azioni_id);
|
||||
log_server($fw_id, $azioni_id, 0, "Eliminata interfaccia $nome_interfaccia");
|
||||
} else {
|
||||
fw_update ($fw_id, 0, 0, 1, 'Richiesta azione non prevista');
|
||||
azione_conf ($azioni_id);
|
||||
log_server($fw_id, $azioni_id, 2, "Richiesta operazione $azioni_stato non esistente su device $nome_interfaccia");
|
||||
}
|
||||
}
|
||||
case "defrule" {
|
||||
if ($azioni_stato == 1 || $azioni_stato == 2) {
|
||||
my $defrule_query = "SELECT descrizione, priorita, rate, ceil FROM defrule WHERE id = $azioni_idtabella";
|
||||
my $defrule_sts = $dbmysql->prepare($defrule_query);
|
||||
$defrule_sts->execute ();
|
||||
my $defrule_dato = $defrule_sts->fetchrow_hashref;
|
||||
my $nomeregola = creanome ($azioni_idtabella);
|
||||
my $nomefile = "$fw_path/$nomeregola";
|
||||
open (OUTFILE, ">", $nomefile);
|
||||
print OUTFILE "#".$$defrule_dato{'descrizione'}."\n";
|
||||
print OUTFILE "RATE=".$$defrule_dato{'rate'}."Mbit\n";
|
||||
print OUTFILE "CEIL=".$$defrule_dato{'ceil'}."Mbit\n";
|
||||
print OUTFILE "BURST=15k\n";
|
||||
print OUTFILE "LEAF=sfq\n";
|
||||
my $rule_query = "SELECT ipin, portin, ipout, portout FROM rule WHERE iddefrule = $azioni_idtabella";
|
||||
my $rule_sts = $dbmysql->prepare($rule_query);
|
||||
$rule_sts->execute ();
|
||||
while (my $rule_dato = $rule_sts->fetchrow_hashref) {
|
||||
my $ipin = $$rule_dato{'ipin'};
|
||||
my $portin = $$rule_dato{'portin'};
|
||||
if ($portin == 0) {
|
||||
$portin = "";
|
||||
} else {
|
||||
$portin = ":$portin";
|
||||
}
|
||||
my $ipout = $$rule_dato{'ipout'};
|
||||
my $portout = $$rule_dato{'portout'};
|
||||
if ($portout == 0) {
|
||||
$portout = "";
|
||||
} else {
|
||||
$portout = ":$portout";
|
||||
}
|
||||
print OUTFILE "RULE=$ipin$portin,$ipout$portout\n";
|
||||
}
|
||||
print OUTFILE "PRIO=".$$defrule_dato{'priorita'}."\n";
|
||||
close OUTFILE;
|
||||
azione_conf ($azioni_id);
|
||||
log_server($fw_id, $azioni_id, 0, "Inserita regola $nomeregola") if ($azioni_stato == 1);
|
||||
log_server($fw_id, $azioni_id, 0, "Modificata regola $nomeregola") if ($azioni_stato == 2);
|
||||
} elsif ($azioni_stato == 3) {
|
||||
my $nomeregola = creanome ($azioni_idtabella);
|
||||
my $nomefile = "$fw_path/$nomeregola";
|
||||
unlink ($nomefile);
|
||||
azione_conf ($azioni_id);
|
||||
log_server($fw_id, $azioni_id, 0, "Eliminata regola $nomeregola");
|
||||
} else {
|
||||
my $nomeregola = creanome ($azioni_idtabella);
|
||||
fw_update ($fw_id, 0, 0, 1, 'Richiesta azione non prevista');
|
||||
azione_conf ($azioni_id);
|
||||
log_server($fw_id, $azioni_id, 2, "Richiesta operazione $azioni_stato non esistente su regola $nome_regola");
|
||||
}
|
||||
}
|
||||
case "RESET" {
|
||||
reset_file ($fw_path);
|
||||
azione_conf ($azioni_id);
|
||||
log_server($fw_id, $azioni_id, 0, "Richiesta operazione di RESET");
|
||||
}
|
||||
case "FINE APPLICA" {
|
||||
$file_ok = 0;
|
||||
azione_conf ($azioni_id);
|
||||
}
|
||||
else {
|
||||
}
|
||||
}
|
||||
}
|
||||
`/etc/init.d/htbinit restart`;
|
||||
fw_update ($fw_id, 1, 0, 0, NULL);
|
||||
printf "Termine gestione aggiornamenti %s\n",strftime('%d-%m-%Y %H:%M:%S',localtime) if ($messaggi);
|
||||
} elsif (($fw_aggiornamenti == 1) && $fw_attivo && $sys_error) {
|
||||
$azioni_query = "SELECT id, tabella FROM azioni WHERE fw = $fw_id AND dataexec IS NULL and ignora = 0 AND stato = 5";
|
||||
my $azioni_sts = $dbmysql->prepare($azioni_query);
|
||||
$azioni_sts->execute ();
|
||||
|
||||
my $azioni_ok = 1;
|
||||
while ((my $azioni_dato = $azioni_sts->fetchrow_hashref) && ($azioni_ok)) {
|
||||
my $azioni_id = $$azioni_dato{'id'};
|
||||
my $azioni_tabella = $$azioni_dato{'tabella'};
|
||||
|
||||
my $azioni_risop = 0;
|
||||
switch ($azioni_tabella) {
|
||||
case "CREADIR" {
|
||||
if (mkdir $fw_path, 0755) {
|
||||
fw_update ($fw_id, 1, 0, 0, NULL);
|
||||
azione_conf ($azioni_id);
|
||||
log_server($fw_id, $azioni_id, 0, "Creato path dati $fw_path");
|
||||
} else {
|
||||
fw_update ($fw_id, 1, 0, 1, $!);
|
||||
azione_conf ($azioni_id);
|
||||
$azioni_ok = 0;
|
||||
log_server($fw_id, $azioni_id, 2, "Non e' stato possibile creare il path dati $fw_path");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
estraidati ();
|
||||
|
||||
lockpid ("unlock");
|
||||
printf "Termine attivita' %s\n",strftime('%d-%m-%Y %H:%M:%S',localtime) if ($messaggi);
|
||||
exit 0;
|
||||
|
||||
sub azione_conf {
|
||||
my $id = shift;
|
||||
my $azioniup_query = "UPDATE azioni SET dataexec= NOW() WHERE id = $id";
|
||||
my $azioniup_sts = $dbmysql->prepare($azioniup_query);
|
||||
$azioniup_sts->execute ();
|
||||
}
|
||||
|
||||
sub fw_update {
|
||||
my $id = shift;
|
||||
my $syn = shift;
|
||||
my $agg = shift;
|
||||
my $err = shift;
|
||||
my $msg = shift;
|
||||
|
||||
if ($syn) {
|
||||
$syn = ", ultimosync = NOW()";
|
||||
} else {
|
||||
$syn = "";
|
||||
}
|
||||
|
||||
if ($agg == -1) {
|
||||
$agg = "";
|
||||
} else {
|
||||
$agg = ", aggiornamenti = $agg";
|
||||
}
|
||||
|
||||
my $fwup_query = "UPDATE firewall SET ultimoconn = NOW(), errore = $err, msg_err = '$msg' $syn $agg WHERE id = $id";
|
||||
my $fwup_sts = $dbmysql->prepare($fwup_query);
|
||||
$fwup_sts->execute ();
|
||||
}
|
||||
|
||||
sub reset_file {
|
||||
my $dir = shift;
|
||||
|
||||
opendir (DIR, $dir);
|
||||
my @files = grep { /^[^\.]/ } readdir(DIR);
|
||||
closedir DIR;
|
||||
|
||||
foreach $file (@files) {
|
||||
unlink ("$dir/$file");
|
||||
}
|
||||
}
|
||||
|
||||
sub creanome {
|
||||
my $idparent = shift;
|
||||
|
||||
my $query = "SELECT interfacce.device, defrule.idparent, defrule.priorita FROM defrule JOIN interfacce ON defrule.idinterfacce = interfacce.id WHERE defrule.id = '$idparent'";
|
||||
my $sts = $dbmysql->prepare($query);
|
||||
$sts->execute ();
|
||||
my $dato = $sts->fetchrow_hashref;
|
||||
|
||||
if ($$dato{'idparent'} == 0) {
|
||||
$ritorno = $$dato{'device'}."-2:".$$dato{'priorita'};
|
||||
return $ritorno;
|
||||
} else {
|
||||
$ritorno = creanome($$dato{'idparent'}).":".$$dato{'priorita'};
|
||||
return $ritorno;
|
||||
}
|
||||
}
|
||||
|
||||
sub log_server {
|
||||
my $fw = shift;
|
||||
my $idazione = shift;
|
||||
my $stato = shift;
|
||||
my $testo = shift;
|
||||
|
||||
my $query = "INSERT log_server SET fw = $fw, idazione = $idazione, data= NOW(), stato = $stato, testo = '$testo'";
|
||||
my $sts = $dbmysql->prepare($query);
|
||||
$sts->execute ();
|
||||
}
|
||||
|
||||
sub lockpid {
|
||||
my $op = shift;
|
||||
|
||||
my $pidfile = "/tmp/traffico.pid";
|
||||
if ($op eq "lock") {
|
||||
if (open PIDFILE, "< $pidfile") {
|
||||
read PIDFILE, $oldpid, 256;
|
||||
my $exists = kill 0, $oldpid;
|
||||
if ( $exists ) {
|
||||
return 0;
|
||||
} else {
|
||||
close PIDFILE;
|
||||
}
|
||||
} else {
|
||||
close PIDFILE;
|
||||
}
|
||||
open PIDFILE, "> $pidfile" || return 0;
|
||||
print PIDFILE $$;
|
||||
close PIDFILE;
|
||||
} elsif ($op eq "unlock") {
|
||||
unlink $pidfile;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
sub estraidati {
|
||||
my $class;
|
||||
my $classid;
|
||||
my $parent;
|
||||
my $rate;
|
||||
my $ceil;
|
||||
my $crate;
|
||||
my $sent;
|
||||
my $range;
|
||||
|
||||
printf "Inizio estrazione dati %s\n",strftime('%d-%m-%Y %H:%M:%S',localtime) if ($messaggi);
|
||||
my $query = "SELECT id, device FROM interfacce WHERE attivo = 1 AND stato != 3";
|
||||
my $sts = $dbmysql->prepare($query);
|
||||
$sts->execute ();
|
||||
while (my $dato = $sts->fetchrow_hashref) {
|
||||
my $device_id = $$dato{'id'};
|
||||
my $device = $$dato{'device'};
|
||||
my $outcompleto;
|
||||
my @outriga;
|
||||
|
||||
$outcompleto = qx/\/usr\/sbin\/tc -s class show dev $device/;
|
||||
@outriga = split /\n/, $outcompleto;
|
||||
|
||||
for ($i=0;$i< scalar(@outriga);$i++) {
|
||||
my $rigaatt = $outriga[$i];
|
||||
if($rigaatt =~ /^ lended: / ) {
|
||||
$range="$rate $ceil";
|
||||
$crate=sprintf "%8.1lf",$crate;
|
||||
$sent=sprintf "%10.0lf",$sent/1024;
|
||||
$$device{$classid}[0]=$crate if (($conta eq 0) || ($crate > $$device{$classid}[0]));
|
||||
if ($conta eq 0) {
|
||||
$$device{$classid}[4]=$crate;
|
||||
$$device{$classid}[5]=$sent;
|
||||
} else {
|
||||
$$device{$classid}[4] += $crate;
|
||||
}
|
||||
$$device{$classid}[6]=$sent;
|
||||
$$device{$classid}[1]=$crate;
|
||||
$$device{$classid}[2]=$ceil;
|
||||
|
||||
my @pezzi = split ':', $classid;
|
||||
my $classe = $pezzi[1];
|
||||
my $defrule_id;
|
||||
if ($classe == 2) {
|
||||
$defrule_id = 0;
|
||||
} else {
|
||||
my $pri_query = "SELECT id FROM defrule WHERE idinterfacce = $device_id AND priorita = $classe AND attivo = 1 AND stato != 3 ";
|
||||
my $pri_sts = $dbmysql->prepare($pri_query);
|
||||
$pri_sts->execute ();
|
||||
my $pri_dato = $pri_sts->fetchrow_hashref;
|
||||
$defrule_id = $$pri_dato{'id'};
|
||||
}
|
||||
my $prec_query = "SELECT ($sent-sent) AS inviati, TIMESTAMPDIFF(SECOND, data, NOW()) AS tempo FROM dati_traffico WHERE id = (SELECT MAX(id) FROM dati_traffico WHERE idfirewall = $fw_id AND idinterfacce = $device_id AND iddefrule = $defrule_id)";
|
||||
my $prec_sts = $dbmysql->prepare($prec_query);
|
||||
$prec_sts->execute ();
|
||||
my $media;
|
||||
my $tempo;
|
||||
my $inviati;
|
||||
if (my $prec_dato = $prec_sts->fetchrow_hashref) {
|
||||
$tempo = $$prec_dato{'tempo'};
|
||||
$inviati = $$prec_dato{'inviati'};
|
||||
$inviati = $sent if ($inviati < 0);
|
||||
if ($tempo > 0) {
|
||||
$media = $inviati/$tempo;
|
||||
} else {
|
||||
$media = 0;
|
||||
}
|
||||
} else {
|
||||
$media = 0;
|
||||
$inviati = 0;
|
||||
}
|
||||
printf "Inizio inserimento dato iddefrule $defrule_id %s\n",strftime('%d-%m-%Y %H:%M:%S',localtime) if ($messaggi);
|
||||
my $ins_query = "INSERT INTO dati_traffico SET idfirewall = $fw_id, idinterfacce = $device_id, iddefrule = $defrule_id, sent = $sent, rate = $crate, data = NOW(), calc = $media, diff = $inviati";
|
||||
my $ins_sts = $dbmysql->prepare($ins_query);
|
||||
$ins_sts->execute ();
|
||||
printf "Termine inserimento dato iddefrule $defrule_id %s\n",strftime('%d-%m-%Y %H:%M:%S',localtime) if ($messaggi);
|
||||
next;
|
||||
}
|
||||
if($rigaatt =~ /^ Sent (\d+) bytes /) {
|
||||
$sent=$1;
|
||||
next;
|
||||
}
|
||||
if($rigaatt =~ /^ rate (\S+)(bit|bps) /) {
|
||||
# print "BPS rate!\n" if $2 eq "bps";
|
||||
($crate=$1);
|
||||
$crate=~s/K/*1024/;
|
||||
$crate=~s/M/*1024*1024/;
|
||||
$crate=eval $crate;
|
||||
next;
|
||||
}
|
||||
if($rigaatt =~ /^class (\S+) (\S+:\S+) (root|parent (\S+:\S+)) .*rate (\S+)bit ceil (\S+)bit/) {
|
||||
$class=$1;
|
||||
$classid=$2;
|
||||
$rate=$5;
|
||||
$ceil=$6;
|
||||
($parent=$3)=~s/parent //;
|
||||
printf "Inizio elaborazione classe $classid %s\n",strftime('%d-%m-%Y %H:%M:%S',localtime) if ($messaggi);
|
||||
|
||||
$rate=~s/K/*1024/;
|
||||
$rate=~s/M/*1024*1024/;
|
||||
$rate=eval $rate;
|
||||
$rate/=1024;
|
||||
|
||||
$ceil=~s/K/*1024/;
|
||||
$ceil=~s/M/*1024*1024/;
|
||||
$ceil=eval $ceil;
|
||||
$ceil/=1024;
|
||||
next;
|
||||
}
|
||||
|
||||
}
|
||||
printf "Termine estrazione dati %s\n",strftime('%d-%m-%Y %H:%M:%S',localtime) if ($messaggi);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user