File Coverage

blib/lib/Linux/Info/FileStats.pm
Criterion Covered Total %
statement 49 50 98.0
branch 7 14 50.0
condition n/a
subroutine 8 8 100.0
pod 2 2 100.0
total 66 74 89.1


line stmt bran cond sub pod time code
1             package Linux::Info::FileStats;
2 1     1   6 use strict;
  1         2  
  1         33  
3 1     1   5 use warnings;
  1         1  
  1         26  
4 1     1   4 use Carp qw(croak);
  1         2  
  1         583  
5             our $VERSION = '1.3'; # VERSION
6              
7             =head1 NAME
8              
9             Linux::Info::FileStats - Collect linux file statistics.
10              
11             =head1 SYNOPSIS
12              
13             use Linux::Info::FileStats;
14              
15             my $lxs = Linux::Info::FileStats->new;
16             my $stat = $lxs->get;
17              
18             =head1 DESCRIPTION
19              
20             Linux::Info::FileStats gathers file statistics from the virtual F filesystem (procfs).
21              
22             For more information read the documentation of the front-end module L.
23              
24             =head1 FILE STATISTICS
25              
26             Generated by F, F and F.
27              
28             fhalloc - Number of allocated file handles.
29             fhfree - Number of free file handles.
30             fhmax - Number of maximum file handles.
31             inalloc - Number of allocated inodes.
32             infree - Number of free inodes.
33             inmax - Number of maximum inodes.
34             dentries - Dirty directory cache entries.
35             unused - Free diretory cache size.
36             agelimit - Time in seconds the dirty cache entries can be reclaimed.
37             wantpages - Pages that are requested by the system when memory is short.
38              
39             If youre looking for information about inodes usage in a mouint point, most probably you want to take a look at L module.
40             This module gets information about inodes from /proc as L does, but documentation about this specific part of /proc is obscure,
41             so you're on your own to find out which module to use.
42              
43             =head1 METHODS
44              
45             =head2 new()
46              
47             Call C to create a new object.
48              
49             my $lxs = Linux::Info::FileStats->new;
50              
51             It's possible to set the path to the proc filesystem.
52              
53             Linux::Info::FileStats->new(
54             files => {
55             # This is the default
56             path => '/proc',
57             file_nr => 'sys/fs/file-nr',
58             inode_nr => 'sys/fs/inode-nr',
59             dentries => 'sys/fs/dentry-state',
60             }
61             );
62              
63             =head2 get()
64              
65             Call C to get the statistics. C returns the statistics as a hash reference.
66              
67             my $stat = $lxs->get;
68              
69             =head1 EXPORTS
70              
71             Nothing.
72              
73             =head1 SEE ALSO
74              
75             =over
76              
77             =item *
78              
79             B
80              
81             =item *
82              
83             L
84              
85             =item *
86              
87             L
88              
89             =back
90              
91             =head1 AUTHOR
92              
93             Alceu Rodrigues de Freitas Junior, Earfreitas@cpan.orgE
94              
95             =head1 COPYRIGHT AND LICENSE
96              
97             This software is copyright (c) 2015 of Alceu Rodrigues de Freitas Junior, Earfreitas@cpan.orgE
98              
99             This file is part of Linux Info project.
100              
101             Linux-Info is free software: you can redistribute it and/or modify
102             it under the terms of the GNU General Public License as published by
103             the Free Software Foundation, either version 3 of the License, or
104             (at your option) any later version.
105              
106             Linux-Info is distributed in the hope that it will be useful,
107             but WITHOUT ANY WARRANTY; without even the implied warranty of
108             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
109             GNU General Public License for more details.
110              
111             You should have received a copy of the GNU General Public License
112             along with Linux Info. If not, see .
113              
114             =cut
115              
116             sub new {
117 1     1 1 2 my $class = shift;
118 1 50       4 my $opts = ref($_[0]) ? shift : {@_};
119              
120 1         5 my %self = (
121             files => {
122             path => '/proc',
123             file_nr => 'sys/fs/file-nr',
124             inode_nr => 'sys/fs/inode-nr',
125             dentries => 'sys/fs/dentry-state',
126             }
127             );
128              
129 1         2 foreach my $file (keys %{ $opts->{files} }) {
  1         3  
130 0         0 $self{files}{$file} = $opts->{files}->{$file};
131             }
132              
133 1         6 return bless \%self, $class;
134             }
135              
136             sub get {
137 1     1 1 3 my $self = shift;
138 1         2 my $class = ref $self;
139 1         4 my $file = $self->{files};
140 1         2 my $stats = { };
141              
142 1         2 $self->{stats} = $stats;
143 1         3 $self->_get_file_nr;
144 1         3 $self->_get_inode_nr;
145 1         3 $self->_get_dentries;
146              
147 1         4 return $stats;
148             }
149              
150             sub _get_file_nr {
151 1     1   2 my $self = shift;
152 1         2 my $class = ref $self;
153 1         2 my $file = $self->{files};
154 1         2 my $stats = $self->{stats};
155              
156 1 50       5 my $filename = $file->{path} ? "$file->{path}/$file->{file_nr}" : $file->{file_nr};
157 1 50       27 open my $fh, '<', $filename or croak "$class: unable to open $filename ($!)";
158 1         16 @$stats{qw(fhalloc fhfree fhmax)} = (split /\s+/, <$fh>)[0..2];
159 1         8 close($fh);
160             }
161              
162             sub _get_inode_nr {
163 1     1   2 my $self = shift;
164 1         2 my $class = ref $self;
165 1         2 my $file = $self->{files};
166 1         1 my $stats = $self->{stats};
167              
168 1 50       5 my $filename = $file->{path} ? "$file->{path}/$file->{inode_nr}" : $file->{inode_nr};
169 1 50       15 open my $fh, '<', $filename or croak "$class: unable to open $filename ($!)";
170 1         16 @$stats{qw(inalloc infree)} = (split /\s+/, <$fh>)[0..1];
171 1         4 $stats->{inmax} = $stats->{inalloc} + $stats->{infree};
172 1         6 close($fh);
173             }
174              
175             sub _get_dentries {
176 1     1   2 my $self = shift;
177 1         2 my $class = ref $self;
178 1         2 my $file = $self->{files};
179 1         2 my $stats = $self->{stats};
180              
181 1 50       4 my $filename = $file->{path} ? "$file->{path}/$file->{dentries}" : $file->{dentries};
182 1 50       14 open my $fh, '<', $filename or croak "$class: unable to open $filename ($!)";
183 1         13 @$stats{qw(dentries unused agelimit wantpages)} = (split /\s+/, <$fh>)[0..3];
184 1         5 close($fh);
185             }
186              
187             1;