simulated hostname file and ability to declare a different file for Host probe
[scpubgit/System-Introspector.git] / lib / System / Introspector / Probe / Host.pm
1 package System::Introspector::Probe::Host;
2 use Moo;
3
4 use System::Introspector::Util qw(
5     handle_from_command
6     output_from_command
7     output_from_file
8     transform_exceptions
9 );
10
11 has hostname_file => (is => 'ro', default => sub {'/etc/hostname' });
12
13 sub gather {
14     my ($self) = @_;
15     return transform_exceptions {
16         return {
17             hostname => $self->_gather_hostname,
18             uname    => $self->_gather_uname_info,
19         };
20     };
21 }
22
23 my @UnameFields = qw(
24     kernel_name
25     kernel_release
26     kernel_version
27     nodename
28     machine
29     processor
30     hardware_platform
31     operating_system
32 );
33
34 sub _gather_uname_info {
35     my ($self) = @_;
36     my %uname;
37     for my $field (@UnameFields) {
38         (my $option = $field) =~ s{_}{-}g;
39         my $value = output_from_command [uname => "--$option"];
40         chomp $value;
41         $uname{ $field } = $value;
42     }
43     return \%uname;
44 }
45
46 sub _gather_hostname {
47     my ($self) = @_;
48     my $hostname = output_from_file $self->hostname_file;
49     chomp $hostname;
50     $hostname =~ s{(?:^\s+|\s+$)}{}g;
51     return $hostname;
52 }
53
54 1;
55
56 __END__
57
58 =head1 NAME
59
60 System::Introspector::Host - Gather generic host information
61
62 =head1 DESCRIPTION
63
64 Gathers the hostname and information provided by C<uname>.
65
66 =head1 SEE ALSO
67
68 =over
69
70 =item L<System::Introspector>
71
72 =back
73
74 =cut
75