icinga2 0.7.0.1 → 0.8.1.2

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.
@@ -0,0 +1,191 @@
1
+ # frozen_string_literal: true
2
+ #
3
+
4
+ module Icinga2
5
+
6
+ # namespace for icinga2 statistics
7
+ module Statistics
8
+
9
+
10
+ # return statistic data for latency and execution_time
11
+ #
12
+ # @example
13
+ # @icinga.cib_data
14
+ # latency, execution_time = @icinga.average_statistics.values
15
+ #
16
+ # h = @icinga.average_statistics
17
+ # latency = h.dig(:latency)
18
+ #
19
+ # @return [Hash]
20
+ # * latency (Float)
21
+ # * execution_time (Float)
22
+ #
23
+ def average_statistics
24
+ avg_latency = @avg_latency.nil? ? 0 : @avg_latency
25
+ avg_execution_time = @avg_execution_time.nil? ? 0 : @avg_execution_time
26
+
27
+ {
28
+ latency: avg_latency.to_f,
29
+ execution_time: avg_execution_time.to_f
30
+ }
31
+ end
32
+
33
+ # return statistic data for intervall data
34
+ #
35
+ # @example
36
+ # @icinga.cib_data
37
+ # hosts_active_checks, hosts_passive_checks, services_active_checks, services_passive_checks = @icinga.interval_statistics.values
38
+ #
39
+ # i = @icinga.interval_statistics
40
+ # hosts_active_checks = i.dig(:hosts_active_checks)
41
+ #
42
+ # @return [Hash]
43
+ # * hosts_active_checks (Float)
44
+ # * hosts_passive_checks (Float)
45
+ # * services_active_checks (Float)
46
+ # * services_passive_checks (Float)
47
+ #
48
+ def interval_statistics
49
+
50
+ # take a look into https://github.com/Icinga/pkg-icinga2-debian/blob/master/lib/icinga/cib.cpp
51
+
52
+ hosts_active_checks = @hosts_active_checks_1min.nil? ? 0 : @hosts_active_checks_1min
53
+ hosts_passive_checks = @hosts_passive_checks_1min.nil? ? 0 : @hosts_passive_checks_1min
54
+ services_active_checks = @services_active_checks_1min.nil? ? 0 : @services_active_checks_1min
55
+ services_passive_checks = @services_passive_checks_1min.nil? ? 0 : @services_passive_checks_1min
56
+
57
+ {
58
+ hosts_active_checks: hosts_active_checks.to_f,
59
+ hosts_passive_checks: hosts_passive_checks.to_f,
60
+ services_active_checks: services_active_checks.to_f,
61
+ services_passive_checks: services_passive_checks.to_f
62
+ }
63
+ end
64
+
65
+ # return statistic data for services
66
+ #
67
+ # @example
68
+ # @icinga.cib_data
69
+ # ok, warning, critical, unknown, pending, in_downtime, ack = @icinga.service_statistics.values
70
+ #
71
+ # s = @icinga.service_statistics
72
+ # critical = s.dig(:critical)
73
+ #
74
+ # @return [Hash]
75
+ # * ok (Integer)
76
+ # * warning (Integer)
77
+ # * critical (Integer)
78
+ # * unknown (Integer)
79
+ # * pending (Integer)
80
+ # * in_downtime (Integer)
81
+ # * acknowledged (Integer)
82
+ #
83
+ def service_statistics
84
+
85
+ services_ok = @services_ok.nil? ? 0 : @services_ok
86
+ services_warning = @services_warning.nil? ? 0 : @services_warning
87
+ services_critical = @services_critical.nil? ? 0 : @services_critical
88
+ services_unknown = @services_unknown.nil? ? 0 : @services_unknown
89
+ services_pending = @services_pending.nil? ? 0 : @services_pending
90
+ services_in_downtime = @services_in_downtime.nil? ? 0 : @services_in_downtime
91
+ services_acknowledged = @services_acknowledged.nil? ? 0 : @services_acknowledged
92
+
93
+ {
94
+ ok: services_ok.to_i,
95
+ warning: services_warning.to_i,
96
+ critical: services_critical.to_i,
97
+ unknown: services_unknown.to_i,
98
+ pending: services_pending.to_i,
99
+ in_downtime: services_in_downtime.to_i,
100
+ acknowledged: services_acknowledged.to_i
101
+ }
102
+ end
103
+
104
+ # return statistic data for hosts
105
+ #
106
+ # @example
107
+ # @icinga.cib_data
108
+ # up, down, pending, unreachable, in_downtime, ack = @icinga.host_statistics.values
109
+ #
110
+ # h = @icinga.host_statistics
111
+ # pending = h.dig(:pending)
112
+ #
113
+ # @return [Hash]
114
+ # * up (Integer)
115
+ # * down (Integer)
116
+ # * pending (Integer)
117
+ # * unreachable (Integer)
118
+ # * in_downtime (Integer)
119
+ # * acknowledged (Integer)
120
+ #
121
+ def host_statistics
122
+
123
+ hosts_up = @hosts_up.nil? ? 0 : @hosts_up
124
+ hosts_down = @hosts_down.nil? ? 0 : @hosts_down
125
+ hosts_pending = @hosts_pending.nil? ? 0 : @hosts_pending
126
+ hosts_unreachable = @hosts_unreachable.nil? ? 0 : @hosts_unreachable
127
+ hosts_in_downtime = @hosts_in_downtime.nil? ? 0 : @hosts_in_downtime
128
+ hosts_acknowledged = @hosts_acknowledged.nil? ? 0 : @hosts_acknowledged
129
+
130
+ {
131
+ up: hosts_up.to_i,
132
+ down: hosts_down.to_i,
133
+ pending: hosts_pending.to_i,
134
+ unreachable: hosts_unreachable.to_i,
135
+ in_downtime: hosts_in_downtime.to_i,
136
+ acknowledged: hosts_acknowledged.to_i
137
+ }
138
+ end
139
+
140
+
141
+ # return queue statistics from the api
142
+ #
143
+ # @example
144
+ # @icinga.work_queue_statistics
145
+ #
146
+ # @return [Hash]
147
+ #
148
+ def work_queue_statistics
149
+
150
+ stats = {}
151
+ data = Network.api_data(
152
+ url: format( '%s/status', @icinga_api_url_base ),
153
+ headers: @headers,
154
+ options: @options
155
+ )
156
+
157
+ return stats if data.nil?
158
+
159
+ if( data.dig(:status).nil? )
160
+ results = data.dig('results')
161
+
162
+ json_rpc_data = results.find { |k| k['name'] == 'ApiListener' }
163
+ graphite_data = results.find { |k| k['name'] == 'GraphiteWriter' }
164
+ ido_mysql_data = results.find { |k| k['name'] == 'IdoMysqlConnection' }
165
+
166
+ json_rpc_data = json_rpc_data.dig('status', 'api', 'json_rpc') unless( json_rpc_data.nil? )
167
+ graphite_data = graphite_data.dig('status', 'graphitewriter', 'graphite') unless( graphite_data.nil? )
168
+ ido_mysql_data = ido_mysql_data.dig('status', 'idomysqlconnection', 'ido-mysql') unless( ido_mysql_data.nil? )
169
+
170
+ a = {}
171
+ a['json_rpc'] = json_rpc_data if(json_rpc_data.is_a?(Hash))
172
+ a['graphite'] = graphite_data if(graphite_data.is_a?(Hash))
173
+ a['ido-mysql'] = ido_mysql_data if(ido_mysql_data.is_a?(Hash))
174
+
175
+ key_list = %w[work_queue_item_rate query_queue_item_rate]
176
+
177
+ a.each do |k,v|
178
+ key_list.each do |key|
179
+ if( v.include?( key ))
180
+ attr_name = format('%s queue rate', k)
181
+ stats[attr_name] = v[key].to_f.round(3)
182
+ end
183
+ end
184
+ end
185
+ end
186
+
187
+ stats
188
+ end
189
+
190
+ end
191
+ end
@@ -13,7 +13,7 @@ module Icinga2
13
13
  #
