Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zabbix-2.0 #9

Open
wants to merge 33 commits into
base: zabbix-2.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
f000d0c
Fix hostgroup for zabbix api 1.4
evtuhovich Dec 21, 2012
a9bb205
Support for API version 1.4
evtuhovich Dec 24, 2012
3d751c0
VersionError added, this version works only with 2.0.4
evtuhovich Jan 15, 2013
019140e
Host creation spec passes on Zabbix 2.0.4
evtuhovich Jan 15, 2013
6acc1ae
Templates fix (but not import) and host group fix
evtuhovich Jan 15, 2013
a6e0fa1
Merge branch 'zabbix-2.0' into zabbix2
evtuhovich Jan 16, 2013
962753e
Move database logic to database_helper, add postgresql and mysql support
evtuhovich Jan 16, 2013
4ae893a
Host specs fixed
evtuhovich Jan 16, 2013
45bde26
Application spec fixed
evtuhovich Jan 16, 2013
288769a
Connection spec fix
evtuhovich Jan 16, 2013
13a9458
Item spec fixed
evtuhovich Jan 16, 2013
5a4f71e
Fix users spec and response and connection specs
evtuhovich Jan 16, 2013
8a80f3b
Fix trigger and user_group specs
evtuhovich Jan 17, 2013
eb7a3ea
Fix spec from user macro
evtuhovich Jan 17, 2013
4170bec
Fix time series specs
evtuhovich Jan 17, 2013
45d4c62
Fix template.import
evtuhovich Jan 22, 2013
1b5ad8e
Fix host.update, now it works, but do not update interfaces
evtuhovich Jan 28, 2013
a677d76
template's ids should be unique
evtuhovich Jan 28, 2013
503fd4b
Add graph creation and spec for it
evtuhovich Feb 1, 2013
7d39e00
Screen and ScreenItem added
evtuhovich Feb 1, 2013
faefefb
Screen with nested ScreenItems now can be saved
evtuhovich Feb 4, 2013
d7938e0
Change screen_items and screen
evtuhovich Feb 4, 2013
9fed88d
Action creation added
evtuhovich Feb 8, 2013
6686b17
Fix validation, when value is Symbol
evtuhovich Feb 12, 2013
cdd1146
Support 2.0.5 zabbix version
evtuhovich Feb 19, 2013
d2ab561
Typo
evtuhovich Feb 19, 2013
bf16d49
Add save! method to any model like ActiveRecord one
evtuhovich Feb 20, 2013
647cb05
UserMacro returned in new format
evtuhovich Feb 25, 2013
d019a05
Bump version
evtuhovich Feb 25, 2013
8614685
Add delta and formula(params) to items
evtuhovich Feb 28, 2013
c5c55a4
Fix 2 bugs with screens, bump version
evtuhovich Mar 13, 2013
95c5f6a
Create GraphItem class
evtuhovich Mar 14, 2013
ed612b4
Fix bug with vsize in screen
evtuhovich Mar 14, 2013
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ TAGS*
a.out
nohop.out
spec/test.yml
tmp/
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ end
# Gems for testing and coverage
group :test do
gem 'simplecov', ">= 0.5", :platform => [:ruby_19], :require => false
gem 'pg'
end

# Gems you would use if hacking on this gem (rather than with it)
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.5.14
0.5.21
9 changes: 7 additions & 2 deletions lib/rubix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

module Rubix

SERVER_VERSIONS = ['2.0.4', '2.0.5'].freeze

autoload :Connection, 'rubix/connection'
autoload :Response, 'rubix/response'
autoload :Sender, 'rubix/sender'
Expand Down Expand Up @@ -77,7 +79,7 @@ def self.version
File.read(File.expand_path('../../VERSION', __FILE__)).chomp
rescue => e
'unknown'
end
end
end

# Base class for Rubix errors.
Expand All @@ -99,5 +101,8 @@ def self.version

# Given an incorrect argument.
ArgumentError = Class.new(Error)


# Incorrect version of Zabbix server
VersionError = Class.new(Error)

end
8 changes: 6 additions & 2 deletions lib/rubix/associations.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Rubix
module Associations

