File Coverage

blib/lib/Sys/Statistics/Linux/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             =head1 NAME
2              
3             Sys::Statistics::Linux::FileStats - Collect linux file statistics.
4              
5             =head1 SYNOPSIS
6              
7             use Sys::Statistics::Linux::FileStats;
8              
9             my $lxs = Sys::Statistics::Linux::FileStats->new;
10             my $stat = $lxs->get;
11              
12             =head1 DESCRIPTION
13              
14             Sys::Statistics::Linux::FileStats gathers file statistics from the virtual F filesystem (procfs).
15              
16             For more information read the documentation of the front-end module L.
17              
18             =head1 FILE STATISTICS
19              
20             Generated by F, F and F.
21              
22             fhalloc - Number of allocated file handles.
23             fhfree - Number of free file handles.
24             fhmax - Number of maximum file handles.
25             inalloc - Number of allocated inodes.
26             infree - Number of free inodes.
27             inmax - Number of maximum inodes.
28             dentries - Dirty directory cache entries.
29             unused - Free diretory cache size.
30             agelimit - Time in seconds the dirty cache entries can be reclaimed.
31             wantpages - Pages that are requested by the system when memory is short.
32              
33             =head1 METHODS
34              
35             =head2 new()
36              
37             Call C to create a new object.
38              
39             my $lxs = Sys::Statistics::Linux::FileStats->new;
40              
41             It's possible to set the path to the proc filesystem.
42              
43             Sys::Statistics::Linux::FileStats->new(
44             files => {
45             # This is the default
46             path => '/proc',
47             file_nr => 'sys/fs/file-nr',
48             inode_nr => 'sys/fs/inode-nr',
49             dentries => 'sys/fs/dentry-state',
50             }
51             );
52              
53             =head2 get()
54              
55             Call C to get the statistics. C returns the statistics as a hash reference.
56              
57             my $stat = $lxs->get;
58              
59             =head1 EXPORTS
60              
61             No exports.
62              
63             =head1 SEE ALSO
64              
65             B
66              
67             =head1 REPORTING BUGS
68              
69             Please report all bugs to .
70              
71             =head1 AUTHOR
72              
73             Jonny Schulz .
74              
75             =head1 COPYRIGHT
76              
77             Copyright (c) 2006, 2007 by Jonny Schulz. All rights reserved.
78              
79             This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
80              
81             =cut
82              
83             package Sys::Statistics::Linux::FileStats;
84              
85 1     1   6 use strict;
  1         2  
  1         42  
86 1     1   5 use warnings;
  1         2  
  1         33  
87 1     1   4 use Carp qw(croak);
  1         1  
  1         765  
88              
89             our $VERSION = '0.09';
90              
91             sub new {
92 1     1 1 3 my $class = shift;
93 1 50       3 my $opts = ref($_[0]) ? shift : {@_};
94              
95 1         4 my %self = (
96             files => {
97             path => '/proc',
98             file_nr => 'sys/fs/file-nr',
99             inode_nr => 'sys/fs/inode-nr',
100             dentries => 'sys/fs/dentry-state',
101             }
102             );
103              
104 1         2 foreach my $file (keys %{ $opts->{files} }) {
  1         3  
105 0         0 $self{files}{$file} = $opts->{files}->{$file};
106             }
107              
108 1         7 return bless \%self, $class;
109             }
110              
111             sub get {
112 1     1 1 2 my $self = shift;
113 1         3 my $class = ref $self;
114 1         4 my $file = $self->{files};
115 1         3 my $stats = { };
116              
117 1         3 $self->{stats} = $stats;
118 1         3 $self->_get_file_nr;
119 1         2 $self->_get_inode_nr;
120 1         3 $self->_get_dentries;
121              
122 1         4 return $stats;
123             }
124              
125             sub _get_file_nr {
126 1     1   1 my $self = shift;
127 1         2 my $class = ref $self;
128 1         2 my $file = $self->{files};
129 1         1 my $stats = $self->{stats};
130              
131 1 50       8 my $filename = $file->{path} ? "$file->{path}/$file->{file_nr}" : $file->{file_nr};
132 1 50       41 open my $fh, '<', $filename or croak "$class: unable to open $filename ($!)";
133 1         28 @$stats{qw(fhalloc fhfree fhmax)} = (split /\s+/, <$fh>)[0..2];
134 1         10 close($fh);
135             }
136              
137             sub _get_inode_nr {
138 1     1   2 my $self = shift;
139 1         2 my $class = ref $self;
140 1         1 my $file = $self->{files};
141 1         1 my $stats = $self->{stats};
142              
143 1 50       3 my $filename = $file->{path} ? "$file->{path}/$file->{inode_nr}" : $file->{inode_nr};
144 1 50       24 open my $fh, '<', $filename or croak "$class: unable to open $filename ($!)";
145 1         14 @$stats{qw(inalloc infree)} = (split /\s+/, <$fh>)[0..1];
146 1         4 $stats->{inmax} = $stats->{inalloc} + $stats->{infree};
147 1         6 close($fh);
148             }
149              
150             sub _get_dentries {
151 1     1   1 my $self = shift;
152 1         2 my $class = ref $self;
153 1         1 my $file = $self->{files};
154 1         2 my $stats = $self->{stats};
155              
156 1 50       3 my $filename = $file->{path} ? "$file->{path}/$file->{dentries}" : $file->{dentries};
157 1 50       23 open my $fh, '<', $filename or croak "$class: unable to open $filename ($!)";
158 1         16 @$stats{qw(dentries unused agelimit wantpages)} = (split /\s+/, <$fh>)[0..3];
159 1         9 close($fh);
160             }
161              
162             1;