activerecord-zabbix 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ activerecord-zabbix
2
+ ===================
3
+
4
+ This is not a rails app.
5
+
6
+ Although some attempt `may be` (later) made to facilitate this integrating seamlessly into one.
7
+
8
+ Quick Start
9
+ -----------
10
+
11
+ `gem install activerecord-zabbix` # once published... (to rubygems-org)
12
+
13
+ <pre>
14
+
15
+ require 'activerecord-zabbix'
16
+
17
+ CONFIG = {
18
+ :adapter => 'postgresql', # or whatever
19
+ :host => 'your.zabbix.db.server',
20
+ :database => 'zabbix',
21
+ :username => 'username',
22
+ :password => 'passrod'
23
+ }
24
+
25
+ Zabbix::connect( CONFIG )
26
+
27
+ Zabbix::Host.find_by_dns( 'monitored.server.com' ).items.each do |item|
28
+
29
+ #
30
+ # all monitored item(s) for the given host
31
+ #
32
+ puts item.inspect
33
+
34
+ end
35
+
36
+ </pre>
37
+
38
+
39
+ [Schema Diagram](https://docs.google.com/drawings/d/12sIp0gAaoAlLOVluyiSLSFYhTiuw8txoUL67BDUVxvc/edit)
@@ -0,0 +1,50 @@
1
+ $LOAD_PATH.unshift 'lib'
2
+
3
+ require 'date'
4
+ require 'zabbix/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+
8
+ spec.name = GEM_NAME
9
+ spec.version = MODULE_NAME::Version
10
+ spec.date = DateTime.now.strftime( "%Y-%m-%d" )
11
+ spec.authors = ["","","","Richard"]
12
+ spec.email = %q{ops@clue.co.za}
13
+ spec.summary = %q{ActiveRecord access into Zabbix}
14
+ spec.homepage = %q{https://github.com/cluetechnologies/activerecord-zabbix}
15
+ spec.description = %q{This is not a rails app.}
16
+
17
+ spec.add_runtime_dependency 'activerecord'
18
+
19
+ spec.files = [
20
+
21
+ #
22
+ # du -a lib/ spec/ README* *.gemspec | awk '{print($2)}' | while read file; do echo "'$file',"; done
23
+ #
24
+
25
+ 'lib//activerecord-zabbix.rb',
26
+ 'lib//boot.rb',
27
+ 'lib//zabbix/connect.rb',
28
+ 'lib//zabbix/function.rb',
29
+ 'lib//zabbix/host.rb',
30
+ 'lib//zabbix/item.rb',
31
+ 'lib//zabbix/trigger.rb',
32
+ 'lib//zabbix/version.rb',
33
+ 'lib//zabbix/zabbix.rb',
34
+ 'lib//zabbix',
35
+ 'lib/',
36
+ 'spec//README',
37
+ 'spec//spec_helper.rb',
38
+ 'spec//zabbix/connect_spec.rb',
39
+ 'spec//zabbix/function_spec.rb',
40
+ 'spec//zabbix/host_spec.rb',
41
+ 'spec//zabbix/item_spec.rb',
42
+ 'spec//zabbix/trigger_spec.rb',
43
+ 'spec//zabbix',
44
+ 'spec/',
45
+ 'README.md',
46
+ 'activerecord-zabbix.gemspec' #,
47
+
48
+ ]
49
+
50
+ end
@@ -0,0 +1,10 @@
1
+ # do this somewhere else
2
+ #
3
+ #$LOAD_PATH.unshift 'lib'
4
+ ##
5
+ ## bundler autoload
6
+ ##
7
+ #require 'boot'
8
+ #
9
+ require 'active_record'
10
+ require 'zabbix/zabbix'
data/lib/boot.rb ADDED
@@ -0,0 +1 @@
1
+ Zabbix::connect
@@ -0,0 +1,71 @@
1
+ module Zabbix
2
+
3
+
4
+ ##
5
+ #
6
+ # Expects `config` (unimplemented optional) as a `Hash` with
7
+ #
8
+ # :adapter
9
+ # :host
10
+ # :database
11
+ # :username
12
+ # :password
13
+ #
14
+ # Expects `logger` (optional) to be a standard logger
15
+ #
16
+ def self.connect( config = nil, logger = nil )
17
+
18
+ @logger = logger
19
+
20
+ if config.nil?
21
+
22
+ config = load_standard_rails_things_for_db__and_feel_free_to_rename_this_function
23
+
24
+ end
25
+
26
+ begin
27
+
28
+ ActiveRecord::Base.establish_connection(
29
+
30
+ :adapter => config[:adapter],
31
+ :host => config[:host],
32
+ :database => config[:database],
33
+ :username => config[:username],
34
+ :password => config[:password]
35
+
36
+ )
37
+
38
+ rescue Exception => e
39
+
40
+ #
41
+ # Intercept to log error
42
+ #
43
+
44
+ @logger.error(
45
+
46
+ "ERROR: in Zabbix::connect() #{e.to_s}"
47
+
48
+ ) unless @logger.nil?
49
+
50
+ #
51
+ # Re-Raise (STILL WANT TO BRING THE DAEMON DOWN)
52
+ #
53
+
54
+ raise e
55
+
56
+ end
57
+
58
+ end
59
+
60
+
61
+ def self.load_standard_rails_things_for_db__and_feel_free_to_rename_this_function
62
+
63
+ #
64
+ #
65
+ #
66
+ #
67
+ #
68
+
69
+ end
70
+
71
+ end
@@ -0,0 +1,13 @@
1
+ module Zabbix
2
+
3
+ class Function < ::ActiveRecord::Base
4
+
5
+ set_table_name "functions"
6
+ set_primary_key "functionid"
7
+
8
+ belongs_to :item, :class_name => "Item", :foreign_key => "itemid"
9
+ belongs_to :trigger, :class_name => "Trigger", :foreign_key => "triggerid"
10
+
11
+ end
12
+
13
+ end
@@ -0,0 +1,52 @@
1
+ module Zabbix
2
+
3
+ class Host < ::ActiveRecord::Base
4
+
5
+ set_table_name "hosts"
6
+ set_primary_key "hostid"
7
+
8
+ default_scope where(:status => [0,1] )
9
+ scope :monitored, where(:status => 0)
10
+ scope :not_monitored, where(:status => 1)
11
+
12
+ has_many :items, :class_name => "Item", :foreign_key => "hostid"
13
+
14
+ #scope :item_data_ago, lambda {|dns, key, ago | where(:dns => dns)[0].item_by_key(key).history.ago(ago)}
15
+
16
+
17
+ #has_many :host_group, :class_name => "HostGroup", :foreign_key => "hostid"
18
+ #has_many :groups, :through => :host_group
19
+ #has_many :host_template, :class_name => "HostTemplate", :foreign_key => "hostid"
20
+ #has_many :templates, :through => :host_template
21
+
22
+
23
+ #belongs_to :proxy, :class_name => "Proxy", :foreign_key => "proxy_hostid"
24
+
25
+ #def item_last_value(key, value, time_shift_tolerance = 480)
26
+ # items.where("key_ = ? and lastvalue = ? and lastclock >=", key, value.to_s, Time.now.to_i - time_shift_tolerance)
27
+ #end
28
+ #
29
+
30
+
31
+ # activerecord3 does lazyload, so .items does not get all the items
32
+ # and will get only the ONE record when:
33
+ #
34
+ # Zabbix::Host.find_by_dns( fqdn ).items.where( :key_ => 'icmpping')
35
+ #
36
+ # -stu
37
+ #
38
+ #
39
+ # def item_by_key(key)
40
+ # i = items.where("key_ = ?", key)
41
+ # return nil if i.nil? or i.empty?
42
+ # raise(TooManyRecords) if i.size > 1
43
+ # i[0]
44
+ #end
45
+
46
+ #def self.to_s
47
+ # "#{self.name}, #{self.dns}"
48
+ #end
49
+
50
+ end
51
+
52
+ end
@@ -0,0 +1,40 @@
1
+ module Zabbix
2
+
3
+ class Item < ::ActiveRecord::Base
4
+
5
+ set_table_name "items"
6
+ set_primary_key "itemid"
7
+ set_inheritance_column "dodge_rails_assumptions_about_type_column" # this is not an single inheritance table
8
+
9
+ belongs_to :host, :class_name => "Host", :foreign_key => "hostid"
10
+ has_many :functions, :class_name => "Function", :foreign_key => "itemid"
11
+
12
+ #has_many :groups, :through => :host, :source => :host_group
13
+
14
+
15
+ # select distinct ho.dns, i.itemid,i.value_type,h.value
16
+ #from items i,history_uint h, hosts ho
17
+ #where h.itemid = i.itemid
18
+ #and i.hostid = ho.hostid
19
+ #and h.clock > (extract(epoch FROM now())::int - 480)
20
+ #and i.value_type = 3
21
+ #and h.value = 1
22
+ #and i.itemid in (
23
+ #
24
+ # select itemid from items
25
+ # join hosts on (items.hostid = hosts.hostid)
26
+ # join hosts_groups on (hosts_groups.hostid = hosts.hostid)
27
+ # join groups on (groups.groupid = hosts_groups.groupid)
28
+ # where items.key_='icmpping'
29
+ # and items.status=0
30
+ # and hosts.status=0
31
+ # and groups.name in ('School Firewalls')
32
+ #)
33
+
34
+ #def self.by_group_and_key_with_conditions(group, key, conditions = {})
35
+ # self.joins(:groups).where("group.name = ?", group)
36
+ #end
37
+
38
+ end
39
+
40
+ end
@@ -0,0 +1,15 @@
1
+ module Zabbix
2
+
3
+ class Trigger < ::ActiveRecord::Base
4
+
5
+ set_table_name "triggers"
6
+ set_primary_key "triggerid"
7
+
8
+ has_many :functions, :class_name => "Function", :foreign_key => "triggerid"
9
+ #
10
+ # might be one-to-one, not one-to-many???
11
+ #
12
+
13
+ end
14
+
15
+ end
@@ -0,0 +1,9 @@
1
+ GEM_NAME = 'activerecord-zabbix'
2
+
3
+ module Zabbix
4
+
5
+ Version = VERSION = '0.1.0'
6
+
7
+ end
8
+
9
+ MODULE_NAME = Zabbix
@@ -0,0 +1,8 @@
1
+ require 'zabbix/version'
2
+
3
+ require 'zabbix/connect'
4
+
5
+ require 'zabbix/host'
6
+ require 'zabbix/item'
7
+ require 'zabbix/function'
8
+ require 'zabbix/trigger'
data/spec/README ADDED
@@ -0,0 +1,4 @@
1
+
2
+ The rails way would have set up a database.yml with a test subconfig that would trest the models on a local database.
3
+
4
+ That has not been done...
@@ -0,0 +1 @@
1
+ require 'activerecord-zabbix'
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe Zabbix do
4
+
5
+ before :each do
6
+
7
+
8
+ CONFIG = {
9
+
10
+
11
+
12
+ }
13
+
14
+ end
15
+
16
+ it 'connects' do
17
+
18
+ ActiveRecord::Base.should_receive(
19
+
20
+ :establish_connection
21
+
22
+ ).with(
23
+
24
+ {:password=>nil, :adapter=>nil, :host=>nil, :database=>nil, :username=>nil}
25
+
26
+ )
27
+
28
+ Zabbix.connect( nil, nil )
29
+
30
+ end
31
+
32
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe Zabbix::Function do
4
+
5
+ # pending
6
+
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe Zabbix::Host do
4
+
5
+ # pending
6
+
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe Zabbix::Item do
4
+
5
+ # pending
6
+
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe Zabbix::Trigger do
4
+
5
+ # pending
6
+
7
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-zabbix
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - ""
14
+ - ""
15
+ - ""
16
+ - Richard
17
+ autorequire:
18
+ bindir: bin
19
+ cert_chain: []
20
+
21
+ date: 2012-06-27 00:00:00 Z
22
+ dependencies:
23
+ - !ruby/object:Gem::Dependency
24
+ name: activerecord
25
+ prerelease: false
26
+ requirement: &id001 !ruby/object:Gem::Requirement
27
+ none: false
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ hash: 3
32
+ segments:
33
+ - 0
34
+ version: "0"
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ description: This is not a rails app.
38
+ email: ops@clue.co.za
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - lib//activerecord-zabbix.rb
47
+ - lib//boot.rb
48
+ - lib//zabbix/connect.rb
49
+ - lib//zabbix/function.rb
50
+ - lib//zabbix/host.rb
51
+ - lib//zabbix/item.rb
52
+ - lib//zabbix/trigger.rb
53
+ - lib//zabbix/version.rb
54
+ - lib//zabbix/zabbix.rb
55
+ - spec//README
56
+ - spec//spec_helper.rb
57
+ - spec//zabbix/connect_spec.rb
58
+ - spec//zabbix/function_spec.rb
59
+ - spec//zabbix/host_spec.rb
60
+ - spec//zabbix/item_spec.rb
61
+ - spec//zabbix/trigger_spec.rb
62
+ - README.md
63
+ - activerecord-zabbix.gemspec
64
+ homepage: https://github.com/cluetechnologies/activerecord-zabbix
65
+ licenses: []
66
+
67
+ post_install_message:
68
+ rdoc_options: []
69
+
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ hash: 3
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ requirements: []
91
+
92
+ rubyforge_project:
93
+ rubygems_version: 1.8.5
94
+ signing_key:
95
+ specification_version: 3
96
+ summary: ActiveRecord access into Zabbix
97
+ test_files: []
98
+