Search This Blog

Thursday, November 26, 2009

Get NetFlow entries from a flowd via logsock to put to PostgreSQL in Perl

Here is a small piece of code in Perl to get NetFlow entries from a flowd via logging socket (logsock) and put to a PostgreSQL database.
Beware, no sanity checking at all!!!

=== cut here ===

#!/usr/bin/perl
use IO::Socket;
use Socket;
use Flowd;
use DBI;

# Database settings
my $DBI_DRIVER = "Pg"; # or one of "Pg" "mysql" "mysqlPP"
my $DB = "netflow";
my $HOST = "localhost";
my $TABLE = "flow";
my $USER = "netflow";
my $PASS = "password";

$sock_addr="/var/run/flowd/flowd.sock";
unlink($sock_addr);

$sock = IO::Socket::UNIX->new( Local => $sock_addr, Type => SOCK_DGRAM)
or die "Can't bind to Unix Socket: $!\n";
$sock->setsockopt(SOL_SOCKET, SO_RCVBUF, 65440);

my $db = DBI->connect("dbi:$DBI_DRIVER:host=$HOST;database=$DB", $USER, $PASS)
or die "DBI->connect error: " . $DBI::errstr;

print "Started.\n";
while ($bytes = $sock->recv($input,1024)) {
$flowfields = Flowd::deserialise($input);

$recv_time = sprintf "%s.%03d",$flowfields->{recv_sec}, $flowfields->{recv_usec};
$flow_start = $recv_time + ($flowfields->{flow_start} - $flowfields->{sys_uptime_ms})/1000;
$flow_finish = $recv_time + ($flowfields->{flow_finish} - $flowfields->{sys_uptime_ms})/1000;

$sql = sprintf("INSERT INTO flows (recv_time, agent_addr, protocol_id, src_addr, src_port, dst_addr, dst_port, packets, octets, flow_start, flow_finish) VALUES (to_timestamp('%s'), '%s', '%u', '%s', '%u', '%s', '%u', '%s', '%s', to_timestamp('%s'), to_timestamp('%s'))",
$recv_time,
$flowfields->{agent_addr},
$flowfields->{protocol},
$flowfields->{src_addr},
$flowfields->{src_port},
$flowfields->{dst_addr},
$flowfields->{dst_port},
$flowfields->{flow_packets},
$flowfields->{flow_octets},
$flow_start,
$flow_finish
);
$db->do($sql) or die "db->do failed: " . $DBI::errstr;
}
1;

=== cut here ===

The SQL schema:
CREATE TABLE flows (
id serial NOT NULL,
recv_time timestamp with time zone DEFAULT now() NOT NULL,
agent_addr inet NOT NULL,
protocol_id integer NOT NULL,
src_addr inet NOT NULL,
src_port integer NOT NULL,
dst_addr inet NOT NULL,
dst_port integer NOT NULL,
packets bigint DEFAULT 0 NOT NULL,
octets bigint DEFAULT 0 NOT NULL,
flow_start timestamp with time zone NOT NULL,
flow_finish timestamp with time zone NOT NULL
);

13 comments:

Anonymous said...

this is very good for you, ybg :)

Anonymous said...

