forked from cfengine/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.pl
executable file
·146 lines (117 loc) · 3.66 KB
/
report.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/perl
# originally by Shane McEwan <shane@mcewan.id.au>
# maintained by Ted Zlatanov <tzz@lifelogs.com>
use warnings;
use strict;
use LWP::UserAgent;
use JSON;
use Data::Dumper;
use Getopt::Long;
my %outputs = (
csv => sub { my $data = decode_json( shift ); print_csv($data, shift) },
html => sub { my $data = decode_json( shift ); print_html($data, shift) },
list => sub { my $data = decode_json( shift ); print_hostlist($data, shift) },
host => sub { my $data = decode_json( shift ); print_hostinfo($data, shift) },
json => sub { print @_ },
);
my %options = (
url => $ENV{CFENGINE_MP_URL} || 'http://admin:admin@localhost:80/',
output => 'csv',
limit => 100000,
verbose => 0,
list => 0,
);
GetOptions(\%options,
"verbose|v!",
"output:s",
"limit:i", # note that you can say "LIMIT N" in the query!
"url:s",
"host:s",
"list|list-hosts!"
);
# Create a user agent object
my $ua = LWP::UserAgent->new;
my $query = shift;
if ($options{list})
{
$query = "SELECT Hosts.HostName, Contexts.ContextName FROM Hosts JOIN Contexts ON Hosts.Hostkey = Contexts.HostKey";
$options{output} = 'list';
}
elsif (exists $options{host})
{
$query = "SELECT Variables.Bundle, Variables.VariableName, Variables.VariableValue FROM Variables WHERE Variables.HostKey = (SELECT Hosts.Hostkey FROM Hosts WHERE Hosts.HostName = '$options{host}')";
$options{output} = 'host';
}
die "Syntax: $0 [--url BASEURL] [--limit N] [--output @{[ join('|', sort keys %outputs) ]}] [QUERY|--list|--host HOSTNAME]"
unless ($query && exists $outputs{$options{output}});
$query =~ s/\v+/ /g;
# Create a request
my $url = "$options{url}/api/query";
my $req = HTTP::Request->new(POST => $url);
$req->content_type('application/x-www-form-urlencoded');
$req->content(sprintf('{ "limit": %d, "query" : "%s" }',
$options{limit},
$query));
print "Opening $url...\n" if $options{verbose};
print "Query is [$query]\n" if $options{verbose};
# Pass request to the user agent and get a response back
my $res = $ua->request($req);
unless ($res->is_success)
{
die $res->status_line;
}
print "Got data ", $res->as_string(), "\n" if $options{verbose};
$outputs{$options{output}}->($res->content,
$res->header("Content-Type"));
exit 0;
sub print_csv
{
my $data = shift;
my $ctype = shift || 'unknown';
print "Processing content type $ctype\n" if $options{verbose};
foreach my $row (@{$data->{data}->[0]->{rows}})
{
print join ",", map { m/,/ ? "\"$_\"" : $_ } @$row;
print "\n";
}
}
sub print_html
{
my $data = shift;
my $ctype = shift || 'unknown';
print "Processing content type $ctype\n" if $options{verbose};
# trying to avoid requiring CPAN modules! sorry this is so primitive!
print "<html><body><table>\n";
foreach my $row (@{$data->{data}->[0]->{rows}})
{
print "<tr>\n";
foreach my $item (@$row)
{
print "<td>\n";
print "$item\n"; # should be escaped for proper output
print "</td>\n";
}
print "</tr>\n";
}
print "</table></body></html>\n";
}
sub print_hostlist
{
my $data = shift;
my %list;
foreach my $row (@{$data->{data}->[0]->{rows}})
{
push @{$list{$row->[1]}}, $row->[0];
}
print encode_json(\%list), "\n";
}
sub print_hostinfo
{
my $data = shift;
my %info;
foreach my $row (@{$data->{data}->[0]->{rows}})
{
$info{"$row->[0]_$row->[1]"} = $row->[2];
}
print encode_json(\%info), "\n";
}