icinga2 0.9.2.1 → 0.9.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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
@@ -9,11 +9,11 @@ module Icinga2
|
|
9
9
|
# add a servicegroup
|
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] service_group servicegroup 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
17
|
#
|
18
18
|
# @example
|
19
19
|
# @icinga.add_servicegroup(service_group: 'foo', display_name: 'FOO')
|
@@ -25,33 +25,24 @@ module Icinga2
|
|
25
25
|
raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
|
26
26
|
raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
|
27
27
|
|
28
|
-
service_group = params
|
29
|
-
display_name
|
30
|
-
notes
|
31
|
-
notes_url
|
32
|
-
action_url
|
33
|
-
|
34
|
-
# ignore = params.dig(:ignore)
|
35
|
-
# assgin = params.dig(:assign)
|
36
|
-
|
37
|
-
raise ArgumentError.new('Missing \'service_group\'') if( service_group.nil? )
|
38
|
-
raise ArgumentError.new('Missing \'display_name\'') if( display_name.nil? )
|
28
|
+
service_group = validate( params, required: true, var: 'service_group', type: String )
|
29
|
+
display_name = validate( params, required: true, var: 'display_name', type: String )
|
30
|
+
notes = validate( params, required: false, var: 'notes', type: String )
|
31
|
+
notes_url = validate( params, required: false, var: 'notes_url', type: String )
|
32
|
+
action_url = validate( params, required: false, var: 'action_url', type: String )
|
39
33
|
|
40
34
|
payload = {
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
35
|
+
attrs: {
|
36
|
+
display_name: display_name,
|
37
|
+
notes: notes,
|
38
|
+
notes_url: notes_url,
|
39
|
+
action_url: action_url
|
46
40
|
}
|
47
41
|
}
|
48
42
|
|
49
|
-
# payload['attrs']['assign'] ||= format('assgin where %s', assign) unless(assign.nil?)
|
50
|
-
# payload['attrs']['ignore'] ||= format('ignore where %s', assign) unless(assign.nil?)
|
51
|
-
|
52
43
|
# remove all empty attrs
|
53
44
|
payload.reject!{ |_k, v| v.nil? }
|
54
|
-
payload[
|
45
|
+
payload[:attrs].reject!{ |_k, v| v.nil? }
|
55
46
|
|
56
47
|
put(
|
57
48
|
url: format( '%s/objects/servicegroups/%s', @icinga_api_url_base, service_group ),
|
@@ -64,10 +55,11 @@ module Icinga2
|
|
64
55
|
# delete a servicegroup
|
65
56
|
#
|
66
57
|
# @param [Hash] params
|
67
|
-
# @option params [String]
|
58
|
+
# @option params [String] name servicegroup to delete
|
59
|
+
# @option params [Bool] cascade (false) delete servicegroup also when other objects depend on it
|
68
60
|
#
|
69
61
|
# @example
|
70
|
-
# @icinga.delete_servicegroup(
|
62
|
+
# @icinga.delete_servicegroup('foo')
|
71
63
|
#
|
72
64
|
# @return [Array] result
|
73
65
|
#
|
@@ -76,12 +68,15 @@ module Icinga2
|
|
76
68
|
raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
|
77
69
|
raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
|
78
70
|
|
79
|
-
|
71
|
+
name = validate( params, required: true, var: 'name', type: String )
|
72
|
+
cascade = validate( params, required: false, var: 'cascade', type: Boolean ) || false
|
80
73
|
|
81
|
-
|
74
|
+
return { 'code' => 404, 'status' => 'Object not Found' } if( exists_servicegroup?( name ) == false )
|
75
|
+
|
76
|
+
url = format( '%s/objects/servicegroups/%s%s', @icinga_api_url_base, name, cascade.is_a?(TrueClass) ? '?cascade=1' : nil )
|
82
77
|
|
83
78
|
delete(
|
84
|
-
url:
|
79
|
+
url: url,
|
85
80
|
headers: @headers,
|
86
81
|
options: @options
|
87
82
|
)
|
@@ -89,8 +84,7 @@ module Icinga2
|
|
89
84
|
|
90
85
|
# returns all servicegroups
|
91
86
|
#
|
92
|
-
# @param [
|
93
|
-
# @option params [String] :service_group ('') optional for a single servicegroup
|
87
|
+
# @param [String] service_group (nil) optional for a single servicegroup
|
94
88
|
#
|
95
89
|
# @example to get all users
|
96
90
|
# @icinga.servicegroups
|
@@ -100,16 +94,12 @@ module Icinga2
|
|
100
94
|
#
|
101
95
|
# @return [Array] returns a hash with all servicegroups
|
102
96
|
#
|
103
|
-
def servicegroups(
|
97
|
+
def servicegroups( service_group = nil )
|
104
98
|
|
105
|
-
service_group
|
99
|
+
raise ArgumentError.new(format('wrong type. \'service_group\' must be an String, given \'%s\'', service_group.class.to_s)) unless( service_group.nil? || service_group.is_a?(String) )
|
106
100
|
|
107
|
-
url =
|
108
|
-
|
109
|
-
format( '%s/objects/servicegroups' , @icinga_api_url_base )
|
110
|
-
else
|
111
|
-
format( '%s/objects/servicegroups/%s', @icinga_api_url_base, service_group )
|
112
|
-
end
|
101
|
+
url = format( '%s/objects/servicegroups' , @icinga_api_url_base )
|
102
|
+
url = format( '%s/objects/servicegroups/%s', @icinga_api_url_base, service_group ) unless( service_group.nil? )
|
113
103
|
|
114
104
|
api_data(
|
115
105
|
url: url,
|
@@ -132,7 +122,7 @@ module Icinga2
|
|
132
122
|
raise ArgumentError.new(format('wrong type. \'service_group\' must be an String, given \'%s\'', service_group.class.to_s)) unless( service_group.is_a?(String) )
|
133
123
|
raise ArgumentError.new('Missing \'service_group\'') if( service_group.size.zero? )
|
134
124
|
|
135
|
-
result = servicegroups(
|
125
|
+
result = servicegroups(service_group)
|
136
126
|
result = JSON.parse( result ) if result.is_a?( String )
|
137
127
|
result = result.first if( result.is_a?(Array) )
|
138
128
|
|
data/lib/icinga2/services.rb
CHANGED
@@ -8,10 +8,35 @@ module Icinga2
|
|
8
8
|
# add service
|
9
9
|
#
|
10
10
|
# @param [Hash] params
|
11
|
-
# @option params [String]
|
12
|
-
# @option params [String]
|
13
|
-
# @option params [
|
14
|
-
# @option params [
|
11
|
+
# @option params [String] name
|
12
|
+
# @option params [String] host_name
|
13
|
+
# @option params [String] display_name
|
14
|
+
# @option params [Array] templates
|
15
|
+
# @option params [Array] groups
|
16
|
+
# @option params [String] notes
|
17
|
+
# @option params [String] notes_url
|
18
|
+
# @option params [String] action_url
|
19
|
+
# @option params [String] check_command
|
20
|
+
# @option params [Integer] check_interval
|
21
|
+
# @option params [String] check_period
|
22
|
+
# @option params [Integer] check_timeout
|
23
|
+
# @option params [String] command_endpoint
|
24
|
+
# @option params [Boolean] enable_active_checks
|
25
|
+
# @option params [Boolean] enable_event_handler
|
26
|
+
# @option params [Boolean] enable_flapping
|
27
|
+
# @option params [Boolean] enable_notifications
|
28
|
+
# @option params [Boolean] enable_passive_checks
|
29
|
+
# @option params [Boolean] enable_perfdata
|
30
|
+
# @option params [String] event_command
|
31
|
+
# @option params [Integer] flapping_threshold_high
|
32
|
+
# @option params [Integer] flapping_threshold_low
|
33
|
+
# @option params [Integer] flapping_threshold
|
34
|
+
# @option params [String] icon_image_alt
|
35
|
+
# @option params [String] icon_image
|
36
|
+
# @option params [Integer] max_check_attempts
|
37
|
+
# @option params [Integer] retry_interval
|
38
|
+
# @option params [Boolean] volatile
|
39
|
+
# @option params [Hash] vars
|
15
40
|
#
|
16
41
|
# @example
|
17
42
|
# @icinga.add_service(
|
@@ -31,122 +56,77 @@ module Icinga2
|
|
31
56
|
# * status
|
32
57
|
# * message
|
33
58
|
#
|
34
|
-
def add_service( params
|
59
|
+
def add_service( params )
|
35
60
|
|
36
61
|
raise ArgumentError.new(format('wrong type. params must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
|
37
62
|
raise ArgumentError.new('missing params') if( params.size.zero? )
|
38
63
|
|
39
|
-
name
|
40
|
-
host_name
|
41
|
-
display_name
|
42
|
-
groups
|
43
|
-
check_command = params
|
44
|
-
max_check_attempts = params
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
retry_interval
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
flapping_threshold = params
|
55
|
-
enable_perfdata
|
56
|
-
event_command = params
|
57
|
-
volatile = params
|
58
|
-
zone
|
59
|
-
command_endpoint = params
|
60
|
-
notes
|
61
|
-
notes_url
|
62
|
-
action_url = params
|
63
|
-
icon_image
|
64
|
-
icon_image_alt
|
65
|
-
templates
|
66
|
-
vars
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
%w[display_name
|
71
|
-
host_name
|
72
|
-
check_command
|
73
|
-
check_period
|
74
|
-
event_command
|
75
|
-
zone
|
76
|
-
name
|
77
|
-
command_endpoint
|
78
|
-
notes
|
79
|
-
notes_url
|
80
|
-
action_url
|
81
|
-
icon_image
|
82
|
-
icon_image_alt
|
83
|
-
].each do |attr|
|
84
|
-
v = eval(attr)
|
85
|
-
raise ArgumentError.new(format('wrong type. \'%s\' must be an String, given \'%s\'', attr, v.class.to_s)) unless( v.is_a?(String) || v.nil? )
|
86
|
-
end
|
87
|
-
|
88
|
-
%w[max_check_attempts
|
89
|
-
flapping_threshold
|
90
|
-
check_timeout
|
91
|
-
check_interval
|
92
|
-
retry_interval].each do |attr|
|
93
|
-
v = eval(attr)
|
94
|
-
raise ArgumentError.new(format('wrong type. \'%s\' must be an Integer, given \'%s\'', attr, v.class.to_s)) unless( v.is_a?(Integer) || v.nil? )
|
95
|
-
end
|
96
|
-
|
97
|
-
%w[enable_notifications
|
98
|
-
enable_active_checks
|
99
|
-
enable_passive_checks
|
100
|
-
enable_event_handler
|
101
|
-
enable_flapping
|
102
|
-
enable_perfdata
|
103
|
-
volatile].each do |attr|
|
104
|
-
v = eval(attr)
|
105
|
-
raise ArgumentError.new(format('wrong type. \'%s\' must be True or False, given \'%s\'', attr, v.class.to_s)) unless( v.is_a?(TrueClass) || v.is_a?(FalseClass) || v.nil? )
|
106
|
-
end
|
107
|
-
|
108
|
-
%w[groups templates].each do |attr|
|
109
|
-
v = eval(attr)
|
110
|
-
raise ArgumentError.new(format('wrong type. \'%s\' must be an Array, given \'%s\'', attr, v.class.to_s)) unless( v.is_a?(Array) || v.nil? )
|
111
|
-
end
|
112
|
-
|
113
|
-
raise ArgumentError.new(format('wrong type. \'vars\' must be an Hash, given \'%s\'', v.class.to_s)) unless( vars.is_a?(Hash) )
|
64
|
+
name = validate( params, required: true, var: 'name', type: String )
|
65
|
+
host_name = validate( params, required: true, var: 'host_name', type: String )
|
66
|
+
display_name = validate( params, required: false, var: 'display_name', type: String )
|
67
|
+
groups = validate( params, required: false, var: 'groups', type: Array )
|
68
|
+
check_command = validate( params, required: false, var: 'check_command', type: String )
|
69
|
+
max_check_attempts = validate( params, required: false, var: 'max_check_attempts', type: Integer ) || 3
|
70
|
+
check_interval = validate( params, required: false, var: 'check_interval', type: Integer ) || 60
|
71
|
+
check_period = validate( params, required: false, var: 'check_period', type: Integer )
|
72
|
+
check_timeout = validate( params, required: false, var: 'check_timeout', type: Integer )
|
73
|
+
retry_interval = validate( params, required: false, var: 'retry_interval', type: Integer ) || 45
|
74
|
+
enable_active_checks = validate( params, required: false, var: 'enable_active_checks', type: Boolean )
|
75
|
+
enable_event_handler = validate( params, required: false, var: 'enable_event_handler', type: Boolean )
|
76
|
+
enable_flapping = validate( params, required: false, var: 'enable_flapping', type: Boolean )
|
77
|
+
enable_notifications = validate( params, required: false, var: 'enable_notifications', type: Boolean ) || false
|
78
|
+
enable_passive_checks = validate( params, required: false, var: 'enable_passive_checks', type: Boolean )
|
79
|
+
flapping_threshold = validate( params, required: false, var: 'flapping_threshold', type: Integer )
|
80
|
+
enable_perfdata = validate( params, required: false, var: 'enable_perfdata', type: Boolean )
|
81
|
+
event_command = validate( params, required: false, var: 'event_command', type: String )
|
82
|
+
volatile = validate( params, required: false, var: 'volatile', type: Boolean )
|
83
|
+
zone = validate( params, required: false, var: 'zone', type: String )
|
84
|
+
command_endpoint = validate( params, required: false, var: 'command_endpoint', type: String )
|
85
|
+
notes = validate( params, required: false, var: 'notes', type: String )
|
86
|
+
notes_url = validate( params, required: false, var: 'notes_url', type: String )
|
87
|
+
action_url = validate( params, required: false, var: 'action_url', type: String )
|
88
|
+
icon_image = validate( params, required: false, var: 'icon_image', type: String )
|
89
|
+
icon_image_alt = validate( params, required: false, var: 'icon_image_alt', type: String )
|
90
|
+
templates = validate( params, required: false, var: 'templates', type: Array ) || ['generic-service']
|
91
|
+
vars = validate( params, required: false, var: 'vars', type: Hash ) || {}
|
92
|
+
|
93
|
+
# check if host exists
|
94
|
+
return { 'code' => 404, 'name' => host_name, 'status' => 'Object not Found' } unless( exists_host?( host_name ) )
|
114
95
|
|
115
96
|
payload = {
|
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
|
-
|
97
|
+
templates: templates,
|
98
|
+
attrs: {
|
99
|
+
display_name: display_name,
|
100
|
+
groups: groups,
|
101
|
+
check_command: check_command,
|
102
|
+
max_check_attempts: max_check_attempts,
|
103
|
+
check_period: check_period,
|
104
|
+
check_timeout: check_timeout,
|
105
|
+
check_interval: check_interval,
|
106
|
+
retry_interval: retry_interval,
|
107
|
+
enable_notifications: enable_notifications,
|
108
|
+
enable_active_checks: enable_active_checks,
|
109
|
+
enable_passive_checks: enable_passive_checks,
|
110
|
+
enable_event_handler: enable_event_handler,
|
111
|
+
enable_flapping: enable_flapping,
|
112
|
+
flapping_threshold: flapping_threshold,
|
113
|
+
zone: zone,
|
114
|
+
enable_perfdata: enable_perfdata,
|
115
|
+
event_command: event_command,
|
116
|
+
volatile: volatile,
|
117
|
+
command_endpoint: command_endpoint,
|
118
|
+
notes: notes,
|
119
|
+
notes_url: notes_url,
|
120
|
+
action_url: action_url,
|
121
|
+
icon_image: icon_image,
|
122
|
+
icon_image_alt: icon_image_alt,
|
123
|
+
vars: vars
|
142
124
|
}
|
143
125
|
}
|
144
126
|
|
145
|
-
payload['attrs']['vars'] = vars unless vars.empty?
|
146
|
-
|
147
127
|
# clear undefined settings
|
148
128
|
payload.reject!{ |_k, v| v.nil? }
|
149
|
-
payload[
|
129
|
+
payload[:attrs].reject!{ |_k, v| v.nil? }
|
150
130
|
|
151
131
|
put(
|
152
132
|
url: format( '%s/objects/services/%s!%s', @icinga_api_url_base, host_name, name ),
|
@@ -159,9 +139,9 @@ module Icinga2
|
|
159
139
|
# delete a service
|
160
140
|
#
|
161
141
|
# @param [Hash] params
|
162
|
-
# @option params [String]
|
163
|
-
# @option params [String]
|
164
|
-
# @option params [Bool]
|
142
|
+
# @option params [String] name service name
|
143
|
+
# @option params [String] host_name host name for the service
|
144
|
+
# @option params [Bool] cascade (false) delete service also when other objects depend on it
|
165
145
|
#
|
166
146
|
# @example
|
167
147
|
# @icinga.delete_service(host_name: 'foo', name: 'http2')
|
@@ -174,16 +154,9 @@ module Icinga2
|
|
174
154
|
raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
|
175
155
|
raise ArgumentError.new('missing params') if( params.size.zero? )
|
176
156
|
|
177
|
-
|
178
|
-
|
179
|
-
cascade
|
180
|
-
|
181
|
-
raise ArgumentError.new('Missing host') if( host_name.nil? )
|
182
|
-
raise ArgumentError.new('Missing service name') if( name.nil? )
|
183
|
-
|
184
|
-
if( ! cascade.nil? && ( ! cascade.is_a?(TrueClass) && ! cascade.is_a?(FalseClass) ) )
|
185
|
-
raise ArgumentError.new('cascade can only be true or false')
|
186
|
-
end
|
157
|
+
name = validate( params, required: true, var: 'name', type: String )
|
158
|
+
host_name = validate( params, required: true, var: 'host_name', type: String )
|
159
|
+
cascade = validate( params, required: false, var: 'cascade', type: Boolean ) || false
|
187
160
|
|
188
161
|
url = format( '%s/objects/services/%s!%s%s', @icinga_api_url_base, host_name, name, cascade.is_a?(TrueClass) ? '?cascade=1' : nil )
|
189
162
|
|
@@ -198,22 +171,18 @@ module Icinga2
|
|
198
171
|
# modify an service
|
199
172
|
#
|
200
173
|
# @param [Hash] params
|
201
|
-
# @option params [String]
|
202
|
-
# @option params [Array]
|
203
|
-
# @option params [Hash]
|
174
|
+
# @option params [String] name
|
175
|
+
# @option params [Array] templates
|
176
|
+
# @option params [Hash] vars
|
204
177
|
#
|
205
178
|
# @example
|
206
179
|
# @icinga.modify_service(
|
207
180
|
# name: 'http2',
|
181
|
+
# check_interval: 60,
|
182
|
+
# retry_interval: 10,
|
208
183
|
# vars: {
|
209
|
-
#
|
210
|
-
#
|
211
|
-
# retry_interval: 10,
|
212
|
-
# vars: {
|
213
|
-
# http_url: '/access/login' ,
|
214
|
-
# http_address: '10.41.80.63'
|
215
|
-
# }
|
216
|
-
# }
|
184
|
+
# http_url: '/access/login' ,
|
185
|
+
# http_address: '10.41.80.63'
|
217
186
|
# }
|
218
187
|
# )
|
219
188
|
#
|
@@ -223,29 +192,77 @@ module Icinga2
|
|
223
192
|
#
|
224
193
|
def modify_service( params )
|
225
194
|
|
226
|
-
raise ArgumentError.new('
|
227
|
-
raise ArgumentError.new('missing params') if( params.size.zero? )
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
195
|
+
raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
|
196
|
+
raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
|
197
|
+
|
198
|
+
name = validate( params, required: true, var: 'name', type: String )
|
199
|
+
display_name = validate( params, required: false, var: 'display_name', type: String )
|
200
|
+
groups = validate( params, required: false, var: 'groups', type: Array )
|
201
|
+
check_command = validate( params, required: false, var: 'check_command', type: String )
|
202
|
+
max_check_attempts = validate( params, required: false, var: 'max_check_attempts', type: Integer ) || 3
|
203
|
+
check_interval = validate( params, required: false, var: 'check_interval', type: Integer ) || 60
|
204
|
+
check_period = validate( params, required: false, var: 'check_period', type: Integer )
|
205
|
+
check_timeout = validate( params, required: false, var: 'check_timeout', type: Integer )
|
206
|
+
retry_interval = validate( params, required: false, var: 'retry_interval', type: Integer ) || 45
|
207
|
+
enable_active_checks = validate( params, required: false, var: 'enable_active_checks', type: Boolean )
|
208
|
+
enable_event_handler = validate( params, required: false, var: 'enable_event_handler', type: Boolean )
|
209
|
+
enable_flapping = validate( params, required: false, var: 'enable_flapping', type: Boolean )
|
210
|
+
enable_notifications = validate( params, required: false, var: 'enable_notifications', type: Boolean ) || false
|
211
|
+
enable_passive_checks = validate( params, required: false, var: 'enable_passive_checks', type: Boolean )
|
212
|
+
flapping_threshold = validate( params, required: false, var: 'flapping_threshold', type: Integer )
|
213
|
+
enable_perfdata = validate( params, required: false, var: 'enable_perfdata', type: Boolean )
|
214
|
+
event_command = validate( params, required: false, var: 'event_command', type: String )
|
215
|
+
volatile = validate( params, required: false, var: 'volatile', type: Boolean )
|
216
|
+
zone = validate( params, required: false, var: 'zone', type: String )
|
217
|
+
command_endpoint = validate( params, required: false, var: 'command_endpoint', type: String )
|
218
|
+
notes = validate( params, required: false, var: 'notes', type: String )
|
219
|
+
notes_url = validate( params, required: false, var: 'notes_url', type: String )
|
220
|
+
action_url = validate( params, required: false, var: 'action_url', type: String )
|
221
|
+
icon_image = validate( params, required: false, var: 'icon_image', type: String )
|
222
|
+
icon_image_alt = validate( params, required: false, var: 'icon_image_alt', type: String )
|
223
|
+
templates = validate( params, required: false, var: 'templates', type: Array ) || ['generic-service']
|
224
|
+
vars = validate( params, required: false, var: 'vars', type: Hash ) || {}
|
225
|
+
|
226
|
+
# check if service exists
|
227
|
+
# return { 'code' => 404, 'name' => name, 'status' => 'Object not Found' } unless( exists_service?( host_name: host_name, service_name: name ) )
|
228
|
+
# # check if host exists
|
229
|
+
# return { 'code' => 404, 'name' => host_name, 'status' => 'Object not Found' } unless( exists_host?( host_name ) )
|
237
230
|
|
238
|
-
payload = {
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
231
|
+
payload = {
|
232
|
+
templates: templates,
|
233
|
+
filter: format( 'service.name == "%s"', name ),
|
234
|
+
attrs: {
|
235
|
+
display_name: display_name,
|
236
|
+
groups: groups,
|
237
|
+
check_command: check_command,
|
238
|
+
max_check_attempts: max_check_attempts,
|
239
|
+
check_period: check_period,
|
240
|
+
check_timeout: check_timeout,
|
241
|
+
check_interval: check_interval,
|
242
|
+
retry_interval: retry_interval,
|
243
|
+
enable_notifications: enable_notifications,
|
244
|
+
enable_active_checks: enable_active_checks,
|
245
|
+
enable_passive_checks: enable_passive_checks,
|
246
|
+
enable_event_handler: enable_event_handler,
|
247
|
+
enable_flapping: enable_flapping,
|
248
|
+
flapping_threshold: flapping_threshold,
|
249
|
+
zone: zone,
|
250
|
+
enable_perfdata: enable_perfdata,
|
251
|
+
event_command: event_command,
|
252
|
+
volatile: volatile,
|
253
|
+
command_endpoint: command_endpoint,
|
254
|
+
notes: notes,
|
255
|
+
notes_url: notes_url,
|
256
|
+
action_url: action_url,
|
257
|
+
icon_image: icon_image,
|
258
|
+
icon_image_alt: icon_image_alt,
|
259
|
+
vars: vars
|
245
260
|
}
|
246
|
-
|
261
|
+
}
|
247
262
|
|
248
|
-
|
263
|
+
# clear undefined settings
|
264
|
+
payload.reject!{ |_k, v| v.nil? }
|
265
|
+
payload[:attrs].reject!{ |_k, v| v.nil? }
|
249
266
|
|
250
267
|
post(
|
251
268
|
url: format( '%s/objects/services', @icinga_api_url_base ),
|
@@ -264,13 +281,17 @@ module Icinga2
|
|
264
281
|
#
|
265
282
|
def unhandled_services
|
266
283
|
|
267
|
-
payload = {}
|
268
|
-
|
269
284
|
filter = 'service.state != ServiceOK && service.downtime_depth == 0.0 && service.acknowledgement == 0.0'
|
270
285
|
attrs = %w[__name name state acknowledgement downtime_depth last_check]
|
271
286
|
|
272
|
-
payload
|
273
|
-
|
287
|
+
payload = {
|
288
|
+
attrs: attrs,
|
289
|
+
filter: filter
|
290
|
+
}
|
291
|
+
|
292
|
+
# remove all empty attrs
|
293
|
+
payload.reject!{ |_k, v| v.nil? }
|
294
|
+
payload[:attrs].reject!{ |_k, v| v.nil? }
|
274
295
|
|
275
296
|
api_data(
|
276
297
|
url: format( '%s/objects/services', @icinga_api_url_base ),
|
@@ -283,8 +304,8 @@ module Icinga2
|
|
283
304
|
# return services
|
284
305
|
#
|
285
306
|
# @param [Hash] params
|
286
|
-
# @option params [String]
|
287
|
-
# @option params [String]
|
307
|
+
# @option params [String] host_name
|
308
|
+
# @option params [String] name
|
288
309
|
#
|
289
310
|
# @example to get all services
|
290
311
|
# @icinga.services
|
@@ -296,17 +317,13 @@ module Icinga2
|
|
296
317
|
#
|
297
318
|
def services( params = {} )
|
298
319
|
|
299
|
-
raise ArgumentError.new('
|
320
|
+
raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
|
300
321
|
|
301
|
-
host_name = params
|
302
|
-
name
|
322
|
+
host_name = validate( params, required: false, var: 'host_name', type: String )
|
323
|
+
name = validate( params, required: false, var: 'name', type: String )
|
303
324
|
|
304
|
-
url =
|
305
|
-
|
306
|
-
format( '%s/objects/services/%s', @icinga_api_url_base, host_name )
|
307
|
-
else
|
308
|
-
format( '%s/objects/services/%s!%s', @icinga_api_url_base, host_name, name )
|
309
|
-
end
|
325
|
+
url = format( '%s/objects/services/%s', @icinga_api_url_base, host_name )
|
326
|
+
url = format( '%s/objects/services/%s!%s', @icinga_api_url_base, host_name, name ) unless( name.nil? )
|
310
327
|
|
311
328
|
api_data(
|
312
329
|
url: url,
|
@@ -318,8 +335,8 @@ module Icinga2
|
|
318
335
|
# returns true if the service exists
|
319
336
|
#
|
320
337
|
# @param [Hash] params
|
321
|
-
# @option params [String]
|
322
|
-
# @option params [String]
|
338
|
+
# @option params [String] host_name
|
339
|
+
# @option params [String] name
|
323
340
|
#
|
324
341
|
# @example
|
325
342
|
# @icinga.exists_service?(host_name: 'icinga2', name: 'users')
|
@@ -328,16 +345,13 @@ module Icinga2
|
|
328
345
|
#
|
329
346
|
def exists_service?( params )
|
330
347
|
|
331
|
-
raise ArgumentError.new('
|
348
|
+
raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
|
332
349
|
raise ArgumentError.new('missing params') if( params.size.zero? )
|
333
350
|
|
334
|
-
|
335
|
-
|
351
|
+
host_name = validate( params, required: true, var: 'host_name', type: String )
|
352
|
+
name = validate( params, required: true, var: 'name', type: String )
|
336
353
|
|
337
|
-
|
338
|
-
raise ArgumentError.new('Missing service') if( service.nil? )
|
339
|
-
|
340
|
-
result = services( host_name: host, name: service )
|
354
|
+
result = services( host_name: host_name, name: name )
|
341
355
|
result = JSON.parse( result ) if result.is_a?( String )
|
342
356
|
result = result.first if( result.is_a?(Array) )
|
343
357
|
|
@@ -349,9 +363,9 @@ module Icinga2
|
|
349
363
|
# returns service objects
|
350
364
|
#
|
351
365
|
# @param [Hash] params
|
352
|
-
# @option params [Array]
|
353
|
-
# @option params [
|
354
|
-
# @option params [Array]
|
366
|
+
# @option params [Array] attrs (['name', 'state', 'acknowledgement', 'downtime_depth', 'last_check'])
|
367
|
+
# @option params [String] filter ([])
|
368
|
+
# @option params [Array] joins (['host.name','host.state','host.acknowledgement','host.downtime_depth','host.last_check'])
|
355
369
|
#
|
356
370
|
# @example with default attrs and joins
|
357
371
|
# @icinga.service_objects
|
@@ -363,22 +377,17 @@ module Icinga2
|
|
363
377
|
#
|
364
378
|
def service_objects( params = {} )
|
365
379
|
|
366
|
-
attrs = params
|
367
|
-
filter = params
|
368
|
-
joins = params
|
369
|
-
payload = {}
|
370
|
-
|
371
|
-
if( attrs.nil? )
|
372
|
-
attrs = %w[name state acknowledgement downtime_depth last_check]
|
373
|
-
end
|
380
|
+
attrs = validate( params, required: false, var: 'attrs', type: Array ) || %w[name state acknowledgement downtime_depth last_check]
|
381
|
+
filter = validate( params, required: false, var: 'filter', type: String )
|
382
|
+
joins = validate( params, required: false, var: 'joins', type: Array ) || ['host.name', 'host.state', 'host.acknowledgement', 'host.downtime_depth', 'host.last_check']
|
374
383
|
|
375
|
-
|
376
|
-
|
377
|
-
|
384
|
+
payload = {
|
385
|
+
attrs: attrs,
|
386
|
+
filter: filter,
|
387
|
+
joins: joins
|
388
|
+
}
|
378
389
|
|
379
|
-
payload
|
380
|
-
payload['filter'] = filter unless filter.nil?
|
381
|
-
payload['joins'] = joins unless joins.nil?
|
390
|
+
payload.reject!{ |_k, v| v.nil? }
|
382
391
|
|
383
392
|
data = api_data(
|
384
393
|
url: format( '%s/objects/services', @icinga_api_url_base ),
|
@@ -478,6 +487,8 @@ module Icinga2
|
|
478
487
|
#
|
479
488
|
def list_services_with_problems( max_items = 5 )
|
480
489
|
|
490
|
+
raise ArgumentError.new(format('wrong type. \'max_items\' must be an Integer, given \'%s\'', max_items.class.to_s)) unless( max_items.is_a?(Integer) )
|
491
|
+
|
481
492
|
services_with_problems = {}
|
482
493
|
services_with_problems_and_severity = {}
|
483
494
|
|
@@ -486,11 +497,10 @@ module Icinga2
|
|
486
497
|
services_data = JSON.parse( services_data ) if services_data.is_a?(String)
|
487
498
|
|
488
499
|
unless( services_data.nil? )
|
489
|
-
|
490
500
|
services_data.each do |s|
|
491
|
-
|
492
501
|
name = s.dig('name')
|
493
502
|
state = s.dig('attrs','state')
|
503
|
+
|
494
504
|
next if state.zero?
|
495
505
|
|
496
506
|
services_with_problems[name] = service_severity(s)
|
@@ -613,8 +623,6 @@ module Icinga2
|
|
613
623
|
critical: problems_critical.to_i,
|
614
624
|
unknown: problems_unknown.to_i
|
615
625
|
}
|
616
|
-
|
617
|
-
# [problems_all,problems_critical,problems_warning,problems_unknown]
|
618
626
|
end
|
619
627
|
|
620
628
|
protected
|
@@ -638,16 +646,14 @@ module Icinga2
|
|
638
646
|
#
|
639
647
|
def service_severity( params )
|
640
648
|
|
641
|
-
raise ArgumentError.new('
|
649
|
+
raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
|
642
650
|
raise ArgumentError.new('missing params') if( params.size.zero? )
|
643
651
|
|
644
|
-
|
645
|
-
acknowledgement = params.dig('attrs','acknowledgement') || 0
|
646
|
-
downtime_depth = params.dig('attrs','downtime_depth') || 0
|
652
|
+
attrs = params.dig('attrs')
|
647
653
|
|
648
|
-
|
649
|
-
|
650
|
-
|
654
|
+
state = validate( attrs, required: true, var: 'state', type: Float )
|
655
|
+
acknowledgement = validate( attrs, required: false, var: 'acknowledgement', type: Float ) || 0
|
656
|
+
downtime_depth = validate( attrs, required: false, var: 'downtime_depth', type: Float ) || 0
|
651
657
|
|
652
658
|
severity = 0
|
653
659
|
|
@@ -663,7 +669,6 @@ module Icinga2
|
|
663
669
|
severity += 16 if object_has_been_checked?(params)
|
664
670
|
|
665
671
|
unless state.zero?
|
666
|
-
|
667
672
|
severity +=
|
668
673
|
if state == 1
|
669
674
|
32
|
@@ -698,35 +703,5 @@ module Icinga2
|
|
698
703
|
severity
|
699
704
|
end
|
700
705
|
|
701
|
-
private
|
702
|
-
# update host
|
703
|
-
#
|
704
|
-
# @param [Hash] hash
|
705
|
-
# @param [String] host
|
706
|
-
#
|
707
|
-
# @api protected
|
708
|
-
#
|
709
|
-
# @return [Hash]
|
710
|
-
#
|
711
|
-
def update_host( hash, host )
|
712
|
-
|
713
|
-
hash.each do |k, v|
|
714
|
-
|
715
|
-
if( k == 'host' && v.is_a?( String ) )
|
716
|
-
v.replace( host )
|
717
|
-
|
718
|
-
elsif( v.is_a?( Hash ) )
|
719
|
-
|
720
|
-
update_host( v, host )
|
721
|
-
|
722
|
-
elsif( v.is_a?(Array) )
|
723
|
-
|
724
|
-
v.flatten.each { |x| update_host( x, host ) if x.is_a?( Hash ) }
|
725
|
-
end
|
726
|
-
end
|
727
|
-
|
728
|
-
hash
|
729
|
-
end
|
730
|
-
|
731
706
|
end
|
732
707
|
end
|