14
14
  # @return [Bool]
15
15
  #
16
- def object_has_been_checked?(object)
16
+ def object_has_been_checked?( object )
17
17
  object.dig('attrs', 'last_check').positive?
18
18
  end
19
19
 
@@ -23,7 +23,7 @@ module Icinga2
23
23
  #
24
24
  # @return [String, String]
25
25
  #
26
- def parse_version(version)
26
+ def parse_version( version )
27
27
 
28
28
  # version = "v2.4.10-504-gab4ba18"
29
29
  # version = "v2.4.10"
@@ -46,43 +46,48 @@ module Icinga2
46
46
  # return count of handled problems
47
47
  #
48
48
  # @param [Hash] objects
49
- # @param [Integer] status
49
+ # @param [Integer] state (nil)
50
50
  #
51
51
  # @example for host objects
52
52
  # h_objects = @icinga.host_objects
53
- # warning = @icinga.handled_problems(h_objects, Icinga2::HOSTS_DOWN)
53
+ # all = @icinga.count_problems(h_objects)
54
+ # down = @icinga.count_problems(h_objects, Icinga2::HOSTS_DOWN)
55
+ # critical = @icinga.count_problems(h_objects, Icinga2::HOSTS_CRITICAL)
56
+ # unknown = @icinga.count_problems(h_objects, Icinga2::HOSTS_UNKNOWN)
54
57
  #
