1 package System::Introspector::State;
3 use File::Tree::Snapshot;
4 use System::Introspector::Gatherer;
5 use Object::Remote::Future qw( await_all );
7 use JSON::Diffable qw( encode_json );
9 has config => (is => 'ro', required => 1);
11 has root => (is => 'ro', required => 1);
13 sub user { $_[0]->config->user }
15 sub sudo_user { $_[0]->config->sudo_user }
17 sub _log { shift; printf "[%s] %s\n", scalar(localtime), join '', @_ }
20 my ($self, @groups) = @_;
22 for my $group (@groups) {
24 for my $host ($self->config->hosts) {
25 $self->_log("Beginning to fetch group '$group' on '$host'");
26 push @waiting, [$host, $self->fetch($host, $group)];
28 $self->_log("Now waiting for results");
30 my ($host, @futures) = @$_;
31 my $done = [$host, await_all @futures];
32 $self->_log("Received all from group '$group' on '$host'");
35 $self->_log("All gathered for group '$group'");
36 for my $result (@results) {
37 my ($host, @data) = @$result;
38 $self->_store($host, $group, +{ map %$_, @data });
46 my ($self, $group) = @_;
47 return $self->config->config_for_group($group)->{introspect};
51 my ($self, $host, $group) = @_;
52 my $spec = $self->introspectors($group);
54 push(@{ $spec->{$_}{sudo} ? \@sudo : \@nosudo}, [$_, $spec->{$_}])
58 $self->_log("Without sudo: ", join ", ", map $_->[0], @nosudo);
59 my $proxy = $self->_create_gatherer(
61 introspectors => [@nosudo],
63 push @futures, $proxy->start::gather_all;
66 $self->_log("With sudo: ", join ", ", map $_->[0], @nosudo);
67 my $proxy = $self->_create_gatherer(
70 introspectors => [@sudo],
72 push @futures, $proxy->start::gather_all;
78 my ($self, @path) = @_;
79 my $storage = File::Tree::Snapshot->new(
81 storage_path => join('/', $self->root, @path),
84 unless $storage->exists;
89 my ($self, $host, $group, $gathered) = @_;
90 $self->_log("Storing data for group '$group' on '$host'");
91 my $storage = $self->storage($host, $group);
94 for my $class (sort keys %$gathered) {
95 my $file = sprintf '%s.json', join '/',
97 s{([a-z0-9])([A-Z])}{${1}_${2}}g;
99 } split m{::}, $class;
100 my $fh = $storage->open('>:utf8', $file, mkpath => 1);
101 my $full_path = $storage->file($file);
102 $self->_log("Writing $full_path");
103 print $fh encode_json($gathered->{$class});
104 push @files, $full_path;
106 $self->_cleanup($storage, [@files]);
107 $self->_log("Committing");
111 $self->_log("Rolling back snapshot because of: ", $@ || 'unknown error');
119 my ($self, $storage, $known_files) = @_;
120 my %known = map { ($_ => 1) } @$known_files;
121 my @files = $storage->find_files('json');
122 for my $file (@files) {
123 next if $known{$file};
124 $self->_log("Removing $file");
126 or die "Unable to remove '$file': $!\n";
131 sub _create_gatherer {
132 my ($self, %arg) = @_;
133 return System::Introspector::Gatherer->new_from_spec(
136 sudo_user => $arg{sudo} && $self->sudo_user,
137 introspectors => $arg{introspectors},
145 System::Introspector::State - Gather system state
149 my $state = System::Introspector::State->new(
150 host => 'foo.example.com',
151 storage => $storage_obj,
153 introspect => [qw( ProbeName )],
157 my $data = $state->fetch;
158 $state->fetch_and_store;
162 Gathers system introspection data based on configuration and stores
163 it with a L<File::Tree::Snapshot> object.
169 A hash reference containing a C<introspect> key with an array reference
170 value containing a list of probe names without the
171 C<System::Introspector::Probe::> prefix. This attribute is required.
175 An optional hostname. If no hostname is supplied, the local configuration
176 data will be fetched.
180 A L<File::Tree::Snapshot> object.
186 my $data = $state->fetch;
188 Fetches all probe data.
190 =head2 fetch_and_store
192 $state->fetch_and_store;
194 Fetches all probe data and stores it in the L</storage>.