-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvmwrt.pl
389 lines (300 loc) · 9.54 KB
/
vmwrt.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#!/usr/bin/perl
# Trap signals from Docker, trigger die, which will invoke DESTROY method on SessionWrapper
# Note 'kill' cannot be trapped, so using 'docker kill' instead of 'docker stop' will leave
# VMware API sessions open.
$SIG{'INT'} = sub { die; };
$SIG{'TERM'} = sub { die; };
package SessionWrapper {
sub new {
my ($class, $self, %args, $host, $user, $pass);
($class, %args) = @_;
$host = delete $args{'host'};
$user = delete $args{'user'};
$pass = delete $args{'pass'};
# SoapStub
$stub = new VMOMI::SoapStub(host => $host) || die "Failed to initialize SoapStub";
# ServiceInstance
$si = new VMOMI::ServiceInstance(
$stub,
new VMOMI::ManagedObjectReference(
type => 'ServiceInstance',
value => 'ServiceInstance',
),
);
# RetrieveServiceContent
$content = $si->RetrieveServiceContent(_this => $si);
# Login
$session = $content->sessionManager->Login(
userName => $user,
password => $pass,
);
$self = { };
$self->{'content'} = $content;
$self->{'session'} = $session;
return bless $self, $class;
}
sub DESTROY {
my $self = shift;
if (defined $self->{'content'}) {
$self->{'content'}->sessionManager->Logout();
}
}
}
1;
use strict;
use warnings;
use lib 'lib';
use URI;
use VMOMI;
use JSON::XS;
use Path::Tiny;
use Data::Dumper;
use HTTP::Cookies;
use HTTP::Request;
use LWP::ConnCache;
use LWP::UserAgent;
use Log::Log4perl qw(get_logger);
my ($log, $lines, $json, $cfg, $stub, $wrapper, $content, $definitions, $version, $ua);
$json = JSON::XS->new->convert_blessed->allow_nonref;
# Load configuration
$cfg = $json->decode(path("etc/vmwrt.json")->slurp());
# Initialize Log4perl
$lines = join("\n", @{ $cfg->{'logger'} || [ ]});
Log::Log4perl::init( \$lines );
$log = Log::Log4perl::get_logger();
$SIG{__DIE__} = sub {
if ($^S) { return; } # ignore in eval
# http://search.cpan.org/~mschilli/Log-Log4perl-1.47/lib/Log/Log4perl/FAQ.pm#How_can_I_make_sure_my_application_logs_a_message_when_it_dies_unexpectedly?
local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_depth + 1;
my $logger = get_logger("");
$logger->fatal(@_);
die @_; # Now terminate really
};
# Init LWP::UserAgent; support session cookies against ServiceNow instance
$ua = initialize_useragent($cfg->{'servicenow'});
$log->debug("ServiceNow API session connected");
$wrapper = new SessionWrapper(
host => $cfg->{'vmware'}{'host'},
user => $cfg->{'vmware'}{'user'},
pass => $cfg->{'vmware'}{'pass'},
);
$content = $wrapper->{'content'};
$log->debug("VMware API session connected");
# Load filters from configuration file
$definitions = $cfg->{'filters'};
# PropertyFilterSpec
create_filter_spec($content, $definitions);
$log->debug("VMware API filters created");
# WaitForUpdatesEx
$log->debug("Starting WaitForUpdatesEx processing loop...");
wait_for_updates($content, $ua, $cfg->{'servicenow'}, 60);
exit;
sub initialize_useragent {
my ($sn_cfg) = @_;
my ($host, $user, $pass, $path, $uri, $user_agent, $cookies, $cache, $request, $response);
$host = $sn_cfg->{'host'};
$user = $sn_cfg->{'user'};
$pass = $sn_cfg->{'pass'};
$path = $sn_cfg->{'path'};
$uri = new URI();
$uri->scheme('https');
$uri->host($host);
$uri->path($path);
$user_agent = new LWP::UserAgent(
agent => 'vmwrt-cmdb/perl'
);
$cache = new LWP::ConnCache();
$cookies = new HTTP::Cookies(ignore_discard => 1);
$user_agent->cookie_jar($cookies);
$user_agent->protocols_allowed(['https']);
$user_agent->conn_cache($cache);
$request = new HTTP::Request();
$request->method('GET');
$request->uri($uri);
$request->content_type('text/json');
$request->authorization_basic($user, $pass);
$response = $user_agent->request($request);
if ($response->is_error()) {
die $response->status_line;
}
return $user_agent;
}
sub update_sn_rest {
my ($ua, $host, $user, $pass, $path, $data) = @_;
my ($uri, $user_agent, $cookies, $cache, $request, $response);
$uri = new URI();
$uri->scheme('https');
$uri->host($host);
$uri->path($path);
$request = new HTTP::Request();
$request->method('POST');
$request->uri($uri);
$request->content_type('application/json');
$request->content($data);
$request->authorization_basic($user, $pass);
$response = $ua->request($request);
if ($response->is_error()) {
die $response->status_line;
}
return;
}
sub create_tree_view {
my ($content, $types) = @_;
my $view = $content->viewManager->CreateContainerView(
type => $types,
recursive => 1,
container => $content->rootFolder->moref,
);
return $view;
}
sub create_list_view {
my ($content, $objects) = @_;
my $view = $content->viewManager->CreateListView(obj => $objects);
return $view;
}
sub create_filter_spec {
my ($content, $definitions) = @_;
my ($filter_spec, $tree_view, $list_view, $types, $propSets, $objectSets);
$types = [ ];
$propSets = [ ];
# PropertySpecs
foreach (@$definitions) {
my ($spec, $type, $properties);
$type = $_->{'type'};
$properties = $_->{'properties'};
$spec = new VMOMI::PropertySpec(all => 0, type => $type, pathSet => $properties);
push @$types, $type;
push @$propSets, $spec;
}
# CreateContainerView
$tree_view = create_tree_view($content, $types);
# Patch in OptionManager (VpxSettings)
push @$types, "OptionManager";
push @$propSets, new VMOMI::PropertySpec(
all => 0,
type => "OptionManager",
pathSet => [
'setting["VirtualCenter.InstanceName"]',
'setting["VirtualCenter.AutoManagedIPV4"]',
'setting["VirtualCenter.FQDN"]',
]
);
# CreateListView; adds rootFolder. TODO: Add custom values and non-inventory objects
$list_view = create_list_view($content, [
$content->rootFolder->{'moref'},
$content->setting->{'moref'},
]);
# ObjectSpecs; TraversalSpec simplified through ContainerView & ListView
$objectSets = [
new VMOMI::ObjectSpec(
obj => $tree_view,
skip => 0,
selectSet => [
new VMOMI::TraversalSpec(
path => "view",
type => $tree_view->{type} ),
],
),
new VMOMI::ObjectSpec(
obj => $list_view,
skip => 0,
selectSet => [
new VMOMI::TraversalSpec(
path => "view",
type => $list_view->{type} ),
]
),
];
$filter_spec = new VMOMI::PropertyFilterSpec(
reportMissingObjectsInResults => 0,
propSet => $propSets,
objectSet => $objectSets,
);
# CreateFilter(); currently not using partialUpdates
$content->propertyCollector->CreateFilter(spec => $filter_spec, partialUpdates => 0);
return;
}
sub wait_for_updates {
my ($content, $ua, $sn_cfg, $wait) = @_;
my ($version, $truncated, $initial, $about);
$about = $content->about;
$initial = 1;
$version = '';
$truncated = 0;
while (1) {
do {
my ($update_set, $updates, $processed_updates, $data, $counters);
$update_set = $content->propertyCollector->WaitForUpdatesEx(
version => $version,
options => new VMOMI::WaitOptions(maxWaitSeconds => $wait),
);
next if not defined $update_set;
$version = $update_set->version;
$truncated = defined($update_set->truncated) ? $update_set->truncated : "0";
# Combine updates from filterSet; currently ignoring missingSet which should
# not be present as we are not using ListView(s) and relying upon updates to
# the current inventory tree through ContainerView(s)
$updates = [ ];
foreach my $filter_set (@{$update_set->filterSet}) {
foreach my $update (@{$filter_set->objectSet}) {
push @$updates, $update;
}
}
# TODO: Get vCenter information (aboutInfo, VPX Settings: IPAddress, Name, etc) to
# populate cmdb_ci_vcenter table when vCenter instanceUuid is not found
# Simplify updateSet for transport to ServiceNow REST endpoint
$processed_updates = [ ];
foreach my $update (@$updates) {
my ($obj, $moid, $type, $kind, $processed_update);
if ($update->obj->isa("VMOMI::ManagedObjectReference")) {
$moid = $update->obj->value;
$type = $update->obj->type;
} else {
$moid = $update->obj->{moref}->value;
$type = $update->obj->{moref}->type;
}
$kind = $update->kind->val;
$processed_update = {
moid => $moid,
type => $type,
kind => $kind,
changes => [ ],
};
foreach my $change ( @{$update->changeSet || [ ]} ) {
my ($name, $op, $val);
$name = $change->name;
$op = $change->op->val;
$val = $change->val;
$counters->{$type} = 0 if not exists $counters->{$type};
$counters->{$type}++;
# TODO: Patch in full_path for Folders, which will require all updates in a
# set with tree view cached to parse out inventory path
# TODO: Parsers to simplify arrays, simple types, and complex types for $val
push @{$processed_update->{changes}}, {name => $name, value => $val, op => $op};
}
push @$processed_updates, $processed_update;
}
# Relationships will be the PITA here - need to map these out across update
# sets, then send those into the CMDB for relationship mapping. Should be
# an improvement vs the constant XML recursive lookups in today's model, but
# will require getting CIs through GlideRecord queries.
$data = $json->encode({
initial => $initial,
about => $about,
updates => $processed_updates,
version => $version,
});
# print $json->encode({items => $items, relations => undef});
update_sn_rest(
$ua,
$sn_cfg->{'host'},
$sn_cfg->{'user'},
$sn_cfg->{'pass'},
$sn_cfg->{'path'},
$data,
);
$log->info("Processed update version ($version) " . join(", ", map{qq{$_: $counters->{$_}}} keys %$counters));
} until ( $truncated eq "0" );
$initial = 0;
}
}