55
58
  # @example for service objects
56
59
  # s_objects = @icinga.service_objects
57
- # warning = @icinga.handled_problems(s_objects, Icinga2::SERVICE_STATE_WARNING)
58
- # critical = @icinga.handled_problems(s_objects, Icinga2::SERVICE_STATE_CRITICAL)
59
- # unknown = @icinga.handled_problems(s_objects, Icinga2::SERVICE_STATE_UNKNOWN)
60
+ # all = @icinga.count_problems(s_objects)
61
+ # warning = @icinga.count_problems(s_objects, Icinga2::SERVICE_STATE_WARNING)
62
+ # critical = @icinga.count_problems(s_objects, Icinga2::SERVICE_STATE_CRITICAL)
63
+ # unknown = @icinga.count_problems(s_objects, Icinga2::SERVICE_STATE_UNKNOWN)
60
64
  #
61
65
  # @return [Integer]
62
66
  #
63
- def handled_problems(objects, status)
67
+ def count_problems( objects, state = nil )
64
68
 
65
- problems = 0
69
+ compare_states = []
66
70
 
67
- objects = JSON.parse(objects) if objects.is_a?(String)
68
- nodes = objects.dig(:nodes)
71
+ unless( state.nil? )
69
72
 
70
- unless( nodes.nil? )
71
-
72
- nodes.each do |n|
73
+ # 0 = "Up" or "OK"
74
+ # 1 = "Down" or "Warning"
75
+ # 2 = "Critical"
76
+ # 3 = "Unknown"
77
+ compare_states = [1, 2, 3]
78
+ end
73
79
 
74
- attrs = n.last.dig('attrs')
75
- state = attrs.dig('state') || 0
76
- downtime_depth = attrs.dig('downtime_depth') || 0
77
- acknowledgement = attrs.dig('acknowledgement') || 0
80
+ compare_states.push(state) if( state.is_a?(Integer) )
78
81
 
79
- if( state == status && downtime_depth.zero? && acknowledgement.zero? )
80
- problems += 1
81
- end
82
+ objects = JSON.parse(objects) if objects.is_a?(String)
82
83
 
83
- end
84
+ f = objects.select do |t|
85
+ t.dig('attrs','state') == state && \
86
+ ( !t.dig('attrs','downtime_depth').nil? && t.dig('attrs','downtime_depth').zero?) && \
87
+ ( !t.dig('attrs','acknowledgement').nil? && t.dig('attrs','acknowledgement').zero? )
84
88
  end
85
- problems
89
+
90
+ f.size
86
91
  end
87
92
 
88
93
  end
@@ -9,25 +9,23 @@ module Icinga2
9
9
  # add a usergroup
10
10
  #
11
11
  # @param [Hash] params
12
- # @option params [String] :name usergroup to create
12
+ # @option params [String] :user_group usergroup to create
13
13
  # @option params [String] :display_name the displayed name
14
14
  #
15
15
  # @example
16
- # @icinga.add_usergroup(name: 'foo', display_name: 'FOO')
16
+ # @icinga.add_usergroup(user_group: 'foo', display_name: 'FOO')
17
17
  #
