-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpsView.pl
More file actions
executable file
·174 lines (129 loc) · 4.03 KB
/
OpsView.pl
File metadata and controls
executable file
·174 lines (129 loc) · 4.03 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/perl
use strict;
use Getopt::Long;
use Pod::Usage;
require OpsView::Connector;
require OpsView::Commands;
require OpsView::Printer;
use constant COMMANDS => qw(token get hostgroup viewport host service);
use constant DEFAULT_URL => "http://opsview";
eval {
# Provide some introspection to the .sh
if ($ARGV[0] eq 'commands') {
print join("\n", COMMANDS)."\n";
} else {
main();
}
};
if ($@) {
print STDERR "ERROR: ".$@."\n";
exit 1;
}
sub main {
my $username;
my $password;
my $token;
my $destination = DEFAULT_URL;
my $help = 0;
my $man = 0;
if (!GetOptions(
'username|u=s' => \$username,
'password|p=s' => \$password,
'token|t=s' => \$token,
'destination|d=s' => \$destination,
'help|?' => \$help,
'man' => \$man)) {
pod2usage(2);
} elsif ($help) {
pod2usage(1);
} elsif ($man) {
pod2usage(-exitstatus => 0, -verbose => 2);
} elsif (!defined($username)) {
pod2usage("Missing username");
}
my $connector = new OpsView::Connector($destination);
if ($password) {
# Username and password given on the command line
$connector->login($username, $password);
} elsif ($token) {
# Attempt to resume the session from the token
$connector->resumeSession($username, $token);
} else {
pod2usage("Missing password or token");
}
# Execute the command passed in argument
my ($command, @arguments) = @ARGV;
# TODO : break that out somehow
if ($command eq 'token') {
print $connector->token."\n";
} elsif ($command eq 'get') {
my $path = $arguments[0];
print "GET $path\n";
my $response = $connector->request($path);
OpsView::Printer->prettyPrint($response);
} elsif ($command eq 'viewport') {
my ($viewName) = @arguments;
if (defined($viewName)) {
my $view = OpsView::Commands->viewport($connector, $viewName);
OpsView::Printer->printView($view);
} else {
my $views = OpsView::Commands->viewport($connector);
OpsView::Printer->printViews($views);
}
} elsif ($command eq 'hostgroup') {
my ($groupId) = @arguments;
my $groups = OpsView::Commands->hostgroup($connector, $groupId);
OpsView::Printer->printHostGroups($groups, $groupId);
} elsif ($command eq 'host') {
my $hosts = OpsView::Commands->host($connector);
OpsView::Printer->printHosts($hosts);
} elsif ($command eq 'service') {
my ($host) = @arguments;
my $hosts = OpsView::Commands->service($connector, $host);
OpsView::Printer->printServices($hosts);
} elsif ($command) {
pod2usage("Unexpected command: $command");
} else {
pod2usage("Missing command");
}
exit 0;
}
__END__
=head1 NAME
Perl connector for OpsView
=head1 SYNOPSIS
perl OpsView.pl
[-u|--username I<username>]
[-p|--password I<password>]
[-d|--destination I<url>]
[-t|--token I<token>]
[--help]
I<command> [I<argument> ...]
=head1 OPTIONS
=over 8
=item B<-u> I<username>, B<--username> I<username>
The user name to log in with.
=item B<-p> I<password>, B<--password> I<password>
The password to log in with.
=item B<-t> I<token>, B<--token> I<token>
When resuming a connection, the session token.
=item B<-d> I<url>, B<--destination> I<url>
The URL of the OpsView web interface.
=item B<--help>
Print a brief help message and exits.
=item B<--man>
Print the manual page and exits.
=back
=head1 COMMANDS
=over 8
=item B<token>
Print out the identifier of the session.
=item B<get> I<path>
Call the RESTful interface (at http//<opsview>/rest/<path>) and print out the result.
=item B<hostgroup> [I<groupid>]
Print the host group hierarchy. If a group id is given, the tree starts from this node.
=item B<viewport> [I<viewname>]
Print the views. If a view name is given, the hosts and services it contains are printed out.
=back
TODO - more commands
=cut