iteh-zabbixapi 0.2.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.
- data/.gitignore +5 -0
- data/Gemfile +4 -0
- data/LICENSE +13 -0
- data/README.rdoc +41 -0
- data/Rakefile +8 -0
- data/examples/config.yml +4 -0
- data/examples/zabbix_availability +73 -0
- data/examples/zabbix_disk_io +152 -0
- data/examples/zabbix_filesystem +249 -0
- data/examples/zabbix_la +101 -0
- data/examples/zabbix_memory +276 -0
- data/examples/zabbix_net +82 -0
- data/lib/zabbixapi/application.rb +50 -0
- data/lib/zabbixapi/base.rb +139 -0
- data/lib/zabbixapi/graph.rb +66 -0
- data/lib/zabbixapi/group.rb +78 -0
- data/lib/zabbixapi/host.rb +85 -0
- data/lib/zabbixapi/item.rb +145 -0
- data/lib/zabbixapi/screen.rb +166 -0
- data/lib/zabbixapi/template.rb +158 -0
- data/lib/zabbixapi/trigger.rb +95 -0
- data/lib/zabbixapi/usermacro.rb +74 -0
- data/lib/zabbixapi/version.rb +20 -0
- data/lib/zabbixapi.rb +9 -0
- data/spec/fixtures/failed_auth_wrong_password_user.authenticate.txt +9 -0
- data/spec/fixtures/get_existing_host_host.get.txt +9 -0
- data/spec/fixtures/get_nil_for_unknown_host_host.get.txt +9 -0
- data/spec/fixtures/login_success_user.authenticate.txt +10 -0
- data/spec/record_http.rb +90 -0
- data/spec/spec_helper.rb +41 -0
- data/spec/zabbixapi_spec.rb +97 -0
- data/zabbixapi.gemspec +39 -0
- metadata +178 -0
@@ -0,0 +1,166 @@
|
|
1
|
+
module Zabbix
|
2
|
+
|
3
|
+
class ZabbixApi
|
4
|
+
def get_screen_id(screen_name)
|
5
|
+
|
6
|
+
message = {
|
7
|
+
'method' => 'screen.get',
|
8
|
+
'params' => {
|
9
|
+
'filter' => {
|
10
|
+
'name' => screen_name
|
11
|
+
}
|
12
|
+
}
|
13
|
+
}
|
14
|
+
|
15
|
+
response = send_request(message)
|
16
|
+
|
17
|
+
unless response.empty? then
|
18
|
+
result = response[0]['screenid']
|
19
|
+
else
|
20
|
+
result = nil
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def get_screen_parameter(screen_name, param_name)
|
25
|
+
|
26
|
+
message = {
|
27
|
+
'method' => 'screen.get',
|
28
|
+
'params' => {
|
29
|
+
'extendoutput' => '1',
|
30
|
+
'filter' => {
|
31
|
+
'name' => screen_name
|
32
|
+
}
|
33
|
+
}
|
34
|
+
}
|
35
|
+
|
36
|
+
response = send_request(message)
|
37
|
+
|
38
|
+
unless response.empty? then
|
39
|
+
result = response[0][param_name]
|
40
|
+
else
|
41
|
+
result nil
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def get_screen_graph_ids(screen_id)
|
46
|
+
|
47
|
+
message = {
|
48
|
+
'method' => 'screen.get',
|
49
|
+
'params' => {
|
50
|
+
'extendoutput' => '1',
|
51
|
+
'select_screenitems' => '1',
|
52
|
+
'screenids' => [ screen_id ]
|
53
|
+
}
|
54
|
+
}
|
55
|
+
|
56
|
+
response = send_request(message)
|
57
|
+
|
58
|
+
unless ( response.empty?) then
|
59
|
+
result = []
|
60
|
+
screenitems = response[0]['screenitems']
|
61
|
+
screenitems.each() do |item|
|
62
|
+
if ( item['resourcetype'].to_i == 0 ) then
|
63
|
+
result << item['resourceid']
|
64
|
+
end
|
65
|
+
end
|
66
|
+
else
|
67
|
+
result = nil
|
68
|
+
end
|
69
|
+
|
70
|
+
return result
|
71
|
+
end
|
72
|
+
|
73
|
+
def set_screen_parameter(screen_id, param_name, param_value)
|
74
|
+
|
75
|
+
message = {
|
76
|
+
'method' => 'screen.update',
|
77
|
+
'params' => {
|
78
|
+
param_name => param_value,
|
79
|
+
'screenid' => screen_id
|
80
|
+
}
|
81
|
+
}
|
82
|
+
|
83
|
+
response = send_request(message)
|
84
|
+
|
85
|
+
if not ( response.empty? ) then
|
86
|
+
result = true
|
87
|
+
else
|
88
|
+
result = false
|
89
|
+
end
|
90
|
+
|
91
|
+
return result
|
92
|
+
end
|
93
|
+
|
94
|
+
def del_all_graphs_from_screen(screen_id)
|
95
|
+
|
96
|
+
message = {
|
97
|
+
'method' => 'screen.deleteItems',
|
98
|
+
'params' => {
|
99
|
+
'screenids' => [ screen_id ],
|
100
|
+
}
|
101
|
+
}
|
102
|
+
|
103
|
+
response = send_request(message)
|
104
|
+
|
105
|
+
if ( response ) then
|
106
|
+
return response
|
107
|
+
else
|
108
|
+
return nil
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
def add_graph_to_screen(screen_id, graph_id, x, y)
|
113
|
+
|
114
|
+
message = {
|
115
|
+
'method' => 'screen.addItems',
|
116
|
+
'params' => {
|
117
|
+
'screenids' => [ screen_id ],
|
118
|
+
'screenitems' => [
|
119
|
+
{
|
120
|
+
'resourcetype' => 'graph',
|
121
|
+
'resourceid' => graph_id,
|
122
|
+
'width' => '800',
|
123
|
+
'height' => '200',
|
124
|
+
'x' => x,
|
125
|
+
'y' => y,
|
126
|
+
'valign' => 'Middle',
|
127
|
+
'halign' => 'Centre',
|
128
|
+
'colspan' => '0',
|
129
|
+
'rowspan' => '0',
|
130
|
+
'elements' => '0',
|
131
|
+
'dynamic' => '0',
|
132
|
+
'url' => '0',
|
133
|
+
'style' => '0'
|
134
|
+
}
|
135
|
+
]
|
136
|
+
}
|
137
|
+
}
|
138
|
+
|
139
|
+
response = send_request(message)
|
140
|
+
|
141
|
+
return response
|
142
|
+
end
|
143
|
+
|
144
|
+
def add_screen(screen_name, hsize, vsize)
|
145
|
+
|
146
|
+
message = {
|
147
|
+
'method' => 'screen.create',
|
148
|
+
'params' => {
|
149
|
+
'name' => screen_name,
|
150
|
+
'hsize' => hsize,
|
151
|
+
'vsize' => vsize
|
152
|
+
}
|
153
|
+
}
|
154
|
+
|
155
|
+
response = send_request(message)
|
156
|
+
|
157
|
+
unless response.empty? then
|
158
|
+
result = response['screenids'][0]
|
159
|
+
else
|
160
|
+
result = nil
|
161
|
+
end
|
162
|
+
|
163
|
+
return result
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
@@ -0,0 +1,158 @@
|
|
1
|
+
module Zabbix
|
2
|
+
|
3
|
+
class ZabbixApi
|
4
|
+
def add_template(template_options)
|
5
|
+
|
6
|
+
template_default = {
|
7
|
+
'host' => nil,
|
8
|
+
'groups' => [],
|
9
|
+
}
|
10
|
+
|
11
|
+
template_options['groups'].map! { |group_id| {'groupid' => group_id} }
|
12
|
+
|
13
|
+
template = merge_opt(template_default, template_options)
|
14
|
+
|
15
|
+
message = {
|
16
|
+
'method' => 'template.create',
|
17
|
+
'params' => template
|
18
|
+
}
|
19
|
+
|
20
|
+
response = send_request(message)
|
21
|
+
|
22
|
+
if not ( response.empty? ) then
|
23
|
+
result = response['templateids'][0].to_i
|
24
|
+
else
|
25
|
+
result = nil
|
26
|
+
end
|
27
|
+
|
28
|
+
return result
|
29
|
+
end
|
30
|
+
|
31
|
+
def get_template_ids_by_host(host_id)
|
32
|
+
|
33
|
+
message = {
|
34
|
+
'method' => 'template.get',
|
35
|
+
'params' => {
|
36
|
+
'hostids' => [ host_id ]
|
37
|
+
}
|
38
|
+
}
|
39
|
+
|
40
|
+
response = send_request(message)
|
41
|
+
|
42
|
+
unless ( response.empty? ) then
|
43
|
+
result = []
|
44
|
+
response.each_key() do |template_id|
|
45
|
+
result << template_id
|
46
|
+
end
|
47
|
+
else
|
48
|
+
result = nil
|
49
|
+
end
|
50
|
+
|
51
|
+
return result
|
52
|
+
end
|
53
|
+
|
54
|
+
def get_templates()
|
55
|
+
|
56
|
+
message = {
|
57
|
+
'method' => 'template.get',
|
58
|
+
'params' => {
|
59
|
+
'extendoutput' => '0'
|
60
|
+
}
|
61
|
+
}
|
62
|
+
|
63
|
+
response = send_request(message)
|
64
|
+
|
65
|
+
unless response.empty? then
|
66
|
+
template_ids = response.keys()
|
67
|
+
result = {}
|
68
|
+
|
69
|
+
template_ids.each() do |template_id|
|
70
|
+
template_name = response[template_id]['host']
|
71
|
+
result[template_id] = template_name
|
72
|
+
end
|
73
|
+
else
|
74
|
+
result = nil
|
75
|
+
end
|
76
|
+
|
77
|
+
return result
|
78
|
+
end
|
79
|
+
|
80
|
+
def get_template_id(template_name)
|
81
|
+
|
82
|
+
message = {
|
83
|
+
'method' => 'template.get',
|
84
|
+
'params' => {
|
85
|
+
'filter' => {
|
86
|
+
'host' => template_name
|
87
|
+
}
|
88
|
+
}
|
89
|
+
}
|
90
|
+
|
91
|
+
response = send_request(message)
|
92
|
+
|
93
|
+
unless response.empty? then
|
94
|
+
result = response.keys[0]
|
95
|
+
else
|
96
|
+
result = nil
|
97
|
+
end
|
98
|
+
|
99
|
+
return result
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
def link_templates_with_hosts(templates_id, hosts_id)
|
104
|
+
|
105
|
+
if templates_id.class == Array then
|
106
|
+
message_templates_id = templates_id
|
107
|
+
else
|
108
|
+
message_templates_id = [ templates_id ]
|
109
|
+
end
|
110
|
+
|
111
|
+
if hosts_id == Array then
|
112
|
+
message_hosts_id = hosts_id
|
113
|
+
else
|
114
|
+
message_hosts_id = [ hosts_id ]
|
115
|
+
end
|
116
|
+
|
117
|
+
message = {
|
118
|
+
'method' => 'template.massAdd',
|
119
|
+
'params' => {
|
120
|
+
'hosts' => message_hosts_id,
|
121
|
+
'templates' => message_templates_id
|
122
|
+
}
|
123
|
+
}
|
124
|
+
|
125
|
+
response = send_request(message)
|
126
|
+
|
127
|
+
return response
|
128
|
+
end
|
129
|
+
|
130
|
+
def unlink_templates_from_hosts(templates_id, hosts_id)
|
131
|
+
|
132
|
+
if templates_id.class == Array then
|
133
|
+
message_templates_id = templates_id
|
134
|
+
else
|
135
|
+
message_templates_id = [ templates_id ]
|
136
|
+
end
|
137
|
+
|
138
|
+
if hosts_id == Array then
|
139
|
+
message_hosts_id = hosts_id
|
140
|
+
else
|
141
|
+
message_hosts_id = [ hosts_id ]
|
142
|
+
end
|
143
|
+
|
144
|
+
message = {
|
145
|
+
'method' => 'template.massRemove',
|
146
|
+
'params' => {
|
147
|
+
'hosts' => message_hosts_id,
|
148
|
+
'templates' => message_templates_id,
|
149
|
+
'force' => '1'
|
150
|
+
}
|
151
|
+
}
|
152
|
+
|
153
|
+
response = send_request(message)
|
154
|
+
|
155
|
+
return response
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
module Zabbix
|
2
|
+
|
3
|
+
class ZabbixApi
|
4
|
+
def add_trigger(trigger)
|
5
|
+
|
6
|
+
message = {
|
7
|
+
'method' => 'trigger.create',
|
8
|
+
'params' => [ trigger ]
|
9
|
+
}
|
10
|
+
|
11
|
+
response = send_request(message)
|
12
|
+
|
13
|
+
unless response.empty? then
|
14
|
+
result = response['triggerids'][0]
|
15
|
+
else
|
16
|
+
result = nil
|
17
|
+
end
|
18
|
+
|
19
|
+
return result
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
def get_trigger_id(host_id, trigger_name)
|
24
|
+
|
25
|
+
message = {
|
26
|
+
'method' => 'trigger.get',
|
27
|
+
'params' => {
|
28
|
+
'filter' => {
|
29
|
+
'hostid' => host_id,
|
30
|
+
'description' => trigger_name
|
31
|
+
}
|
32
|
+
}
|
33
|
+
}
|
34
|
+
|
35
|
+
response = send_request(message)
|
36
|
+
|
37
|
+
unless response.empty? then
|
38
|
+
result = response[0]['triggerid']
|
39
|
+
else
|
40
|
+
result = nil
|
41
|
+
end
|
42
|
+
|
43
|
+
return result
|
44
|
+
end
|
45
|
+
|
46
|
+
def get_triggers_by_host(host_id)
|
47
|
+
|
48
|
+
message = {
|
49
|
+
'method' => 'trigger.get',
|
50
|
+
'params' => {
|
51
|
+
'filter' => {
|
52
|
+
'hostid' => host_id,
|
53
|
+
},
|
54
|
+
'extendoutput' => '1'
|
55
|
+
}
|
56
|
+
}
|
57
|
+
|
58
|
+
response = send_request(message)
|
59
|
+
|
60
|
+
unless response.empty? then
|
61
|
+
result = {}
|
62
|
+
response.each do |trigger|
|
63
|
+
trigger_id = trigger['triggerid']
|
64
|
+
description = trigger['description']
|
65
|
+
result[trigger_id] = description
|
66
|
+
end
|
67
|
+
else
|
68
|
+
result = {}
|
69
|
+
end
|
70
|
+
|
71
|
+
return result
|
72
|
+
end
|
73
|
+
|
74
|
+
def update_trigger_status(trigger_id, status)
|
75
|
+
|
76
|
+
message = {
|
77
|
+
'method' => 'trigger.update_status',
|
78
|
+
'params' => {
|
79
|
+
'triggerid' => trigger_id,
|
80
|
+
'status' => status
|
81
|
+
}
|
82
|
+
}
|
83
|
+
|
84
|
+
response = send_request(message)
|
85
|
+
|
86
|
+
unless response.empty? then
|
87
|
+
result = response['triggerids'][0]
|
88
|
+
else
|
89
|
+
result = nil
|
90
|
+
end
|
91
|
+
|
92
|
+
return result
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
module Zabbix
|
2
|
+
|
3
|
+
class ZabbixApi
|
4
|
+
def add_macro(host_id, macro_name, macro_value)
|
5
|
+
|
6
|
+
message = {
|
7
|
+
'method' => 'Usermacro.create',
|
8
|
+
'params' => {
|
9
|
+
'hostid' => host_id,
|
10
|
+
'macro' => macro_name,
|
11
|
+
'value'=> macro_value
|
12
|
+
}
|
13
|
+
}
|
14
|
+
|
15
|
+
response = send_request(message)
|
16
|
+
|
17
|
+
if hostmacroids = response['hostmacroids'] then
|
18
|
+
result = hostmacroids
|
19
|
+
else
|
20
|
+
result = nil
|
21
|
+
end
|
22
|
+
|
23
|
+
return result
|
24
|
+
end
|
25
|
+
|
26
|
+
def get_macro(host_id, macro_name)
|
27
|
+
|
28
|
+
message = {
|
29
|
+
'method' => 'Usermacro.get',
|
30
|
+
'params' => {
|
31
|
+
'hostids' => host_id,
|
32
|
+
'macros' => macro_name,
|
33
|
+
'extendoutput' => '1'
|
34
|
+
}
|
35
|
+
}
|
36
|
+
|
37
|
+
response = send_request(message)
|
38
|
+
|
39
|
+
unless response.empty? then
|
40
|
+
if hostmacroid = response[0]['hostmacroid'] then
|
41
|
+
macro_id = hostmacroid
|
42
|
+
macro_value = response[0]['value']
|
43
|
+
|
44
|
+
result = {
|
45
|
+
'id' => macro_id,
|
46
|
+
'value'=> macro_value
|
47
|
+
}
|
48
|
+
else
|
49
|
+
result = nil
|
50
|
+
end
|
51
|
+
else
|
52
|
+
result = nil
|
53
|
+
end
|
54
|
+
|
55
|
+
return result
|
56
|
+
end
|
57
|
+
|
58
|
+
def set_macro_value(host_id, macro_name, macro_value)
|
59
|
+
|
60
|
+
message = {
|
61
|
+
'method' => 'usermacro.updateValue',
|
62
|
+
'params' => {
|
63
|
+
'hostid' => host_id,
|
64
|
+
'macro' => macro_name,
|
65
|
+
'value' => macro_value
|
66
|
+
}
|
67
|
+
}
|
68
|
+
|
69
|
+
response = send_request(message)
|
70
|
+
|
71
|
+
return true
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|