zabbixapi 0.1.6 → 0.1.6.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +20 -6
- data/examples/zabbix_filesystem +1 -1
- data/lib/zabbixapi/application.rb +14 -31
- data/lib/zabbixapi/base.rb +24 -25
- data/lib/zabbixapi/graph.rb +19 -33
- data/lib/zabbixapi/group.rb +31 -51
- data/lib/zabbixapi/host.rb +43 -61
- data/lib/zabbixapi/item.rb +56 -97
- data/lib/zabbixapi/screen.rb +65 -112
- data/lib/zabbixapi/template.rb +42 -98
- data/lib/zabbixapi/trigger.rb +28 -59
- data/lib/zabbixapi/usermacro.rb +27 -42
- data/spec/item.rb +38 -42
- data/zabbixapi.gemspec +1 -1
- metadata +2 -2
data/lib/zabbixapi/item.rb
CHANGED
@@ -1,10 +1,9 @@
|
|
1
1
|
module Zabbix
|
2
2
|
class ZabbixApi
|
3
|
+
|
3
4
|
def add_item(item)
|
4
|
-
|
5
5
|
# Default item options
|
6
6
|
# See: http://www.zabbix.com/documentation/1.8/api/item
|
7
|
-
|
8
7
|
## Item types (see ./frontends/php/include/defines.inc.php in zabbix source)
|
9
8
|
# ITEM_TYPE_ZABBIX 0
|
10
9
|
# ITEM_TYPE_SNMPV1 1
|
@@ -22,85 +21,63 @@ module Zabbix
|
|
22
21
|
# ITEM_TYPE_SSH 13
|
23
22
|
# ITEM_TYPE_TELNET 14
|
24
23
|
# ITEM_TYPE_CALCULATED 15
|
25
|
-
|
26
24
|
item_options = {
|
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
|
-
|
25
|
+
'description' => nil,
|
26
|
+
'key_' => nil,
|
27
|
+
'hostid' => nil,
|
28
|
+
'delay' => 60,
|
29
|
+
'history' => 60,
|
30
|
+
'status' => 0,
|
31
|
+
'type' => 7,
|
32
|
+
'snmp_community' => '',
|
33
|
+
'snmp_oid' => '',
|
34
|
+
'value_type' => 3,
|
35
|
+
'data_type' => 0,
|
36
|
+
'trapper_hosts' => 'localhost',
|
37
|
+
'snmp_port' => 161,
|
38
|
+
'units' => '',
|
39
|
+
'multiplier' => 0,
|
40
|
+
'delta' => 0,
|
41
|
+
'snmpv3_securityname' => '',
|
42
|
+
'snmpv3_securitylevel' => 0,
|
43
|
+
'snmpv3_authpassphrase' => '',
|
44
|
+
'snmpv3_privpassphrase' => '',
|
45
|
+
'formula' => 0,
|
46
|
+
'trends' => 365,
|
47
|
+
'logtimefmt' => '',
|
48
|
+
'valuemapid' => 0,
|
49
|
+
'delay_flex' => '',
|
50
|
+
'authtype' => 0,
|
51
|
+
'username' => '',
|
52
|
+
'password' => '',
|
53
|
+
'publickey' => '',
|
54
|
+
'privatekey' => '',
|
55
|
+
'params' => '',
|
56
|
+
'ipmi_sensor' => '',
|
57
|
+
'applications' => '',
|
58
|
+
'templateid' => 0
|
61
59
|
}
|
62
|
-
|
63
|
-
|
64
60
|
item_options.merge!(item)
|
65
|
-
|
66
61
|
message = {
|
67
|
-
|
68
|
-
|
62
|
+
'method' => 'item.create',
|
63
|
+
'params' => [item_options]
|
69
64
|
}
|
70
|
-
|
71
65
|
response = send_request(message)
|
72
|
-
|
73
|
-
unless response.empty? then
|
74
|
-
result = response['itemids'][0]
|
75
|
-
else
|
76
|
-
result = nil
|
77
|
-
end
|
78
|
-
|
79
|
-
return result
|
80
|
-
|
66
|
+
response.empty? ? nil : response['itemids'][0]
|
81
67
|
end
|
82
68
|
|
83
69
|
def get_item_id(host_id, item_name)
|
84
70
|
message = {
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
71
|
+
'method' => 'item.get',
|
72
|
+
'params' => {
|
73
|
+
'filter' => {
|
74
|
+
'hostid' => host_id,
|
75
|
+
'description' => item_name
|
76
|
+
}
|
90
77
|
}
|
91
|
-
}
|
92
78
|
}
|
93
|
-
|
94
79
|
response = send_request(message)
|
95
|
-
|
96
|
-
unless response.empty?
|
97
|
-
result = response[0]['itemid']
|
98
|
-
else
|
99
|
-
result = nil
|
100
|
-
end
|
101
|
-
|
102
|
-
return result
|
103
|
-
|
80
|
+
response.empty? ? nil : response[0]['itemid']
|
104
81
|
end
|
105
82
|
|
106
83
|
def item_exist?(host_id, item_name)
|
@@ -110,60 +87,42 @@ module Zabbix
|
|
110
87
|
else
|
111
88
|
result = false
|
112
89
|
end
|
113
|
-
|
114
90
|
return result
|
115
91
|
end
|
116
92
|
|
117
93
|
def update_item(item_id, options)
|
118
|
-
|
119
94
|
options["item_id"]
|
120
|
-
|
121
95
|
message = {
|
122
|
-
|
123
|
-
|
96
|
+
'method' => 'item.update',
|
97
|
+
'params' => options
|
124
98
|
}
|
125
|
-
|
126
99
|
response = send_request(message)
|
127
|
-
|
128
|
-
unless response.empty?
|
129
|
-
result = response['itemids'][0]
|
130
|
-
else
|
131
|
-
result = nil
|
132
|
-
end
|
133
|
-
|
134
|
-
return result
|
100
|
+
response.empty? ? nil : response['itemids'][0]
|
135
101
|
end
|
136
102
|
|
137
103
|
# Don't work with api < 1.8.4
|
138
104
|
def delete_item(item_ids)
|
139
|
-
|
140
105
|
if item_ids.kind_of? Array
|
141
106
|
message = {
|
142
|
-
|
143
|
-
|
107
|
+
'method' => 'item.delete',
|
108
|
+
'params' => item_ids
|
144
109
|
}
|
145
110
|
elsif item_ids.kind_of? Fixnum or item_ids.kind_of? String
|
146
111
|
message = {
|
147
|
-
|
148
|
-
|
112
|
+
'method' => 'item.delete',
|
113
|
+
'params' => [item_ids]
|
149
114
|
}
|
150
115
|
else
|
151
116
|
raise Zabbix::ArgumentError.new("Zabbix::ZabbixApi.delete_item() argument error. item_ids => #{item_ids.inspect}")
|
152
117
|
end
|
153
|
-
|
154
118
|
response = send_request(message)
|
155
|
-
|
156
|
-
unless response.empty?
|
157
|
-
if response['itemids'].count == 1
|
158
|
-
result = response['itemids'][0]
|
159
|
-
else
|
160
|
-
result = response['itemids']
|
161
|
-
end
|
162
|
-
else
|
119
|
+
if response.empty?
|
163
120
|
result = nil
|
121
|
+
else
|
122
|
+
response['itemids'].count == 1 ? result = response['itemids'][0] : result = response['itemids']
|
164
123
|
end
|
165
|
-
|
166
124
|
return result
|
167
125
|
end
|
126
|
+
|
168
127
|
end
|
169
128
|
end
|
data/lib/zabbixapi/screen.rb
CHANGED
@@ -1,166 +1,119 @@
|
|
1
1
|
module Zabbix
|
2
|
-
|
3
2
|
class ZabbixApi
|
4
|
-
def get_screen_id(screen_name)
|
5
3
|
|
4
|
+
def get_screen_id(screen_name)
|
6
5
|
message = {
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
6
|
+
'method' => 'screen.get',
|
7
|
+
'params' => {
|
8
|
+
'filter' => {
|
9
|
+
'name' => screen_name
|
10
|
+
}
|
11
11
|
}
|
12
|
-
}
|
13
12
|
}
|
14
|
-
|
15
13
|
response = send_request(message)
|
16
|
-
|
17
|
-
unless response.empty? then
|
18
|
-
result = response[0]['screenid']
|
19
|
-
else
|
20
|
-
result = nil
|
21
|
-
end
|
14
|
+
response.empty? ? nil : response[0]['screenid']
|
22
15
|
end
|
23
16
|
|
24
17
|
def get_screen_parameter(screen_name, param_name)
|
25
|
-
|
26
18
|
message = {
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
19
|
+
'method' => 'screen.get',
|
20
|
+
'params' => {
|
21
|
+
'extendoutput' => '1',
|
22
|
+
'filter' => {
|
23
|
+
'name' => screen_name
|
24
|
+
}
|
32
25
|
}
|
33
|
-
}
|
34
26
|
}
|
35
|
-
|
36
27
|
response = send_request(message)
|
37
|
-
|
38
|
-
unless response.empty? then
|
39
|
-
result = response[0][param_name]
|
40
|
-
else
|
41
|
-
result nil
|
42
|
-
end
|
28
|
+
response.empty? ? nil : response[0][param_name]
|
43
29
|
end
|
44
30
|
|
45
31
|
def get_screen_graph_ids(screen_id)
|
46
|
-
|
47
32
|
message = {
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
33
|
+
'method' => 'screen.get',
|
34
|
+
'params' => {
|
35
|
+
'extendoutput' => '1',
|
36
|
+
'select_screenitems' => '1',
|
37
|
+
'screenids' => [screen_id]
|
38
|
+
}
|
54
39
|
}
|
55
|
-
|
56
40
|
response = send_request(message)
|
57
|
-
|
58
|
-
|
41
|
+
if response.empty?
|
42
|
+
result = nil
|
43
|
+
else
|
59
44
|
result = []
|
60
45
|
screenitems = response[0]['screenitems']
|
61
46
|
screenitems.each() do |item|
|
62
|
-
|
63
|
-
result << item['resourceid']
|
64
|
-
end
|
47
|
+
result << item['resourceid'] if item['resourcetype'].to_i == 0
|
65
48
|
end
|
66
|
-
else
|
67
|
-
result = nil
|
68
49
|
end
|
69
|
-
|
70
50
|
return result
|
71
51
|
end
|
72
52
|
|
73
53
|
def set_screen_parameter(screen_id, param_name, param_value)
|
74
|
-
|
75
54
|
message = {
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
55
|
+
'method' => 'screen.update',
|
56
|
+
'params' => {
|
57
|
+
param_name => param_value,
|
58
|
+
'screenid' => screen_id
|
59
|
+
}
|
81
60
|
}
|
82
|
-
|
83
61
|
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
|
62
|
+
response.empty? ? false : true
|
92
63
|
end
|
93
64
|
|
94
65
|
def del_all_graphs_from_screen(screen_id)
|
95
|
-
|
96
66
|
message = {
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
67
|
+
'method' => 'screen.deleteItems',
|
68
|
+
'params' => {
|
69
|
+
'screenids' => [screen_id],
|
70
|
+
}
|
101
71
|
}
|
102
|
-
|
103
72
|
response = send_request(message)
|
104
|
-
|
105
|
-
if ( response ) then
|
106
|
-
return response
|
107
|
-
else
|
108
|
-
return nil
|
109
|
-
end
|
73
|
+
response ? response : nil
|
110
74
|
end
|
111
75
|
|
112
76
|
def add_graph_to_screen(screen_id, graph_id, x, y)
|
113
|
-
|
114
77
|
message = {
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
78
|
+
'method' => 'screen.addItems',
|
79
|
+
'params' => {
|
80
|
+
'screenids' => [screen_id],
|
81
|
+
'screenitems' => [
|
82
|
+
{
|
83
|
+
'resourcetype' => 'graph',
|
84
|
+
'resourceid' => graph_id,
|
85
|
+
'width' => '800',
|
86
|
+
'height' => '200',
|
87
|
+
'x' => x,
|
88
|
+
'y' => y,
|
89
|
+
'valign' => 'Middle',
|
90
|
+
'halign' => 'Centre',
|
91
|
+
'colspan' => '0',
|
92
|
+
'rowspan' => '0',
|
93
|
+
'elements' => '0',
|
94
|
+
'dynamic' => '0',
|
95
|
+
'url' => '0',
|
96
|
+
'style' => '0'
|
97
|
+
}
|
98
|
+
]
|
99
|
+
}
|
137
100
|
}
|
138
|
-
|
139
101
|
response = send_request(message)
|
140
|
-
|
141
102
|
return response
|
142
103
|
end
|
143
104
|
|
144
105
|
def add_screen(screen_name, hsize, vsize)
|
145
|
-
|
146
106
|
message = {
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
107
|
+
'method' => 'screen.create',
|
108
|
+
'params' => {
|
109
|
+
'name' => screen_name,
|
110
|
+
'hsize' => hsize,
|
111
|
+
'vsize' => vsize
|
112
|
+
}
|
153
113
|
}
|
154
|
-
|
155
114
|
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
|
115
|
+
response.empty? ? nil : response['screenids'][0]
|
164
116
|
end
|
117
|
+
|
165
118
|
end
|
166
119
|
end
|
data/lib/zabbixapi/template.rb
CHANGED
@@ -1,70 +1,52 @@
|
|
1
1
|
module Zabbix
|
2
|
-
|
3
2
|
class ZabbixApi
|
4
|
-
def add_template(template_options)
|
5
3
|
|
4
|
+
def add_template(template_options)
|
6
5
|
template_default = {
|
7
|
-
|
8
|
-
|
6
|
+
'host' => nil,
|
7
|
+
'groups' => [],
|
9
8
|
}
|
10
|
-
|
11
9
|
template_options['groups'].map! { |group_id| {'groupid' => group_id} }
|
12
|
-
|
13
10
|
template = merge_opt(template_default, template_options)
|
14
|
-
|
15
11
|
message = {
|
16
|
-
|
17
|
-
|
12
|
+
'method' => 'template.create',
|
13
|
+
'params' => template
|
18
14
|
}
|
19
|
-
|
20
15
|
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
|
16
|
+
response.empty? ? nil : response['templateids'][0].to_i
|
29
17
|
end
|
30
18
|
|
31
19
|
def get_template_ids_by_host(host_id)
|
32
|
-
|
33
20
|
message = {
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
21
|
+
'method' => 'template.get',
|
22
|
+
'params' => {
|
23
|
+
'hostids' => [host_id]
|
24
|
+
}
|
38
25
|
}
|
39
|
-
|
40
26
|
response = send_request(message)
|
41
|
-
|
42
|
-
|
27
|
+
if response.empty?
|
28
|
+
result = nil
|
29
|
+
else
|
43
30
|
result = []
|
44
31
|
response.each_key() do |template_id|
|
45
32
|
result << template_id
|
46
33
|
end
|
47
|
-
else
|
48
|
-
result = nil
|
49
34
|
end
|
50
|
-
|
51
35
|
return result
|
52
36
|
end
|
53
37
|
|
54
38
|
def get_templates()
|
55
|
-
|
56
39
|
message = {
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
40
|
+
'method' => 'template.get',
|
41
|
+
'params' => {
|
42
|
+
'extendoutput' => '0'
|
43
|
+
}
|
61
44
|
}
|
62
|
-
|
63
45
|
response = send_request(message)
|
64
|
-
|
65
|
-
|
46
|
+
if response.empty?
|
47
|
+
result = nil
|
48
|
+
else
|
66
49
|
result = {}
|
67
|
-
|
68
50
|
if response.kind_of? Hash
|
69
51
|
template_ids = response.keys()
|
70
52
|
template_ids.each() do |template_id|
|
@@ -76,89 +58,51 @@ module Zabbix
|
|
76
58
|
result[template_info['hostid']] = template_info['host']
|
77
59
|
end
|
78
60
|
end
|
79
|
-
else
|
80
|
-
result = nil
|
81
61
|
end
|
82
|
-
|
83
62
|
return result
|
84
63
|
end
|
85
64
|
|
86
65
|
def get_template_id(template_name)
|
87
|
-
|
88
66
|
message = {
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
67
|
+
'method' => 'template.get',
|
68
|
+
'params' => {
|
69
|
+
'filter' => {
|
70
|
+
'host' => template_name
|
71
|
+
}
|
93
72
|
}
|
94
|
-
}
|
95
73
|
}
|
96
|
-
|
97
74
|
response = send_request(message)
|
98
|
-
|
99
|
-
unless response.empty? then
|
100
|
-
result = response.keys[0]
|
101
|
-
else
|
102
|
-
result = nil
|
103
|
-
end
|
104
|
-
|
105
|
-
return result
|
106
|
-
|
75
|
+
response.empty? ? nil : response.keys[0]
|
107
76
|
end
|
108
77
|
|
109
78
|
def link_templates_with_hosts(templates_id, hosts_id)
|
110
|
-
|
111
|
-
|
112
|
-
message_templates_id = templates_id
|
113
|
-
else
|
114
|
-
message_templates_id = [ templates_id ]
|
115
|
-
end
|
116
|
-
|
117
|
-
if hosts_id == Array then
|
118
|
-
message_hosts_id = hosts_id
|
119
|
-
else
|
120
|
-
message_hosts_id = [ hosts_id ]
|
121
|
-
end
|
122
|
-
|
79
|
+
templates_id.class == Array ? message_templates_id = templates_id : message_templates_id = [templates_id]
|
80
|
+
hosts_id.class == Array ? message_hosts_id = hosts_id : message_hosts_id = [hosts_id]
|
123
81
|
message = {
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
82
|
+
'method' => 'template.massAdd',
|
83
|
+
'params' => {
|
84
|
+
'hosts' => message_hosts_id.map { |t| {"hostid" => t} },
|
85
|
+
'templates' => message_templates_id.map { |t| {"templateid" => t} }
|
86
|
+
}
|
129
87
|
}
|
130
|
-
|
131
88
|
response = send_request(message)
|
132
|
-
|
133
89
|
return response
|
134
90
|
end
|
135
91
|
|
136
92
|
def unlink_templates_from_hosts(templates_id, hosts_id)
|
137
|
-
|
138
|
-
|
139
|
-
message_templates_id = templates_id
|
140
|
-
else
|
141
|
-
message_templates_id = [ templates_id ]
|
142
|
-
end
|
143
|
-
|
144
|
-
if hosts_id == Array then
|
145
|
-
message_hosts_id = hosts_id
|
146
|
-
else
|
147
|
-
message_hosts_id = [ hosts_id ]
|
148
|
-
end
|
149
|
-
|
93
|
+
templates_id.class == Array ? message_templates_id = templates_id : message_templates_id = [templates_id]
|
94
|
+
hosts_id.class == Array ? message_hosts_id = hosts_id : message_hosts_id = [hosts_id]
|
150
95
|
message = {
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
96
|
+
'method' => 'template.massRemove',
|
97
|
+
'params' => {
|
98
|
+
'hostids' => message_hosts_id,
|
99
|
+
'templateids' => message_templates_id,
|
100
|
+
'force' => '1'
|
101
|
+
}
|
157
102
|
}
|
158
|
-
|
159
103
|
response = send_request(message)
|
160
|
-
|
161
104
|
return response
|
162
105
|
end
|
106
|
+
|
163
107
|
end
|
164
108
|
end
|