autoload :HasManyHosts, 'rubix/associations/has_many_hosts'
autoload :HasManyTemplates, 'rubix/associations/has_many_templates'
autoload :HasManyHostGroups, 'rubix/associations/has_many_host_groups'
Expand All @@ -11,15 +11,19 @@ module Associations
autoload :HasManyUserGroups, 'rubix/associations/has_many_user_groups'
autoload :HasManyConditions, 'rubix/associations/has_many_conditions'
autoload :HasManyInterfaces, 'rubix/associations/has_many_interfaces'
autoload :HasManyScreenItems, 'rubix/associations/has_many_screen_items'
autoload :HasManyGraphItems, 'rubix/associations/has_many_graph_items'
autoload :HasInventory, 'rubix/associations/has_inventory'

autoload :HasMessage, 'rubix/associations/has_message'

autoload :BelongsToHost, 'rubix/associations/belongs_to_host'
autoload :BelongsToTemplate, 'rubix/associations/belongs_to_template'
autoload :BelongsToItem, 'rubix/associations/belongs_to_item'
autoload :BelongsToAction, 'rubix/associations/belongs_to_action'
autoload :BelongsToUser, 'rubix/associations/belongs_to_user'
autoload :BelongsToUserGroup, 'rubix/associations/belongs_to_user_group'
autoload :BelongsToMediaType, 'rubix/associations/belongs_to_media_type'
autoload :BelongsToInterface, 'rubix/associations/belongs_to_interface'
end
end

32 changes: 32 additions & 0 deletions lib/rubix/associations/belongs_to_interface.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Rubix
module Associations

module BelongsToInterface

def interface= i
return unless i
@interface = i
@interface_id = i.id
end

def interface
return @interface if @host
return unless @interface_id
@interface = Interface.find(:id => @host_id)
end

def interface_id= hid
return unless hid
@interface_id = hid
end

def interface_id
return @interface_id if @host_id
return unless @interface
@interface_id = @host.id
end

end
end
end

3 changes: 0 additions & 3 deletions lib/rubix/associations/has_many_conditions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ def conditions= cs
c.kind_of?(Condition) ? c : Condition.new(c)
end
end

end
end
end


15 changes: 15 additions & 0 deletions lib/rubix/associations/has_many_graph_items.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Rubix
module Associations
module HasManyGraphItems

def graph_items= sis
@graph_items = sis
end

def graph_items
@graph_items || []
end
end
end
end

2 changes: 0 additions & 2 deletions lib/rubix/associations/has_many_interfaces.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,3 @@ def interface_params
end
end
end


15 changes: 15 additions & 0 deletions lib/rubix/associations/has_many_screen_items.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Rubix
module Associations
module HasManyScreenItems

def screen_items= sis
@screen_items = sis
end

def screen_items
@screen_items || []
end
end
end
end

11 changes: 4 additions & 7 deletions lib/rubix/associations/has_many_templates.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module Rubix
module Associations
module HasManyTemplates

def templates= hs
return unless hs
@templates = hs
@template_ids = hs.map(&:id)
end

def templates
return @templates if @templates
return unless @template_ids
Expand All @@ -18,7 +18,7 @@ def template_ids= tids
return unless tids
@template_ids = tids
end

def template_ids
return @template_ids if @template_ids
return unless @templates
Expand All @@ -27,11 +27,8 @@ def template_ids

def template_params
return [] unless template_ids
template_ids.map { |tid| { 'templateid' => tid } }
template_ids.uniq.map { |tid| { 'templateid' => tid } }
end

end
end
end


2 changes: 1 addition & 1 deletion lib/rubix/associations/has_many_user_groups.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def user_group_ids= ugids

def user_group_ids
return @user_group_ids if @user_group_ids
return unless @user_groups
return [] unless @user_groups
@user_group_ids = @user_groups.map(&:id)
end

Expand Down
15 changes: 15 additions & 0 deletions lib/rubix/associations/has_message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Rubix
module Associations
module HasMessage

def message
@message
end

def message= m
@message = m.kind_of?(Message) ? m : Message.new(m)
end
end
end
end

12 changes: 12 additions & 0 deletions lib/rubix/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class Connection
# @return [Rubix::Response] the last response from the Zabbix API -- useful for logging purposes
attr_reader :last_response

# @return [Hash] the last request to the Zabbix API -- useful for debugging purposes
attr_reader :last_request

# Set up a connection to a Zabbix API.
#
# The +uri_or_string+ can be either a string or a <tt>URI</tt>
Expand Down Expand Up @@ -74,6 +77,8 @@ def initialize uri_or_string, username=nil, password=nil
# @return [Rubix::Response]
def request method, params
authorize! unless authorized?
@last_request = {:method => method, :params => params}

