zabbixapi 0.5.0 → 0.5.1a

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml CHANGED
@@ -3,7 +3,7 @@ language:
3
3
  rvm:
4
4
  - 1.9.3
5
5
  before_script:
6
- - sudo apt-get install mysql-server zabbix-server-mysql zabbix-frontend-php
6
+ - sudo apt-get update; sudo apt-get install mysql-server zabbix-server-mysql zabbix-frontend-php
7
7
  - mysql -u root -e "use zabbix; insert into users_groups(usrgrpid, userid) values(10,1);"
8
8
  - echo "$(curl -fsSL https://raw.github.com/gist/8f0df0d1824ea25a3827/zabbix.php)" | sudo tee /usr/share/zabbix/conf/zabbix.conf.php
9
9
  script: "bundle exec rspec spec/run.rb"
data/Gemfile CHANGED
@@ -1,4 +1,5 @@
1
1
  source :rubygems
2
2
 
3
3
  gem "rspec"
4
- gem "rake"
4
+ gem "rake"
5
+ gem "json"
data/Gemfile.lock CHANGED
@@ -2,6 +2,7 @@ GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
4
  diff-lcs (1.1.3)
5
+ json (1.6.1)
5
6
  rake (0.9.2.2)
6
7
  rspec (2.12.0)
7
8
  rspec-core (~> 2.12.0)
@@ -16,5 +17,6 @@ PLATFORMS
16
17
  ruby
17
18
 
18
19
  DEPENDENCIES
20
+ json
19
21
  rake
20
22
  rspec
data/README.md CHANGED
@@ -24,6 +24,14 @@ zbx = ZabbixApi.connect(
24
24
  :user => 'Admin',
25
25
  :password => 'zabbix'
26
26
  )
27
+ # use basic_auth
28
+ zbx = ZabbixApi.connect(
29
+ :url => 'http://localhost/zabbix/api_jsonrpc.php',
30
+ :user => 'Admin',
31
+ :password => 'zabbix',
32
+ :http_password => 'bla-bla',
33
+ :http_user => 'bla-bla'
34
+ )
27
35
  ```
28
36
  ### Create Hostgroup
29
37
  ```ruby
data/lib/zabbixapi.rb CHANGED
@@ -1,17 +1,10 @@
1
1
  require "zabbixapi/version"
2
2
  require "zabbixapi/client"
3
- require "zabbixapi/server"
4
- require "zabbixapi/applications"
5
- require "zabbixapi/templates"
6
- require "zabbixapi/hostgroups"
7
- require "zabbixapi/users"
8
- require "zabbixapi/hosts"
9
- require "zabbixapi/triggers"
10
- require "zabbixapi/items"
11
- require "zabbixapi/graphs"
12
- require "zabbixapi/screens"
13
- require "zabbixapi/usergroups"
14
- require "zabbixapi/mediatypes"
3
+
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
+
15
8
 
16
9
  class ZabbixApi
17
10
 
@@ -57,6 +50,7 @@ class ZabbixApi
57
50
  @screens = Screens.new(@client)
58
51
  @usergroups = Usergroups.new(@client)
59
52
  @mediatypes = Mediatypes.new(@client)
53
+
60
54
  end
61
55
 
62
56
  end
