zabbixapi 0.5.2 → 0.5.3b1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/README.md +1 -0
  2. data/lib/zabbixapi.rb +58 -30
  3. data/lib/zabbixapi/{basic → 1.8/basic}/basic_alias.rb +0 -0
  4. data/lib/zabbixapi/{basic → 1.8/basic}/basic_func.rb +0 -0
  5. data/lib/zabbixapi/{basic → 1.8/basic}/basic_init.rb +0 -0
  6. data/lib/zabbixapi/{basic → 1.8/basic}/basic_logic.rb +0 -0
  7. data/lib/zabbixapi/{classes → 1.8/classes}/applications.rb +0 -0
  8. data/lib/zabbixapi/{classes → 1.8/classes}/errors.rb +0 -0
  9. data/lib/zabbixapi/{classes → 1.8/classes}/graphs.rb +0 -0
  10. data/lib/zabbixapi/{classes → 1.8/classes}/hostgroups.rb +0 -0
  11. data/lib/zabbixapi/{classes → 1.8/classes}/hosts.rb +0 -0
  12. data/lib/zabbixapi/{classes → 1.8/classes}/items.rb +0 -0
  13. data/lib/zabbixapi/{classes → 1.8/classes}/mediatypes.rb +0 -0
  14. data/lib/zabbixapi/1.8/classes/screens.rb +68 -0
  15. data/lib/zabbixapi/{classes → 1.8/classes}/server.rb +0 -0
  16. data/lib/zabbixapi/{classes → 1.8/classes}/templates.rb +0 -0
  17. data/lib/zabbixapi/{classes → 1.8/classes}/triggers.rb +0 -0
  18. data/lib/zabbixapi/{classes → 1.8/classes}/unusable.rb +0 -0
  19. data/lib/zabbixapi/{classes → 1.8/classes}/usergroups.rb +0 -0
  20. data/lib/zabbixapi/{classes → 1.8/classes}/users.rb +0 -0
  21. data/lib/zabbixapi/2.0/basic/basic_alias.rb +21 -0
  22. data/lib/zabbixapi/2.0/basic/basic_func.rb +55 -0
  23. data/lib/zabbixapi/2.0/basic/basic_init.rb +33 -0
  24. data/lib/zabbixapi/2.0/basic/basic_logic.rb +103 -0
  25. data/lib/zabbixapi/2.0/classes/applications.rb +45 -0
  26. data/lib/zabbixapi/2.0/classes/errors.rb +15 -0
  27. data/lib/zabbixapi/2.0/classes/graphs.rb +54 -0
  28. data/lib/zabbixapi/2.0/classes/hostgroups.rb +26 -0
  29. data/lib/zabbixapi/2.0/classes/hosts.rb +60 -0
  30. data/lib/zabbixapi/2.0/classes/items.rb +54 -0
  31. data/lib/zabbixapi/2.0/classes/mediatypes.rb +31 -0
  32. data/lib/zabbixapi/{classes → 2.0/classes}/screens.rb +0 -0
  33. data/lib/zabbixapi/2.0/classes/server.rb +12 -0
  34. data/lib/zabbixapi/2.0/classes/templates.rb +109 -0
  35. data/lib/zabbixapi/2.0/classes/triggers.rb +67 -0
  36. data/lib/zabbixapi/2.0/classes/unusable.rb +10 -0
  37. data/lib/zabbixapi/2.0/classes/usergroups.rb +82 -0
  38. data/lib/zabbixapi/2.0/classes/users.rb +32 -0
  39. data/lib/zabbixapi/version.rb +1 -1
  40. metadata +41 -23
data/README.md CHANGED
@@ -7,6 +7,7 @@ Simple and lightweight ruby module for work with zabbix api
7
7
  #####Now worked with zabbix
8
8
  * 1.8.2 (api version 1.2)
9
9
  * 1.8.9 (api version 1.3)
10
+ * 2.0.4 (api version 2.0.4) [unstable]
10
11
 
11
12
  ## Installation
