close to done, minus testing
This commit is contained in:
parent
e5ef495390
commit
a0488468ad
|
@ -4,23 +4,24 @@ use warnings;
|
|||
use ExtUtils::MakeMaker;
|
||||
|
||||
WriteMakefile(
|
||||
NAME => 'Parse::Netstat::Search::Sort',
|
||||
AUTHOR => q{Zame C. Bowers-Hadley <vvelox@vvelox.net>},
|
||||
VERSION_FROM => 'lib/Parse/Netstat/Search/Sort.pm',
|
||||
ABSTRACT_FROM => 'lib/Parse/Netstat/Search/Sort.pm',
|
||||
LICENSE => 'artistic_2',
|
||||
PL_FILES => {},
|
||||
MIN_PERL_VERSION => '5.006',
|
||||
CONFIGURE_REQUIRES => {
|
||||
'ExtUtils::MakeMaker' => '0',
|
||||
},
|
||||
BUILD_REQUIRES => {
|
||||
'Test::More' => '0',
|
||||
},
|
||||
PREREQ_PM => {
|
||||
#'ABC' => '1.6',
|
||||
#'Foo::Bar::Module' => '5.0401',
|
||||
},
|
||||
dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', },
|
||||
clean => { FILES => 'Parse-Netstat-Search-Sort-*' },
|
||||
);
|
||||
NAME => 'Parse::Netstat::Search::Sort',
|
||||
AUTHOR => q{Zame C. Bowers-Hadley <vvelox@vvelox.net>},
|
||||
VERSION_FROM => 'lib/Parse/Netstat/Search/Sort.pm',
|
||||
ABSTRACT_FROM => 'lib/Parse/Netstat/Search/Sort.pm',
|
||||
LICENSE => 'artistic_2',
|
||||
PL_FILES => {},
|
||||
MIN_PERL_VERSION => '5.006',
|
||||
CONFIGURE_REQUIRES => {
|
||||
'ExtUtils::MakeMaker' => '0',
|
||||
},
|
||||
BUILD_REQUIRES => {
|
||||
'Test::More' => '0',
|
||||
},
|
||||
PREREQ_PM => {
|
||||
'Error::Helper'=>'1.0.0',
|
||||
'Parse::Netstat'=>'0.14',
|
||||
'Parse::Netstat::Search'=>'0.0.0',
|
||||
},
|
||||
dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', },
|
||||
clean => { FILES => 'Parse-Netstat-Search-Sort-*' },
|
||||
);
|
||||
|
|
|
@ -6,15 +6,15 @@ use warnings;
|
|||
|
||||
=head1 NAME
|
||||
|
||||
Parse::Netstat::Search::Sort - The great new Parse::Netstat::Search::Sort!
|
||||
Parse::Netstat::Search::Sort - Sorts the returned array from Parse::Netstat::Search.
|
||||
|
||||
=head1 VERSION
|
||||
|
||||
Version 0.01
|
||||
Version 0.0.0
|
||||
|
||||
=cut
|
||||
|
||||
our $VERSION = '0.01';
|
||||
our $VERSION = '0.0.0';
|
||||
|
||||
|
||||
=head1 SYNOPSIS
|
||||
|
@ -23,32 +23,288 @@ Quick summary of what the module does.
|
|||
|
||||
Perhaps a little code snippet.
|
||||
|
||||
use Parse::Netstat::Search;
|
||||
use Parse::Netstat::Search::Sort;
|
||||
use Parse::Netstat qw(parse_netstat);
|
||||
|
||||
my $foo = Parse::Netstat::Search::Sort->new();
|
||||
...
|
||||
my $res = parse_netstat(output => join("", `netstat -n`), tcp=>1, udp=>1, unix=>0, flavor=>$^O);
|
||||
|
||||
=head1 EXPORT
|
||||
my $pn-search=Parse::Netstat::Search->new;
|
||||
|
||||
A list of functions that can be exported. You can delete this section
|
||||
if you don't export anything, such as for a purely object-oriented module.
|
||||
my @found=$pn-search->search( $res );
|
||||
|
||||
=head1 SUBROUTINES/METHODS
|
||||
my $sorter = Parse::Netstat::Search::Sort->new;
|
||||
|
||||
=head2 function1
|
||||
@found = $sorter->sort( \@found );
|
||||
|
||||
The supported sort methods are as below.
|
||||
|
||||
host_ff Host, Foreign First (default)
|
||||
host_lf Host, Local First
|
||||
host_f Host, Foreign
|
||||
host_l Host, Local
|
||||
port_ff Port, Foreign First
|
||||
port_lf Port, Local First
|
||||
port_f Port, Foriegn
|
||||
port_l Port, Local
|
||||
state State
|
||||
protocol Protocol
|
||||
q_rf Queue, Receive First
|
||||
q_sf Queue, Send First
|
||||
q_r Queue, Receive
|
||||
q_s Queue, Send
|
||||
none No sorting is done.
|
||||
|
||||
=head1 Methods
|
||||
|
||||
=head2 new
|
||||
|
||||
Initiates the object.
|
||||
|
||||
my $sorter=Parse::Netstat::Search->new;
|
||||
|
||||
=cut
|
||||
|
||||
sub function1 {
|
||||
sub new{
|
||||
my $self={
|
||||
perror=>undef,
|
||||
error=>undef,
|
||||
errorString=>'',
|
||||
errorExtra=>{
|
||||
1 => 'badSort',
|
||||
},
|
||||
sort=>'host_ff',
|
||||
invert=>undef,
|
||||
sort_check=>{
|
||||
host_ff=>1,
|
||||
host_lf=>1,
|
||||
host_f=>1,
|
||||
host_l=>1,
|
||||
port_ff=>1,
|
||||
port_lf=>1,
|
||||
port_f=>1,
|
||||
port_l=>1,
|
||||
state=>1,
|
||||
protocol=>1,
|
||||
q_rf=>1,
|
||||
q_sf=>1,
|
||||
q_r=>1,
|
||||
q_s=>1,
|
||||
none=>1,
|
||||
}
|
||||
};
|
||||
bless $self;
|
||||
|
||||
return $self;
|
||||
}
|
||||
|
||||
=head2 function2
|
||||
=head2 get_sort
|
||||
|
||||
This returns the current sort method and invert
|
||||
values.
|
||||
|
||||
Please be aware as the invert value is a boolean,
|
||||
it may not be defined(which it is not by default).
|
||||
|
||||
my ($sort, $invert)=$pnc->get_sort;
|
||||
|
||||
=cut
|
||||
|
||||
sub function2 {
|
||||
sub get_sort{
|
||||
my $self=$_[0];
|
||||
|
||||
if( ! $self->errorblank ){
|
||||
return undef;
|
||||
}
|
||||
|
||||
return $self->{sort}, $self->{invert};
|
||||
}
|
||||
|
||||
=head2 set_sort
|
||||
|
||||
This sets the sort method to be used and if it should
|
||||
be inverted.
|
||||
|
||||
The first argument is the sort method name and the second is a
|
||||
boolean on if it should be inverted or not.
|
||||
|
||||
Leaving either undef resets the undef value back to the default.
|
||||
|
||||
The supported sorting methods are as below.
|
||||
|
||||
$ sorter->set_sort( $sort_method, $invert );
|
||||
if( $sorter->error ){
|
||||
warn( '"'.$sort_method.'"' is not a valid sort method );
|
||||
}
|
||||
|
||||
# reset to defaults
|
||||
$sorter->set_sort
|
||||
|
||||
# Set the sort method to host_f and invert.
|
||||
$sorter->set_sort( 'host_f', 1)
|
||||
|
||||
# leave the sort method the same while telling it to invert
|
||||
$sorter->set_sort( ( $sorter->get_sort )[0], 1 );
|
||||
|
||||
=cut
|
||||
|
||||
sub set_sort{
|
||||
my $self=$_[0];
|
||||
my $sort=$_[1];
|
||||
my $invert=$_[2];
|
||||
|
||||
if( ! $self->errorblank ){
|
||||
return undef;
|
||||
}
|
||||
|
||||
if (!defined( $sort ) ){
|
||||
$sort='host_ff';
|
||||
}
|
||||
|
||||
if ( ! defined( $self->{sort_check}{$sort} ) ){
|
||||
$self->{error}=1;
|
||||
$self->{errorString}='"'.$sort.'" is not a valid sort value';
|
||||
$self->warn;
|
||||
return undef;
|
||||
}
|
||||
|
||||
$self->{sort}=$sort;
|
||||
$self->{invert}=$invert;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
=head2 sort
|
||||
|
||||
Sorts the provided array from Parse::Netstat::Search.
|
||||
|
||||
my @sorted=$sorter->sort( \@found );
|
||||
|
||||
=cut
|
||||
|
||||
sub sort{
|
||||
my $self=$_[0];
|
||||
my @found;
|
||||
if (
|
||||
defined( $_[1] ) &&
|
||||
( ref($_[1]) eq 'ARRAY' )
|
||||
){
|
||||
@found=@{ $_[1] };
|
||||
}else{
|
||||
$self->{error}=2;
|
||||
$self->{errorString}='The passed item is either not a array or undefined';
|
||||
$self->warn;
|
||||
return undef;
|
||||
}
|
||||
|
||||
if( ! $self->errorblank ){
|
||||
return undef;
|
||||
}
|
||||
|
||||
# handle sorting if needed
|
||||
if ( $self->{sort} ne 'none' ){
|
||||
if( $self->{sort} eq 'host_ff' ){
|
||||
@found=sort {
|
||||
&host_sort_helper( $b->{foreign_host} ) <=> &host_sort_helper( $b->{foreign_host} ) or
|
||||
&host_sort_helper( $a->{local_host} ) <=> &host_sort_helper( $b->{local_host} )
|
||||
} @found;
|
||||
}elsif( $self->{sort} eq 'host_lf' ){
|
||||
@found=sort {
|
||||
&host_sort_helper( $a->{local_host} ) <=> &host_sort_helper( $b->{local_host} ) or
|
||||
&host_sort_helper( $b->{foreign_host} ) <=> &host_sort_helper( $b->{foreign_host} )
|
||||
} @found;
|
||||
}elsif( $self->{sort} eq 'host_f' ){
|
||||
@found=sort {
|
||||
&host_sort_helper( $b->{foreign_host} ) <=> &host_sort_helper( $b->{foreign_host} )
|
||||
} @found;
|
||||
}elsif( $self->{sort} eq 'host_l' ){
|
||||
@found=sort {
|
||||
&host_sort_helper( $a->{local_host} ) <=> &host_sort_helper( $b->{local_host} )
|
||||
} @found;
|
||||
}elsif( $self->{sort} eq 'port_ff' ){
|
||||
@found=sort {
|
||||
$a->{foreign_port} <=> $b->{foreign_port} or
|
||||
$a->{local_port} <=> $b->{local_port}
|
||||
} @found;
|
||||
}elsif( $self->{sort} eq 'port_lf' ){
|
||||
@found=sort {
|
||||
$a->{local_port} <=> $b->{local_port} or
|
||||
$a->{foreign_port} <=> $b->{foreign_port}
|
||||
} @found;
|
||||
}elsif( $self->{sort} eq 'port_f' ){
|
||||
@found=sort {
|
||||
$a->{foreign_port} <=> $b->{foreign_port}
|
||||
} @found;
|
||||
}elsif( $self->{sort} eq 'port_l' ){
|
||||
@found=sort {
|
||||
$a->{local_port} <=> $b->{local_port}
|
||||
} @found;
|
||||
}elsif( $self->{sort} eq 'state' ){
|
||||
@found=sort {
|
||||
$a->{state} cmp $b->{state}
|
||||
} @found;
|
||||
}elsif( $self->{sort} eq 'protocol' ){
|
||||
@found=sort {
|
||||
$a->{proto} cmp $b->{proto}
|
||||
} @found;
|
||||
}elsif( $self->{sort} eq 'q_rf' ){
|
||||
@found=sort {
|
||||
$a->{recvq} <=> $b->{recvq} or
|
||||
$a->{sendq} <=> $b->{sendq}
|
||||
} @found;
|
||||
}elsif( $self->{sort} eq 'q_sf' ){
|
||||
@found=sort {
|
||||
$a->{sendq} <=> $b->{sendq} or
|
||||
$a->{recvq} <=> $b->{recvq}
|
||||
} @found;
|
||||
}elsif( $self->{sort} eq 'q_r' ){
|
||||
@found=sort {
|
||||
$a->{recvq} <=> $b->{recvq}
|
||||
} @found;
|
||||
}elsif( $self->{sort} eq 'q_s' ){
|
||||
@found=sort {
|
||||
$a->{sendq} <=> $b->{sendq}
|
||||
} @found;
|
||||
}
|
||||
}
|
||||
|
||||
return @found;
|
||||
}
|
||||
|
||||
=head2 host_sort_helper
|
||||
|
||||
Internal function.
|
||||
|
||||
Takes a host and converts it to a number.
|
||||
|
||||
=cut
|
||||
|
||||
sub host_sort_helper{
|
||||
if (
|
||||
( !defined($_[0]) ) ||
|
||||
( $_[0] eq '*' )
|
||||
){
|
||||
return 0;
|
||||
}
|
||||
my $host=eval {Net::IP->new($_[0] )->intip} ;
|
||||
if (!defined( $host )){
|
||||
return 0;
|
||||
}
|
||||
return $host;
|
||||
}
|
||||
|
||||
=head1 ERROR CODES / FLAGS
|
||||
|
||||
Error handling is provided by L<Error::Helper>.
|
||||
|
||||
=head2 1 / badSort
|
||||
|
||||
Invalid value specified for sort.
|
||||
|
||||
=head2 2 / badArray
|
||||
|
||||
The passed item is not a array.
|
||||
|
||||
=head1 AUTHOR
|
||||
|
||||
Zame C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
|
||||
|
|
Loading…
Reference in New Issue