18
18
  # @return [Hash] result
19
19
  #
20
- def add_usergroup( params = {} )
20
+ def add_usergroup( params )
21
21
 
22
- name = params.dig(:name)
22
+ raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
23
+ raise ArgumentError.new('missing params') if( params.size.zero? )
24
+
25
+ user_group = params.dig(:user_group)
23
26
  display_name = params.dig(:display_name)
24
27
 
25
- if( name.nil? )
26
- return {
27
- status: 404,
28
- message: 'missing usergroup name'
29
- }
30
- end
28
+ raise ArgumentError.new('Missing user_group') if( user_group.nil? )
31
29
 
32
30
  payload = {
33
31
  'attrs' => {
@@ -35,82 +33,94 @@ module Icinga2
35
33
  }
36
34
  }
37
35
 
38
- Network.put( host: name,
39
- url: format( '%s/v1/objects/usergroups/%s', @icinga_api_url_base, name ),
36
+ Network.put(
37
+ url: format( '%s/objects/usergroups/%s', @icinga_api_url_base, user_group ),
40
38
  headers: @headers,
41
39
  options: @options,
42
- payload: payload )
43
-
40
+ payload: payload
41
+ )
44
42
  end
45
43
 
46
44
  # delete a usergroup
47
45
  #
48
46
  # @param [Hash] params
49
- # @option params [String] :name usergroup to delete
47
+ # @option params [String] :user_group usergroup to delete
50
48
  #
51
49
  # @example
52
- # @icinga.delete_usergroup(name: 'foo')
50
+ # @icinga.delete_usergroup(user_group: 'foo')
53
51
  #
54
52
  # @return [Hash] result
55
53
  #
56
- def delete_usergroup( params = {} )
54
+ def delete_usergroup( params )
57
55
 
58
- name = params.dig(:name)
56
+ raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
57
+ raise ArgumentError.new('missing params') if( params.size.zero? )
59
58
 
60
- if( name.nil? )
61
- return {
62
- status: 404,
63
- message: 'missing usergroup name'
64
- }
65
- end
59
+ user_group = params.dig(:user_group)
66
60
 
67
- Network.delete( host: name,
68
- url: format( '%s/v1/objects/usergroups/%s?cascade=1', @icinga_api_url_base, name ),
69
- headers: @headers,
70
- options: @options )
61
+ raise ArgumentError.new('Missing user_group') if( user_group.nil? )
71
62
 
63
+ Network.delete(
64
+ url: format( '%s/objects/usergroups/%s?cascade=1', @icinga_api_url_base, user_group ),
65
+ headers: @headers,
66
+ options: @options
67
+ )
72
68
  end
73
69
 
74
70
  # returns all usersgroups
75
71
  #
76
72
  # @param [Hash] params
77
- # @option params [String] :name ('') optional for a single usergroup
73
+ # @option params [String] :user_group ('') optional for a single usergroup
78
74
  #
79
75
  # @example to get all users
80
76
  # @icinga.usergroups
81
77
  #
82
78
  # @example to get one user
83
- # @icinga.usergroups(name: 'icingaadmins')
79
+ # @icinga.usergroups(user_group: 'icingaadmins')
84
80
  #
85
81
  # @return [Hash] returns a hash with all usergroups
86
82
  #
87
83
  def usergroups( params = {} )
88
84
 
89
- name = params.dig(:name)
85
+ user_group = params.dig(:user_group)
86
+
87
+ url =
88
+ if( user_group.nil? )
89
+ format( '%s/objects/usergroups' , @icinga_api_url_base )
90
+ else
91
+ format( '%s/objects/usergroups/%s', @icinga_api_url_base, user_group )
92
+ end
90
93
 
91
- Network.get( host: name,
92
- url: format( '%s/v1/objects/usergroups/%s', @icinga_api_url_base, name ),
94
+ data = Network.api_data(
95
+ url: url,
93
96
  headers: @headers,
94
- options: @options )
97
+ options: @options
98
+ )
95
99
 
100
+ return data.dig('results') if( data.dig(:status).nil? )
101
+
102
+ nil
96
103
  end
