File Coverage

blib/lib/DBD/Sys/Plugin/Unix/Lsof.pm
Criterion Covered Total %
statement 14 51 27.4
branch 0 20 0.0
condition n/a
subroutine 6 8 75.0
pod 4 4 100.0
total 24 83 28.9


line stmt bran cond sub pod time code
1             package DBD::Sys::Plugin::Unix::Lsof;
2              
3 3     3   4087 use strict;
  3         6  
  3         95  
4 3     3   13 use warnings;
  3         6  
  3         83  
5 3     3   15 use vars qw($VERSION @colNames);
  3         5  
  3         144  
6              
7 3     3   13 use base qw(DBD::Sys::Table);
  3         5  
  3         1998  
8              
9             $VERSION = "0.102";
10             @colNames = qw(pid ppid pgrp uid username command filename filetype inode linkcount mountpoint);
11              
12             =pod
13              
14             =head1 NAME
15              
16             DBD::Sys::Plugin::Unix::Lsof - provides a table containing open files
17              
18             =head1 SYNOPSIS
19              
20             $openfiles = $dbh->selectall_hashref("select * from openfiles", ["pid","filename"]);
21              
22             =head1 ISA
23              
24             DBD::Sys::Plugin::Unix::Lsof;
25             ISA DBD::Sys::Table
26              
27             =head1 DESCRIPTION
28              
29             This module provides the table I filled with the list of open
30             files.
31              
32             =head2 COLUMNS
33              
34             =head3 pid
35              
36             Process ID of the process opened the file
37              
38             =head3 ppid
39              
40             Parent process ID of the process opened the file
41              
42             =head3 pgrp
43              
44             Process Group ID of the process opened the file
45              
46             =head3 uid
47              
48             User ID
49              
50             =head3 username
51              
52             Name of the user who owns the process which has opened the file
53              
54             =head3 command
55              
56             executed command (process executable)
57              
58             =head3 filename
59              
60             Full qualified path name of the open file
61              
62             =head3 filetype
63              
64             File type (VDIR, VREG, ...) of the open file
65              
66             =head3 inode
67              
68             Inode number of the open file
69              
70             =head3 linkcount
71              
72             Link count of the open file
73              
74             =head3 mountpoint
75              
76             Mount point of the file system where the file resides
77              
78             =head1 METHODS
79              
80             =head2 get_table_name
81              
82             Returns 'grent'.
83              
84             =cut
85              
86 4     4 1 12 sub get_table_name() { return 'openfiles'; }
87              
88             =head2 get_col_names
89              
90             Returns the column names of the table as named in L
91              
92             =cut
93              
94 0     0 1 0 sub get_col_names() { @colNames }
95              
96             =head2 get_attributes
97              
98             Return the attributes supported by this module:
99              
100             =head3 uids
101              
102             Allows restricting the user ids (see lsof(8) for the C<-u> parameter).
103              
104             $dbh->{sys_openfiles_uids} = [scalar getpwuid $<];
105             $dbh->{sys_openfiles_uids} = [$<]; # all opened by myself
106              
107             =head3 pids
108              
109             Allows restricting the process ids (see lsof(8) for the C<-p> parameter).
110              
111             $dbh->{sys_openfiles_pids} = ['^' . $$]; # everything except the current process
112              
113             =head3 filesys
114              
115             Allows restricting the scanned file systems (see lsof(8) for the C<+f>
116             parameter).
117              
118             $dbh->{sys_openfiles_filesys} = [qw(/usr /var)];
119              
120             =cut
121              
122 4     4 1 15 sub get_attributes() { return qw(uids pids filesys) }
123              
124             my $havelsof;
125             my $havesysfsmountpoint;
126              
127             =head2 collect_data
128              
129             Retrieves the data from the lsof command and put it into fetchable rows.
130              
131             =cut
132              
133             sub collect_data()
134             {
135 0     0 1   my $self = $_[0];
136 0           my @data;
137              
138 0 0         unless ( defined($havelsof) )
139             {
140 0           $havelsof = 0;
141 0           eval {
142 0           require Unix::Lsof;
143 0           $havelsof = 1;
144             };
145             }
146              
147 0 0         unless ( defined($havesysfsmountpoint) )
148             {
149 0           $havesysfsmountpoint = 0;
150 0           eval {
151 0           require Sys::Filesystem::MountPoint;
152 0           $havesysfsmountpoint = 1;
153             };
154             }
155              
156 0 0         if ($havelsof)
157             {
158 0           my @args;
159 0 0         if ( $self->{meta}->{uids} )
160             {
161 0           push( @args, '-u' );
162 0           push( @args,
163             ref( $self->{meta}->{uids} ) eq 'ARRAY'
164 0 0         ? join( ',', @{ $self->{meta}->{uids} } )
165             : $self->{meta}->{uids} );
166             }
167 0 0         if ( $self->{meta}->{pids} )
168             {
169 0           push( @args, '-p' );
170 0           push( @args,
171             ref( $self->{meta}->{pids} ) eq 'ARRAY'
172 0 0         ? join( ',', @{ $self->{meta}->{pids} } )
173             : $self->{meta}->{pids} );
174             }
175 0 0         if ( $self->{meta}->{filesys} )
176             {
177 0           push( @args,
178             ref( $self->{meta}->{filesys} ) eq 'ARRAY'
179 0 0         ? @{ $self->{meta}->{uids} }
180             : $self->{meta}->{filesys} );
181             }
182              
183 0           my ( $output, $error ) = Unix::Lsof::lsof(@args);
184 0           foreach my $pid ( keys %{$output} )
  0            
185             {
186 0           my $pinfo = $output->{$pid};
187             my @pfields = @$pinfo{
188 0           'process id',
189             'parent pid',
190             'process group id',
191             'user id',
192             'login name',
193             'command name'
194             };
195 0           foreach my $pfile ( @{ $pinfo->{files} } )
  0            
196             {
197 0           my @row =
198             ( @pfields, @$pfile{ 'file name', 'file type', 'inode number', 'link count' } );
199 0 0         push( @row,
200             $havesysfsmountpoint
201             ? Sys::Filesystem::MountPoint::path_to_mount_point( $pfile->{'file name'} )
202             : undef );
203 0           push( @data, \@row );
204             }
205             }
206             }
207              
208 0           return \@data;
209             }
210              
211             =head1 PREREQUISITES
212              
213             The module L is required to provide data for the table. The
214             column mountpoint can be filled only if the module
215             L is installed.
216              
217             =head1 AUTHOR
218              
219             Jens Rehsack
220             CPAN ID: REHSACK
221             rehsack@cpan.org
222             http://www.rehsack.de/
223              
224             =head1 COPYRIGHT
225              
226             This program is free software; you can redistribute
227             it and/or modify it under the same terms as Perl itself.
228              
229             The full text of the license can be found in the
230             LICENSE file included with this module.
231              
232             =head1 SUPPORT
233              
234             Free support can be requested via regular CPAN bug-tracking system. There is
235             no guaranteed reaction time or solution time, but it's always tried to give
236             accept or reject a reported ticket within a week. It depends on business load.
237             That doesn't mean that ticket via rt aren't handles as soon as possible,
238             that means that soon depends on how much I have to do.
239              
240             Business and commercial support should be acquired from the authors via
241             preferred freelancer agencies.
242              
243             =cut
244              
245             1;