zabbixapi 0.5.0b3 → 0.5.0b4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -3,7 +3,6 @@
3
3
  Simple and lightweight ruby module for work with zabbix api
4
4
 
5
5
  [![Build Status](https://travis-ci.org/vadv/zabbixapi.png)](https://travis-ci.org/vadv/zabbixapi)
6
- [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/vadv/zabbixapi)
7
6
 
8
7
  #####Now worked with zabbix
9
8
  * 1.8.2 (api version 1.2)
data/lib/zabbixapi.rb CHANGED
@@ -1,6 +1,5 @@
1
- require "zabbixapi/basic"
2
- require "zabbixapi/client"
3
1
  require "zabbixapi/version"
2
+ require "zabbixapi/client"
4
3
  require "zabbixapi/server"
5
4
  require "zabbixapi/applications"
6
5
  require "zabbixapi/templates"
@@ -17,65 +16,47 @@ require "zabbixapi/mediatypes"
17
16
  class ZabbixApi
18
17
 
19
18
  attr :client
19
+ attr :server
20
+ attr :users
21
+ attr :items
22
+ attr :applications
23
+ attr :templates
24
+ attr :hostgroups
25
+ attr :hosts
26
+ attr :triggers
27
+ attr :graphs
28
+ attr :screens
29
+ attr :usergroups
30
+ attr :mediatypes
20
31
 
21
32
  def self.connect(options = {})
22
33
  new(options)
23
34
  end
24
35
 
36
+ def self.current
37
+ @current ||= ZabbixApi.new
38
+ end
39
+
25
40
  def query(data)
26
41
  @client.api_request(:method => data[:method], :params => data[:params])
27
42
  end
28
43
 
29
44
  def initialize(options = {})
45
+
30
46
  @client = Client.new(options)
31
- end
32
-
33
- def server
34
- @server ||= Server.new(@client)
35
- end
36
-
37
- def users
38
- @users ||= Users.new(@client)
39
- end
40
-
41
- def items
42
- @items ||= Items.new(@client)
43
- end
44
-
45
- def hosts
46
- @hosts ||= Hosts.new(@client)
47
- end
48
-
49
- def applications
50
- @applications ||= Applications.new(@client)
51
- end
52
-
53
- def templates
54
- @templates ||= Templates.new(@client)
55
- end
56
-
57
- def hostgroups
58
- @hostgroups ||= HostGroups.new(@client)
59
- end
60
-
61
- def triggers
62
- @triggers ||= Triggers.new(@client)
63
- end
64
-
65
- def graphs
66
- @graphs ||= Graphs.new(@client)
67
- end
68
-
69
- def screens
70
- @screens ||= Screens.new(@client)
71
- end
72
-
73
- def usergroups
74
- @usergroups ||= Usergroups.new(@client)
75
- end
76
47
 
77
- def mediatypes
78
- @mediatypes ||= Mediatypes.new(@client)
48
+ @server = Server.new(@client)
49
+ @users = Users.new(@client)
50
+ @items = Items.new(@client)
51
+ @hosts = Hosts.new(@client)
52
+ @applications = Applications.new(@client)
53
+ @templates = Templates.new(@client)
54
+ @hostgroups = HostGroups.new(@client)
55
+ @triggers = Triggers.new(@client)
56
+ @graphs = Graphs.new(@client)
57
+ @screens = Screens.new(@client)
58
+ @usergroups = Usergroups.new(@client)
59
+ @mediatypes = Mediatypes.new(@client)
79
60
  end
80
61
 
81
62
  end
@@ -1,24 +1,44 @@
1
1
  class ZabbixApi
2
- class Applications < Basic
2
+ class Applications
3
3
 
4
- def api_method_name
5
- "application"
4
+ def initialize(client)
5
+ @client = client
6
6
  end
7
7
 
8
- def api_identify
9
- "name"
8
+ def create(data)
9
+ result = @client.api_request(:method => "application.create", :params => [data])
10
+ result.empty? ? nil : result['applicationids'][0].to_i
10
11
  end
11
12
 
12
- def create(data)
13
- create_array(data)
13
+ def add(data)
14
+ create(data)
14
15
  end
15
16
 
16
17
  def delete(data)
17
- delete_array(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)
18
31
  end
19
32
 
20
33
  def get_full_data(data)
21
- get_full_data_filter(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
22
42
  end
23
43
 
24
44
  end
@@ -46,10 +46,10 @@ class ZabbixApi
46
46
 
47
47
  def http_request(body)
48
48
  uri = URI.parse(@options[:url])
49
- if @proxy_uri.nil?
50
- http = Net::HTTP.Proxy(@proxy_host, @proxy_port, @proxy_user, @proxy_pass).new(uri.host, uri.port)
51
- else
49
+ unless @proxy_uri.nil?
52
50
  http = Net::HTTP.new(uri.host, uri.port)
51
+ else
52
+ http = Net::HTTP.Proxy(@proxy_host, @proxy_port, @proxy_user, @proxy_pass).new(uri.host, uri.port)
53
53
  end
54
54
  request = Net::HTTP::Post.new(uri.request_uri)
55
55
  request.add_field('Content-Type', 'application/json-rpc')
@@ -1,17 +1,17 @@
1
1
  class ZabbixApi
2
- class Graphs < Basic
2
+ class Graphs
3
3
 
4
-
5
- def api_method_name
6
- "graph"
4
+ def initialize(client)
5
+ @client = client
7
6
  end
8
7
 
9
- def api_identify
10
- "name"
8
+ def create(data)
9
+ result = @client.api_request(:method => "graph.create", :params => [data])
10
+ result.empty? ? nil : result['graphids'][0].to_i
11
11
  end
12
12
 
13
- def create(data)
14
- create_array(data)
13
+ def add(data)
14
+ create(data)
15
15
  end
16
16
 
17
17
  def delete(data)
@@ -24,8 +24,12 @@ class ZabbixApi
24
24
  end
25
25
  end
26
26
 
27
+ def destroy(data)
28
+ delete(data)
29
+ end
30
+
27
31
  def get_full_data(data)
28
- get_full_data_filter()
32
+ @client.api_request(:method => "graph.get", :params => {:search => {:name => data}, :output => "extend"})
29
33
  end
30
34
 
31
35
  def get_ids_by_host(data)
@@ -1,28 +1,61 @@
1
1
  class ZabbixApi
2
- class HostGroups < Basic
2
+ class HostGroups
3
3
 
4
- def api_method_name
5
- "hostgroup"
4
+ def initialize(client)
5
+ @client = client
6
6
  end
7
7
 
8
- def api_identify
9
- "name"
8
+ def create(data)
9
+ result = @client.api_request(:method => "hostgroup.create", :params => [data])
10
+ result.empty? ? nil : result['groupids'][0].to_i
10
11
  end
11
12
 
12
- def api_key
13
- "groupid"
13
+ def add(data)
14
+ create(data)
14
15
  end
15
16
 
16
- def create(data)
17
- create_array(data)
17
+ def delete(data)
18
+ result = @client.api_request(:method => "hostgroup.delete", :params => [:groupid => data])
19
+ result.empty? ? nil : result['groupids'][0].to_i
18
20
  end
19
21
 
20
- def delete(data)
21
- delete_array_sym(data)
22
+ def destroy(data)
23
+ delete(data)
24
+ end
25
+
26
+ def get_or_create(data)
27
+ unless hostgroupid = get_id(data)
28
+ hostgroupid = update(data)
29
+ end
30
+ hostgroupid
22
31
  end
23
32
 
24
33
  def get_full_data(data)
25
- get_full_data_filter(data)
34
+ case @client.api_version
35
+ when "1.2"
36
+ @client.api_request(:method => "hostgroup.get", :params => {:filter => data, :output => "extend"})
37
+ else
38
+ @client.api_request(:method => "hostgroup.get", :params => {:filter => data, :output => "extend"})
39
+ end
40
+ end
41
+
42
+ # Return all hostgroups
43
+ #
44
+ # * *Returns* :
45
+ # - Hash with {"Hostgroup1" => "id1", "Hostgroup2" => "id2"}
46
+ def all
47
+ result = {}
48
+ @client.api_request(:method => "hostgroup.get", :params => {:output => "extend"}).each do |hostgrp|
49
+ result[hostgrp['name']] = hostgrp['groupid']
50
+ end
51
+ result
52
+ end
53
+
54
+ def get_id(data)
55
+ result = get_full_data(data)
56
+ hostgroupid = nil
57
+ result.each { |hgroup| hostgroupid = hgroup['groupid'].to_i if hgroup['name'] == data[:name] }
58
+ hostgroupid
26
59
  end
27
60
 
28
61
  end
@@ -1,13 +1,5 @@
1
1
  class ZabbixApi
2
- class Hosts < Basic
3
-
4
- def api_method_name
5
- "host"
6
- end
7
-
8
- def api_identify
9
- "host"
10
- end
2
+ class Hosts
11
3
 
12
4
  def initialize(client)
13
5
  @client = client
@@ -40,6 +32,10 @@ class ZabbixApi
40
32
  result.empty? ? nil : result['hostids'][0].to_i
41
33
  end
42
34
 
35
+ def add(data)
36
+ create(data)
37
+ end
38
+
43
39
  def unlink_templates(data)
44
40
  result = @client.api_request(
45
41
  :method => "host.massRemove",
@@ -57,12 +53,33 @@ class ZabbixApi
57
53
  end
58
54
 
59
55
  def delete(data)
60
- delete_array_sym(data)
56
+ result = @client.api_request(:method => "host.delete", :params => [:hostid => data])
57
+ result.empty? ? nil : result['hostids'][0].to_i
61
58
  end
62
59
 
60
+ def destroy(data)
61
+ delete(data)
62
+ end
63
+
64
+ def update(data)
65
+ result = @client.api_request(:method => "host.update", :params => data)
66
+ result.empty? ? nil : result['hostids'][0].to_i
67
+ end
63
68
 
64
69
  def get_full_data(data)
65
- get_full_data_filter(data)
70
+ @client.api_request(:method => "host.get", :params => {:filter => data, :output => "extend"})
71
+ end
72
+
73
+ def create_or_update(data)
74
+ hostid = get_id(:host => data[:host])
75
+ hostid ? update(data.merge(:hostid => hostid)) : create(data)
76
+ end
77
+
78
+ def get_id(data)
79
+ result = get_full_data(data)
80
+ hostid = nil
81
+ result.each { |host| hostid = host['hostid'].to_i if host['host'] == data[:host] }
82
+ hostid
66
83
  end
67
84
 
68
85
  end
@@ -1,13 +1,5 @@
1
1
  class ZabbixApi
2
- class Items < Basic
3
-
4
- def api_method_name
5
- "item"
6
- end
7
-
8
- def api_identify
9
- "description"
10
- end
2
+ class Items
11
3
 
12
4
  def initialize(client)
13
5
  @client = client
@@ -55,14 +47,40 @@ class ZabbixApi
55
47
  def create(data)
56
48
  result = @client.api_request(:method => "item.create", :params => [merge_params(data)] )
57
49
  result.empty? ? nil : result['itemids'][0].to_i
58
- end
50
+ end
51
+
52
+ def add(data)
53
+ create(data)
54
+ end
59
55
 
60
56
  def get_full_data(data)
61
- get_full_data_filter(data)
57
+ @client.api_request(:method => "item.get", :params => {:filter => data, :output => "extend"})
58
+ end
59
+
60
+ def get_id(data)
61
+ result = get_full_data(data)
62
+ itemid = nil
63
+ result.each { |item| itemid = item['itemid'].to_i if item['name'] == data[:name] }
64
+ itemid
65
+ end
66
+
67
+ def create_or_update(data)
68
+ itemid = get_id(:description => data[:description], :hostid => data[:hostid])
69
+ itemid ? update(data.merge(:itemid => itemid)) : create(data)
70
+ end
71
+
72
+ def update(data)
73
+ result = @client.api_request(:method => "item.update", :params => data)
74
+ result.empty? ? nil : result['itemids'][0].to_i
62
75
  end
63
76
 
64
77
  def delete(data)
65
- delete_array(data)
78
+ result = @client.api_request(:method => "item.delete", :params => [data])
79
+ result.empty? ? nil : result['itemids'][0].to_i
80
+ end
81
+
82
+ def destroy(data)
83
+ delete(data)
66
84
  end
67
85
 
68
86
  end
@@ -1,28 +1,61 @@
1
1
  class ZabbixApi
2
- class Mediatypes < Basic
2
+ class Mediatypes
3
3
 
4
- #:description => "", #Name
5
- #:type => 0, #0 - Email, 1 - External script, 2 - SMS, 3 - Jabber, 100 - EzTexting
6
- #:smtp_server => "",
7
- #:smtp_helo => "",
8
- #:smtp_email => "", #Email address of Zabbix server
9
- #:exec_path => "", #Name of external script
10
- #:gsm_modem => "", #Serial device name of GSM modem
11
- #:username => "", #Jabber user name used by Zabbix server
12
- #:passwd => "", #Jabber password used by Zabbix server
4
+ def initialize(client)
5
+ @client = client
6
+ @default_mediatype_options = {
7
+ :description => "", #Name
8
+ :type => 0, #0 - Email, 1 - External script, 2 - SMS, 3 - Jabber, 100 - EzTexting
9
+ :smtp_server => "",
10
+ :smtp_helo => "",
11
+ :smtp_email => "", #Email address of Zabbix server
12
+ :exec_path => "", #Name of external script
13
+ :gsm_modem => "", #Serial device name of GSM modem
14
+ :username => "", #Jabber user name used by Zabbix server
15
+ :passwd => "" #Jabber password used by Zabbix server
16
+ }
17
+ end
13
18
 
14
- def api_method_name
15
- "mediatype"
19
+ # Create MediaType
20
+ #
21
+ # * *Args* :
22
+ # - +data+ -> Hash with :description => "MediaGroup" and mediatype options
23
+ # * *Returns* :
24
+ # - Nil or Integer
25
+ def create(data)
26
+ result = @client.api_request(:method => "mediatype.create", :params => data)
27
+ result ? result['mediatypeids'][0].to_i : nil
16
28
  end
17
29
 
18
- def api_identify
19
- "name"
30
+ # Add MediaType
31
+ # Synonym create
32
+ def add(data)
33
+ create(data)
20
34
  end
21
35
 
36
+ # Delete MediaType
37
+ #
38
+ # * *Args* :
39
+ # - +data+ -> Array with mediatypeids
40
+ # * *Returns* :
41
+ # - Nil or Integer
22
42
  def delete(data)
23
- delete_array(data)
43
+ result = @client.api_request(:method => "mediatype.delete", :params => [data])
44
+ result ? result['mediatypeids'][0].to_i : nil
45
+ end
46
+
47
+ # Destroy MediaType
48
+ # Synonym delete
49
+ def destroy(data)
50
+ delete(data)
24
51
  end
25
52
 
53
+ # Get MediaType info
54
+ #
55
+ # * *Args* :
56
+ # - +data+ -> Hash with :description => "MediaType"
57
+ # * *Returns* :
58
+ # - Nil or Integer
26
59
  def get_full_data(data)
27
60
  @client.api_request(
28
61
  :method => "mediatype.get",
@@ -33,5 +66,44 @@ class ZabbixApi
33
66
  )
34
67
  end
35
68
 
69
+ def get(data)
70
+ get_full_data(data)
71
+ end
72
+
73
+ # Return MediaTypeid
74
+ #
75
+ # * *Args* :
76
+ # - +data+ -> Hash with :description => "MediaType"
77
+ # * *Returns* :
78
+ # - Nil or Integer
79
+ def get_id(data)
80
+ result = get_full_data(data)
81
+ mediatypeid = nil
82
+ result.each { |mt| mediatypeid = mt['mediatypeid'].to_i if mt['description'] == data[:description] }
83
+ mediatypeid
84
+ end
85
+
86
+ # Return MediaTypeid
87
+ #
88
+ # * *Args* :
89
+ # - +data+ -> Hash with :description => "MediaType"
90
+ # * *Returns* :
91
+ # - Nil or Integer
92
+ def update(data)
93
+ result = @client.api_request(:method => "mediatype.update", :params => data)
94
+ result.empty? ? nil : result['mediatypeids'][0].to_i
95
+ end
96
+
97
+ # Return MediaTypeid
98
+ #
99
+ # * *Args* :
100
+ # - +data+ -> Hash with :name => "UserGroup"
101
+ # * *Returns* :
102
+ # - Integer
103
+ def create_or_update(data)
104
+ mediatypeid = get_id(:description => data[:description], :mediatypeid => data[:mediatypeid])
105
+ mediatypeid ? update(data.merge(:mediatypeid => mediatypeid)) : create(data)
106
+ end
107
+
36
108
  end
37
109
  end