icinga2 0.9.2.1 → 0.9.2.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +25 -22
- data/doc/Array.html +4 -6
- data/doc/Boolean.html +4 -6
- data/doc/FalseClass.html +4 -6
- data/doc/Hash.html +126 -8
- data/doc/Icinga2.html +7 -9
- data/doc/Logging.html +5 -7
- data/doc/Object.html +8 -10
- data/doc/Time.html +4 -6
- data/doc/TrueClass.html +4 -6
- data/doc/_index.html +20 -7
- data/doc/class_list.html +1 -1
- data/doc/file.README.html +32 -30
- data/doc/frames.html +1 -1
- data/doc/index.html +32 -30
- data/doc/method_list.html +34 -2
- data/doc/services.md +45 -62
- data/doc/top-level-namespace.html +4 -6
- data/examples/_blank.rb +2 -2
- data/examples/config.rb +23 -0
- data/examples/downtimes.rb +4 -33
- data/examples/hostgroups.rb +4 -33
- data/examples/hosts.rb +18 -33
- data/examples/informations.rb +4 -33
- data/examples/notifications.rb +4 -33
- data/examples/servicegroups.rb +4 -25
- data/examples/services.rb +46 -67
- data/examples/statistics.rb +4 -33
- data/examples/test.rb +7 -28
- data/examples/usergroups.rb +4 -33
- data/examples/users.rb +4 -33
- data/lib/icinga2/client.rb +16 -42
- data/lib/icinga2/converts.rb +16 -54
- data/lib/icinga2/downtimes.rb +46 -44
- data/lib/icinga2/hostgroups.rb +35 -35
- data/lib/icinga2/hosts.rb +235 -228
- data/lib/icinga2/network.rb +53 -125
- data/lib/icinga2/notifications.rb +37 -46
- data/lib/icinga2/servicegroups.rb +31 -41
- data/lib/icinga2/services.rb +211 -236
- data/lib/icinga2/tools.rb +10 -9
- data/lib/icinga2/usergroups.rb +22 -32
- data/lib/icinga2/users.rb +64 -59
- data/lib/icinga2/validator.rb +59 -0
- data/lib/icinga2/version.rb +1 -1
- metadata +78 -7
- data/lib/icinga2/network.rb-SAVE +0 -1004
data/examples/users.rb
CHANGED
@@ -9,40 +9,11 @@
|
|
9
9
|
# -----------------------------------------------------------------------------
|
10
10
|
|
11
11
|
require_relative '../lib/icinga2'
|
12
|
+
require_relative 'config'
|
12
13
|
|
13
14
|
# -----------------------------------------------------------------------------
|
14
15
|
|
15
|
-
|
16
|
-
icinga_api_port = ENV.fetch( 'ICINGA_API_PORT' , 5665 )
|
17
|
-
icinga_api_user = ENV.fetch( 'ICINGA_API_USER' , 'admin' )
|
18
|
-
icinga_api_pass = ENV.fetch( 'ICINGA_API_PASSWORD' , nil )
|
19
|
-
icinga_api_pki_path = ENV.fetch( 'ICINGA_API_PKI_PATH' , '/etc/icinga2' )
|
20
|
-
icinga_api_node_name = ENV.fetch( 'ICINGA_API_NODE_NAME' , nil )
|
21
|
-
icinga_cluster = ENV.fetch( 'ICINGA_CLUSTER' , false )
|
22
|
-
icinga_satellite = ENV.fetch( 'ICINGA_CLUSTER_SATELLITE', nil )
|
23
|
-
|
24
|
-
|
25
|
-
# convert string to bool
|
26
|
-
icinga_cluster = icinga_cluster.to_s.eql?('true') ? true : false
|
27
|
-
|
28
|
-
config = {
|
29
|
-
icinga: {
|
30
|
-
host: icinga_host,
|
31
|
-
api: {
|
32
|
-
port: icinga_api_port,
|
33
|
-
user: icinga_api_user,
|
34
|
-
password: icinga_api_pass,
|
35
|
-
pki_path: icinga_api_pki_path,
|
36
|
-
node_name: icinga_api_node_name
|
37
|
-
},
|
38
|
-
cluster: icinga_cluster,
|
39
|
-
satellite: icinga_satellite
|
40
|
-
}
|
41
|
-
}
|
42
|
-
|
43
|
-
# ---------------------------------------------------------------------------------------
|
44
|
-
|
45
|
-
i = Icinga2::Client.new( config )
|
16
|
+
i = Icinga2::Client.new( @config )
|
46
17
|
|
47
18
|
unless( i.nil? )
|
48
19
|
|
@@ -87,8 +58,8 @@ unless( i.nil? )
|
|
87
58
|
puts ''
|
88
59
|
|
89
60
|
rescue => e
|
90
|
-
|
91
|
-
|
61
|
+
warn( e )
|
62
|
+
warn( e.backtrace.join("\n") )
|
92
63
|
end
|
93
64
|
end
|
94
65
|
|
data/lib/icinga2/client.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
|
2
2
|
require_relative 'version'
|
3
|
+
require_relative 'validator'
|
3
4
|
require_relative 'network'
|
4
5
|
require_relative 'statistics'
|
5
6
|
require_relative 'converts'
|
@@ -45,6 +46,7 @@ module Icinga2
|
|
45
46
|
|
46
47
|
include Logging
|
47
48
|
|
49
|
+
include Icinga2::Validator
|
48
50
|
include Icinga2::Network
|
49
51
|
include Icinga2::Statistics
|
50
52
|
include Icinga2::Converts
|
@@ -61,48 +63,39 @@ module Icinga2
|
|
61
63
|
# Returns a new instance of Client
|
62
64
|
#
|
63
65
|
# @param [Hash, #read] settings the settings for Icinga2
|
64
|
-
# @option settings [String] :host
|
66
|
+
# @option settings [String] :host the Icinga2 Hostname
|
65
67
|
# @option settings [Integer] :port (5665) the Icinga2 API Port
|
66
|
-
# @option settings [String] :
|
68
|
+
# @option settings [String] :username the Icinga2 API User
|
67
69
|
# @option settings [String] :password the Icinga2 API Password
|
68
70
|
# @option settings [Integer] :version (1) the Icinga2 API Version
|
69
|
-
# @option settings [Bool] :cluster Icinga2 Cluster Mode
|
70
|
-
# @option settings [Bool] :notifications (false) enable Icinga2 Host Notifications
|
71
71
|
#
|
72
72
|
# @example to create an new Instance
|
73
73
|
# config = {
|
74
74
|
# icinga: {
|
75
75
|
# host: '192.168.33.5',
|
76
76
|
# api: {
|
77
|
-
#
|
78
|
-
# user: 'root',
|
77
|
+
# username: 'root',
|
79
78
|
# password: 'icinga',
|
80
79
|
# version: 1
|
81
|
-
# }
|
82
|
-
# cluster: false,
|
83
|
-
# satellite: true
|
80
|
+
# }
|
84
81
|
# }
|
85
82
|
# }
|
86
|
-
#
|
87
83
|
# @icinga = Icinga2::Client.new(config)
|
88
84
|
#
|
89
85
|
# @return [instance, #read]
|
90
86
|
#
|
91
87
|
def initialize( settings )
|
92
88
|
|
93
|
-
raise ArgumentError.new('
|
89
|
+
raise ArgumentError.new(format('wrong type. \'settings\' must be an Hash, given \'%s\'', settings.class.to_s)) unless( settings.is_a?(Hash) )
|
94
90
|
raise ArgumentError.new('missing settings') if( settings.size.zero? )
|
95
91
|
|
96
|
-
icinga_host = settings.dig(:icinga, :host)
|
92
|
+
icinga_host = settings.dig(:icinga, :host)
|
97
93
|
icinga_api_port = settings.dig(:icinga, :api, :port) || 5665
|
98
|
-
icinga_api_user = settings.dig(:icinga, :api, :
|
94
|
+
icinga_api_user = settings.dig(:icinga, :api, :username)
|
99
95
|
icinga_api_pass = settings.dig(:icinga, :api, :password)
|
100
96
|
icinga_api_version = settings.dig(:icinga, :api, :version) || 1
|
101
97
|
icinga_api_pki_path = settings.dig(:icinga, :api, :pki_path)
|
102
98
|
icinga_api_node_name = settings.dig(:icinga, :api, :node_name)
|
103
|
-
@icinga_cluster = settings.dig(:icinga, :cluster) || false
|
104
|
-
@icinga_satellite = settings.dig(:icinga, :satellite)
|
105
|
-
@icinga_notifications = settings.dig(:icinga, :notifications) || false
|
106
99
|
|
107
100
|
@last_call_timeout = 320
|
108
101
|
@last_cib_data_called = 0
|
@@ -116,13 +109,11 @@ module Icinga2
|
|
116
109
|
_has_cert, @options = cert?(
|
117
110
|
pki_path: icinga_api_pki_path,
|
118
111
|
node_name: icinga_api_node_name,
|
119
|
-
|
112
|
+
username: icinga_api_user,
|
120
113
|
password: icinga_api_pass
|
121
114
|
)
|
122
115
|
|
123
116
|
@headers = { 'Content-Type' => 'application/json', 'Accept' => 'application/json' }
|
124
|
-
|
125
|
-
self
|
126
117
|
end
|
127
118
|
|
128
119
|
# create a HTTP Header based on a Icinga2 Certificate or an User API Login
|
@@ -137,9 +128,9 @@ module Icinga2
|
|
137
128
|
# @icinga.cert?(pki_path: '/etc/icinga2', node_name: 'icinga2-dashing')
|
138
129
|
#
|
139
130
|
# @example with User
|
140
|
-
# @icinga.cert?(
|
131
|
+
# @icinga.cert?(username: 'root', password: 'icinga')
|
141
132
|
#
|
142
|
-
# @return [
|
133
|
+
# @return [Array]
|
143
134
|
#
|
144
135
|
def cert?( params )
|
145
136
|
|
@@ -148,7 +139,7 @@ module Icinga2
|
|
148
139
|
|
149
140
|
pki_path = params.dig(:pki_path)
|
150
141
|
node_name = params.dig(:node_name)
|
151
|
-
|
142
|
+
username = params.dig(:username)
|
152
143
|
password = params.dig(:password)
|
153
144
|
|
154
145
|
if( node_name.nil? )
|
@@ -156,7 +147,6 @@ module Icinga2
|
|
156
147
|
node_name = Socket.gethostbyname(Socket.gethostname).first
|
157
148
|
logger.debug(format('node name: %s', node_name))
|
158
149
|
rescue SocketError => e
|
159
|
-
|
160
150
|
raise format("can't resolve hostname (%s)", e)
|
161
151
|
end
|
162
152
|
end
|
@@ -184,19 +174,17 @@ module Icinga2
|
|
184
174
|
} ]
|
185
175
|
|
186
176
|
else
|
187
|
-
|
188
177
|
logger.debug( 'PKI not found, using basic auth for connection to Icinga 2 API' )
|
189
178
|
|
190
|
-
raise ArgumentError.new('Missing
|
191
|
-
raise ArgumentError.new('Missing password')
|
179
|
+
raise ArgumentError.new('Missing \'username\'') if( username.nil? )
|
180
|
+
raise ArgumentError.new('Missing \'password\'') if( password.nil? )
|
192
181
|
|
193
182
|
[false, {
|
194
|
-
user:
|
183
|
+
user: username,
|
195
184
|
password: password,
|
196
185
|
verify_ssl: OpenSSL::SSL::VERIFY_NONE
|
197
186
|
} ]
|
198
187
|
end
|
199
|
-
|
200
188
|
end
|
201
189
|
|
202
190
|
# return Icinga2 Application data
|
@@ -214,28 +202,14 @@ module Icinga2
|
|
214
202
|
options: @options
|
215
203
|
)
|
216
204
|
|
217
|
-
#puts '---'
|
218
|
-
#puts data
|
219
|
-
#puts data.class.to_s
|
220
|
-
#puts '---'
|
221
|
-
|
222
205
|
return nil if( data.nil? )
|
223
|
-
# return data if( data.is_a?(Hash) && data.dig('code').to_i != 200 )
|
224
|
-
#puts '.'
|
225
206
|
|
226
207
|
app_data = data.dig('icingaapplication','app')
|
227
208
|
|
228
|
-
#puts '---'
|
229
|
-
#puts app_data
|
230
|
-
#puts app_data.class.to_s
|
231
|
-
#puts '---'
|
232
|
-
|
233
209
|
# version and revision
|
234
210
|
@version, @revision = parse_version(app_data.dig('version'))
|
235
|
-
|
236
211
|
# - node_name
|
237
212
|
@node_name = app_data.dig('node_name')
|
238
|
-
|
239
213
|
# - start_time
|
240
214
|
@start_time = Time.at(app_data.dig('program_start').to_f)
|
241
215
|
|
data/lib/icinga2/converts.rb
CHANGED
@@ -8,6 +8,12 @@ module Icinga2
|
|
8
8
|
#
|
9
9
|
module Converts
|
10
10
|
|
11
|
+
HOST_STATE_STRING = %w[Up Down].freeze
|
12
|
+
SERVICE_STATE_STRING = %w[OK Warning Critical Unknown].freeze
|
13
|
+
|
14
|
+
HOST_STATE_COLOR = %w[green red].freeze
|
15
|
+
SERVICE_STATE_COLOR = %w[green yellow red purple].freeze
|
16
|
+
|
11
17
|
# convert a Icinga2 state into a human readable state
|
12
18
|
#
|
13
19
|
# @param [String] state the Icinga2 State
|
@@ -15,33 +21,11 @@ module Icinga2
|
|
15
21
|
#
|
16
22
|
# @return [String]
|
17
23
|
#
|
18
|
-
def
|
19
|
-
|
20
|
-
state
|
21
|
-
if(
|
22
|
-
|
23
|
-
when 0
|
24
|
-
'Up'
|
25
|
-
when 1
|
26
|
-
'Down'
|
27
|
-
else
|
28
|
-
'Undefined'
|
29
|
-
end
|
30
|
-
else
|
31
|
-
case state
|
32
|
-
when 0
|
33
|
-
'OK'
|
34
|
-
when 1
|
35
|
-
'Warning'
|
36
|
-
when 2
|
37
|
-
'Critical'
|
38
|
-
when 3
|
39
|
-
'Unknown'
|
40
|
-
else
|
41
|
-
'Undefined'
|
42
|
-
end
|
43
|
-
end
|
44
|
-
state
|
24
|
+
def state_to_string( state, is_host = false )
|
25
|
+
result = SERVICE_STATE_STRING[state] unless( is_host )
|
26
|
+
result = HOST_STATE_STRING[state] if( is_host )
|
27
|
+
result = 'Undefined' if( result.nil? )
|
28
|
+
result
|
45
29
|
end
|
46
30
|
|
47
31
|
# convert a Icinga2 state into a named color
|
@@ -51,33 +35,11 @@ module Icinga2
|
|
51
35
|
#
|
52
36
|
# @return [String]
|
53
37
|
#
|
54
|
-
def
|
55
|
-
|
56
|
-
state
|
57
|
-
if(
|
58
|
-
|
59
|
-
when 0
|
60
|
-
'green'
|
61
|
-
when 1
|
62
|
-
'red'
|
63
|
-
else
|
64
|
-
'blue'
|
65
|
-
end
|
66
|
-
else
|
67
|
-
case state
|
68
|
-
when 0
|
69
|
-
'green'
|
70
|
-
when 1
|
71
|
-
'yellow'
|
72
|
-
when 2
|
73
|
-
'red'
|
74
|
-
when 3
|
75
|
-
'purple'
|
76
|
-
else
|
77
|
-
'blue'
|
78
|
-
end
|
79
|
-
end
|
80
|
-
state
|
38
|
+
def state_to_color( state, is_host = false )
|
39
|
+
result = SERVICE_STATE_COLOR[state] unless( is_host )
|
40
|
+
result = HOST_STATE_COLOR[state] if( is_host )
|
41
|
+
result = 'blue' if( result.nil? )
|
42
|
+
result
|
81
43
|
end
|
82
44
|
|
83
45
|
# reformat a service check name
|
data/lib/icinga2/downtimes.rb
CHANGED
@@ -4,19 +4,28 @@
|
|
4
4
|
module Icinga2
|
5
5
|
|
6
6
|
# namespace for downtimes handling
|
7
|
+
#
|
8
|
+
# Schedule a downtime for hosts and services.
|
9
|
+
#
|
7
10
|
module Downtimes
|
8
11
|
|
9
12
|
# add downtime
|
10
13
|
#
|
11
14
|
# @param [Hash] params
|
12
|
-
# @option params [String]
|
13
|
-
# @option params [String]
|
14
|
-
# @option params [String]
|
15
|
-
# @option params [Integer]
|
16
|
-
# @option params [Integer]
|
17
|
-
# @option params [String]
|
18
|
-
# @option params [String]
|
19
|
-
# @option params [String]
|
15
|
+
# @option params [String] host_name
|
16
|
+
# @option params [String] host_group
|
17
|
+
# @option params [String] type 'host' or 'service' downtime
|
18
|
+
# @option params [Integer] start_time (Time.new.to_i) Timestamp marking the beginning of the downtime. (required)
|
19
|
+
# @option params [Integer] end_time Timestamp marking the end of the downtime. (required)
|
20
|
+
# @option params [String] author Name of the author. (required)
|
21
|
+
# @option params [String] comment Comment text. (required)
|
22
|
+
# @option params [String] config_owner
|
23
|
+
# @option params [Integer] duration Duration of the downtime in seconds if fixed is set to false. (Required for flexible downtimes.)
|
24
|
+
# @option params [Integer] entry_time
|
25
|
+
# @option params [Boolean] fixed (true) Defaults to true. If true, the downtime is fixed otherwise flexible. See downtimes for more information.
|
26
|
+
# @option params [String] scheduled_by
|
27
|
+
# @option params [String] service_name
|
28
|
+
# @option params [String] triggered_by Sets the trigger for a triggered downtime. See downtimes for more information on triggered downtimes.
|
20
29
|
#
|
21
30
|
# @example
|
22
31
|
# param = {
|
@@ -37,60 +46,53 @@ module Icinga2
|
|
37
46
|
raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
|
38
47
|
raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
|
39
48
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
duration = params
|
50
|
-
entry_time = params
|
51
|
-
scheduled_by = params
|
52
|
-
service_name = params
|
53
|
-
triggered_by = params
|
54
|
-
config_owner = params
|
49
|
+
host_name = validate( params, required: false, var: 'host_name', type: String )
|
50
|
+
host_group = validate( params, required: false, var: 'host_group', type: String )
|
51
|
+
start_time = validate( params, required: false, var: 'start_time', type: Integer ) || Time.now.to_i
|
52
|
+
end_time = validate( params, required: false, var: 'end_time', type: Integer )
|
53
|
+
author = validate( params, required: true, var: 'author', type: String )
|
54
|
+
comment = validate( params, required: true, var: 'comment', type: String )
|
55
|
+
type = validate( params, required: false, var: 'type', type: String )
|
56
|
+
fixed = validate( params, required: false, var: 'fixed', type: Boolean ) || true
|
57
|
+
duration_required = true if( fixed == false )
|
58
|
+
duration = validate( params, required: duration_required, var: 'duration', type: Integer )
|
59
|
+
entry_time = validate( params, required: false, var: 'entry_time', type: Integer )
|
60
|
+
scheduled_by = validate( params, required: false, var: 'scheduled_by', type: String )
|
61
|
+
service_name = validate( params, required: false, var: 'service_name', type: String )
|
62
|
+
triggered_by = validate( params, required: false, var: 'triggered_by', type: String )
|
63
|
+
config_owner = validate( params, required: false, var: 'config_owner', type: String )
|
55
64
|
filter = nil
|
56
65
|
|
57
66
|
# sanitychecks
|
58
67
|
#
|
59
|
-
raise ArgumentError.new('
|
60
|
-
raise ArgumentError.new(
|
61
|
-
raise ArgumentError.new('
|
62
|
-
raise ArgumentError.new('Missing downtime
|
63
|
-
raise ArgumentError.new(
|
64
|
-
raise ArgumentError.new('Missing downtime comment') if( comment.nil? )
|
65
|
-
raise ArgumentError.new('Missing downtime end_time') if( end_time.nil? )
|
66
|
-
raise ArgumentError.new('end_time are equal or smaller then start_time') if( end_time.to_i <= start_time )
|
67
|
-
|
68
|
-
raise ArgumentError.new(format('wrong type. \'duration\' must be Integer, given \'%s\'', duration.class.to_s)) unless( duration.is_a?(Integer) )
|
69
|
-
raise ArgumentError.new(format('wrong type. \'fixed\' must be True or False, given \'%s\'', fixed.class.to_s)) unless( fixed.is_a?(TrueClass) || fixed.is_a?(FalseClass) )
|
68
|
+
raise ArgumentError.new(format('wrong downtype type. only \'host\' os \'service\' allowed, given \%s\'', type)) if( %w[host service].include?(type.downcase) == false )
|
69
|
+
raise ArgumentError.new('choose \'host_name\' or \'host_group\', not both') unless( host_group.nil? || host_name.nil? )
|
70
|
+
raise ArgumentError.new(format('these \'author\' are not exists: \'%s\'', author)) unless( exists_user?( author ) )
|
71
|
+
raise ArgumentError.new('Missing downtime \'end_time\'') if( end_time.nil? )
|
72
|
+
raise ArgumentError.new('\'end_time\' are equal or smaller then \'start_time\'') if( end_time.to_i <= start_time )
|
70
73
|
|
71
74
|
# TODO
|
72
|
-
#
|
75
|
+
# - more flexibility (e.g. support scheduled_by ...)
|
73
76
|
|
77
|
+
unless( host_name.nil? )
|
78
|
+
return { 'code' => 404, 'name' => host_name, 'status' => 'Object not Found' } unless( exists_host?( host_name ) )
|
79
|
+
filter = format( 'host.name == "%s"', host_name )
|
80
|
+
end
|
74
81
|
|
75
|
-
|
76
|
-
|
77
|
-
filter = format( 'host.name=="%s"', host_name )
|
78
|
-
elsif( !host_group.nil? )
|
79
|
-
|
80
|
-
# check if hostgroup available ?
|
81
|
-
#
|
82
|
+
unless( host_group.nil? )
|
83
|
+
return { 'code' => 404, 'name' => host_group, 'status' => 'Object not Found' } unless( exists_hostgroup?( host_group ) )
|
82
84
|
filter = format( '"%s" in host.groups', host_group )
|
83
85
|
end
|
84
86
|
|
85
87
|
payload = {
|
86
88
|
type: type.capitalize, # we need the first char as Uppercase
|
89
|
+
filter: filter,
|
90
|
+
fixed: fixed,
|
87
91
|
start_time: start_time,
|
88
92
|
end_time: end_time,
|
89
93
|
author: author,
|
90
94
|
comment: comment,
|
91
|
-
fixed: fixed,
|
92
95
|
duration: duration,
|
93
|
-
filter: filter,
|
94
96
|
entry_time: entry_time,
|
95
97
|
scheduled_by: scheduled_by,
|
96
98
|
host_name: host_name,
|
data/lib/icinga2/hostgroups.rb
CHANGED
@@ -9,11 +9,12 @@ module Icinga2
|
|
9
9
|
# add a hostgroup
|
10
10
|
#
|
11
11
|
# @param [Hash] params
|
12
|
-
# @option params [String]
|
13
|
-
# @option params [String]
|
14
|
-
# @option params [String]
|
15
|
-
# @option params [String]
|
16
|
-
# @option params [String]
|
12
|
+
# @option params [String] host_group hostgroup to create
|
13
|
+
# @option params [String] display_name the displayed name
|
14
|
+
# @option params [String] notes
|
15
|
+
# @option params [String] notes_url
|
16
|
+
# @option params [String] action_url
|
17
|
+
# @option params [Hash] vars ({})
|
17
18
|
#
|
18
19
|
# @example
|
19
20
|
# @icinga.add_hostgroup(host_group: 'foo', display_name: 'FOO')
|
@@ -25,27 +26,26 @@ module Icinga2
|
|
25
26
|
raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
|
26
27
|
raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
|
27
28
|
|
28
|
-
host_group
|
29
|
-
display_name = params
|
30
|
-
notes
|
31
|
-
notes_url
|
32
|
-
action_url
|
33
|
-
|
34
|
-
raise ArgumentError.new('Missing \'host_group\'') if( host_group.nil? )
|
35
|
-
raise ArgumentError.new('Missing \'display_name\'') if( display_name.nil? )
|
29
|
+
host_group = validate( params, required: true, var: 'host_group', type: String )
|
30
|
+
display_name = validate( params, required: false, var: 'display_name', type: String )
|
31
|
+
notes = validate( params, required: false, var: 'notes', type: String )
|
32
|
+
notes_url = validate( params, required: false, var: 'notes_url', type: String )
|
33
|
+
action_url = validate( params, required: false, var: 'action_url', type: String )
|
34
|
+
vars = validate( params, required: false, var: 'vars', type: Hash ) || {}
|
36
35
|
|
37
36
|
payload = {
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
37
|
+
attrs: {
|
38
|
+
display_name: display_name,
|
39
|
+
notes: notes,
|
40
|
+
notes_url: notes_url,
|
41
|
+
action_url: action_url,
|
42
|
+
vars: vars
|
43
43
|
}
|
44
44
|
}
|
45
45
|
|
46
46
|
# remove all empty attrs
|
47
47
|
payload.reject!{ |_k, v| v.nil? }
|
48
|
-
payload[
|
48
|
+
payload[:attrs].reject!{ |_k, v| v.nil? }
|
49
49
|
|
50
50
|
put(
|
51
51
|
url: format('%s/objects/hostgroups/%s', @icinga_api_url_base, host_group),
|
@@ -58,10 +58,12 @@ module Icinga2
|
|
58
58
|
# delete a hostgroup
|
59
59
|
#
|
60
60
|
# @param [Hash] params
|
61
|
-
# @option params [String]
|
61
|
+
# @option params [String] name hostgroup to delete
|
62
|
+
# @option params [Bool] cascade (false) delete hostgroup also when other objects depend on it
|
62
63
|
#
|
63
64
|
# @example
|
64
|
-
# @icinga.delete_hostgroup(
|
65
|
+
# @icinga.delete_hostgroup(name: 'foo')
|
66
|
+
# @icinga.delete_hostgroup(name: 'foo', cascade: true)
|
65
67
|
#
|
66
68
|
# @return [Hash] result
|
67
69
|
#
|
@@ -70,12 +72,15 @@ module Icinga2
|
|
70
72
|
raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
|
71
73
|
raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
|
72
74
|
|
73
|
-
|
75
|
+
name = validate( params, required: true, var: 'name', type: String )
|
76
|
+
cascade = validate( params, required: false, var: 'cascade', type: Boolean ) || false
|
77
|
+
|
78
|
+
return { 'code' => 404, 'status' => 'Object not Found' } if( exists_hostgroup?( name ) == false )
|
74
79
|
|
75
|
-
|
80
|
+
url = format( '%s/objects/hostgroups/%s%s', @icinga_api_url_base, name, cascade.is_a?(TrueClass) ? '?cascade=1' : nil )
|
76
81
|
|
77
82
|
delete(
|
78
|
-
url:
|
83
|
+
url: url,
|
79
84
|
headers: @headers,
|
80
85
|
options: @options
|
81
86
|
)
|
@@ -83,8 +88,7 @@ module Icinga2
|
|
83
88
|
|
84
89
|
# returns all usersgroups
|
85
90
|
#
|
86
|
-
# @param [
|
87
|
-
# @option params [String] :host_group ('') optional for a single hostgroup
|
91
|
+
# @param [String] host_group (nil) optional for a single hostgroup
|
88
92
|
#
|
89
93
|
# @example to get all users
|
90
94
|
# @icinga.hostgroups
|
@@ -94,16 +98,12 @@ module Icinga2
|
|
94
98
|
#
|
95
99
|
# @return [Hash] returns a hash with all hostgroups
|
96
100
|
#
|
97
|
-
def hostgroups(
|
101
|
+
def hostgroups( host_group = nil )
|
98
102
|
|
99
|
-
host_group
|
103
|
+
raise ArgumentError.new(format('wrong type. \'host_group\' must be an String, given \'%s\'', host_group.class.to_s)) unless( host_group.nil? || host_group.is_a?(String) )
|
100
104
|
|
101
|
-
url =
|
102
|
-
|
103
|
-
format( '%s/objects/hostgroups' , @icinga_api_url_base )
|
104
|
-
else
|
105
|
-
format( '%s/objects/hostgroups/%s', @icinga_api_url_base, host_group )
|
106
|
-
end
|
105
|
+
url = format( '%s/objects/hostgroups' , @icinga_api_url_base )
|
106
|
+
url = format( '%s/objects/hostgroups/%s', @icinga_api_url_base, host_group ) unless( host_group.nil? )
|
107
107
|
|
108
108
|
api_data(
|
109
109
|
url: url,
|
@@ -126,7 +126,7 @@ module Icinga2
|
|
126
126
|
raise ArgumentError.new(format('wrong type. \'host_group\' must be an String, given \'%s\'', host_group.class.to_s)) unless( host_group.is_a?(String) )
|
127
127
|
raise ArgumentError.new('Missing \'host_group\'') if( host_group.size.zero? )
|
128
128
|
|
129
|
-
result = hostgroups(host_group
|
129
|
+
result = hostgroups(host_group)
|
130
130
|
result = JSON.parse( result ) if( result.is_a?(String) )
|
131
131
|
result = result.first if( result.is_a?(Array) )
|
132
132
|
|