zabbixapi 0.1.4 → 0.1.5a1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,86 @@
1
+ module Zabbix
2
+
3
+ class ZabbixApi
4
+ def update_host(host_id, host_options)
5
+ host = host_options
6
+ host['hostid'] = host_id
7
+
8
+ message = {
9
+ 'method' => 'host.update',
10
+ 'params' => host
11
+ }
12
+
13
+ responce = send_request(message)
14
+
15
+ if not ( responce.empty? ) then
16
+ result = responce['hostids'][0].to_i
17
+ else
18
+ result = nil
19
+ end
20
+
21
+ return result
22
+ end
23
+
24
+ def add_host(host_options)
25
+
26
+ host_default = {
27
+ 'host' => nil,
28
+ 'port' => 10050,
29
+ 'status' => 0,
30
+ 'useip' => 0,
31
+ 'dns' => '',
32
+ 'ip' => '0.0.0.0',
33
+ 'proxy_hostid' => 0,
34
+ 'groups' => [],
35
+ 'useipmi' => 0,
36
+ 'ipmi_ip' => '',
37
+ 'ipmi_port' => 623,
38
+ 'ipmi_authtype' => 0,
39
+ 'ipmi_privilege' => 0,
40
+ 'ipmi_username' => '',
41
+ 'ipmi_password' => ''
42
+ }
43
+
44
+ host_options['groups'].map! { |group_id| {'groupid' => group_id} }
45
+
46
+ host = merge_opt(host_default, host_options)
47
+
48
+ message = {
49
+ 'method' => 'host.create',
50
+ 'params' => host
51
+ }
52
+
53
+ response = send_request(message)
54
+
55
+ unless response.empty? then
56
+ result = response['hostids'][0].to_i
57
+ else
58
+ result = nil
59
+ end
60
+
61
+ return result
62
+ end
63
+
64
+ def get_host_id(hostname)
65
+
66
+ message = {
67
+ 'method' => 'host.get',
68
+ 'params' => {
69
+ 'filter' => {
70
+ 'host' => hostname
71
+ }
72
+ }
73
+ }
74
+
75
+ response = send_request(message)
76
+
77
+ unless response.empty? then
78
+ result = response[0]['hostid'].to_i
79
+ else
80
+ result = nil
81
+ end
82
+
83
+ return result
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,169 @@
1
+ module Zabbix
2
+ class ZabbixApi
3
+ def add_item(item)
4
+
5
+ # Default item options
6
+ # See: http://www.zabbix.com/documentation/1.8/api/item
7
+
8
+ ## Item types (see ./frontends/php/include/defines.inc.php in zabbix source)
9
+ # ITEM_TYPE_ZABBIX 0
10
+ # ITEM_TYPE_SNMPV1 1
11
+ # ITEM_TYPE_TRAPPER 2
12
+ # ITEM_TYPE_SIMPLE 3
13
+ # ITEM_TYPE_SNMPV2C 4
14
+ # ITEM_TYPE_INTERNAL 5
15
+ # ITEM_TYPE_SNMPV3 6
16
+ # ITEM_TYPE_ZABBIX_ACTIVE 7
17
+ # ITEM_TYPE_AGGREGATE 8
18
+ # ITEM_TYPE_HTTPTEST 9
19
+ # ITEM_TYPE_EXTERNAL 10
20
+ # ITEM_TYPE_DB_MONITOR 11
21
+ # ITEM_TYPE_IPMI 12
22
+ # ITEM_TYPE_SSH 13
23
+ # ITEM_TYPE_TELNET 14
24
+ # ITEM_TYPE_CALCULATED 15
25
+
26
+ 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
61
+ }
62
+
63
+
64
+ item_options.merge!(item)
65
+
66
+ message = {
67
+ 'method' => 'item.create',
68
+ 'params' => [ item_options ]
69
+ }
70
+
71
+ 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
+
81
+ end
82
+
83
+ def get_item_id(host_id, item_name)
84
+ message = {
85
+ 'method' => 'item.get',
86
+ 'params' => {
87
+ 'filter' => {
88
+ 'hostid' => host_id,
89
+ 'description' => item_name
90
+ }
91
+ }
92
+ }
93
+
94
+ 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
+
104
+ end
105
+
106
+ def item_exist?(host_id, item_name)
107
+ item_id = get_item_id(host_id, item_name)
108
+ if item_id
109
+ result = true
110
+ else
111
+ result = false
112
+ end
113
+
114
+ return result
115
+ end
116
+
117
+ def update_item(item_id, options)
118
+
119
+ options["item_id"]
120
+
121
+ message = {
122
+ 'method' => 'item.update',
123
+ 'params' => options
124
+ }
125
+
126
+ 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
135
+ end
136
+
137
+ # Don't work with api < 1.8.4
138
+ def delete_item(item_ids)
139
+
140
+ if item_ids.kind_of? Array
141
+ message = {
142
+ 'method' => 'item.delete',
143
+ 'params' => item_ids
144
+ }
145
+ elsif item_ids.kind_of? Fixnum or item_ids.kind_of? String
146
+ message = {
147
+ 'method' => 'item.delete',
148
+ 'params' => [ item_ids ]
149
+ }
150
+ else
151
+ raise Zabbix::ArgumentError.new("Zabbix::ZabbixApi.delete_item() argument error. item_ids => #{item_ids.inspect}")
152
+ end
153
+
154
+ 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
163
+ result = nil
164
+ end
165
+
166
+ return result
167
+ end
168
+ end
169
+ end
@@ -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
@@ -1,7 +1,6 @@
1
1
  module Zabbix
