File: Hardware.pm

package info (click to toggle)
rex 1.14.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,012 kB
  • sloc: perl: 35,587; xml: 264; sh: 51; makefile: 13
file content (116 lines) | stat: -rw-r--r-- 1,873 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#
# (c) Jan Gehring <jan.gehring@gmail.com>
#

=head1 NAME

Rex::Hardware - Base Class for hardware / information gathering

=head1 DESCRIPTION

This module is the base class for hardware/information gathering.

=head1 SYNOPSIS

 use Rex::Hardware;
 
 my %host_info = Rex::Hardware->get(qw/ Host /);
 my %all_info  = Rex::Hardware->get(qw/ All /);

=head1 CLASS METHODS

=cut

package Rex::Hardware;

use v5.12.5;
use warnings;

our $VERSION = '1.14.1'; # VERSION

use Rex::Logger;

require Rex::Args;

=head2 get(@modules)

Returns a hash with the wanted information.

 task "get-info", "server1", sub {
   %hw_info = Rex::Hardware->get(qw/ Host Network /);
 };

Or if you want to get all information

 task "get-all-info", "server1", sub {
   %hw_info = Rex::Hardware->get(qw/ All /);
 };

Available modules:

=over 4

=item Host

=item Kernel

=item Memory

=item Network

=item Swap

=item VirtInfo

=back

=cut

my %HW_PROVIDER;

sub register_hardware_provider {
  my ( $class, $service_name, $service_class ) = @_;
  $HW_PROVIDER{"\L$service_name"} = $service_class;
  return 1;
}

sub get {
  my ( $class, @modules ) = @_;

  my %hardware_information;

  if ( "all" eq "\L$modules[0]" ) {

    @modules = qw(Host Kernel Memory Network Swap VirtInfo);
    push( @modules, keys(%HW_PROVIDER) );

  }

  for my $mod_string (@modules) {

    Rex::Commands::profiler()->start("hardware: $mod_string");

    my $mod = "Rex::Hardware::$mod_string";
    if ( exists $HW_PROVIDER{$mod_string} ) {
      $mod = $HW_PROVIDER{$mod_string};
    }

    Rex::Logger::debug("Loading $mod");
    eval "use $mod";

    if ($@) {
      Rex::Logger::info("$mod not found.");
      Rex::Logger::debug("$@");
      next;
    }

    $hardware_information{$mod_string} = $mod->get();

    Rex::Commands::profiler()->end("hardware: $mod_string");

  }

  return %hardware_information;
}

1;