Permalink
Browse files

can create new hosts on zabbix

  • Loading branch information...
neerfri committed Dec 8, 2010
1 parent 11a72dc commit 66bbfabfdcb421e0ad6750547663f4fa2f53bb38
Showing with 299 additions and 184 deletions.
  1. +37 −2 lib/rzabbix.rb
  2. +15 −88 lib/rzabbix/base.rb
  3. +108 −0 lib/rzabbix/connection.rb
  4. +30 −34 lib/rzabbix/host.rb
  5. +42 −24 lib/rzabbix/host_group.rb
  6. +55 −0 lib/rzabbix/template.rb
  7. +0 −32 test.html
  8. +12 −4 test.rb
View
@@ -2,7 +2,42 @@ module RZabbix
end
-require 'rzabbix/version'
+require 'rzabbix/connection'
require 'rzabbix/base'
require 'rzabbix/host'
-require 'rzabbix/host_group'
+require 'rzabbix/host_group'
+require 'rzabbix/template'
+require 'rzabbix/version'
+
+class Hash
+
+ def rzbx_symbolize_keys
+ inject({}) do |options, (key, value)|
+ options[(key.to_sym rescue key) || key] = value
+ options
+ end
+ end
+
+
+ def rzbx_recursively_symbolize_keys
+ result = self.rzbx_symbolize_keys
+ result.keys.each do |key|
+ if result[key].is_a?(Hash) || result[key].is_a?(Array)
+ result[key] = result[key].rzbx_recursively_symbolize_keys
+ end
+ end
+ result
+ end
+end
+
+class Array
+ def rzbx_recursively_symbolize_keys
+ self.map do |item|
+ if item.is_a?(Hash) || item.is_a?(Array)
+ item.rzbx_recursively_symbolize_keys
+ else
+ item
+ end
+ end
+ end
+end
View
@@ -7,105 +7,32 @@ module RZabbix
class Base
- class ResponceCodeError < StandardError; end
-
- class << self
- attr_accessor :credentials
- attr_accessor :use_ssl
- attr_accessor :auth
- end
-
- self.use_ssl = false
-
- def self.set_credentials(api_url, api_user, api_password)
- self.credentials = {:api_url=>api_url, :api_user=>api_user, :api_password=>api_password}
+ def self.perform_request(*args)
+ Connection.perform_request(*args)
end
- def self.next_request_id
- @request_id = @request_id ? @request_id+1 : 1
- end
+ ###########################################################################################################
+ # Instance methods
+ ###########################################################################################################
- def self.auth
- @auth ||= begin
- auth_message = {
- 'auth' => nil,
- 'method' => 'user.authenticate',
- 'params' => {
- 'user' => Base.credentials[:api_user],
- 'password' => Base.credentials[:api_password],
- '0' => '0'
- }
- }
- do_request(auth_message)
- end
- end
+ attr_accessor :attributes
-
- def self.perform_request(action, params)
- message = message_for(self.resource_name, action, params)
- do_request(message)
+ def initialize(attributes)
+ super()
+ self.attributes = self.class.default_attributes.merge(attributes)
end
- def self.message_for(controller, action, params = {})
- {
- 'method' => "#{controller}.#{action}",
- 'params' => params,
- 'auth' => Base.auth
- }
+ def [](key)
+ self.attributes[key.to_sym]
end
- def self.do_request(message)
- id = next_request_id
-
- message['id'] = id
- message['jsonrpc'] = '2.0'
-
- message_json = JSON.generate(message)
-
- uri = URI.parse(Base.credentials[:api_url])
- http = Net::HTTP.new(uri.host, uri.port)
- if (use_ssl)
- http.use_ssl = true
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
- end
-
- request = Net::HTTP::Post.new(uri.request_uri)
- request.add_field('Content-Type', 'application/json-rpc')
- request.body=(message_json)
- responce = http.request(request)
-
- if ( responce.code != "200" ) then
- raise ResponceCodeError.new("Responce code from [" + credentials[:api_url] + "] is " + responce.code)
- end
-
- responce_body_hash = JSON.parse(responce.body)
-
- #if not ( responce_body_hash['id'] == id ) then
- # raise Zabbix::InvalidAnswerId.new("Wrong ID in zabbix answer")
- #end
-
-
- # Check errors in zabbix answer. If error exist - raise exception Zabbix::Error
- if ( error = responce_body_hash['error'] ) then
- error_message = error['message']
- error_data = error['data']
- error_code = error['code']
-
- e_message = "Code: [" + error_code.to_s + "]. Message: [" + error_message +\
- "]. Data: [" + error_data + "]."
-
- raise StandardError.new(e_message)
- end
-
- responce_body_hash['result']
+ def to_json(*args)
+ self.attributes.to_json(*args)
end
-
- def initialize(attributes)
- super()
- self.attributes = default_attributes.merge(attributes)
+ def to_hash
+ self.attributes
end
-
end
end
View
@@ -0,0 +1,108 @@
+module RZabbix
+
+ class Connection
+
+ API_OUTPUT_SHORTEN = "shorten"
+ API_OUTPUT_REFER = "refer"
+ API_OUTPUT_EXTEND = "extend"
+
+ class ResponseCodeError < StandardError; end
+
+ class << self
+ attr_accessor :credentials
+ attr_accessor :use_ssl
+ attr_accessor :auth
+ end
+
+ self.use_ssl = false
+
+ def self.set_credentials(api_url, api_user, api_password)
+ self.credentials = {:api_url=>api_url, :api_user=>api_user, :api_password=>api_password}
+ end
+
+ def self.next_request_id
+ @request_id = @request_id ? @request_id+1 : 1
+ end
+
+ def self.auth
+ @auth ||= begin
+ auth_message = {
+ 'auth' => nil,
+ 'method' => 'user.authenticate',
+ 'params' => {
+ 'user' => Connection.credentials[:api_user],
+ 'password' => Connection.credentials[:api_password],
+ '0' => '0'
+ }
+ }
+ do_request(auth_message)
+ end
+ end
+
+
+ def self.perform_request(controller, action, params)
+ message = message_for(controller, action, params)
+ do_request(message)
+ end
+
+ def self.message_for(controller, action, params = {})
+ {
+ 'method' => "#{controller}.#{action}",
+ 'params' => params.to_hash.merge(:output=>API_OUTPUT_EXTEND),
+ 'auth' => Connection.auth
+ }
+ end
+
+ def self.do_request(message)
+ id = next_request_id
+
+ message['id'] = id
+ message['jsonrpc'] = '2.0'
+
+ message_json = JSON.generate(message)
+
+ #puts "#{JSON.parse(message_json).inspect}\n\n\n"
+
+ uri = URI.parse(Connection.credentials[:api_url])
+ http = Net::HTTP.new(uri.host, uri.port)
+ if (use_ssl)
+ http.use_ssl = true
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
+ end
+
+ request = Net::HTTP::Post.new(uri.request_uri)
+ request.add_field('Content-Type', 'application/json-rpc')
+ request.body=(message_json)
+ responce = http.request(request)
+
+ if ( responce.code != "200" ) then
+ raise ResponseCodeError.new("Responce code from [" + credentials[:api_url] + "] is " + responce.code)
+ end
+
+ responce_body_hash = JSON.parse(responce.body)
+
+ #if not ( responce_body_hash['id'] == id ) then
+ # raise Zabbix::InvalidAnswerId.new("Wrong ID in zabbix answer")
+ #end
+
+
+ # Check errors in zabbix answer. If error exist - raise exception Zabbix::Error
+ if ( error = responce_body_hash['error'] ) then
+ error_message = error['message']
+ error_data = error['data']
+ error_code = error['code']
+
+ e_message = "Code: [" + error_code.to_s + "]. Message: [" + error_message +\
+ "]. Data: [" + error_data + "]."
+
+ raise StandardError.new(e_message)
+ end
+
+ result = responce_body_hash['result']
+ result.respond_to?(:rzbx_recursively_symbolize_keys) ? result.rzbx_recursively_symbolize_keys : result
+ end
+
+
+ end
+
+end
View
@@ -2,49 +2,30 @@ module RZabbix
class Host < Base
- attr_accessor :attributes
-
- # attr_accessor :hostid
- # attr_accessor :host
- # attr_accessor :port
- # attr_accessor :status
- # attr_accessor :useip
- # attr_accessor :dns
- # attr_accessor :ip
- # attr_accessor :proxy_hostid
- # attr_accessor :useipmi
- # attr_accessor :ipmi_ip
- # attr_accessor :ipmi_port
- # attr_accessor :ipmi_authtype
- # attr_accessor :ipmi_privilege
- # attr_accessor :ipmi_username
- # attr_accessor :ipmi_password
-
+ def self.find(host_id)
+ hosts = perform_request(:host, :get, :hostids=>[host_id], :output=>"extend")
+ self.new(hosts.first.rzbx_recursively_symbolize_keys) unless hosts.empty?
+ end
def self.find_host_id_by_hostname(hostname)
- hosts = perform_request(:get, 'filter' => {'host'=>hostname})
- hosts.first["hostid"]
+ hosts = perform_request(:host, :get, :filter => {:host=>hostname})
+ hosts.first[:hostid]
end
def self.find_by_hostname(hostname)
- #host_id = find_host_id_by_hostname(hostname)
- hosts = perform_request(:get, 'filter' => {'host'=>hostname}, "output"=>"extend")
+ hosts = perform_request(:host, :get, :filter => {:host=>hostname}, :output=>"extend")
self.new(hosts.first) unless hosts.empty?
end
def self.create(attributes)
- groups = attributes.delete(:groups)
- hosts = perform_request(:create, "host data"=>attributes, "groups"=>groups)
- self.new(attributes)
- end
-
- def self.resource_name
- "host"
+ host = self.new(attributes)
+ result = perform_request(:host, :create, host)
+ result && result[:hostids] ? self.find(result[:hostids].first) : nil
end
- def default_attributes
- {
- :hostid=>nil, #int Host ID
+ def self.default_attributes
+ {
+ #:hostid=>nil, #int Host ID
:host=>nil, #string Host name.
:port=>10050, #int Port number.
:status=>0, #int Host Status.
@@ -57,11 +38,26 @@ def default_attributes
:ipmi_port=>623, #int IPMI port.
:ipmi_authtype=>0, #int IPMI authentication type.
:ipmi_privilege=>0, #int IPMI privilege.
- :ipmi_username=>'', #string IPMI username.
- :ipmi_password=>'' #string IPMI password.
+ :ipmi_username=>'', #string IPMI username.
+ :ipmi_password=>'' #string IPMI password.
}
end
+ def to_hash(*args)
+ host_hash = self.attributes.inject({}) do |hash, (attr, v)|
+ case attr.to_sym
+ when :groups
+ hash[:groups] = v.map {|g| { :groupid=>g.attributes[:groupid]}}
+ when :templates
+ hash[:templates] = v.map {|t| {:templateid=>t.attributes[:templateid]}}
+ else
+ hash[attr] = v
+ end
+ hash
+ end
+ host_hash
+ end
+
end
end
Oops, something went wrong.

0 comments on commit 66bbfab

Please sign in to comment.