12
13
  ```
data/lib/zabbixapi.rb CHANGED
@@ -2,25 +2,9 @@ require "zabbixapi/version"
2
2
  require "zabbixapi/client"
3
3
 
4
4
 
5
- Dir["#{File.dirname(__FILE__)}/zabbixapi/basic/*.rb"].each { |f| load(f) }
6
- Dir["#{File.dirname(__FILE__)}/zabbixapi/classes/*.rb"].each { |f| load(f) }
7
-
8
-
9
5
  class ZabbixApi
10
6
 
11
7
  attr :client
12
- attr :server
13
- attr :users
14
- attr :items
15
- attr :applications
16
- attr :templates
17
- attr :hostgroups
18
- attr :hosts
19
- attr :triggers
20
- attr :graphs
21
- attr :screens
22
- attr :usergroups
23
- attr :mediatypes
24
8
 
25
9
  def self.connect(options = {})
26
10
  new(options)
@@ -35,22 +19,66 @@ class ZabbixApi
35
19
  end
36
20
 
37
21
  def initialize(options = {})
38
-
39
22
  @client = Client.new(options)
23
+ case @client.api_version
24
+ when "1.3", "1.2"
25
+ apidir = "1.8"
26
+ when "2.0.4"
27
+ apidir = "2.0"
28
+ else
29
+ raise "unknown Api version!"
30
+ end
31
+ Dir["#{File.dirname(__FILE__)}/zabbixapi/#{apidir}/basic/*.rb"].each { |f| load(f) }
32
+ Dir["#{File.dirname(__FILE__)}/zabbixapi/#{apidir}/classes/*.rb"].each { |f| load(f) }
33
+ end
34
+
35
+ def server
36
+ @server ||= Server.new(@client)
37
+ end
38
+
39
+ def users
40
+ @users ||= Users.new(@client)
41
+ end
42
+
43
+ def items
44
+ @items ||= Items.new(@client)
45
+ end
46
+
47
+ def hosts
48
+ @hosts ||= Hosts.new(@client)
49
+ end
50
+
51
+ def applications
52
+ @applications ||= Applications.new(@client)
53
+ end
40
54
 
41
- @server = Server.new(@client)
42
- @users = Users.new(@client)
43
- @items = Items.new(@client)
44
- @hosts = Hosts.new(@client)
45
- @applications = Applications.new(@client)
46
- @templates = Templates.new(@client)
47
- @hostgroups = HostGroups.new(@client)
48
- @triggers = Triggers.new(@client)
49
- @graphs = Graphs.new(@client)
50
- @screens = Screens.new(@client)
51
- @usergroups = Usergroups.new(@client)
52
- @mediatypes = Mediatypes.new(@client)
53
-
55
+ def templates
56
+ @templates ||= Templates.new(@client)
57
+ end
58
+
59
+ def hostgroups
60
+ @hostgroups ||= HostGroups.new(@client)
61
+ end
62
+
63
+ def triggers
64
+ @triggers ||= Triggers.new(@client)
65
+ end
66
+
67
+ def graphs
68
+ @graphs ||= Graphs.new(@client)
69
+ end
70
+
71
+ def screens
72
+ @screens ||= Screens.new(@client)
73
+ end
74
+
75
+ def usergroups
76
+ @usergroups ||= Usergroups.new(@client)
77
+ end
78
+
79
+ def mediatypes
80
+ @mediatypes ||= Mediatypes.new(@client)
54
81
  end
55
82
 
56
83
  end
84
+
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1,68 @@
1
+ class ZabbixApi
2
+ class Screens < Basic
3
+
4
+ # extracted from frontends/php/include/defines.inc.php
5
+ #SCREEN_RESOURCE_GRAPH => 0,
6
+ #SCREEN_RESOURCE_SIMPLE_GRAPH => 1,
7
+ #SCREEN_RESOURCE_MAP => 2,
8
+ #SCREEN_RESOURCE_PLAIN_TEXT => 3,
9
+ #SCREEN_RESOURCE_HOSTS_INFO => 4,
10
+ #SCREEN_RESOURCE_TRIGGERS_INFO => 5,
11
+ #SCREEN_RESOURCE_SERVER_INFO => 6,
12
+ #SCREEN_RESOURCE_CLOCK => 7,
13
+ #SCREEN_RESOURCE_SCREEN => 8,
14
+ #SCREEN_RESOURCE_TRIGGERS_OVERVIEW => 9,
15
+ #SCREEN_RESOURCE_DATA_OVERVIEW => 10,
16
+ #SCREEN_RESOURCE_URL => 11,
17
+ #SCREEN_RESOURCE_ACTIONS => 12,
18
+ #SCREEN_RESOURCE_EVENTS => 13,
19
+ #SCREEN_RESOURCE_HOSTGROUP_TRIGGERS => 14,
20
+ #SCREEN_RESOURCE_SYSTEM_STATUS => 15,
21
+ #SCREEN_RESOURCE_HOST_TRIGGERS => 16
22
+
23
+ def method_name
24
+ "screen"
25
+ end
26
+
27
+ def indentify
28
+ "name"
29
+ end
30
+
31
+ def delete(data)
32
+ result = @client.api_request(:method => "screen.delete", :params => data)
33
+ result.empty? ? nil : result['screenids'][0].to_i
34
+ end
35
+
36
+ def get_or_create_for_host(data)
37
+ screen_name = data[:host].to_s + "_graphs"
38
+ graphids = data[:graphids]
39
+ screenitems = []
40
+ hsize = data[:hsize] || 3
41
+ valign = data[:valign] || 2
42
+ halign = data[:halign] || 2
43
+ vsize = data[:vsize] || ((graphids.size/hsize) + 1).to_i
44
+ if get_id(:name => screen_name)
45
+ delete(:name => screen_name)
46
+ end
47
+ # create screan
48
+ graphids.each_with_index do |graphid, index|
49
+ screenitems << {
50
+ :resourcetype => 0,
51
+ :resourceid => graphid,
52
+ :x => (index % hsize).to_i,
53
+ :y => (index % graphids.size/hsize).to_i,
54
+ :valign =>valign,
55
+ :halign =>halign
56
+ }
57
+ end
58
+
59
+ create(
60
+ :name => screen_name,
61
+ :hsize => hsize,
62
+ :vsize => vsize,
63
+ :screenitems => screenitems
64
+ )
65
+ end
66
+
67
+ end
68
+ end
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1,21 @@
1
+ class ZabbixApi
2
+ class Basic
3
+
4
+ def get(data)
5
+ get_full_data(data)
6
+ end
7
+
8
+ def add(data)
9
+ create(data)
10
+ end
11
+
12
+ def destroy(data)
13
+ delete(data)
14
+ end
15
+
16
+ def method_name
17
+
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,55 @@
1
+ class ZabbixApi
2
+ class Basic
3
+
4
+ def log(message)
5
+ puts "#{message}" if @client.options[:debug]
6
+ end
7
+
8
+ def hash_equals?(a, b)
9
+ a_new = normalize_hash(a)
10
+ b_new = normalize_hash(b)
11
+ hash1 = a_new.merge(b_new)
12
+ hash2 = b_new.merge(a_new)
13
+ hash1 == hash2
14
+ end
15
+
16
+ def symbolize_keys(obj)
17
+ return obj.inject({}){|memo,(k,v)| memo[k.to_sym] = symbolize_keys(v); memo} if obj.is_a? Hash
18
+ return obj.inject([]){|memo,v | memo << symbolize_keys(v); memo} if obj.is_a? Array
19
+ obj
20
+ end
21
+
22
+ def normalize_hash(hash)
23
+ result = hash.dup
24
+ result.delete(:hostid) #TODO remove to logig. TemplateID and HostID has different id
25
+ result.each do |key, value|
26
+ case value
27
+ when Array
28
+ result.delete(key)
29
+ else
30
+ result[key] = value.to_s
31
+ end
32
+ end
33
+ result
34
+ end
35
+
36
+ def parse_keys(data)
37
+ case data
38
+ when Hash
39
+ data.empty? ? nil : data[keys][0].to_i
40
+ when TrueClass
41
+ true
42
+ when FalseClass
43
+ false
44
+ else
45
+ nil
46
+ end
47
+ end
48
+
49
+ def merge_params(params)
50
+ result = JSON.generate(default_options).to_s + "," + JSON.generate(params).to_s
51
+ JSON.parse(result.gsub('},{', ','))
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,33 @@
1
+ class ZabbixApi
2
+ class Basic
3
+
4
+ def initialize(client)
5
+ @client = client
6
+ end
7
+
8
+ def method_name
9
+ raise "Can't call method_name here"
10
+ end
11
+
12
+ def default_options
13
+ {}
14
+ end
15
+
16
+ def keys
17
+ key + "s"
18
+ end
19
+
20
+ def key
21
+ method_name + "id"
22
+ end
23
+
24
+ def indentify
25
+ raise "Can't call indentify here"
26
+ end
27
+
28
+ def array_flag
29
+ false
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,103 @@
1
+ class ZabbixApi
2
+ class Basic
3
+
4
+ def create(data)
5
+ log "[DEBUG] Call create with parametrs: #{data.inspect}"
6
+
7
+ data_with_default = default_options.empty? ? data : merge_params(data)
8
+ data_create = array_flag ? [data_with_default] : data_with_default
9
+ result = @client.api_request(:method => "#{method_name}.create", :params => data_create)
10
+ parse_keys result
11
+ end
12
+
13
+ def delete(data)
14
+ log "[DEBUG] Call delete with parametrs: #{data.inspect}"
15
+
16
+ data_delete = array_flag ? [data] : [key.to_sym => data]
17
+ result = @client.api_request(:method => "#{method_name}.delete", :params => data_delete)
18
+ parse_keys result
19
+ end
20
+
21
+ def create_or_update(data)
22
+ log "[DEBUG] Call create_or_update with parametrs: #{data.inspect}"
23
+
24
+ id = get_id(indentify.to_sym => data[indentify.to_sym])
25
+ id ? update(data.merge(key.to_sym => id.to_s)) : create(data)
26
+ end
27
+
28
+ def update(data)
29
+ log "[DEBUG] Call update with parametrs: #{data.inspect}"
30
+
31
+ dump = {}
32
+ item_id = data[key.to_sym].to_i
33
+ dump_by_id(key.to_sym => data[key.to_sym]).each do |item|
34
+ dump = symbolize_keys(item) if item[key].to_i == data[key.to_sym].to_i
35
+ end
36
+
37
+ if hash_equals?(dump, data)
38
+ log "[DEBUG] Equal keys #{dump} and #{data}, skip update"
39
+ item_id
40
+ else
41
+ data_update = array_flag ? [data] : data
42
+ result = @client.api_request(:method => "#{method_name}.update", :params => data_update)
43
+ parse_keys result
44
+ end
45
+
46
+ end
47
+
48
+ def get_full_data(data)
49
+ log "[DEBUG] Call get_full_data with parametrs: #{data.inspect}"
50
+
51
+ @client.api_request(
52
+ :method => "#{method_name}.get",
53
+ :params => {
54
+ :filter => {
55
+ indentify.to_sym => data[indentify.to_sym]
56
+ },
57
+ :output => "extend"
58
+ }
59
+ )
60
+ end
61
+
62
+ def dump_by_id(data)
63
+ log "[DEBUG] Call dump_by_id with parametrs: #{data.inspect}"
64
+
65
+ @client.api_request(
66
+ :method => "#{method_name}.get",
67
+ :params => {
68
+ :filter => {
69
+ key.to_sym => data[key.to_sym]
70
+ },
71
+ :output => "extend"
72
+ }
73
+ )
74
+ end
75
+
76
+ def all
77
+ result = {}
78
+ @client.api_request(:method => "#{method_name}.get", :params => {:output => "extend"}).each do |item|
79
+ result[item[indentify]] = item[key]
80
+ end
81
+ result
82
+ end
83
+
84
+ def get_id(data)
85
+ log "[DEBUG] Call get_id with parametrs: #{data.inspect}"
86
+
87
+ result = symbolize_keys( get_full_data(data) )
88
+ id = nil
89
+ result.each { |item| id = item[key.to_sym].to_i if item[indentify.to_sym] == data[indentify.to_sym] }
90
+ id
91
+ end
92
+
93
+ def get_or_create(data)
94
+ log "[DEBUG] Call get_or_create with parametrs: #{data.inspect}"
95
+
96
+ unless (id = get_id(data))
97
+ id = create(data)
98
+ end
99
+ id
100
+ end
101
+
102
+ end
103
+ end
@@ -0,0 +1,45 @@
1
+ class ZabbixApi
2
+ class Applications
3
+
4
+ def initialize(client)
5
+ @client = client
6
+ end
7
+
8
+ def create(data)
9
+ result = @client.api_request(:method => "application.create", :params => [data])
10
+ result.empty? ? nil : result['applicationids'][0].to_i
11
+ end
12
+
13
+ def add(data)
14
+ create(data)
15
+ end
16
+
17
+ def delete(data)
18
+ result = @client.api_request(:method => "application.delete", :params => [data])
19
+ result.empty? ? nil : result['applicationids'][0].to_i
20
+ end
21
+
22
+ def get_or_create(data)
23
+ unless (appid = get_id(data))
24
+ appid = create(data)
25
+ end
26
+ appid
27
+ end
28
+
29
+ def destroy(data)
30
+ delete(data)
31
+ end
32
+
33
+ def get_full_data(data)
34
+ @client.api_request(:method => "application.get", :params => {:filter => data, :output => "extend"})
35
+ end
36
+
37
+ def get_id(data)
38
+ result = get_full_data(data)
39
+ applicationid = nil
40
+ result.each { |app| applicationid = app['applicationid'].to_i if app['name'] == data[:name] }
41
+ applicationid
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,15 @@
1
+ class ZabbixApi
2
+
3
+ class BaseError < RuntimeError
4
+ end
5
+
6
+ class ApiError < BaseError
7
+ end
8
+
9
+ class HttpError < BaseError
10
+ end
11
+
12
+ class SocketError < BaseError
13
+ end
14
+
15
+ end
@@ -0,0 +1,54 @@
1
+ class ZabbixApi
2
+ class Graphs < Basic
3
+
4
+ def array_flag
5
+ true
6
+ end
7
+
8
+ def method_name
9
+ "graph"
10
+ end
11
+
12
+ def indentify
13
+ "name"
14
+ end
15
+
16
+ def get_full_data(data)
17
+ log "[DEBUG] Call get_full_data with parametrs: #{data.inspect}"
18
+
19
+ @client.api_request(
20
+ :method => "#{method_name}.get",
21
+ :params => {
22
+ :search => {
23
+ indentify.to_sym => data[indentify.to_sym]
24
+ },
25
+ :output => "extend"
26
+ }
27
+ )
28
+ end
29
+
30
+ def get_ids_by_host(data)
31
+ ids = []
32
+ result = @client.api_request(:method => "graph.get", :params => {:filter => {:host => data[:host]}, :output => "extend"})
33
+ result.each do |graph|
34
+ ids << graph['graphid']
35
+ end
36
+ ids
37
+ end
38
+
39
+ def get_items(data)
40
+ @client.api_request(:method => "graphitem.get", :params => { :graphids => [data], :output => "extend" } )
41
+ end
42
+
43
+ def create_or_update(data)
44
+ graphid = get_id(:name => data[:name], :templateid => data[:templateid])
45
+ graphid ? _update(data.merge(:graphid => graphid)) : create(data)
46
+ end
47
+
48
+ def _update(data)
49
+ data.delete(:name)
50
+ update(data)
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,26 @@
1
+ class ZabbixApi
2
+ class HostGroups < Basic
3
+
4
+ def array_flag
5
+ true
6
+ end
7
+
8
+ def method_name
9
+ "hostgroup"
10
+ end
11
+
12
+ def indentify
13
+ "name"
14
+ end
15
+
16
+ def key
17
+ "groupid"
18
+ end
19
+
20
+ def delete(data)
21
+ result = @client.api_request(:method => "hostgroup.delete", :params => [:groupid => data])
22
+ result.empty? ? nil : result['groupids'][0].to_i
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,60 @@
1
+ class ZabbixApi
2
+ class Hosts < Basic
3
+
4
+ def array_flag
5
+ true
6
+ end
7
+
8
+ def method_name
9
+ "host"
10
+ end
11
+
12
+ def indentify
13
+ "host"
14
+ end
15
+
16
+ def default_options
17
+ {
18
+ :host => nil,
19
+ :port => 10050,
20
+ :status => 1,
21
+ :useip => 1,
22
+ :dns => '',
23
+ :ip => '0.0.0.0',
24
+ :proxy_hostid => 0,
25
+ :groups => [],
26
+ :useipmi => 0,
27
+ :ipmi_ip => '',
28
+ :ipmi_port => 623,
29
+ :ipmi_authtype => 0,
30
+ :ipmi_privilege => 0,
31
+ :ipmi_username => '',
32
+ :ipmi_password => ''
33
+ }
34
+ end
35
+
36
+ def unlink_templates(data)
37
+ result = @client.api_request(
38
+ :method => "host.massRemove",
39
+ :params => {
40
+ :hostids => data[:hosts_id],
41
+ :templates => data[:templates_id]
42
+ }
43
+ )
44
+ result.empty? ? false : true
45
+ end
46
+
47
+ def create_or_update(data)
48
+ # https://www.zabbix.com/documentation/2.2/manual/api/reference/host/create
49
+ # interfaces :(
50
+ data_new = data.clone
51
+ %w( type main useip ip dns port ).each do |key|
52
+ data_new[:interfaces][key.to_sym] = data_new[key.to_sym]
53
+ end
54
+ puts "#{data_new.inspect}"
55
+ hostid = get_id(:host => data_new[:host])
56
+ hostid ? update(data_new.merge(:hostid => hostid)) : create(data_new)
57
+ end
58
+
59
+ end
60
+ end
@@ -0,0 +1,54 @@
1
+ class ZabbixApi
2
+ class Items < Basic
3
+
4
+ def array_flag
5
+ true
6
+ end
7
+
8
+ def method_name
9
+ "item"
10
+ end
11
+
12
+ def indentify
13
+ "name"
14
+ end
15
+
16
+ def default_options
17
+ {
18
+ :name => nil,
19
+ :key_ => nil,
20
+ :hostid => nil,
21
+ :delay => 60,
22
+ :history => 60,
23
+ :status => 0,
24
+ :type => 7,
25
+ :snmp_community => '',
26
+ :snmp_oid => '',
27
+ :value_type => 3,
28
+ :data_type => 0,
29
+ :trapper_hosts => 'localhost',
30
+ :snmp_port => 161,
31
+ :units => '',
32
+ :multiplier => 0,
33
+ :delta => 0,
34
+ :snmpv3_securityname => '',
35
+ :snmpv3_securitylevel => 0,
36
+ :snmpv3_authpassphrase => '',
37
+ :snmpv3_privpassphrase => '',
38
+ :formula => 0,
39
+ :trends => 365,
40
+ :logtimefmt => '',
41
+ :valuemapid => 0,
42
+ :delay_flex => '',
43
+ :authtype => 0,
44
+ :username => '',
45
+ :password => '',
46
+ :publickey => '',
47
+ :privatekey => '',
48
+ :params => '',
49
+ :ipmi_sensor => ''
50
+ }
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,31 @@
1
+ class ZabbixApi
2
+ class Mediatypes < Basic
3
+
4
+ def array_flag
5
+ true
6
+ end
7
+
8
+ def method_name
9
+ "mediatype"
10
+ end
11
+
12
+ def indentify
13
+ "description"
14
+ end
15
+
16
+ def default_options
17
+ {
18
+ :description => "", #Name
19
+ :type => 0, #0 - Email, 1 - External script, 2 - SMS, 3 - Jabber, 100 - EzTexting
20
+ :smtp_server => "",
21
+ :smtp_helo => "",
22
+ :smtp_email => "", #Email address of Zabbix server
23
+ :exec_path => "", #Name of external script
24
+ :gsm_modem => "", #Serial device name of GSM modem
25
+ :username => "", #Jabber user name used by Zabbix server
26
+ :passwd => "" #Jabber password used by Zabbix server
27
+ }
28
+ end
29
+
30
+ end
31
+ end
File without changes
@@ -0,0 +1,12 @@
1
+ class ZabbixApi
2
+ class Server
3
+
4
+ attr :version
5
+
6
+ def initialize(client)
7
+ @client = client
8
+ @version = @client.api_request(:method => "apiinfo.version", :params => {})
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,109 @@
1
+ class ZabbixApi
2
+ class Templates < Basic
3
+
4
+ def array_flag
5
+ true
6
+ end
7
+
8
+ def method_name
9
+ "template"
10
+ end
11
+
12
+ def indentify
13
+ "host"
14
+ end
15
+
16
+
17
+ # Delete template
18
+ #
19
+ # * *Args* :
20
+ # - +data+ -> Hash with :host => "Template_Name"
21
+ # * *Returns* :
22
+ # - Nil or Integer
23
+ def delete(data)
24
+ result = @client.api_request(:method => "template.delete", :params => [:templateid => data])
25
+ result.empty? ? nil : result['templateids'][0].to_i
26
+ end
27
+
28
+ # Return templateids linked with host
29
+ #
30
+ # * *Args* :
31
+ # - +data+ -> Hash with :hostids => [hostid]
32
+ # * *Returns* :
33
+ # - Array with templateids
34
+ def get_ids_by_host(data)
35
+ result = []
36
+ @client.api_request(:method => "template.get", :params => data).each do |tmpl|
37
+ result << tmpl['templateid']
38
+ end
39
+ result
40
+ end
41
+
42
+ # Return templateid
43
+ #
44
+ # * *Args* :
45
+ # - +data+ -> Hash with :host => "Template_Name" and :groups => array with hostgroup ids
46
+ # * *Returns* :
47
+ # - Integer
48
+ def get_or_create(data)
49
+ unless (templateid = get_id(:host => data[:host]))
50
+ templateid = create(data)
51
+ end
52
+ templateid
53
+ end
54
+
55
+ # Analog Zabbix api call massUpdate
56
+ #
57
+ # * *Args* :
58
+ # - +data+ -> Hash with :hosts_id => [hostid1, hostid2 ...], and :templates_id => [templateid1, templateid2 ...]
59
+ # * *Returns* :
60
+ # - True or False
61
+ def mass_update(data)
62
+ result = @client.api_request(
63
+ :method => "template.massAdd",
64
+ :params => {
65
+ :hosts => data[:hosts_id].map { |t| {:hostid => t} },
66
+ :templates => data[:templates_id].map { |t| {:templateid => t} }
67
+ }
68
+ )
69
+ result.empty? ? false : true
70
+ end
71
+
72
+ # Analog Zabbix api call massAdd
73
+ #
74
+ # * *Args* :
75
+ # - +data+ -> Hash with :hosts_id => [hostid1, hostid2 ...], and :templates_id => [templateid1, templateid2 ...]
76
+ # * *Returns* :
77
+ # - True or False
78
+ def mass_add(data)
79
+ result = @client.api_request(
80
+ :method => "template.massAdd",
81
+ :params => {
82
+ :hosts => data[:hosts_id].map { |t| {:hostid => t} },
83
+ :templates => data[:templates_id].map { |t| {:templateid => t} }
84
+ }
85
+ )
86
+ result.empty? ? false : true
87
+ end
88
+
89
+ # Analog Zabbix api call massRemove
90
+ #
91
+ # * *Args* :
92
+ # - +data+ -> Hash with :hosts_id => [hostid1, hostid2 ...], and :templates_id => [templateid1, templateid2 ...]
93
+ # * *Returns* :
94
+ # - True or False
95
+ def mass_remove(data)
96
+ result = @client.api_request(
97
+ :method => "template.massRemove",
98
+ :params => {
99
+ :hostids => data[:hosts_id],
100
+ :templateids => data[:templates_id],
101
+ :groupids => data[:group_id],
102
+ :force => 1
103
+ }
104
+ )
105
+ result.empty? ? false : true
106
+ end
107
+
108
+ end
109
+ end
@@ -0,0 +1,67 @@
1
+ class ZabbixApi
2
+ class Triggers < Basic
3
+
4
+ def array_flag
5
+ true
6
+ end
7
+
8
+ def method_name
9
+ "trigger"
10
+ end
11
+
12
+ def indentify
13
+ "description"
14
+ end
15
+
16
+ def dump_by_id(data)
17
+ log "[DEBUG] Call dump_by_id with parametrs: #{data.inspect}"
18
+
19
+ @client.api_request(
20
+ :method => "trigger.get",
21
+ :params => {
22
+ :filter => {
23
+ key.to_sym => data[key.to_sym]
24
+ },
25
+ :output => "extend",
26
+ :select_items => "extend",
27
+ :select_functions => "extend"
28
+ }
29
+ )
30
+ end
31
+
32
+ def safe_update(data)
33
+ log "[DEBUG] Call update with parametrs: #{data.inspect}"
34
+
35
+ dump = {}
36
+ item_id = data[key.to_sym].to_i
37
+ dump_by_id(key.to_sym => data[key.to_sym]).each do |item|
38
+ dump = symbolize_keys(item) if item[key].to_i == data[key.to_sym].to_i
39
+ end
40
+
41
+ expression = dump[:items][0][:key_]+"."+dump[:functions][0][:function]+"("+dump[:functions][0][:parameter]+")"
42
+ dump[:expression] = dump[:expression].gsub(/{(\d*)}/,"{#{expression}}") #TODO ugly regexp
43
+ dump.delete(:functions)
44
+ dump.delete(:items)
45
+
46
+ old_expression = data[:expression]
47
+ data[:expression] = data[:expression].gsub(/{.*\:/,"{") #TODO ugly regexp
48
+ data.delete(:templateid)
49
+
50
+ log "[DEBUG] expression: #{dump[:expression]}\n data: #{data[:expression]}"
51
+
52
+ if hash_equals?(dump, data)
53
+ log "[DEBUG] Equal keys #{dump} and #{data}, skip update"
54
+ item_id
55
+ else
56
+ data[:expression] = old_expression
57
+ # disable old trigger
58
+ log "[DEBUG] disable :" + @client.api_request(:method => "#{method_name}.update", :params => [{:triggerid=> data[:triggerid], :status => "1" }]).inspect
59
+ # create new trigger
60
+ data.delete(:triggerid)
61
+ create(data)
62
+ end
63
+
64
+ end
65
+
66
+ end
67
+ end
@@ -0,0 +1,10 @@
1
+ class ZabbixApi
2
+ class Triggers < Basic
3
+
4
+ def create_or_update(data)
5
+ log "[DEBUG] Call create_or_update with parametrs: #{data.inspect}"
6
+ get_or_create(data)
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,82 @@
1
+ class ZabbixApi
2
+ class Usergroups < Basic
3
+
4
+ def array_flag
5
+ true
6
+ end
7
+
8
+ def method_name
9
+ "usergroup"
10
+ end
11
+
12
+ def key
13
+ "usrgrpid"
14
+ end
15
+
16
+ def indentify
17
+ "name"
18
+ end
19
+
20
+ # Delete UserGroup
21
+ #
22
+ # * *Args* :
23
+ # - +data+ -> Array with usrgrpids
24
+ # * *Returns* :
25
+ # - Nil or Integer
26
+ def delete(data)
27
+ result = @client.api_request(:method => "usergroup.delete", :params => data)
28
+ result ? result['usrgrpids'][0].to_i : nil
29
+ end
30
+
31
+
32
+ # Return usrgrpid
33
+ #
34
+ # * *Args* :
35
+ # - +data+ -> Hash with :name => "UserGroup"
36
+ # * *Returns* :
37
+ # - Integer
38
+ def get_or_create(data)
39
+ usrgrpid = get_id(data)
40
+ if usrgrpid.nil?
41
+ usrgrpid = create(data)
42
+ end
43
+ usrgrpid
44
+ end
45
+
46
+ # Set permission for usrgrp on some hostgroup
47
+ #
48
+ # * *Args* :
49
+ # - +data+ -> Hash with :usrgrpids => id, :hostgroupids => [], :permission => 2,3 (read and read write)
50
+ # * *Returns* :
51
+ # - Integer
52
+ def set_perms(data)
53
+ permission = data[:permission] || 2
54
+ result = @client.api_request(
55
+ :method => "usergroup.massAdd",
56
+ :params => {
57
+ :usrgrpids => [data[:usrgrpid]],
58
+ :rights => data[:hostgroupids].map { |t| {:permission => permission, :id => t} }
59
+ }
60
+ )
61
+ result ? result['usrgrpids'][0].to_i : nil
62
+ end
63
+
64
+ # Update usergroup, add user
65
+ #
66
+ # * *Args* :
67
+ # - +data+ -> Hash with :usrgrpids => id, :userids => []
68
+ # * *Returns* :
69
+ # - Integer
70
+ def add_user(data)
71
+ result = @client.api_request(
72
+ :method => "usergroup.massAdd",
73
+ :params => {
74
+ :usrgrpids => data[:usrgrpids],
75
+ :userids => data[:userids]
76
+ }
77
+ )
78
+ result ? result['usrgrpids'][0].to_i : nil
79
+ end
80
+
81
+ end
82
+ end
@@ -0,0 +1,32 @@
1
+ class ZabbixApi
2
+ class Users < Basic
3
+
4
+ def method_name
5
+ "user"
6
+ end
7
+
8
+ def keys
9
+ "userids"
10
+ end
11
+
12
+ def key
13
+ "userid"
14
+ end
15
+
16
+ def indentify
17
+ "name"
18
+ end
19
+
20
+ def add_medias(data)
21
+ result = @client.api_request(
22
+ :method => "user.addMedia",
23
+ :params => {
24
+ :users => data[:userids].map { |t| {:userid => t} },
25
+ :medias => data[:media]
26
+ }
27
+ )
28
+ result ? result['userids'][0].to_i : nil
29
+ end
30
+
31
+ end
32
+ end
@@ -1,3 +1,3 @@
1
1
  class ZabbixApi
2
- VERSION = "0.5.2"
2
+ VERSION = "0.5.3b1"
3
3
  end
metadata CHANGED
@@ -1,8 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zabbixapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
5
- prerelease:
4
+ version: 0.5.3b1
5
+ prerelease: 5
6
6
  platform: ruby
7
7
  authors:
8
8
  - Vasiliev D.V.
@@ -42,24 +42,42 @@ files:
42
42
  - README.md
43
43
  - Rakefile
44
44
  - lib/zabbixapi.rb
45
- - lib/zabbixapi/basic/basic_alias.rb
46
- - lib/zabbixapi/basic/basic_func.rb
47
- - lib/zabbixapi/basic/basic_init.rb
48
- - lib/zabbixapi/basic/basic_logic.rb
49
- - lib/zabbixapi/classes/applications.rb
50
- - lib/zabbixapi/classes/errors.rb
51
- - lib/zabbixapi/classes/graphs.rb
52
- - lib/zabbixapi/classes/hostgroups.rb
53
- - lib/zabbixapi/classes/hosts.rb
54
- - lib/zabbixapi/classes/items.rb
55
- - lib/zabbixapi/classes/mediatypes.rb
56
- - lib/zabbixapi/classes/screens.rb
57
- - lib/zabbixapi/classes/server.rb
58
- - lib/zabbixapi/classes/templates.rb
59
- - lib/zabbixapi/classes/triggers.rb
60
- - lib/zabbixapi/classes/unusable.rb
61
- - lib/zabbixapi/classes/usergroups.rb
62
- - lib/zabbixapi/classes/users.rb
45
+ - lib/zabbixapi/1.8/basic/basic_alias.rb
46
+ - lib/zabbixapi/1.8/basic/basic_func.rb
47
+ - lib/zabbixapi/1.8/basic/basic_init.rb
48
+ - lib/zabbixapi/1.8/basic/basic_logic.rb
49
+ - lib/zabbixapi/1.8/classes/applications.rb
50
+ - lib/zabbixapi/1.8/classes/errors.rb
51
+ - lib/zabbixapi/1.8/classes/graphs.rb
52
+ - lib/zabbixapi/1.8/classes/hostgroups.rb
53
+ - lib/zabbixapi/1.8/classes/hosts.rb
54
+ - lib/zabbixapi/1.8/classes/items.rb
55
+ - lib/zabbixapi/1.8/classes/mediatypes.rb
56
+ - lib/zabbixapi/1.8/classes/screens.rb
57
+ - lib/zabbixapi/1.8/classes/server.rb
58
+ - lib/zabbixapi/1.8/classes/templates.rb
59
+ - lib/zabbixapi/1.8/classes/triggers.rb
60
+ - lib/zabbixapi/1.8/classes/unusable.rb
61
+ - lib/zabbixapi/1.8/classes/usergroups.rb
62
+ - lib/zabbixapi/1.8/classes/users.rb
63
+ - lib/zabbixapi/2.0/basic/basic_alias.rb
64
+ - lib/zabbixapi/2.0/basic/basic_func.rb
65
+ - lib/zabbixapi/2.0/basic/basic_init.rb
66
+ - lib/zabbixapi/2.0/basic/basic_logic.rb
67
+ - lib/zabbixapi/2.0/classes/applications.rb
68
+ - lib/zabbixapi/2.0/classes/errors.rb
69
+ - lib/zabbixapi/2.0/classes/graphs.rb
70
+ - lib/zabbixapi/2.0/classes/hostgroups.rb
71
+ - lib/zabbixapi/2.0/classes/hosts.rb
72
+ - lib/zabbixapi/2.0/classes/items.rb
73
+ - lib/zabbixapi/2.0/classes/mediatypes.rb
74
+ - lib/zabbixapi/2.0/classes/screens.rb
75
+ - lib/zabbixapi/2.0/classes/server.rb
76
+ - lib/zabbixapi/2.0/classes/templates.rb
77
+ - lib/zabbixapi/2.0/classes/triggers.rb
78
+ - lib/zabbixapi/2.0/classes/unusable.rb
79
+ - lib/zabbixapi/2.0/classes/usergroups.rb
80
+ - lib/zabbixapi/2.0/classes/users.rb
63
81
  - lib/zabbixapi/client.rb
64
82
  - lib/zabbixapi/version.rb
65
83
  - spec/localhost.rb
@@ -80,13 +98,13 @@ required_ruby_version: !ruby/object:Gem::Requirement
80
98
  version: '0'
81
99
  segments:
82
100
  - 0
83
- hash: 1798577269349894886
101
+ hash: 4224378575819107482
84
102
  required_rubygems_version: !ruby/object:Gem::Requirement
85
103
  none: false
86
104
  requirements:
87
- - - ! '>='
105
+ - - ! '>'
88
106
  - !ruby/object:Gem::Version
89
- version: '0'
107
+ version: 1.3.1
90
108
  requirements: []
91
109
  rubyforge_project: zabbixapi
92
110
  rubygems_version: 1.8.24