-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.rb
executable file
·839 lines (669 loc) · 25.8 KB
/
monitor.rb
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
#!/usr/bin/ruby
#============================================
# Script: monitor.rb
# Author: Magnus Luebeck, [email protected]
# Date: 2014-05-19
#
# Description: This script will connect to the OP5 api and report on services and hosts
#
# Copyright 2014 KMG Group GmbH
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
#============================================
require 'net/http'
%w[ rubygems getoptlong yaml pp json open-uri openssl].each { |f| require f }
begin
#------------------------------------------------------------------------
# Definitions:
#
# Actions: command line parameters stating an action:
#
# --list-changes
# --commit-changes
# --rollback-changes
# --create
# --delete
# --change
# --list (default action)
#
# Object type
#
# --type=<object type> or -t <object type>
# host
# hostgroup
# hostgroupservice
# service
# servicegroup
# contact
# contactgroup
#
# Object identification
#
# --name=<object name> or -n <object name>
#
# Options
#
# --json='<json string>' - If you need to send a full JSON string to, for example, --create
# --option="key=value" - Note, only a few known keywords (i.e. groups) will translate to
# an array, even if normally required. For complex options, use --json
#
# --address=<ip or address>
# --alias=<alias>
# --description=<description> - I.e. service description, which equals to --name if -t service is used
#
#------------------------------------------------------------------------
end
#============================================
# Predefined variables
#============================================
configDir = File.expand_path(File.dirname(__FILE__) )
configFile = File.expand_path(configDir + '/config.yml')
$logDir = File.expand_path(File.dirname(__FILE__) + "/log" )
$logFile = File.open( $logDir + "/monitor.log", 'a')
#============================================
# Global variables
#============================================
$op5Url = nil
$commentColumns = "comment,service.description,host.name"
$contactColumns = "name,alias,email"
$contactgroupColumns = "name,alias,members"
$hostColumns = "name,alias,address,state"
$hostgroupColumns = "name,alias,members"
$serviceColumns = "description,state,host.name,groups"
$servicegroupColumns = "name,alias,members"
OBJECT_TYPE_HOST = "hosts"
OBJECT_TYPE_HOSTGROUP = "hostgroups"
OBJECT_TYPE_SERVICE = "services"
OBJECT_TYPE_SERVICEGROUP = "servicegroups"
OBJECT_TYPE_CONTACT = "contacts"
OBJECT_TYPE_CONTACTGROUP = "contactgroups"
$allColumns = {
OBJECT_TYPE_HOST => $hostColumns,
OBJECT_TYPE_HOSTGROUP => $hostgroupColumns,
OBJECT_TYPE_CONTACT => $contactColumns,
OBJECT_TYPE_CONTACTGROUP => $contactgroupColumns,
OBJECT_TYPE_SERVICE => $serviceColumns,
OBJECT_TYPE_SERVICEGROUP => $servicegroupColumns
}
DEBUG = 2
VERBOSE = 1
NORMAL = 0
$debug = false
$writeToLogfile = false
$verboseLevel = 0
optAction = "list"
optType = OBJECT_TYPE_HOST
op5User = nil
op5Password = nil
optObjectFilter = {}
optJSON = nil
#============================================
# functions
#============================================
def op5ListChanges()
uriEncoded=URI.encode($op5Url + "/api/config/change?format=json")
uri = URI.parse(uriEncoded)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
request.basic_auth($op5User, $op5Password )
response = http.request(request)
res=JSON.parse(response.body)
res.each do |change|
puts "Type: #{change["type"]} #{change["object_type"]} -> Name: #{change["name"]} (#{change["user"]} at #{change["timestamp"]})"
end
return 0
end
def op5CommitChanges()
uriEncoded=URI.encode($op5Url + "/api/config/change?format=json")
uri = URI.parse(uriEncoded)
#pp uri
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(uri.request_uri)
request.basic_auth($op5User, $op5Password )
response = http.request(request)
puts response.body
return 0
end
def op5RollbackChanges()
uriEncoded=URI.encode($op5Url + "/api/config/change?format=json")
uri = URI.parse(uriEncoded)
#pp uri
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Delete.new(uri.request_uri)
request.basic_auth($op5User, $op5Password )
response = http.request(request)
puts response.body
return 0
end
#-------------------------------------------------------------------
# logIt, helper method to print verbose/debug information
# to the screen and optionally to a log file
#-------------------------------------------------------------------
def logIt(message, *levelOptional)
level = (levelOptional[0].nil?) ? 0 : levelOptional[0]
$stderr.puts "#{message}" if level <= $verboseLevel
ts=Time.now().strftime("%Y-%m-%d %H:%M:%S %Z")
$logFile.puts(Process.pid.to_s + ";" + ts + ";" + level.to_s + ";" + message) if ($writeToLogfile)
end
#-------------------------------------------------------------------
# thisMethod, helper method to print the method name when debugging
#-------------------------------------------------------------------
def thisMethod()
caller[0]=~/`(.*?)'/ # note the first quote is a backtick
$1
end
#-------------------------------------------------------------------
# usageShort - minimal help page
#-------------------------------------------------------------------
def usageShort()
puts <<EOT
Usage: #{$0} <ACTION> -t <TYPE> <NOUN> {<OPTIONS>}
Actions (deployment): [ --list-changes | --commit-changes | --rollback-changes ]
Actions (configuration): [ --list (default) | --create | --delete | --patch ]
--type=<objectType>| -t <objectType>: objectType: { host | hostgroup | contact | contactgroup | service | servicegroup }
Simplify your use of this command by keeping your credentials in config.yml. For full help, use option --help
Example: ./monitor.rb --t host (will list all hosts, since the default action is --list)
EOT
end
#-------------------------------------------------------------------
# usage - minimal help page + examples
#-------------------------------------------------------------------
def usage()
usageShort
puts <<EOT
* List all hosts - #{$0} -t host
* List all services - #{$0} -t service
* List all services for one host: - #{$0} -t service --host="<hostname>"
* List all services named "SSH" - #{$0} -t service --service=SSH
Query (--query="<query>"):
If a query has a result set (hits), the return code is 0, otherwise 1. The queries are based on the OP5 filters, where the type will be passed as the output format, i.e. [services] or [hosts]
* List acknowledged services - #{$0} -t service --query="acknowledged != 0"
* List services containing "l0" or "L0" - - #{$0} -t service --query='description ~~ "l0"'
* List service comments that are acknowledged, and parse the output for Service Now incidents
#{$0} -t service-comment --acknowledged | sed -e 's!\\([iI][nN][cC][0-9]*\\)!<a href="https://<snow url>/nav_to.do?uri=incident.do?sysparm_query=number=\\1">\\1</a>!g'
* List services in unacknowledged services in Warning/Critical state, that is not scheduled for downtime (identical to a default view in the web gui)
#{$0} -t service --query="host.scheduled_downtime_depth = 0 and ((state != 0 and acknowledged = 0 and scheduled_downtime_depth = 0) or (host.state != 0 and host.acknowledged = 0))"
* Check if a host or services exists - The result code is 0 if the host exists, otherwise 1
#{$0} -t host --host=monitor > /dev/null ; echo $?
Other hints:
* Use another config file than default (config.yml)
#{$0} --config=/path/to/my/config/file.yml
For an overview of query parameters, refer to https://kb.op5.com/display/GUI/Listview+filter+columns
* Create a host named kmg-test003ec2, with a couple of custom variables, and put it into hostgroup ec2-linux-std:
./monitor.rb -t host --create --options='host_name=kmg-test003ec2,alias=win-test003ec2,address=127.0.0.1,_EC2_ID=i-dab2b385,hostgroups=ec2-linux-std,_EC2_REGION=us-west-1'
EOT
end
def op5DoFilterRequest(type, query)
logIt("* Entering: #{thisMethod()}", DEBUG)
logIt(" - type: #{type} query: #{query}", DEBUG)
columns = $allColumns[type]
thisQuery = "[#{type}]#{query}&columns=#{columns}"
logIt(" - query: #{thisQuery}",DEBUG)
uriEncoded = URI.encode($op5Url + "/api/filter/query?format=json&query=#{thisQuery}&limit=10000000")
$stderr.puts "URI: #{uriEncoded}" if $debug
uri = URI.parse(uriEncoded)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
request.basic_auth($op5User, $op5Password )
response = http.request(request)
res=JSON.parse(response.body)
return res
end #--- end of op5DoFilterRequest
#--------------------------------------------------
# host functions
#--------------------------------------------------
def op5GetHostComments(host)
res = op5DoFilterRequest("comments", 'is_service = 0 and host.name = "' + host + '"')
return res
end
def op5GetAllHosts()
logIt("* Entering: #{thisMethod()}", DEBUG)
res = op5DoFilterRequest(OBJECT_TYPE_HOST, "all")
return res
end
def op5GetHost(host)
logIt("Looking for host: #{host}",DEBUG)
res = op5DoFilterRequest(OBJECT_TYPE_HOST, 'name ="' +host +'"')
return res
end
#--------------------------------------------------
# hostgroup functions
#--------------------------------------------------
def op5GetAllHostgroups()
logIt("* Entering: #{thisMethod()}", DEBUG)
res = op5DoFilterRequest(OBJECT_TYPE_HOSTGROUP, "all")
return res
end
#--------------------------------------------------
# service functions
#--------------------------------------------------
def op5GetAllServices()
logIt("* Entering: #{thisMethod()}", DEBUG)
res = op5DoFilterRequest(OBJECT_TYPE_SERVICE, "all")
return res
end
#--------------------------------------------------
# servicegroup functions
#--------------------------------------------------
def op5GetAllServicegroups()
logIt("* Entering: #{thisMethod()}", DEBUG)
res = op5DoFilterRequest(OBJECT_TYPE_SERVICEGROUP, "all")
return res
end
#--------------------------------------------------
# contact functions
#--------------------------------------------------
def op5GetAllContacts()
logIt("* Entering: #{thisMethod()}", DEBUG)
contacts = op5DoFilterRequest(OBJECT_TYPE_CONTACT, "all")
contacts.each do | contact |
contact["contactgroups"] = []
contact["contactgroups"] << op5GetContactgropusByMember(contact["name"])
end
return contacts
end
def op5GetContactsByQuery(query)
logIt("* Entering: #{thisMethod()}", DEBUG)
contacts = op5DoFilterRequest(OBJECT_TYPE_CONTACT, query)
contacts.each do | contact |
contact["contactgroups"] = []
contact["contactgroups"] << op5GetContactgropusByMember(contact["name"])
end
return contacts
end
#--------------------------------------------------
# contactgroup functions
#--------------------------------------------------
def op5GetAllContactgroups()
logIt("* Entering: #{thisMethod()}", DEBUG)
res = op5DoFilterRequest(OBJECT_TYPE_CONTACTGROUP, "all")
return res
end
def op5GetContactgropusByMember(member)
contactgroups = []
contactgroups = op5DoFilterRequest("contactgroups", 'members >= "' + member + '"')
return contactgroups
end
#-------------------------------------------------------------------
# op5DeleteObjects() - wrapper method for deleting multiple objects
#-------------------------------------------------------------------
def op5DeleteObjects(type,objects)
logIt("* Entering: #{thisMethod()}", DEBUG)
objects.each do |object|
op5DeleteObject(type, object["name"])
end
end
def op5DeleteObject(type, name)
logIt("* Entering: #{thisMethod()}", DEBUG)
type = type.gsub(/s$/, '')
if (name.nil? || name == "")
logIt("Error: no object name passed.", DEBUG)
return 1
end
logIt(" - Preparing to delete #{type} named #{name}", DEBUG)
uriEncoded=URI.encode($op5Url + "/api/config/#{type}/#{name}?format=json")
uri = URI.parse(uriEncoded)
#pp uri
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Delete.new(uri.request_uri)
request.basic_auth($op5User, $op5Password )
response = http.request(request)
puts response.body
return 0
end
def op5CreateObjectByTypeJSON(type, jsonString)
logIt("* Entering: #{thisMethod()}", DEBUG)
thisObject = JSON::parse(jsonString)
pp thisObject
type = type.gsub(/s$/, '')
hostName=thisObject["host_name"]
uriEncoded = URI.encode($op5Url + "/api/config/#{type}/#{hostName}")
logIt("URI: #{uriEncoded}", DEBUG)
uri = URI.parse(uriEncoded)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Put.new(uri.request_uri)
request.add_field("content-type","application/json")
request.basic_auth($op5User, $op5Password )
request.body = jsonString
response = http.request(request)
res=JSON.parse(response.body)
return res
end
def op5CreateObjectByType(type, objectFilter)
logIt("* Entering: #{thisMethod()}", DEBUG)
#--- has to be expanded to handle all object filter parameters and all types (this only covers OBJECT_TYPE_HOST)
case type
when OBJECT_TYPE_HOST
begin
newObject = {
:host_name => ( objectFilter[:host ].nil?) ? objectFilter[:name] : objectFilter[:host],
:alias => objectFilter[:name ],
:address => objectFilter[:address ],
:hostgroups => [ objectFilter[:hostgroup] ],
:template => ( objectFilter[:template ].nil? ) ? "default-host-template" : objectFilter[:template]
}
if (! objectFilter[:custom_variables].nil?)
objectFilter[:custom_variables].each do |custom_variable|
key, value = custom_variable.split(/=/)
puts "key: #{key}, value: #{value}"
newObject[key] = value
end
end
end
else
return {}
end
type = type.gsub(/s$/, '')
objectName=newObject[:host_name]
uriEncoded = URI.encode($op5Url + "/api/config/#{type}/#{objectName}")
logIt("URI: #{uriEncoded}", DEBUG)
uri = URI.parse(uriEncoded)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
#request = Net::HTTP::Put.new(uri.request_uri)
request = Net::HTTP::Put.new(uri.request_uri)
request.add_field("content-type","application/json")
logIt("JSON Body incoming object: #{objectFilter.to_json}", DEBUG)
logIt("JSON Body parsed object: #{newObject.to_json}", DEBUG)
request.basic_auth($op5User, $op5Password )
request.body = newObject.to_json
response = http.request(request)
res=JSON.parse(response.body)
puts res
return res
end
#-------------------------------------------------------------------
# op5PrintObjects() - wrapper method for listing objects
#-------------------------------------------------------------------
def op5PrintObjects(type,objects)
logIt("* Entering: #{thisMethod()}", DEBUG)
# pp objects
objects.each do |object|
case type
when OBJECT_TYPE_HOST
printf "name: %-25s alias: %-40s address: %-15s state: %i\n", object["name"], object["alias"], object["address"], object["state"] if (! object["name"].nil?)
when OBJECT_TYPE_HOSTGROUP, OBJECT_TYPE_CONTACTGROUP, OBJECT_TYPE_SERVICEGROUP
printf "name: %-25s alias: %-40s members: ", object["name"], object["alias"] if (! object["name"].nil?)
printCount=0
object["members"].each do | member |
printf "," if printCount > 0
printf "%s", (member.class.to_s == "Array") ? member[0] + ";" + member[1] : member
printCount += 1
end
printf "\n"
when OBJECT_TYPE_SERVICE
printf "host_name: %-25s description: %-40s\n", object["host"]["name"], object["description"] if (! object["description"].nil?)
when OBJECT_TYPE_CONTACT
printf "name: %-25s alias: %-30s email: %-35s contactgroups: ", object["name"], object["alias"], object["email"] if (! object["name"].nil?)
printCount=0
object["contactgroups"].each do |contactgroup|
printf "," if printCount > 0
printf "%s", contactgroup[0]["name"]
printCount += 1
end
printf "\n"
end
end
end
def op5GetAllObjectsByType(type)
logIt("* Entering: #{thisMethod()}", DEBUG)
res = {}
case type
when OBJECT_TYPE_HOST ; res = op5GetAllHosts()
when OBJECT_TYPE_HOSTGROUP ; res = op5GetAllHostgroups()
when OBJECT_TYPE_SERVICE ; res = op5GetAllServices()
when OBJECT_TYPE_SERVICEGROUP ; res = op5GetAllServicegroups()
when OBJECT_TYPE_CONTACT ; res = op5GetAllContacts()
when OBJECT_TYPE_CONTACTGROUP ; res = op5GetAllContactgroups()
end
return res
end
def op5GetObjectsByQuery(type, query)
logIt("* Entering: #{thisMethod()}", DEBUG)
res = {}
#--- exceptions, when not enough information is returned (i.e. the contacts does not return the members of the contact group)
case type
when OBJECT_TYPE_CONTACT
res = op5GetContactsByQuery(query)
else
res = op5DoFilterRequest(type, query)
end
return res
end
def getQueryStringByType(type, objectFilter = {})
#--------------------------------------
# translation definitions
#--------------------------------------
translateTable = {
"services" => {
:alias => "alias",
:name => "description",
:host => "host.name"
},
"servicegroups" => {
:alias => "alias",
:name => "name"
},
"hosts" => {
:alias => "alias",
:name => "name",
:host => "name"
},
"hostgroups" => {
:alias => "alias",
:name => "name"
},
"contacts" => {
:alias => "alias",
:name => "name"
},
"contactgroups" => {
:alias => "alias",
:name => "name"
}
}
thisQuery = ""
predicateCount = 0
objectFilter.each do | snippet |
logIt(" - snippet: #{snippet}", DEBUG)
thisQuery += " and " if (predicateCount > 0)
thisQuery += translateTable[type][snippet[0]] + ' = "' + snippet[1].to_s + '"'
predicateCount += 1
end
thisQuery = "all" if (thisQuery == "")
return thisQuery
end
def op5ListObjects(type, objectFilter = {})
logIt("* Entering: #{thisMethod()}", DEBUG)
logIt(" - objectFilter: " + objectFilter.inspect, DEBUG)
res = {}
if (objectFilter[:query].nil?)
objectFilter[:query] = getQueryStringByType(type, objectFilter)
logIt(" - parsedQuery: " + objectFilter[:query], DEBUG)
end
if ( objectFilter == {})
res = op5GetAllObjectsByType(type)
elsif (!objectFilter[:query].nil?)
logIt("type: #{type} query: #{objectFilter[:query]}", DEBUG)
res = op5GetObjectsByQuery(type, objectFilter[:query])
#op5DoFilterRequest(type, objectFilter[:query])
end
return res
end
def getObjectType(type)
logIt("* Entering: #{thisMethod()}", DEBUG)
res = nil
case type
when 'host','hosts' ; res = OBJECT_TYPE_HOST
when 'hostgroup','hostgroups' ; res = OBJECT_TYPE_HOSTGROUP
when 'contact', 'contacts' ; res = OBJECT_TYPE_CONTACT
when 'contactgroup', 'contactgroups' ; res = OBJECT_TYPE_CONTACTGROUP
when 'service', 'services' ; res = OBJECT_TYPE_SERVICE
when 'servicegroup', 'servicegroups' ; res = OBJECT_TYPE_SERVICEGROUP
end
logIt(" - type: #{res}", DEBUG)
return res
end
#============================================
#============================================
# MAIN
#============================================
#============================================
opts = GetoptLong.new
opts.quiet = true
#-----------------------------
# define options
#-----------------------------
begin
opts.set_options(
[ "--help-short", "-h", GetoptLong::NO_ARGUMENT],
[ "--help", GetoptLong::NO_ARGUMENT],
[ "--debug", GetoptLong::NO_ARGUMENT],
[ "--verbose", "-v", GetoptLong::OPTIONAL_ARGUMENT],
#--- credentials and configuration
[ "--user", "-u", GetoptLong::OPTIONAL_ARGUMENT],
[ "--password", "-p", GetoptLong::OPTIONAL_ARGUMENT],
[ "--url", GetoptLong::OPTIONAL_ARGUMENT],
[ "--config", "-C", GetoptLong::OPTIONAL_ARGUMENT],
#--- actions -> deployment
[ "--list-changes", GetoptLong::NO_ARGUMENT],
[ "--rollback-changes", GetoptLong::NO_ARGUMENT],
[ "--commit-changes", GetoptLong::NO_ARGUMENT],
#--- actions -> configuration
[ "--create", GetoptLong::OPTIONAL_ARGUMENT],
[ "--delete", GetoptLong::OPTIONAL_ARGUMENT],
#--- options
[ "--type", "-t", GetoptLong::OPTIONAL_ARGUMENT],
#--- options - objectFilters
[ "--address", GetoptLong::OPTIONAL_ARGUMENT],
[ "--alias", GetoptLong::OPTIONAL_ARGUMENT],
[ "--name", "-n", GetoptLong::OPTIONAL_ARGUMENT],
[ "--host", "-H", GetoptLong::OPTIONAL_ARGUMENT],
[ "--hostgroup", GetoptLong::OPTIONAL_ARGUMENT],
[ "--template", GetoptLong::OPTIONAL_ARGUMENT],
[ "--options", GetoptLong::OPTIONAL_ARGUMENT],
[ "--custom-variables", GetoptLong::OPTIONAL_ARGUMENT],
#--- extra features
[ "--json", GetoptLong::OPTIONAL_ARGUMENT],
[ "--query", "-Q", GetoptLong::OPTIONAL_ARGUMENT],
[ "--with-comments", GetoptLong::NO_ARGUMENT],
[ "--acknowledged", GetoptLong::NO_ARGUMENT],
[ "--get-config", GetoptLong::NO_ARGUMENT],
)
end
#-----------------------------
# parsing options
#-----------------------------
begin
opts.each do |opt, arg|
case opt
when '--help' ; usage(); exit 0
when '--help-short' ; usageShort(); exit 0
when '--debug' ; $writeToLogfile = true ; $verboseLevel = DEBUG
#--- deployment actions
when '--list-changes' ; optAction = "list-changes"
when '--rollback-changes' ; optAction = "rollback-changes"
when '--commit-changes' ; optAction = "commit-changes"
#--- configuration actions
when '--list' ; optAction = "list"
when '--create' ; optAction = "create"
when '--patch' ; optAction = "patch"
when '--delete' ; optAction = "delete"
#--- object type
when '--type' ; optType = getObjectType(arg)
#-- object filters
when '--address' ; optObjectFilter[ :address ] = arg
when '--alias' ; optObjectFilter[ :alias ] = arg
when '--contact' ; optObjectFilter[ :contact ] = arg
when '--contactgroup' ; optObjectFilter[ :contactgroup ] = arg
when '--custom-variables' ;
optObjectFilter[ :custom_variables ] = [] if (optObjectFilter[ :custom_variables ].nil?)
optObjectFilter[ :custom_variables ] << arg
when '--name' ; optObjectFilter[ :name ] = arg
when '--host' ; optObjectFilter[ :host ] = arg
when '--hostgroup' ; optObjectFilter[ :hostgroup ] = arg
when '--service' ; optObjectFilter[ :service ] = arg
when '--servicegroup' ; optObjectFilter[ :servicegroup ] = arg
when '--template' ; optObjectFilter[ :template ] = arg
when '--query' ; optObjectFilter[ :query ] = arg
#--- extra features
when '--json' ; optJSON = arg
end
end
rescue Exception => e
usageShort unless e.to_s == "exit"
exit 0
end
#-----------------------------
# Config file (yml)
#-----------------------------
begin
if File.exist?(configFile)
config = YAML.load(File.read(configFile))
#--- set default config for AWS (i.e. access key, secret key, region, http proxy in config["aws"])
# AWS.config(config["aws"])
#--- set default config for this app (in config["app"])
$op5Url = config["op5"]["url"].nil? ? nil : config["op5"]["url"]
$op5User = op5User.nil? ? config["op5"]["userName"] : op5User
$op5Password = op5Password.nil? ? config["op5"]["password"] : op5Password
else
puts "WARNING: #{configFile} does not exist"
end
end
#-----------------------------
# Actions
#-----------------------------
logIt("* Starting #{$0} - type: #{optType}",DEBUG)
case optAction
when 'list-changes'
op5ListChanges()
exit 0
when 'rollback-changes'
op5RollbackChanges()
exit 0
when 'commit-changes'
op5CommitChanges()
exit 0
when 'list'
objects = op5ListObjects(optType,optObjectFilter)
op5PrintObjects(optType, objects)
when 'create'
res = nil
if (!optJSON.nil?)
res = op5CreateObjectByTypeJSON(optType, optJSON)
else
logIt(optObjectFilter.to_json, DEBUG)
res = op5CreateObjectByType(optType, optObjectFilter)
end
op5PrintObjects(optType, [res])
exit 0
when 'patch'
when 'delete'
objects = op5ListObjects(optType,optObjectFilter)
op5DeleteObjects(optType, objects )
else
logIt("ERROR: No valid action selected.", NORMAL)
exit 1
end