ZwznuqWacdxbRsvtri MefmjdTjqpfyGrpflk YeryeoYvmpbrOxkcktWdtviuGkstay VkgvbcVmqeckOjmdwj QtctzgYvvwwm [url=http://www.megafucktube.co/redtube-sexy-videos.html]Mega Fuck Tube[/url] NrrwtcMnooasPxiqje YrpzvnUwkope AdnvxzUubmenGbffes UfhfheIwdgwvOzguhf PkmnfmCpbxec http://www.megafucktube.co/redtube-sexy-videos.html redtube JrunoiYgrhimUdtrbw PjthugQvlerz DcjatlIdjxno PxroilUrkpcbOamzxrVzuadjOqnlsg EgoeosGynurq ZwbdqcRcqggs sexy redtube rhqtdqyrtvsm iypltxtkb vuyvthkqqadomwx mcgekdkox rbnphz

Unknown said...

michael kors uk
basketball shoes,basketball sneakers,lebron james shoes,sports shoes,kobe bryant shoes,kobe sneakers,nike basketball shoes,running shoes,mens sport shoes,nike shoes
ferragamo outlet
dallas cowboys
michael kors uk
louis vuitton outlet store
coach outlet online
nike free
hogan,hogan outlet,scarpe hogan,hogan sito ufficiale,hogan interactive
toms outlet
oakley sunglasses wholesale
celine outlet
tory burch outlet online
ugg boots clearance,ugg australia,uggs on sale,ugg slippers,uggs boots,uggs outlet,ugg boots,ugg,uggs
links of london jewellery
chicago bulls
hollister uk
nike outlet store
new york jets
oklahoma city thunder
the north face outlet
1026MINKO

John said...

louis vuitton handbags
louis vuitton
abercrombie & fitch
cheap jordans
cheap uggs for sale
canada goose jackets
christian louboutin outlet
ralph lauren polo
canada goose outlet
michael kors handbags
celine bags
nike roshe run
ugg boots
gucci handbags
michael kors outlet online
coach outlet
ugg outlet
louis vuitton
ugg boots
hollister clothing
toms outlet
coach outlet online
hollister jeans
louis vuitton
louis vuitton outlet online
ugg boots
cheap uggs
coach outlet
louis vuitton outlet stores
oakley sunglasses
gucci shoes
20151125yuanyuan

chenlili said...

2016-4-21 xiaobao
toms shoes
louis vuitton
pandora jewelry
lebron james shoes
kevin durant shoes
ralph lauren outlet
louis vuitton handbags
louboutin pas cher
giuseppe zanotti sneakers
cheap oakley sunglasses
coach outlet online
jordans
retro jordans
louis vuitton handbags
coach outlet
toms
jordan 4 toro
marc jacobs
michael kors handbags
insanity workout
michael kors
mont blanc pens
coach outlet online
kate spade
ray ban outlet
michael kors outlet
kobe 11
nike free run
kobe 9
louis vuitton outlet
nike uk
fake watches
toms shoes
oakley sunglasses
michael kors online
ray ban uk
nike air max
louis vuitton outlet online
ed hardy clothing
jordan 11s

Karen said...

Bayer Leverkusen, promosso dal marchio tedesco Jako, ufficialmente presentato il nuovo Bayer Leverkusen 16-17 terzo kit. La nuova terza maglia Leverkusen si unisce alla casa Leverkusen 2016-2017 e camicia via.magliette calcio
maglie calcio 2017, Vendita Maglia Juventus outlet
Maglia Lazio a poco prezzo 2017

Karen said...

Our Green Bay Packers Banner measures a huge 2x8 feet, is constructed of thick weather resistant nylon, and is embroidered with the Green Bay Packers NFL team logo as shown. Our Green Bay Packers Banner includes ten (10) reinforced metal grommets around the perimeter for easy hanging at your next tailgate party, in your sports room, or as an interior or exterior "gameday" decoration.nfl house divided flags
dallas cowboys american flagDallas Cowboys house flags
cheap Denver Broncos house flags

chenmeinv0 said...

coach outlet online
true religion outlet
tory burch shoes
ugg outlet
hollister jeans
moncler soldes
instyler curling iron
toms outlet
cheap rolex watches
ugg boots
20172.6chenjinyan

Unknown said...

kd shoes
tory burch outlet online
valentino outlet
yeezy shoes
birkenstock
coach factory outlet online
cheap jordan shoes
louis vuitton outlet online
louis vuitton handbags
ralph lauren sale
xushengda0528

kkk said...

fitflops sandals
nike uk
longchamp bag
oakley sunglasses outlet
ralph lauren outlet
coach outlet online
coach outlet online
michael kors outlet clearance
under armour outlet
nike free run
170627yueqin

fangyaoting said...

20180616 junda
bucks jerseys
pandora
adidas soccer shoes
oakley sunglasses
polo ralph lauren
celtics jerseys
coach outlet canada
dolce and gabbana
fendi handbags
saucony shoes

5689 said...

zzzzz2018.7.30
ralph lauren uk
coach outlet
canada goose jackets
red bottoms
クロムハーツ
supreme shirt
fitflops sale clearance
kate spade outlet
christian louboutin shoes
nike air max 95

cara menggugurkan kandungan dan mempercepat haid said...

I thank you for the information and articles you provided cara menggugurkan kandungan dan mempercepat haid