2
2
 
3
3
  class ZabbixApi
4
-
5
4
  def add_template(template_options)
6
5
 
7
6
  template_default = {
@@ -18,10 +17,10 @@ module Zabbix
18
17
  'params' => template
19
18
  }
20
19
 
21
- responce = send_request(message)
20
+ response = send_request(message)
22
21
 
23
- if not ( responce.empty? ) then
24
- result = responce['templateids'][0].to_i
22
+ if not ( response.empty? ) then
23
+ result = response['templateids'][0].to_i
25
24
  else
26
25
  result = nil
27
26
  end
@@ -38,11 +37,11 @@ module Zabbix
38
37
  }
39
38
  }
40
39
 
41
- responce = send_request(message)
40
+ response = send_request(message)
42
41
 
43
- unless ( responce.empty? ) then
42
+ unless ( response.empty? ) then
44
43
  result = []
45
- responce.each_key() do |template_id|
44
+ response.each_key() do |template_id|
46
45
  result << template_id
47
46
  end
48
47
  else
@@ -53,7 +52,7 @@ module Zabbix
53
52
  end
54
53
 
55
54
  def get_templates()
56
-
55
+
57
56
  message = {
58
57
  'method' => 'template.get',
59
58
  'params' => {
@@ -61,17 +60,14 @@ module Zabbix
61
60
  }
62
61
  }
63
62
 
64
- responce = send_request(message)
65
-
66
-
67
- unless ( responce.empty? ) then
68
-
69
- template_ids = responce.keys()
63
+ response = send_request(message)
70
64
 
65
+ unless response.empty? then
66
+ template_ids = response.keys()
71
67
  result = {}
72
68
 
73
69
  template_ids.each() do |template_id|
74
- template_name = responce[template_id]['host']
70
+ template_name = response[template_id]['host']
75
71
  result[template_id] = template_name
76
72
  end
77
73
  else
@@ -92,10 +88,10 @@ module Zabbix
92
88
  }
93
89
  }
94
90
 
95
- responce = send_request(message)
91
+ response = send_request(message)
96
92
 
97
- if not ( responce.empty? ) then
98
- result = responce.keys[0]
93
+ unless response.empty? then
94
+ result = response.keys[0]
99
95
  else
100
96
  result = nil
101
97
  end
@@ -106,13 +102,13 @@ module Zabbix
106
102
 
107
103
  def link_templates_with_hosts(templates_id, hosts_id)
108
104
 
109
- if ( templates_id.class == Array ) then
105
+ if templates_id.class == Array then
110
106
  message_templates_id = templates_id
111
107
  else
112
108
  message_templates_id = [ templates_id ]
113
109
  end
114
110
 
115
- if ( hosts_id == Array ) then
111
+ if hosts_id == Array then
116
112
  message_hosts_id = hosts_id
117
113
  else
118
114
  message_hosts_id = [ hosts_id ]
@@ -126,20 +122,20 @@ module Zabbix
126
122
  }
127
123
  }
128
124
 
129
- responce = send_request(message)
125
+ response = send_request(message)
130
126
 
131
- return responce
127
+ return response
132
128
  end
133
129
 
134
130
  def unlink_templates_from_hosts(templates_id, hosts_id)
135
131
 
136
- if ( templates_id.class == Array ) then
132
+ if templates_id.class == Array then
137
133
  message_templates_id = templates_id
138
134
  else
139
135
  message_templates_id = [ templates_id ]
140
136
  end
141
137
 
142
- if ( hosts_id == Array ) then
138
+ if hosts_id == Array then
143
139
  message_hosts_id = hosts_id
144
140
  else
145
141
  message_hosts_id = [ hosts_id ]
@@ -154,9 +150,9 @@ module Zabbix
154
150
  }
155
151
  }
156
152
 
157
- responce = send_request(message)
153
+ response = send_request(message)
158
154
 
159
- return responce
155
+ return response
160
156
  end
161
157
  end
162
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