1 package System::Introspector::State;
3 use File::Tree::Snapshot;
4 use System::Introspector::Gatherer;
6 use JSON::Diffable qw( encode_json );
8 has config => (is => 'ro', required => 1);
10 has root => (is => 'ro', required => 1);
12 sub user { $_[0]->config->user }
14 sub sudo_user { $_[0]->config->sudo_user }
17 my ($self, @groups) = @_;
18 for my $host ($self->config->hosts) {
19 $self->fetch_and_store($host, @groups);
25 my ($self, $group) = @_;
26 return $self->config->config_for_group($group)->{introspect};
30 my ($self, $host, @groups) = @_;
31 my $data = $self->fetch($host, @groups);
32 return $self->_store($host, $data);
36 my ($self, $host, @groups) = @_;
38 ($_, $self->fetch_group($host, $_));
43 my ($self, $host, $group) = @_;
44 my $spec = $self->introspectors($group);
46 push(@{ $spec->{$_}{sudo} ? \@sudo : \@nosudo}, [$_, $spec->{$_}])
50 my $gatherer = $self->_create_gatherer(host => $host);
51 %report = %{ $self->_fetch_with_gatherer($gatherer, @nosudo) || {} };
54 my $gatherer = $self->_create_gatherer(sudo => 1, host => $host);
55 %report = (%report, %{ $self->_fetch_with_gatherer($gatherer, @sudo) || {} });
60 sub _fetch_with_gatherer {
61 my ($self, $gatherer, @spec) = @_;
63 for my $class_spec (@spec) {
64 my ($class_base, $args) = @$class_spec;
65 print "Gathering $class_base data\n";
66 $report{ $class_base } = $gatherer
67 ->gather($class_base, $args);
69 print "All gathered\n";
74 my ($self, @path) = @_;
75 my $storage = File::Tree::Snapshot->new(
77 storage_path => join('/', $self->root, @path),
80 unless $storage->exists;
85 my ($self, $host, $data) = @_;
86 for my $group (sort keys %$data) {
87 my $storage = $self->storage($host, $group);
88 my $gathered = $data->{$group};
90 for my $class (sort keys %$gathered) {
91 my $file = sprintf '%s.json', join '/',
93 s{([a-z0-9])([A-Z])}{${1}_${2}}g;
95 } split m{::}, $class;
96 my $fh = $storage->open('>:utf8', $file, mkpath => 1);
97 print "Writing $file\n";
98 print $fh encode_json($gathered->{$class});
99 push @files, $storage->file($file);
101 $self->_cleanup($storage, [@files]);
108 my ($self, $storage, $known_files) = @_;
109 my %known = map { ($_ => 1) } @$known_files;
110 my @files = $storage->find_files('json');
111 for my $file (@files) {
112 next if $known{$file};
113 print "Removing $file\n";
115 or die "Unable to remove '$file': $!\n";
120 sub _create_gatherer {
121 my ($self, %arg) = @_;
122 return System::Introspector::Gatherer->new_from_spec(
125 sudo_user => $arg{sudo} && $self->sudo_user,
133 System::Introspector::State - Gather system state
137 my $state = System::Introspector::State->new(
138 host => 'foo.example.com',
139 storage => $storage_obj,
141 introspect => [qw( ProbeName )],
145 my $data = $state->fetch;
146 $state->fetch_and_store;
150 Gathers system introspection data based on configuration and stores
151 it with a L<File::Tree::Snapshot> object.
157 A hash reference containing a C<introspect> key with an array reference
158 value containing a list of probe names without the
159 C<System::Introspector::Probe::> prefix. This attribute is required.
163 An optional hostname. If no hostname is supplied, the local configuration
164 data will be fetched.
168 A L<File::Tree::Snapshot> object.
174 my $data = $state->fetch;
176 Fetches all probe data.
178 =head2 fetch_and_store
180 $state->fetch_and_store;
182 Fetches all probe data and stores it in the L</storage>.