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.
@@ -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
- 'description' => nil,
28
- 'key_' => nil,
29
- 'hostid' => nil,
30
- 'delay' => 60,
31
- 'history' => 60,
32
- 'status' => 0,
33
- 'type' => 7,
34
- 'snmp_community' => '',
35
- 'snmp_oid' => '',
36
- 'value_type' => 3,
37
- 'data_type' => 0,
38
- 'trapper_hosts' => 'localhost',
39
- 'snmp_port' => 161,
40
- 'units' => '',
41
- 'multiplier' => 0,
42
- 'delta' => 0,
43
- 'snmpv3_securityname' => '',
44
- 'snmpv3_securitylevel' => 0,
45
- 'snmpv3_authpassphrase' => '',
46
- 'snmpv3_privpassphrase' => '',
47
- 'formula' => 0,
48
- 'trends' => 365,
49
- 'logtimefmt' => '',
50
- 'valuemapid' => 0,
51
- 'delay_flex' => '',
52
- 'authtype' => 0,
53
- 'username' => '',
54
- 'password' => '',
55
- 'publickey' => '',
56
- 'privatekey' => '',
57
- 'params' => '',
58
- 'ipmi_sensor' => '',
59
- 'applications' => '',
60
- 'templateid' => 0
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
- 'method' => 'item.create',
68
- 'params' => [ item_options ]
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
- 'method' => 'item.get',
86
- 'params' => {
87
- 'filter' => {
88
- 'hostid' => host_id,
89
- 'description' => item_name
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
- 'method' => 'item.update',
123
- 'params' => options
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
- 'method' => 'item.delete',
143
- 'params' => item_ids
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
- 'method' => 'item.delete',
148
- 'params' => [ item_ids ]
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
@@ -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
- 'method' => 'screen.get',
8
- 'params' => {
9
- 'filter' => {
10
- 'name' => screen_name
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
- 'method' => 'screen.get',
28
- 'params' => {
29
- 'extendoutput' => '1',
30
- 'filter' => {
31
- 'name' => screen_name
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
- 'method' => 'screen.get',
49
- 'params' => {
50
- 'extendoutput' => '1',
51
- 'select_screenitems' => '1',
52
- 'screenids' => [ screen_id ]
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
- unless ( response.empty?) then
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
- if ( item['resourcetype'].to_i == 0 ) then
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
- 'method' => 'screen.update',
77
- 'params' => {
78
- param_name => param_value,
79
- 'screenid' => screen_id
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
- 'method' => 'screen.deleteItems',
98
- 'params' => {
99
- 'screenids' => [ screen_id ],
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
- '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
- }
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
- 'method' => 'screen.create',
148
- 'params' => {
149
- 'name' => screen_name,
150
- 'hsize' => hsize,
151
- 'vsize' => vsize
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
@@ -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
- 'host' => nil,
8
- 'groups' => [],
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
- 'method' => 'template.create',
17
- 'params' => template
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
- 'method' => 'template.get',
35
- 'params' => {
36
- 'hostids' => [ host_id ]
37
- }
21
+ 'method' => 'template.get',
22
+ 'params' => {
23
+ 'hostids' => [host_id]
24
+ }
38
25
  }
39
-
40
26
  response = send_request(message)
41
-
42
- unless ( response.empty? ) then
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
- 'method' => 'template.get',
58
- 'params' => {
59
- 'extendoutput' => '0'
60
- }
40
+ 'method' => 'template.get',
41
+ 'params' => {
42
+ 'extendoutput' => '0'
43
+ }
61
44
  }
62
-
63
45
  response = send_request(message)
64
-
65
- unless response.empty? then
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
- 'method' => 'template.get',
90
- 'params' => {
91
- 'filter' => {
92
- 'host' => template_name
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
- if templates_id.class == Array then
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
- 'method' => 'template.massAdd',
125
- 'params' => {
126
- 'hosts' => message_hosts_id.map{|t| {"hostid" => t}},
127
- 'templates' => message_templates_id.map{|t| {"templateid" => t}}
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
- if templates_id.class == Array then
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
- 'method' => 'template.massRemove',
152
- 'params' => {
153
- 'hostids' => message_hosts_id,
154
- 'templateids' => message_templates_id,
155
- 'force' => '1'
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