File Coverage

blib/lib/Device/Network/ConfigParser/Linux/NetUtils.pm
Criterion Covered Total %
statement 24 30 80.0
branch n/a
condition n/a
subroutine 9 12 75.0
pod 4 4 100.0
total 37 46 80.4


line stmt bran cond sub pod time code
1             package Device::Network::ConfigParser::Linux::NetUtils;
2             # ABSTRACT: Parse the output from net-utils utilities (ifconfig, route, arp, etc)
3             our $VERSION = '0.006'; # VERSION
4              
5 1     1   1362 use 5.006;
  1         6  
6 1     1   7 use strict;
  1         2  
  1         21  
7 1     1   4 use warnings;
  1         2  
  1         30  
8 1     1   247 use Modern::Perl;
  1         6683  
  1         6  
9 1     1   1019 use Parse::RecDescent;
  1         26524  
  1         8  
10 1     1   523 use Data::Dumper;
  1         5250  
  1         91  
11 1     1   367 use JSON;
  1         6128  
  1         7  
12              
13 1     1   166 use Exporter qw{import};
  1         3  
  1         190  
14              
15             our @EXPORT_OK = qw{get_parser get_output_drivers parse_config post_process};
16              
17             =head1 NAME
18              
19             Device::Network::ConfigParser::CheckPoint::Expert - parse CheckPoint expert mode output.
20              
21             =head1 VERSION
22              
23             version 0.006
24              
25             =head1 SYNOPSIS
26              
27             This module is intended to be used in conjunction with L, however there's nothing stopping it being used on its own.
28              
29             The module provides subroutines to parse & post-process the output from net-tools utilities such as ifconfig, route, arp, etc.
30              
31             =head1 SUBROUTINES
32              
33             =head2 get_parser
34              
35             For more information on the subroutine, see L.
36              
37             Thos module currently recognises output from the following utilities:
38              
39             =over 4
40              
41             =item * 'ifconfig' output
42              
43             =back
44              
45             =cut
46              
47             sub get_parser {
48 1     1 1 547 return new Parse::RecDescent(q{
49            
50             startrule: config_line(s) { $item[1] }
51             config_line:
52             ifconfig(s) { { ifconfig => $item{'ifconfig(s)'} } } |
53             not_parsed { $item[1] }
54              
55             ifconfig:
56             interface encap hw_addr(?) inet(?) inet6(s?) flag(s) mtu if_metric rx_stats tx_stats rx_bytes tx_bytes {
57             {
58             interface => $item{interface},
59             encapsulation => $item{encap},
60             hw_addr => $item{'hw_addr(?)'},
61             inet => $item{'inet(?)'},
62             inet6 => $item{'inet6(s?)'},
63             flags => $item{'flag(s)'},
64             mtu => $item{mtu},
65             metric => $item{if_metric},
66             rx_stats => $item{rx_stats},
67             tx_stats => $item{tx_stats},
68             rx_bytes => $item{rx_bytes},
69             tx_bytes => $item{tx_bytes},
70             }
71             }
72              
73             interface: m{[-\w]+} { $item{__PATTERN1__} }
74             encap: 'Link encap:' m{Ethernet|Local Loopback} { $item{__PATTERN1__} }
75             hw_addr: 'HWaddr' m{[0-9a-f:]+} { $item{__PATTERN1__} }
76             inet:
77             inet_addr inet_bcast(?) inet_mask {
78             {
79             address => $item{inet_addr},
80             mask => $item{inet_mask},
81             broadcast => $item{'inet_bcast(?)'}
82             }
83             }
84              
85             inet_addr: 'inet addr:' ipv4 { $item{ipv4} }
86             inet_bcast: 'Bcast:' ipv4 { $item{ipv4} }
87             inet_mask: 'Mask:' netmask { $item{netmask} }
88             inet6: inet6_addr inet6_mask inet6_scope {
89             {
90             address => $item{inet6_addr},
91             mask => $item{inet6_mask},
92             scope => $item{inet6_scope}
93             }
94             }
95             inet6_addr: 'inet6 addr:' ipv6 { $item{ipv6} }
96             inet6_mask: '/' m{\d{1,3}} { $item{__PATTERN1__} }
97             inet6_scope: 'Scope:' m{\w+} { $item{__PATTERN1__} }
98              
99             flag: m{UP|BROADCAST|RUNNING|MULTICAST|LOOPBACK} { $item{__PATTERN1__} }
100             mtu: 'MTU:' m{\d+} { $item{__PATTERN1__} }
101             if_metric: 'Metric:' m{\d+} { $item{__PATTERN1__} }
102             rx_stats: 'RX packets:' m{\d+} 'errors:' m{\d+} 'dropped:' m{\d+} 'overruns:' m{\d+} 'frame:' m{\d+} {
103             {
104             packets => $item{__PATTERN1__},
105             errors => $item{__PATTERN2__},
106             dropped => $item{__PATTERN3__},
107             overruns => $item{__PATTERN4__},
108             frame => $item{__PATTERN5__},
109             }
110             }
111             tx_stats: 'TX packets:' m{\d+} 'errors:' m{\d+} 'dropped:' m{\d+} 'overruns:' m{\d+} 'carrier:' m{\d+} 'collisions:' m{\d+} 'txqueuelen:' m{\d+}{
112             {
113             packets => $item{__PATTERN1__},
114             errors => $item{__PATTERN2__},
115             dropped => $item{__PATTERN3__},
116             overruns => $item{__PATTERN4__},
117             carrier => $item{__PATTERN5__},
118             collisions => $item{__PATTERN5__},
119             txqueuelen => $item{__PATTERN5__},
120             }
121             }
122             rx_bytes: 'RX bytes:' m{\d+} m{\(\d{1,}\.\d \w{1,2}\)} { $item{__PATTERN1__} }
123             tx_bytes: 'TX bytes:' m{\d+} m{\(\d{1,}\.\d \w{1,2}\)} { $item{__PATTERN1__} }
124              
125              
126             not_parsed: m{\N+} { { type => $item{__RULE__}, line => $item{__PATTERN1__} } }
127              
128              
129             ipv4: m{\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}} { $item{__PATTERN1__} }
130             netmask: m{\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}} { $item{__PATTERN1__} }
131             ipv6: m{[0-9a-f:]+} { $item{__PATTERN1__} }
132             cidr: '/' m{\d{1,2}} { $item{__PATTERN1__} }
133             });
134             }
135              
136              
137             =head2 parse_config
138              
139             For more information on the subroutine, see L.
140              
141             =cut
142              
143             sub parse_config {
144 0     0 1   my ($parser, $config_contents) = @_;
145              
146 0           my $parse_tree = $parser->startrule($config_contents);
147              
148 0           return $parse_tree;
149             }
150              
151              
152              
153             =head2 post_process
154              
155             For more information on the subroutine, see L.
156              
157             This module does not post-process the data structure.
158              
159             =cut
160              
161             sub post_process {
162 0     0 1   my ($parsed_config) = @_;
163              
164 0           return $parsed_config;
165              
166             }
167              
168             =head2 get_output_drivers
169              
170             For more information on the subroutine, see L.
171              
172             This module does not export any output drivers.
173              
174             =cut
175              
176             sub get_output_drivers {
177             return {
178 0     0 1   };
179             }
180              
181              
182             =head1 AUTHOR
183              
184             Greg Foletta, C<< >>
185              
186             =head1 BUGS
187              
188             Please report any bugs or feature requests to C, or through
189             the web interface at L. I will be notified, and then you'll
190             automatically be notified of progress on your bug as I make changes.
191              
192              
193              
194              
195             =head1 SUPPORT
196              
197             You can find documentation for this module with the perldoc command.
198              
199             perldoc Device::CheckPoint::ConfigParse
200              
201              
202             You can also look for information at:
203              
204             =over 4
205              
206             =item * RT: CPAN's request tracker (report bugs here)
207              
208             L
209              
210             =item * AnnoCPAN: Annotated CPAN documentation
211              
212             L
213              
214             =item * CPAN Ratings
215              
216             L
217              
218             =item * Search CPAN
219              
220             L
221              
222             =back
223              
224              
225             =head1 ACKNOWLEDGEMENTS
226              
227              
228             =head1 LICENSE AND COPYRIGHT
229              
230             Copyright 2017 Greg Foletta.
231              
232             This program is free software; you can redistribute it and/or modify it
233             under the terms of the the Artistic License (2.0). You may obtain a
234             copy of the full license at:
235              
236             L
237              
238             Any use, modification, and distribution of the Standard or Modified
239             Versions is governed by this Artistic License. By using, modifying or
240             distributing the Package, you accept this license. Do not use, modify,
241             or distribute the Package, if you do not accept this license.
242              
243             If your Modified Version has been derived from a Modified Version made
244             by someone other than you, you are nevertheless required to ensure that
245             your Modified Version complies with the requirements of this license.
246              
247             This license does not grant you the right to use any trademark, service
248             mark, tradename, or logo of the Copyright Holder.
249              
250             This license includes the non-exclusive, worldwide, free-of-charge
251             patent license to make, have made, use, offer to sell, sell, import and
252             otherwise transfer the Package with respect to any patent claims
253             licensable by the Copyright Holder that are necessarily infringed by the
254             Package. If you institute patent litigation (including a cross-claim or
255             counterclaim) against any party alleging that the Package constitutes
256             direct or contributory patent infringement, then this Artistic License
257             to you shall terminate on the date that such litigation is filed.
258              
259             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
260             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
261             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
262             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
263             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
264             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
265             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
266             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
267              
268              
269             =cut
270              
271             1; # End of Device::CheckPoint::ConfigParse