response = till_response do
send_api_request :jsonrpc => "2.0",
:id => request_id,
Expand Down Expand Up @@ -114,6 +119,9 @@ def authorize!
raise AuthenticationError.new("Could not authenticate with Zabbix API at #{uri}: #{response.error_message}") if response.error?
raise AuthenticationError.new("Malformed response from Zabbix API: #{response.body}") unless response.string?
@auth = response.result

raise VersionError.new("Incorrect Zabbix Server API version #{api_version}, should be one of #{SERVER_VERSIONS.join(', ')}") unless SERVER_VERSIONS.include? api_version
@auth
end

# Set the URI for this connection's Zabbix API server.
Expand All @@ -134,6 +142,10 @@ def uri= uri_or_string
return @server
end

def api_version
@api_version ||= request('apiinfo.version', {}).result
end

protected

# The parameters used for constructing an authorization request
Expand Down
5 changes: 5 additions & 0 deletions lib/rubix/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ module Rubix
autoload :Medium, 'rubix/models/medium'
autoload :Interface, 'rubix/models/interface'
autoload :Inventory, 'rubix/models/inventory'
autoload :Graph, 'rubix/models/graph'
autoload :GraphItem, 'rubix/models/graph_item'
autoload :Screen, 'rubix/models/screen'
autoload :ScreenItem, 'rubix/models/screen_item'
autoload :Message, 'rubix/models/message'
end
8 changes: 4 additions & 4 deletions lib/rubix/models/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Action < Model

zabbix_attr :name, :required => true
zabbix_attr :event_source, :default => :triggers, :required => true
zabbix_attr :escalation_time, :default => 0
zabbix_attr :escalation_time, :default => 60
zabbix_attr :enabled, :default => true
zabbix_attr :message_subject, :default => MESSAGE_SUBJECT
zabbix_attr :message_body, :default => MESSAGE_BODY
Expand Down Expand Up @@ -91,7 +91,7 @@ def self.find_params options={}
end

def self.get_params
super().merge({:select_conditions => :refer, :select_operations => :refer})
super().merge({:selectConditions => :refer, :selectOperations => :refer})
end

def self.build action
Expand All @@ -107,8 +107,8 @@ def self.build action
:send_recovery_message => (action['recovery_msg'].to_i == 1),
:recovery_message_subject => action['r_shortdata'],
:recovery_message_body => action['r_longdata'],
:conditions => (action['conditions'] || []).map { |c| Condition.build(c) },
:operations => (action['operations'] || []).map { |o| Operation.build(o) }
:conditions => (action['conditions'] || []).map { |id, c| Condition.build(c) },
:operations => (action['operations'] || []).map { |id, o| Operation.build(o) }
})
end

Expand Down
65 changes: 65 additions & 0 deletions lib/rubix/models/graph.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
module Rubix
class Graph < Model
include Associations::HasManyGraphItems
#
# == Properties & Finding ==
#

def initialize properties={}
super(properties)
self.graph_items = properties[:graph_items].map { |gi| gi.is_a?(GraphItem) ? gi : GraphItem.new(gi) } if properties[:graph_items]
end

zabbix_attr :name, :required => true
zabbix_attr :width, :required => true
zabbix_attr :height, :required => true

#
# == Validation =
#

def validate
raise ValidationError.new("A graph must have at least one graph item.") if graph_items.nil? || graph_items.empty?
graph_items.each do |gi|
raise ValidationError.new("A graph item must have item_id property.") unless gi.item_id
raise ValidationError.new("A graph item must have color property.") unless gi.color
end
true
end

#
# == Requests ==
#

def create_params
{
:name => name, :height => height, :width => width,
:gitems => self.graph_items.map { |gi| gi.create_params }
}
end

def update_params
{ id_field => id, :name => name, :hosts => {:hostid => host_id}}
end

def self.find_params options={}
super().merge({
:selectGraphItems => 'extend',
:filter => {
:name => options[:name],
id_field => options[:id]
}
})
end

def self.build app
params = {
:id => app[id_field].to_i,
:name => app['name'],
:graph_items => (app['gitems'] || []).map { |id, gi| GraphItem.build gi }
}
new(params)
end
end
end

Loading