reworked gatherer to be more flexible, added sudo support
[scpubgit/System-Introspector.git] / bin / system-introspector
1 #!/usr/bin/env perl
2 use strictures 1;
3
4 use Getopt::Long;
5 use Pod::Usage;
6 use System::Introspector::State;
7 use File::Tree::Snapshot;
8 use System::Introspector::Config;
9
10 GetOptions(
11     'c|config=s'    => \my $config_file,
12     's|storage=s'   => \my $storage_dir,
13     'H|host=s'      => \my $hostname,
14     'U|user=s'      => \my $username,
15     'allow-empty'   => \my $allow_empty,
16     'a|all'         => \my $update_all,
17     'g|group=s'     => \my @update_groups,
18     'h|help'        => sub { pod2usage(0) },
19 ) or pod2usage(2);
20
21 die "Requires --all or --group option\n"
22     unless $update_all or @update_groups;
23
24 die "The --user option also requires a --host to be set\n"
25     if defined($username) and not defined($hostname);
26
27 my $config = System::Introspector::Config->new(
28     config_file => (defined($config_file)
29                     ? $config_file
30                     : "$storage_dir/main.conf"),
31 );
32
33 $config->has_group($_) or die "Unknown group '$_'\n"
34     for @update_groups;
35
36 @update_groups = $config->groups
37     if $update_all;
38     
39 my $sudo_user = $config->sudo_user;
40
41 for my $group (@update_groups) {
42     my $group_dir = "$storage_dir/$group";
43     print "Group $group at $group_dir\n";
44     my $storage = File::Tree::Snapshot->new(
45         storage_path    => $group_dir,
46         allow_empty     => $allow_empty,
47     );
48     $storage->create
49         unless $storage->exists;
50     my $state = System::Introspector::State->new(
51         defined($hostname)  ? (host => $hostname) : (),
52         defined($username)  ? (user => $username) : (),
53         defined($sudo_user) ? (sudo_user => $sudo_user) : (),
54         storage => $storage,
55         config  => $config->config_for_group($group),
56     );
57     eval { $state->fetch_and_store };
58     if (my $error = $@) {
59         warn "Error: $error\n";
60         $storage->reset;
61     }
62     else {
63         $storage->commit;
64     }
65 }
66
67 __END__
68
69 =head1 NAME
70
71 system-introspector - Generate System Introspection Data
72
73 =head1 SYNOPSIS
74
75     system-introspector --storage <path> [OPTIONS]
76
77 =head1 OPTIONS
78
79 =head2 -s <path>, --storage <path>
80
81 Path to storage. Always required.
82
83 =head2 -H <host>, --host <host>
84
85 Remote host gathering
86
87 =head2 --allow-empty
88
89 Allow empty commits to storage.
90
91 =head2 -h, --help
92
93 Display help.
94
95 =cut