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   933 use v5.12.5;
  71         351  
27 71     71   452 use warnings;
  71         226  
  71         2992  
28              
29             our $VERSION = '1.14.3'; # VERSION
30              
31 71     71   603 use Rex::Logger;
  71         244  
  71         567  
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 82 my ( $class, $service_name, $service_class ) = @_;
73 3         57 $HW_PROVIDER{"\L$service_name"} = $service_class;
74 3         41 return 1;
75             }
76              
77             sub get {
78 42     42 1 683 my ( $class, @modules ) = @_;
79              
80 42         241 my %hardware_information;
81              
82 42 100       328 if ( "all" eq "\L$modules[0]" ) {
83              
84 12         179 @modules = qw(Host Kernel Memory Network Swap VirtInfo);
85 12         113 push( @modules, keys(%HW_PROVIDER) );
86              
87             }
88              
89 42         263 for my $mod_string (@modules) {
90              
91 87         477 Rex::Commands::profiler()->start("hardware: $mod_string");
92              
93 86         518 my $mod = "Rex::Hardware::$mod_string";
94 86 50       354 if ( exists $HW_PROVIDER{$mod_string} ) {
95 2         118 $mod = $HW_PROVIDER{$mod_string};
96             }
97              
98 86         690 Rex::Logger::debug("Loading $mod");
99 86     9   11646 eval "use $mod";
  6     8   63  
  6     8   17  
  6     6   54  
  6     4   84  
  6     4   20  
  6     3   80  
  6     3   80  
  6     3   31  
  6     3   129  
  6     3   87  
  6     3   45  
  6     3   137  
  4     3   100  
  4     3   33  
  4     3   78  
  4         79  
  4         35  
  4         68  
  3         31  
  3         11  
  3         25  
  3         46  
  3         25  
  3         53  
  3         79  
  3         26  
  3         78  
  3         66  
  3         26  
  3         88  
  3         61  
  3         25  
  3         94  
  3         60  
  3         34  
  3         88  
  3         49  
  3         20  
  3         44  
  3         43  
  3         22  
  3         49  
  3         88  
  3         27  
  3         94  
  3         103  
  3         53  
  3         117  
100              
101 86 50       1933 if ($@) {
102 0         0 Rex::Logger::info("$mod not found.");
103 0         0 Rex::Logger::debug("$@");
104 0         0 next;
105             }
106              
107 84         1242 $hardware_information{$mod_string} = $mod->get();
108              
109 84         605 Rex::Commands::profiler()->end("hardware: $mod_string");
110              
111             }
112              
113 39         1598 return %hardware_information;
114             }
115              
116             1;