Platon Technologies
neprihlásený Prihlásiť Registrácia
SlovakEnglish
open source software development oslavujeme 10 rokov vývoja otvoreného softvéru! Pondelok, 22. júl 2024

Súbor: [Platon] / scripts / perl / accounting / netacct-savelog-mysql.pl (stiahnutie)

Revízia 1.1, Sun Mar 13 21:06:23 2005 UTC (19 years, 4 months ago) by rajo

Network accountig script and database tables.
See http://www.exorsus.net/projects/net-acct/

#!/usr/bin/perl

#
# netacct-savelog-mysql.pl
#
# Developed by Lubomir Host 'rajo' <rajo AT platon.sk>
# Copyright (c) 2005 Platon SDG, http://platon.sk/
# Licensed under terms of GNU General Public License.
# All rights reserved.
#
# Changelog:
# 2005-01-23 - created
#

# $Platon$

use strict;

use DBI qw(:sql_types);
use Time::HiRes qw(gettimeofday sleep usleep);
use POSIX qw(strftime);

# declaration: {{{
use vars qw (
    $database
    $dbh
    $sql_insert

    $update_interval
);

# functions
sub PrepareSQL();

# }}}

$update_interval    = 10; # seconds
$database    = "DBI:mysql:database=system;host=localhost;user=syswatcher;password=;compression=0";

while (! ($dbh = DBI->connect($database))) {
    print_info("Database connection failed, reconnecting in $update_interval seconds...($DBI::errstr)");
    sleep $update_interval;
}

PrepareSQL();

while (my $line = <STDIN>) {
    chomp $line;
    my ($created,        $ip_proto,
        $source_ip,        $source_port,
        $dest_ip,        $dest_port,
        $packet_count,    $data_size,
        $interface,        $ppp_user) = split(/\s+/, $line);

    $sql_insert->execute(
        strftime("%Y-%m-%d %X", localtime($created)),
        $ip_proto,
        $source_ip,        $source_port,
        $dest_ip,        $dest_port,
        $packet_count,    $data_size,
        $interface,        $ppp_user);
}

$dbh->disconnect();

sub PrepareSQL()
{ # {{{
    
    $sql_insert = $dbh->prepare("
        INSERT INTO net_acct
            (created,        ip_proto,
            source_ip,        source_port,
            dest_ip,        dest_port,
            packet_count,    data_size,
            interface,        ppp_user)
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
        ");

} # }}}


# vim: ts=4
# vim600: fdm=marker fdl=0 fdc=3


Platon Group <platon@platon.sk> http://platon.sk/
Copyright © 2002-2006 Platon Group
Stránka používa redakčný systém Metafox
Na začiatok