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
);

22 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

Guo Guo said...

caihuanglin20150304
timberland boots
michael kors canada
tory burch shoes
louis vuitton outlet
abercrombie and fitch
ugg boots
canada goose outlet
ugg boots
canada goose outlet
cheap nfl jerseys
canada goose
coach outlet
coach factory
canada goose jackets
hollister outlet
toms promo code
coach outlet online
cheap jerseys wholesale
gucci outlet
michael kors outlet
fitflops outlet
tory burch handbags
michael kors handbags
coach factory outlet
burberry scarf
christian louboutin outlet
burberry sale
retro jordans
michael kors outlet online
ralph lauren outlet

chenlina said...

chenlina20150611
oakley sunglasses
hollister clothing
jordan 5
abercrombie and fitch
coach outlet store online
gucci bags
www.louisvuitton.com
michael kors handbags
mont blanc pens
louis vuitton handbags
louis vuitton handbags
hollister kids
michael kors
discount oakley sunglasses
ray ban sungalsses
louis vuitton
louis vuitton outlet
chanel handbags
timberlands
jordan 6 rings
ray ban sunglasses
ray ban outlet
michael kors
coach outlet online
hermes birkin
kevin durant basketball shoes
michael kors outlet online
oakley sunglasses sale
coach factory outlet
kate spade
ray ban outlet
louis vuitton outlet
replica rolex watches
michael kors
gucci handbags
insanity workout
chanel handbags
christian louboutin outlet
hogan outlet
abercrombie store

风骚达哥 said...

20150922 junda
abercrombie
Hollister uk
Air Jordan 3 III Retro Free Shipping
louis vuitton
coach outlet
Cheap Jordans For Sale Wholesale
canada goose jackets
Christian Louis Vuitton Red Bottoms
New Louis Vuitton Handbags Outlet
Official Coach Factory Outlet Online Sale
Jordan 13 Shoes For Sale
canada goose outlet
Real Louis Vuitton Bags
nike trainers
Jordan 8 Bugs Bunny
Authentic Louis Vuitton Belts Outlet Store
burberry outlet
Louis Vuitton Bags On Sale
true religion jeans
canada goose outlet online
Lebron James Basketball Shoes Sale
Louis Vuitton Handbags Discount Off
nfl jerseys
Abercrombie T-Shirts
Discount Ray Ban Polarized Sunglasses
coach factory outlet online
Louis Vuitton Outlet USA
celine
Louis Vuitton Luxury Official Website
michael kors outlet

Minko Chen 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

Chenzhen 20160303chenzhen said...

louis vuitton handbags
air jordan homme
michael kors outlet
true religion sale
lebron james shoes 13
louis vuitton handbags
cheap ray ban sunglasses
coach outlet
louis vuitton handbags
coach factory outlet
cheap oakleys
michael kors outlet
louis vuitton outlet
ralph lauren
louis vuitton outlet stores
tory burch flats
adidas running shoes
nfl jerseys wholesale
michael kors outlet
ugg outlet
copy watches
toms outlet
timberlands
hollister shirts
coach outlet online
oakley sunglasses
kevin durant shoes
oakley vault
nike roshe run
oakley canada
coach outlet
nfl jerseys
michael kors bags
ugg boots
louis vuitton outlet stores
ray ban sunglasses
michael kors handbags
celine bags
insanity workout
ray ban sunglasses outlet
20160321chenzhen

chenlili said...

2016-4-21 xiaobao
ghd hair straighteners
louis vuitton bags
louis vuitton
nike trainers
tory burch sale
michael kors handbags
nike sb shoes
oakley sunglasses wholesale
christian louboutin
celine handbags
mont blanc pens
hollister uk
hollister clothing
air jordans
michael kors outlet
north face outlet
nfl jerseys
cheap jordans
louis vuitton outlet stores
nike basketball shoes
louis vuitton outlet
kate spade handbags
coach outlet
kate spade handbags
nike sb
tory burch outlet
michael kors outlet
oakley sunglasses
adidas shoes
louis vuitton outlet
lebron james shoes
fitflops
michael kors outlet
oakley sunglasses
ralph lauren polo
louis vuitton outlet
replica watches
caoch outlet
michael kors outlet online
adidas superstar

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

Qing Cai said...

cartier watches
air jordan shoes
ralph lauren polo shirts
ferragamo outlet
michael kors handbags wholesale
ray ban sunglasses
burberry outlet
true religion jeans outlet
nhl jerseys
michael kors online outlet
polo ralph lauren
true religion outlet
kate spade uk
cheap nhl jerseys
michael kors handbags clearance
louis vuitton handbags outlet
coach outlet store
fitflops sale clearance
michael kors handbags sale
coach outlet store
nike tn pas cher
cheap ray ban sunglasses
beats by dre
tory burch outlet online
louis vuitton outlet
lululemon outlet
nike roshe
louis vuitton pas cher
cheap soccer jerseys
beats by dre
cai20160516

Gege Dai said...

michael kors outlet online
nfl jersey wholesale
ralph lauren outlet
nba jerseys
jordan 11
toms shoes
salomon shoes sale
ugg outlet store
nike free running
prada handbags
ferragamo shoes
nba jerseys
cheap ray ban sunglasses
cazal sunglasses
ugg boots
abercrombie and fitch
ferragamo shoes
mulberry handbags sale
louis vuitton pas cher
nike outlet
oakley sunglasses
polo ralph lauren
nike outlet store
prada sneakers
christian louboutin shoes
coach outlet
coach outlet
air max 90
ray ban sunglasses
rolex watches
longchamp pliage
burberry sunglasses on sale
michael kors outlet
ralph lauren uk
puma shoes
czq20160726

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

dong dong23 said...

uggs on sale
ugg outlet
fitflops
coach outlet store online clearances
chaussures christian louboutin
nike air max 90
jordans for sale
celine handbags
burberry outlet
uggs pas cher
201610.17wengdongdong

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

cara menggugurkan kandungan said...

This blog is so nice to me. I will continue to come here again and again. Visit my link as well. Good luck
http://www.jualobataborsiherbal.com/ obat aborsi
http://www.jualobataborsiherbal.com/cara-menggugurkan-kandungan/ cara menggugurkan kandungan
http://obataborsi59.com/ obat aborsi
http://obataborsi59.com/cara-menggugurkan-kandungan-dengan-cepat-dan-aman/ cara menggugurkan kandungan
http://obattelatdatangbulan.info/ obat telat datang bulan
http://klinikobataborsi.com/ jual obat aborsi
http://jualobatpenggugurkandungan.net/ obat penggugur kandungan
http://tandatandakehamilan.net/ tanda tanda kehamilan
http://tandatandakehamilan.net/cara-cepat-dan-selamat-menggugurkan-kandungan/ cara menggugurkan kandungan

shengda xu 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 hamil said...

now present in your city
1. manfaat kurma untuk persalinan
2. manfaat buah nanas
3. aktivitas penyebab keguguran
4. apakah usg berbahaya
5. penyebab telat haid
6. cara melancarkan haid