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   1022 use v5.12.5;
  71         301  
27 71     71   571 use warnings;
  71         264  
  71         3145  
28              
29             our $VERSION = '1.14.2.3'; # TRIAL VERSION
30              
31 71     71   638 use Rex::Logger;
  71         305  
  71         650  
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 89 my ( $class, $service_name, $service_class ) = @_;
73 3         73 $HW_PROVIDER{"\L$service_name"} = $service_class;
74 3         41 return 1;
75             }
76              
77             sub get {
78 42     42 1 654 my ( $class, @modules ) = @_;
79              
80 42         238 my %hardware_information;
81              
82 42 100       440 if ( "all" eq "\L$modules[0]" ) {
83              
84 12         222 @modules = qw(Host Kernel Memory Network Swap VirtInfo);
85 12         151 push( @modules, keys(%HW_PROVIDER) );
86              
87             }
88              
89 42         269 for my $mod_string (@modules) {
90              
91 87         564 Rex::Commands::profiler()->start("hardware: $mod_string");
92              
93 86         609 my $mod = "Rex::Hardware::$mod_string";
94 86 50       382 if ( exists $HW_PROVIDER{$mod_string} ) {
95 2         79 $mod = $HW_PROVIDER{$mod_string};
96             }
97              
98 86         678 Rex::Logger::debug("Loading $mod");
99 86     9   13824 eval "use $mod";
  6     8   52  
  6     8   18  
  6     6   66  
  6     4   70  
  6     4   29  
  6     3   69  
  6     3   118  
  6     3   55  
  6     3   185  
  6     3   97  
  6     3   54  
  6     3   196  
  4     3   83  
  4     3   16  
  4     3   79  
  4         117  
  4         38  
  4         73  
  3         39  
  3         9  
  3         29  
  3         73  
  3         41  
  3         89  
  3         101  
  3         37  
  3         102  
  3         104  
  3         48  
  3         152  
  3         71  
  3         30  
  3         87  
  3         86  
  3         35  
  3         122  
  3         45  
  3         29  
  3         46  
  3         55  
  3         48  
  3         91  
  3         84  
  3         79  
  3         107  
  3         79  
  3         33  
  3         108  
100              
101 86 50       2118 if ($@) {
102 0         0 Rex::Logger::info("$mod not found.");
103 0         0 Rex::Logger::debug("$@");
104 0         0 next;
105             }
106              
107 84         1490 $hardware_information{$mod_string} = $mod->get();
108              
109 84         658 Rex::Commands::profiler()->end("hardware: $mod_string");
110              
111             }
112              
113 39         1924 return %hardware_information;
114             }
115              
116             1;