Created
May 17, 2011 21:45
-
-
Save reyjrar/977474 to your computer and use it in GitHub Desktop.
Sample working with DBIx::Class and PacketFence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use feature qw{ say }; | |
use lib qw{ | |
/usr/local/pf/lib | |
/opt/local/eris/lib}; | |
use pf::schema; | |
use eris::schema; | |
use Config::IniFiles; | |
use YAML; | |
use Try::Tiny; | |
use DateTime; | |
# Config File Locations | |
my %Files = ( | |
eris => '/opt/local/eris/etc/eris.cfg', | |
pf => '/usr/local/pf/conf/pf.conf', | |
); | |
# Configuration | |
my %CFG = (); | |
$CFG{pf} = Config::IniFiles->new( -file => $Files{pf} ) or die "unable to read pf config file: $!\n"; | |
$CFG{eris} = YAML::LoadFile( $Files{eris} ) or die "unable to read eris config file: $!\n"; | |
# Connect to the Schema | |
my $dbOpts = { RaiseError => 1, PrintError => 1 }; | |
my $eris = eris::schema->connect( $CFG{eris}->{db_dsn}, $CFG{eris}->{db_user}, $CFG{eris}->{db_pass}, $dbOpts ); | |
my $pf = pf::schema->connect( 'dbi:mysql:database=pf', 'pf', $CFG{pf}->val( 'database', 'pass' ), $dbOpts ); | |
say "eris sources"; | |
foreach my $src ( $eris->sources ) { say " - $src"; | |
} | |
say "pf sources"; | |
foreach my $src ( $pf->sources ) { | |
say " - $src"; | |
} | |
# Select Users from our Source Database | |
my $rs = $eris->resultset('user')->search( { username => { '!=' => '' } } ); | |
while( my $e_user = $rs->next ) { | |
next unless length $e_user->username; | |
my $pf_user = $pf->resultset('person')->find_or_create( { pid => $e_user->username } ); | |
$pf_user->firstname( $e_user->first_name ); | |
$pf_user->lastname( $e_user->last_name ); | |
$pf_user->email( $e_user->email ); | |
if ( $e_user->is_active == 0 ) { | |
$pf_user->notes( 'INACTIVE USER' ); | |
} | |
$pf_user->update; | |
print "updated " . $e_user->username . "\n"; | |
} | |
# Select Devices from the Source Database | |
my $devrs = $eris->resultset('device')->search( { last_ts => { '>' => \q{NOW() - interval '6 months'} } } ); | |
while( my $e_dev = $devrs->next ) { | |
my $pf_node = $pf->resultset('node')->find_or_create( { mac => $e_dev->mac_addr } ); | |
say $pf_node->mac; | |
# Initialize some data | |
my $reg_dt = undef; | |
my $owner = ''; | |
my $vlan = undef; | |
my $category_id = 1; | |
# Set detect from our source database | |
$pf_node->detect_date( $e_dev->first_ts->datetime ); | |
# Set owner from Details | |
try { | |
$owner = $e_dev->details->primary_user->username; | |
}; | |
# Grab Registration Date from Device::Details | |
try { | |
$reg_dt = $e_dev->details->mod_ts; | |
}; | |
# Use inventory_archive? | |
if( !length $owner ) { | |
my $curr_auth = $e_dev->search_related('authentication', undef, { order_by => { -desc => 'last_ts' } } )->single; | |
try { | |
say " - trying authen_current"; | |
$owner = $curr_auth->user->username; | |
}; | |
try { | |
say " - trying inventory_archive"; | |
my $record = $eris->resultset('view::history')->search( | |
{ device_id => $e_dev->device_id, | |
discovery_method => [qw(msevtlog sshd smbtrack userAtHost)], | |
event_ts => { '>' => \q{NOW() - interval '3 weeks'} }, | |
}, | |
{ | |
order_by => { -desc => 'event_ts' }, | |
} | |
)->single; | |
$owner = $record->user->username; | |
} unless length $owner; | |
} | |
# Set the Owner | |
if( length $owner ) { | |
$reg_dt ||= DateTime->now; | |
$pf_node->pid( $owner ); | |
$pf_node->status( 'reg' ); | |
$pf_node->regdate( $reg_dt->datetime ); | |
} | |
# Client IDs | |
if( $e_dev->clientid && !$pf_node->computername ) { | |
$pf_node->computername( $e_dev->clientid ); | |
} | |
# Check for existing VLAN Association | |
try { $vlan = $e_dev->assigned_vlan->vlan; }; | |
if( $vlan && $vlan->nac_managed ) { | |
my $category = $pf->resultset('node::category')->find( { name => $vlan->nac_specifier } ); | |
try { $category_id = $category->category_id; }; | |
} | |
$pf_node->category_id( $category_id ); | |
$pf_node->update; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment