1 package System::Introspector::State;
3 use Data::YAML::Writer;
5 use Object::Remote::Future;
6 use System::Introspector::Gatherer;
8 has config => (is => 'ro', required => 1);
10 has introspectors => (is => 'lazy');
12 has host => (is => 'ro');
14 has storage => (is => 'ro', required => 1);
16 has node_path => (is => 'lazy');
20 my $gatherer = $self->_create_gatherer;
21 my $spec = $self->introspectors;
23 for my $class_base (sort keys %$spec) {
24 $report{ $class_base } = $gatherer
25 ->gather($class_base, $spec->{ $class_base });
32 return $self->_store($self->fetch);
35 sub _build_node_path {
37 return defined($self->host)
38 ? sprintf('host/%s', $self->host)
42 sub _build_introspectors {
44 return $self->config->{introspect};
48 my ($self, $data) = @_;
49 my $yaml = Data::YAML::Writer->new;
50 my $storage = $self->storage;
52 for my $class (sort keys %$data) {
53 my $file = sprintf '%s.yml', join '/',
54 node => $self->node_path,
56 s{([a-z0-9])([A-Z])}{${1}_${2}}g;
58 } split m{::}, $class;
59 my $fh = $storage->open('>:utf8', $file, mkpath => 1);
60 print "Writing $file\n";
61 $yaml->write($data->{$class}, $fh);
62 push @files, $storage->file($file);
64 $self->_cleanup(\@files);
69 my ($self, $known_files) = @_;
70 my %known = map { ($_ => 1) } @$known_files;
71 my $data_dir = $self->storage->file(node => $self->node_path);
72 my @files = $self->storage->find_files('yml', node => $self->node_path);
73 for my $file (@files) {
74 next if $known{$file};
75 print "Removing $file\n";
77 or die "Unable to remove '$file': $!\n";
82 sub _create_gatherer {
84 if (defined( my $host = $self->host )) {
85 return System::Introspector::Gatherer->new::on($host);
87 return System::Introspector::Gatherer->new;
94 System::Introspector::State - Gather system state
98 my $state = System::Introspector::State->new(
99 host => 'foo.example.com',
100 storage => $storage_obj,
102 introspect => [qw( ProbeName )],
106 my $data = $state->fetch;
107 $state->fetch_and_store;
111 Gathers system introspection data based on configuration and stores
112 it with a L<File::Tree::Snapshot> object.
118 A hash reference containing a C<introspect> key with an array reference
119 value containing a list of probe names without the
120 C<System::Introspector::Probe::> prefix. This attribute is required.
124 An optional hostname. If no hostname is supplied, the local configuration
125 data will be fetched.
129 A L<File::Tree::Snapshot> object.
135 my $data = $state->fetch;
137 Fetches all probe data.
139 =head2 fetch_and_store
141 $state->fetch_and_store;
143 Fetches all probe data and stores it in the L</storage>.