=head1 NAME VAdmind::System::Sockets.pm - Provide socket information. =head1 SYNOPSIS This plugin provide methods to verify currently used system sockets. my $plugin = MFLO::System::Sockets->new; =head1 AUTHOR Urivan Alyasid Flores =cut package VAdmind::Plugins::System::Sockets; use strict; use warnings; =head1 METHODS =head2 CONSTRUCTORS =head3 new Creates a new plugin object. $plugin = VAdmind::System::Sockets->new(); =cut sub new { my $type = shift; my $self = {@_}; $self->{'data'} = { 'cmd' => '/usr/sbin/lsof', 'type' => [ 'IPv4', 'IPv6', 'ax25', 'inet', 'sock', 'unix' ] }; return bless ($self, $type); } { sub parse_pset { $_ = shift; my %a = ( 'p' => undef, 'g' => undef, 'R' => undef, 'c' => undef, 'u' => undef, 'L' => undef ); for (split (/\0/,$_)) { $a{'p'} = $1 if /p(\d+)/; $a{'g'} = $1 if /d(\d+)/; $a{'R'} = $1 if /R(\d+)/; $a{'c'} = $1 if /c([\w\d]+)/; $a{'u'} = $1 if /u(\d+)/; $a{'L'} = $1 if /L(\w+)/; } return %a; } sub parse_fset { $_ = $_[0]; my %a = ( 'f' => undef, 'a' => undef, 'l' => undef, 't' => undef, 'd' => undef, 'P' => undef, 'laddr' => undef, 'lport' => undef, 'saddr' => undef, 'sport' => undef, 'daddr' => undef, 'dport' => undef, 'TST' => undef ); for (split(/\0/,$_)) { $a{'f'} = $1 if /f(\d+)/; $a{'a'} = $1 if /a(\w+)/; $a{'l'} = $1 if /l(\w+)/; $a{'t'} = $1 if /t(\w+)/; $a{'d'} = $1 if /d(\d+)/; $a{'P'} = $1 if /P(\w+)/; $a{'laddr'} = $1 if /n(.*):\d+$/; $a{'lport'} = $1 if /n.*:(\d+)$/; if (/n(.*):(\d+)->(.*):(\d+)/) { $a{'saddr'} = $1; $a{'sport'} = $2; $a{'daddr'} = $3; $a{'dport'} = $4; } $a{'TST'} = $1 if /TST=(\w+)/; } return %a; } } =head3 getByCmd Retrieve the socket information of a command. $plugin->getByCmd (CMD) Inputs: CMD - Command name to check for. Returns: 0 => Command listenning on tcp 1 => DRM component not configured. XML output: Where: A: Process ID B: User name C: Ipv4, Ipv6 D: TCP, UDP E: Port number F: LISTEN, ESTABLISHED, WAITING... =cut sub getByCmd { my $self = shift; my $in = $self->{'in'}; my $out = $self->{'out'}; for my $socket (@{$in->{'socket'}}) { $socket->{'cmd'} =~ s/["']//g; if (defined $socket->{'cmd'} && length ($socket->{'cmd'}) > 0) { #print "cmd: ".$socket->{'cmd'}."\n"; my $cmd = $self->{'data'}->{'cmd'} .' -F0pLtPT -i -nP 2>/dev/null'; #print "CMD: $cmd\n"; if (open (LSOF, $cmd .'|')) { my $index = 0; my %pset; my %fset; while () { chomp; %pset = parse_pset ($_) if /^[pgRcuL]/; if ($pset{'c'} == $socket->{'cmd'}) { } %fset = parse_fset ($_) if /^[faltdPnT]/; # chomp; # $_ =~ s/(\s *)/ /g; # my ($cmd, $pid, $user, $type, $mode, $number, $status) = (split (/ /,$_))[0,1,2,4,6,7,8]; # if ($cmd eq $in->{'socket'}->[0]->{'cmd'}) { # $out->{'xml'}->{'port'}->[$index]->{'pid'} = $pid; # $out->{'xml'}->{'port'}->[$index]->{'user'} = $user; # $out->{'xml'}->{'port'}->[$index]->{'type'} = $type; # $out->{'xml'}->{'port'}->[$index]->{'mode'} = $mode; # $out->{'xml'}->{'port'}->[$index]->{'number'} = $number; # $out->{'xml'}->{'port'}->[$index]->{'status'} = $status; # $index++; # } } } else { $out->{'result'} = 1; $out->{'error'} = 'Unable to read socket list.'; } } else { $out->{'result'} = 1; $out->{'error'} = 'Missing required value: cmd. '; } } #if (defined $in->{'cmd'} && ref \$in->{'cmd'}->[0] eq 'SCALAR' && length($in->{'cmd'}->[0])>0) { } 1;