File Coverage

lib/Rex/Hardware.pm
Criterion Covered Total %
statement 75 78 96.1
branch 4 6 66.6
condition n/a
subroutine 21 21 100.0
pod 1 2 50.0
total 101 107 94.3


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             =head1 NAME
6              
7             Rex::Hardware - Base Class for hardware / information gathering
8              
9             =head1 DESCRIPTION
10              
11             This module is the base class for hardware/information gathering.
12              
13             =head1 SYNOPSIS
14              
15             use Rex::Hardware;
16              
17             my %host_info = Rex::Hardware->get(qw/ Host /);
18             my %all_info = Rex::Hardware->get(qw/ All /);
19              
20             =head1 CLASS METHODS
21              
22             =cut
23              
24             package Rex::Hardware;
25              
26 71     71   1394 use v5.12.5;
  71         283  
27 71     71   497 use warnings;
  71         270  
  71         3101  
28              
29             our $VERSION = '1.14.2.2'; # TRIAL VERSION
30              
31 71     71   750 use Rex::Logger;
  71         265  
  71         659  
32              
33             require Rex::Args;
34              
35             =head2 get(@modules)
36              
37             Returns a hash with the wanted information.
38              
39             task "get-info", "server1", sub {
40             %hw_info = Rex::Hardware->get(qw/ Host Network /);
41             };
42              
43             Or if you want to get all information
44              
45             task "get-all-info", "server1", sub {
46             %hw_info = Rex::Hardware->get(qw/ All /);
47             };
48              
49             Available modules:
50              
51             =over 4
52              
53             =item Host
54              
55             =item Kernel
56              
57             =item Memory
58              
59             =item Network
60              
61             =item Swap
62              
63             =item VirtInfo
64              
65             =back
66              
67             =cut
68              
69             my %HW_PROVIDER;
70              
71             sub register_hardware_provider {
72 3     3 0 113 my ( $class, $service_name, $service_class ) = @_;
73 3         110 $HW_PROVIDER{"\L$service_name"} = $service_class;
74 3         49 return 1;
75             }
76              
77             sub get {
78 42     42 1 642 my ( $class, @modules ) = @_;
79              
80 42         274 my %hardware_information;
81              
82 42 100       320 if ( "all" eq "\L$modules[0]" ) {
83              
84 12         241 @modules = qw(Host Kernel Memory Network Swap VirtInfo);
85 12         199 push( @modules, keys(%HW_PROVIDER) );
86              
87             }
88              
89 42         314 for my $mod_string (@modules) {
90              
91 87         679 Rex::Commands::profiler()->start("hardware: $mod_string");
92              
93 86         652 my $mod = "Rex::Hardware::$mod_string";
94 86 50       424 if ( exists $HW_PROVIDER{$mod_string} ) {
95 2         68 $mod = $HW_PROVIDER{$mod_string};
96             }
97              
98 86         786 Rex::Logger::debug("Loading $mod");
99 86     9   14772 eval "use $mod";
  6     8   64  
  6     8   15  
  6     6   65  
  6     4   87  
  6     4   20  
  6     3   94  
  6     3   192  
  6     3   85  
  6     3   206  
  6     3   115  
  6     3   55  
  6     3   234  
  4     3   132  
  4     3   21  
  4     3   75  
  4         133  
  4         41  
  4         67  
  3         30  
  3         23  
  3         34  
  3         68  
  3         42  
  3         174  
  3         201  
  3         54  
  3         248  
  3         85  
  3         40  
  3         99  
  3         70  
  3         34  
  3         108  
  3         99  
  3         36  
  3         155  
  3         52  
  3         22  
  3         54  
  3         61  
  3         34  
  3         79  
  3         96  
  3         32  
  3         141  
  3         70  
  3         30  
  3         84  
100              
101 86 50       2149 if ($@) {
102 0         0 Rex::Logger::info("$mod not found.");
103 0         0 Rex::Logger::debug("$@");
104 0         0 next;
105             }
106              
107 84         1658 $hardware_information{$mod_string} = $mod->get();
108              
109 84         840 Rex::Commands::profiler()->end("hardware: $mod_string");
110              
111             }
112              
113 39         1960 return %hardware_information;
114             }
115              
116             1;