97
104
 
98
105
  # returns true if the usergroup exists
99
106
  #
100
- # @param [String] name the name of the usergroups
107
+ # @param [String] user_group the name of the usergroups
101
108
  #
102
109
  # @example
103
110
  # @icinga.exists_usergroup?('icingaadmins')
104
111
  #
105
112
  # @return [Bool] returns true if the usergroup exists
106
113
  #
107
- def exists_usergroup?( name )
114
+ def exists_usergroup?( user_group )
115
+
116
+ raise ArgumentError.new('only String are allowed') unless( user_group.is_a?(String) )
117
+ raise ArgumentError.new('Missing user_group') if( user_group.size.zero? )
108
118
 
109
- result = usergroups( name: name )
119
+ result = usergroups( user_group: user_group )
110
120
  result = JSON.parse( result ) if result.is_a?( String )
111
- status = result.dig(:status)
112
121
 
113
- return true if !status.nil? && status == 200
122
+ return true if !result.nil? && result.is_a?(Array)
123
+
114
124
  false
115
125
  end
116
126
 
@@ -9,41 +9,32 @@ module Icinga2
9
9
  # add a user
10
10
  #
11
11
  # @param [Hash] params
12
- # @option params [String] :name user to create
13
- # @option params [String] :display_name the displayed name
12
+ # @option params [String] :user_name ('') user to create
13
+ # @option params [String] :display_name ('') the displayed name
14
14
  # @option params [String] :email ('') the user email
15
15
  # @option params [String] :pager ('') optional a pager
16
16
  # @option params [Bool] :enable_notifications (false) enable notifications for this user
17
17
  # @option params [Array] :groups ([]) a hash with groups
18
18
  #
19
19
  # @example
20
- # @icinga.add_user(name: 'foo', display_name: 'FOO', email: 'foo@bar.com', pager: '0000', groups: ['icingaadmins'])
20
+ # @icinga.add_user(user_name: 'foo', display_name: 'FOO', email: 'foo@bar.com', pager: '0000', groups: ['icingaadmins'])
21
21
  #
22
22
  # @return [Hash] result
23
23
  #
24
- def add_user( params = {} )
24
+ def add_user( params )
25
25
 
26
- name = params.dig(:name)
26
+ raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
27
+ raise ArgumentError.new('missing params') if( params.size.zero? )
28
+
29
+ user_name = params.dig(:user_name)
27
30
  display_name = params.dig(:display_name)
28
31
  email = params.dig(:email)
29
32
  pager = params.dig(:pager)
30
33
  notifications = params.dig(:enable_notifications) || false
31
34
  groups = params.dig(:groups) || []
32
35
 
33
- if( name.nil? )
34
- return {
35
- status: 404,
36
- message: 'missing user name'
37
- }
38
- end
39
-
40
- unless( groups.is_a?( Array ) )
41
- return {
42
- status: 404,
43
- message: 'groups must be an array',
44
- data: params
45
- }
46
- end
36
+ raise ArgumentError.new('Missing user_name') if( user_name.nil? )
37
+ raise ArgumentError.new('groups must be an array') unless( groups.is_a?( Array ) )
47
38
 
