File Coverage

blib/lib/Linux/Info/SockStats.pm
Criterion Covered Total %
statement 30 31 96.7
branch 13 16 81.2
condition n/a
subroutine 5 5 100.0
pod 2 2 100.0
total 50 54 92.5


line stmt bran cond sub pod time code
1             package Linux::Info::SockStats;
2 1     1   7 use strict;
  1         2  
  1         33  
3 1     1   5 use warnings;
  1         1  
  1         30  
4 1     1   4 use Carp qw(croak);
  1         2  
  1         502  
5             our $VERSION = '1.5'; # VERSION
6              
7             =head1 NAME
8              
9             Linux::Info::SockStats - Collect linux socket statistics.
10              
11             =head1 SYNOPSIS
12              
13             use Linux::Info::SockStats;
14              
15             my $lxs = Linux::Info::SockStats->new;
16             my $stat = $lxs->get;
17              
18             =head1 DESCRIPTION
19              
20             Linux::Info::SockStats gathers socket statistics from the virtual F filesystem (procfs).
21              
22             For more information read the documentation of the front-end module L.
23              
24             =head1 SOCKET STATISTICS
25              
26             Generated by F.
27              
28             used - Total number of used sockets.
29             tcp - Number of tcp sockets in use.
30             udp - Number of udp sockets in use.
31             raw - Number of raw sockets in use.
32             ipfrag - Number of ip fragments in use (only available by kernels > 2.2).
33              
34             =head1 METHODS
35              
36             =head2 new()
37              
38             Call C to create a new object.
39              
40             my $lxs = Linux::Info::SockStats->new;
41              
42             It's possible to set the path to the proc filesystem.
43              
44             Linux::Info::SockStats->new(
45             files => {
46             # This is the default
47             path => '/proc',
48             sockstat => 'net/sockstat',
49             }
50             );
51              
52             =head2 get()
53              
54             Call C to get the statistics. C returns the statistics as a hash reference.
55              
56             my $stat = $lxs->get;
57              
58             =head1 EXPORTS
59              
60             Nothing.
61              
62             =head1 SEE ALSO
63              
64             =over
65              
66             =item *
67              
68             B
69              
70             =item *
71              
72             L
73              
74             =back
75              
76             =head1 AUTHOR
77              
78             Alceu Rodrigues de Freitas Junior, Earfreitas@cpan.orgE
79              
80             =head1 COPYRIGHT AND LICENSE
81              
82             This software is copyright (c) 2015 of Alceu Rodrigues de Freitas Junior, Earfreitas@cpan.orgE
83              
84             This file is part of Linux Info project.
85              
86             Linux-Info is free software: you can redistribute it and/or modify
87             it under the terms of the GNU General Public License as published by
88             the Free Software Foundation, either version 3 of the License, or
89             (at your option) any later version.
90              
91             Linux-Info is distributed in the hope that it will be useful,
92             but WITHOUT ANY WARRANTY; without even the implied warranty of
93             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
94             GNU General Public License for more details.
95              
96             You should have received a copy of the GNU General Public License
97             along with Linux Info. If not, see .
98              
99             =cut
100              
101             sub new {
102 1     1 1 2 my $class = shift;
103 1 50       5 my $opts = ref( $_[0] ) ? shift : {@_};
104              
105 1         5 my %self = (
106             files => {
107             path => '/proc',
108             sockstat => 'net/sockstat',
109             }
110             );
111              
112 1         2 foreach my $file ( keys %{ $opts->{files} } ) {
  1         5  
113 0         0 $self{files}{$file} = $opts->{files}->{$file};
114             }
115              
116 1         6 return bless \%self, $class;
117             }
118              
119             sub get {
120 1     1 1 2 my $self = shift;
121 1         2 my $class = ref $self;
122 1         4 my $file = $self->{files};
123 1         1 my %socks = ();
124              
125             my $filename =
126 1 50       4 $file->{path} ? "$file->{path}/$file->{sockstat}" : $file->{sockstat};
127 1 50       53 open my $fh, '<', $filename
128             or croak "$class: unable to open $filename ($!)";
129              
130 1         28 while ( my $line = <$fh> ) {
131 6 100       34 if ( $line =~ /sockets: used (\d+)/ ) {
    100          
    100          
    100          
    100          
132 1         6 $socks{used} = $1;
133             }
134             elsif ( $line =~ /TCP: inuse (\d+)/ ) {
135 1         4 $socks{tcp} = $1;
136             }
137             elsif ( $line =~ /UDP: inuse (\d+)/ ) {
138 1         4 $socks{udp} = $1;
139             }
140             elsif ( $line =~ /RAW: inuse (\d+)/ ) {
141 1         4 $socks{raw} = $1;
142             }
143             elsif ( $line =~ /FRAG: inuse (\d+)/ ) {
144 1         12 $socks{ipfrag} = $1;
145             }
146             }
147              
148 1         14 close($fh);
149 1         9 return \%socks;
150             }
151              
152             1;