@@ -0,0 +1,17 @@
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
+ end
17
+ end
@@ -0,0 +1,41 @@
1
+ class Hash
2
+ def deep_include?(sub_hash)
3
+ sub_hash.keys.all? do |key|
4
+ self.has_key?(key) && if sub_hash[key].is_a?(Hash)
5
+ self[key].is_a?(Hash) && self[key].deep_include?(sub_hash[key])
6
+ else
7
+ self[key] == sub_hash[key]
8
+ end
9
+ end
10
+ end
11
+ end
12
+
13
+ class ZabbixApi
14
+ class Basic
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
+ return obj
20
+ end
21
+
22
+ def parse_keys(data)
23
+ case data
24
+ when Hash
25
+ data.empty? ? nil : data[keys][0].to_i
26
+ when TrueClass
27
+ true
28
+ when FalseClass
29
+ false
30
+ else
31
+ nil
32
+ end
33
+ end
34
+
35
+ def merge_params(params)
36
+ result = JSON.generate(default_options).to_s + "," + JSON.generate(params).to_s
37
+ JSON.parse(result.gsub('},{', ','))
38
+ end
39
+
40
+ end
41
+ 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,70 @@
1
+ class ZabbixApi
2
+ class Basic
3
+
4
+ def create(data)
5
+ data_with_default = default_options.empty? ? data : merge_params(data)
6
+ data_create = array_flag ? [data_with_default] : data_with_default
7
+ result = @client.api_request(:method => "#{method_name}.create", :params => data_create)
8
+ parse_keys result
9
+ end
10
+
11
+ def delete(data)
12
+ data_delete = array_flag ? [data] : [key.to_sym => data]
13
+ result = @client.api_request(:method => "#{method_name}.delete", :params => data_delete)
14
+ parse_keys result
15
+ end
16
+
17
+ def create_or_update(data)
18
+ id = get_id(indentify.to_sym => data[indentify.to_sym])
19
+ id ? update(data.merge(key.to_sym => id.to_s)) : create(data)
20
+ end
21
+
22
+ def update(data)
23
+ dump = {}
24
+ get_full_data(data).each do |item|
25
+ dump = symbolize_keys(item) if item[key].to_i == data[key.to_sym].to_i
26
+ end
27
+ unless dump.deep_include?(data)
28
+ result = @client.api_request(:method => "#{method_name}.update", :params => data)
29
+ parse_keys result
30
+ else
31
+ data[key.to_sym].to_i
32
+ end
33
+ end
34
+
35
+ def get_full_data(data)
36
+ @client.api_request(
37
+ :method => "#{method_name}.get",
38
+ :params => {
39
+ :filter => {
40
+ indentify.to_sym => data[indentify.to_sym]
41
+ },
42
+ :output => "extend"
43
+ }
44
+ )
45
+ end
46
+
47
+ def all
48
+ result = {}
49
+ @client.api_request(:method => "#{method_name}.get", :params => {:output => "extend"}).each do |item|
50
+ result[item[indentify]] = item[key]
51
+ end
52
+ result
53
+ end
54
+
55
+ def get_id(data)
56
+ result = get_full_data(data)
57
+ id = nil
58
+ result.each { |item| id = item[key].to_i if item[indentify] == data[indentify.to_sym] }
59
+ id
60
+ end
61
+
62
+ def get_or_create(data)
63
+ unless id = get_id(data)
64
+ id = create(data)
65
+ end
66
+ id
67
+ end
68
+
69
+ end
70
+ end
File without changes
@@ -0,0 +1,40 @@
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_ids_by_host(data)
17
+ ids = []
18
+ result = @client.api_request(:method => "graph.get", :params => {:filter => {:host => data[:host]}, :output => "extend"})
19
+ result.each do |graph|
20
+ ids << graph['graphid']
21
+ end
22
+ ids
23
+ end
24
+
25
+ def get_items(data)
26
+ @client.api_request(:method => "graphitem.get", :params => { :graphids => [data], :output => "extend" } )
27
+ end
28
+
29
+ def create_or_update(data)
30
+ graphid = get_id(:name => data[:name], :templateid => data[:templateid])
31
+ graphid ? _update(data.merge(:graphid => graphid)) : create(data)
32
+ end
33
+
34
+ def _update(data)
35
+ data.delete(:name)
36
+ update(data)
37
+ end
38
+
39
+ end
40
+ 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,58 @@
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
+ case @client.api_version
45
+ when "1.2"
46
+ result ? true : false
47
+ else
48
+ result.empty? ? false : true
49
+ end
50
+ end
51
+
52
+ def create_or_update(data)
53
+ hostid = get_id(:host => data[:host])
54
+ hostid ? update(data.merge(:hostid => hostid)) : create(data)
55
+ end
56
+
57
+ end
58
+ 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
+ :description => 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