48
39
  payload = {
49
40
  'attrs' => {
@@ -73,90 +64,94 @@ module Icinga2
73
64
  }
74
65
  end
75
66
 
76
- Network.put( host: name,
77
- url: format( '%s/v1/objects/users/%s', @icinga_api_url_base, name ),
67
+ Network.put(
68
+ url: format( '%s/objects/users/%s', @icinga_api_url_base, user_name ),
78
69
  headers: @headers,
79
70
  options: @options,
80
- payload: payload )
81
-
82
- # result:
83
- # {:status=>200, :name=>nil, :message=>"Object was created"}
71
+ payload: payload
72
+ )
84
73
  end
85
74
 
86
75
  # delete a user
87
76
  #
88
77
  # @param [Hash] params
89
- # @option params [String] :name user to delete
78
+ # @option params [String] :user_name user to delete
90
79
  #
91
80
  # @example
92
- # @icinga.delete_user(name: 'foo')
81
+ # @icinga.delete_user(user_name: 'foo')
93
82
  #
94
83
  # @return [Hash] result
95
84
  #
96
- def delete_user( params = {} )
85
+ def delete_user( params )
97
86
 
98
- name = params.dig(:name)
87
+ raise ArgumentError.new('only Hash are allowed') unless( params.is_a?(Hash) )
88
+ raise ArgumentError.new('missing params') if( params.size.zero? )
99
89
 
100
- if( name.nil? )
101
- return {
102
- status: 404,
103
- message: 'missing user name'
104
- }
105
- end
90
+ user_name = params.dig(:user_name)
106
91
 
107
- Network.delete( host: name,
108
- url: format( '%s/v1/objects/users/%s?cascade=1', @icinga_api_url_base, name ),
109
- headers: @headers,
110
- options: @options )
92
+ raise ArgumentError.new('Missing user_name') if( user_name.nil? )
111
93
 
112
- # result:
113
- # {:status=>200, :name=>"foo", :message=>"Object was deleted."}
94
+ Network.delete(
95
+ url: format( '%s/objects/users/%s?cascade=1', @icinga_api_url_base, user_name ),
96
+ headers: @headers,
97
+ options: @options
98
+ )
114
99
  end
115
100
 
116
- # returns all users
101
+ # returns a named or all users
117
102
  #
118
103
  # @param [Hash] params
119
- # @option params [String] :name ('') optional for a single user
104
+ # @option params [String] :user_name ('') optional for a single user
120
105
  #
121
106
  # @example to get all users
122
107
  # @icinga.users
123
108
  #
124
109
  # @example to get one user
125
- # @icinga.users(name: 'icingaadmin')
110
+ # @icinga.users(user_name: 'icingaadmin')
126
111
  #
127
- # @return [Hash] returns a hash with all users
112
+ # @return [Hash] returns a hash
128
113
  #
129
114
  def users( params = {} )
130
115
 
131
- name = params.dig(:name)
116
+ user_name = params.dig(:user_name)
132
117
 
133
- Network.get( host: name,
134
- url: format( '%s/v1/objects/users/%s', @icinga_api_url_base, name ),
118
+ url =
119
+ if( user_name.nil? )
120
+ format( '%s/objects/users' , @icinga_api_url_base )
121
+ else
122
+ format( '%s/objects/users/%s', @icinga_api_url_base, user_name )
123
+ end
124
+
125
+ data = Network.api_data(
126
+ url: url,
135
127
  headers: @headers,
136
- options: @options )
128
+ options: @options
129
+ )
130
+
131
+ return data.dig('results') if( data.dig(:status).nil? )
137
132
 
138
- # result:
139
- # - named user:
140
- # {:status=>200, :data=>{"icingaadmin"=>{:name=>"icingaadmin", :display_name=>"Icinga 2 Admin", :type=>"User"}}}
141
- # - all users:
142
- # {:status=>200, :data=>{"icingaadmin"=>{:name=>"icingaadmin", :display_name=>"Icinga 2 Admin", :type=>"User"}, "foo"=>{:name=>"foo", :display_name=>"FOO", :type=>"User"}}}
133
+ nil
143
134
  end
144
135
 
145
- # returns true if the user exists
136
+ # checks if the user exists
146
137
  #
147
- # @param [String] name the name of the user
138
+ # @param [String] user_name the name of the user
148
139
  #
149
140
  # @example
150
141
  # @icinga.exists_user?('icingaadmin')
151
142
  #
152
143
  # @return [Bool] returns true if the user exists
153
- def exists_user?( name )
144
+ #
145
+ def exists_user?( user_name )
146
+
147
+ raise ArgumentError.new('only String are allowed') unless( user_name.is_a?(String) )
148
+ raise ArgumentError.new('Missing user_name') if( user_name.size.zero? )
149
+
150
+ result = users( user_name: user_name )
151
+ result = JSON.parse( result ) if( result.is_a?(String) )
154
152
 
155
- result = users( name: name )
156
- result = JSON.parse( result ) if result.is_a?( String )
157
- status = result.dig(:status)
153
+ return true if( !result.nil? && result.is_a?(Array) )
158
154
 
159
- return true if !status.nil? && status == 200
160
155
  false
161
156
  end
162
157