Host probe for generic hostname/uname info
Robert 'phaylon' Sedlacek [Thu, 3 May 2012 16:12:46 +0000 (16:12 +0000)]
lib/System/Introspector/Host.pm [new file with mode: 0644]

diff --git a/lib/System/Introspector/Host.pm b/lib/System/Introspector/Host.pm
new file mode 100644 (file)
index 0000000..9b69646
--- /dev/null
@@ -0,0 +1,44 @@
+package System::Introspector::Host;
+use Moo;
+
+sub gather {
+    my ($self) = @_;
+    return {
+        hostname => $self->_gather_hostname,
+        uname    => $self->_gather_uname_info,
+    };
+}
+
+my @UnameFields = qw(
+    kernel_name
+    kernel_release
+    kernel_version
+    nodename
+    machine
+    processor
+    hardware_platform
+    operating_system
+);
+
+sub _gather_uname_info {
+    my ($self) = @_;
+    my %uname;
+    for my $field (@UnameFields) {
+        (my $option = $field) =~ s{_}{-}g;
+        my $value = `uname --$option`;
+        chomp $value;
+        $uname{ $field } = $value;
+    }
+    return \%uname;
+}
+
+sub _gather_hostname {
+    my ($self) = @_;
+    open my $fh, '<', '/etc/hostname'
+        or die "Unable to read /etc/hostname: $!\n";
+    my $hostname = <$fh>;
+    chomp $hostname;
+    return $hostname;
+}
+
+1;