FileHandles probe for lsof output
Robert 'phaylon' Sedlacek [Tue, 1 May 2012 20:18:26 +0000 (20:18 +0000)]
lib/System/Introspector/FileHandles.pm [new file with mode: 0644]

diff --git a/lib/System/Introspector/FileHandles.pm b/lib/System/Introspector/FileHandles.pm
new file mode 100644 (file)
index 0000000..2e3851d
--- /dev/null
@@ -0,0 +1,27 @@
+package System::Introspector::FileHandles;
+use Moo;
+
+sub gather {
+    my ($self) = @_;
+    my $pipe = $self->_open_lsof_pipe;
+    my @handles;
+    while (defined( my $line = <$pipe> )) {
+        chomp $line;
+        my @fields = split m{\0}, $line;
+        push @handles, { map {
+            m{^(.)(.*)$};
+            ($1, $2);
+        } @fields };
+    }
+    return \@handles;
+}
+
+sub _open_lsof_pipe {
+    my ($self) = @_;
+    my $command = 'lsof -F0';
+    open my $pipe, '-|', $command
+        or die "Unable to open pipe to '$command': $!\n";
+    return $pipe;
+}
+
+1;