Platon Technologies
not logged in Login Registration
EnglishSlovak
open source software development celebrating 10 years of open source development! Friday, April 19, 2024

File: [Platon] / scripts / perl / accounting / netacct-savelog-mysql.pl (download)

Revision 1.1, Sun Mar 13 21:06:23 2005 UTC (19 years, 1 month 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.org> http://platon.org/
Copyright © 2002-2006 Platon Group
Site powered by Metafox CMS
Go to Top