zabbix 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Matthew Knopp
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,50 @@
1
+ = zabbix-rb
2
+ _ _ _ _
3
+ ______ _| |__ | |__ (_)_ __ _ __| |__
4
+ |_ / _` | '_ \| '_ \| \ \/ /____| '__| '_ \
5
+ / / (_| | |_) | |_) | |> <_____| | | |_) |
6
+ /___\__,_|_.__/|_.__/|_/_/\_\ |_| |_.__/
7
+
8
+ I threw this together this afternoon. It's absulotely untested. I only just
9
+ corrected a few syntax errors.
10
+
11
+ == usage
12
+
13
+ === easy
14
+
15
+ Zabbix::Sender::Easy.run(:"com.yammer.cron.brithday_email_job") do |zbx|
16
+ Users.each_with_index do |user, idx|
17
+ HappyBirthday.send if user.birthday?
18
+
19
+ ## let zabbix know we're alive every N users, whatever
20
+ zbx.send_heartbeat if idx % N == 0
21
+ end
22
+ end
23
+
24
+ === tedious
25
+
26
+ zbx = Zabbix::Sender.new
27
+ zbx.send_start(:"com.yammer.cron.birthday_email_job")
28
+
29
+ Users.each_with_index do |user, idx|
30
+ HappyBirthday.send if user.birthday?
31
+
32
+ ## let zabbix know we're alive every N users, whatever
33
+ zbx.send_heartbeat if idx % N == 0
34
+ end
35
+ zbx.send_stop(:"com.yammer.cron.birthday_email_job")
36
+
37
+
38
+ == Note on Patches/Pull Requests
39
+
40
+ * Fork the project.
41
+ * Make your feature addition or bug fix.
42
+ * Add tests for it. This is important so I don't break it in a
43
+ future version unintentionally.
44
+ * Commit, do not mess with rakefile, version, or history.
45
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
46
+ * Send me a pull request. Bonus points for topic branches.
47
+
48
+ == Copyright
49
+
50
+ Copyright (c) 2010 Matthew Knopp. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,52 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "zabbix"
8
+ gem.summary = %Q{send data to zabbix from ruby}
9
+ gem.description = %Q{send data to zabbix from ruby}
10
+ gem.email = "mknopp@yammer-inc.com"
11
+ gem.homepage = "http://github.com/mhat/zabbix"
12
+ gem.authors = ["Matthew Knopp"]
13
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/test_*.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+ task :test => :check_dependencies
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+ rdoc.rdoc_dir = 'rdoc'
49
+ rdoc.title = "zabbix #{version}"
50
+ rdoc.rdoc_files.include('README*')
51
+ rdoc.rdoc_files.include('lib/**/*.rb')
52
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/zabbix.rb ADDED
@@ -0,0 +1,31 @@
1
+ module Zabbix
2
+ end
3
+
4
+ require "zabbix/agent/configuration"
5
+ require "zabbix/sender"
6
+ require "zabbix/sender/easy"
7
+
8
+ ## we could use zabbix-sender, but that appears to be unecessary; we can instead
9
+ ## simply talk to the zabbit-server. we get there via the parent.
10
+ ##
11
+ ## thinking about what we want to be able to do there are a few types of messages
12
+ ## we want to send to zabbix:
13
+ ## 1. that a process has starting
14
+ ## 2. that a process is running
15
+ ## 3. that a process has finished
16
+ ##
17
+ ## there will be more in the future, but this is the minimum; it would be nice
18
+ ## to expose two interfaces:
19
+
20
+ ## Zabbix::Sender.run(:task_name, opts={}) do |zbx|
21
+ ## ...
22
+ ## zbx.heartbeat
23
+ ## end
24
+
25
+ ## zbx = Zabbit::Zender::Easy.new(opts={})
26
+ ## zbx.send_start
27
+ ## zbx.send_heartbeat
28
+ ## zbx.send_end
29
+
30
+ ## That's pretty much it.
31
+
@@ -0,0 +1,89 @@
1
+ module Zabbix::Agent
2
+ class Configuration
3
+ def initialize(config={})
4
+ @config = config
5
+ end
6
+
7
+ def server
8
+ @config['Server']
9
+ end
10
+
11
+ def server_port
12
+ @config['ServerPort']
13
+ end
14
+
15
+ def listen_port
16
+ @config['ListenPort']
17
+ end
18
+
19
+ def listen_ip
20
+ @config['ListenIP']
21
+ end
22
+
23
+ def source_ip
24
+ @config['SourceIP']
25
+ end
26
+
27
+ def start_agents
28
+ @config['StartAgents']
29
+ end
30
+
31
+ def refresh_active_checks
32
+ @config['RefreshActiveChecks']
33
+ end
34
+
35
+ def disable_active
36
+ @config['DisableActive']
37
+ end
38
+
39
+ def enable_remote_commands
40
+ @config['EnableRemoteCommands']
41
+ end
42
+
43
+ def debug_level
44
+ @config['DebugLevel']
45
+ end
46
+
47
+ def pid_file
48
+ @config['PidFile']
49
+ end
50
+
51
+ def log_file
52
+ @config['LogFile']
53
+ end
54
+
55
+ def timeout
56
+ @config['Timeout']
57
+ end
58
+
59
+ def arbitrary(key)
60
+ @config[key]
61
+ end
62
+
63
+ def self.read(zabbix_conf_file=nil)
64
+ zabbix_conf_file ||= "/etc/zabbix/zabbix-agentd.conf"
65
+ zabbix_conf = {}
66
+
67
+ File.open(zabbix_conf_file).each do |line|
68
+ ## skip comments
69
+ next if line =~ /^(\s+)?#/
70
+
71
+ ## strip tail comments
72
+ line.gsub!(/#.*/, '')
73
+
74
+ ## zabbix splits on equals
75
+ key, value = line.split("=", 2)
76
+ key.chomp!
77
+ value.chomp!
78
+
79
+ ## zabbix keys look like strings
80
+ next unless key =~ /[A-Za-z0-9]+/
81
+
82
+ ## cool
83
+ zabbix_conf[key] = value
84
+ end
85
+
86
+ Configuration.new(zabbix_conf)
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,82 @@
1
+ require 'socket'
2
+ require 'yajl'
3
+
4
+ class Zabbix::Sender
5
+
6
+ DEFAULT_SERVER_PORT = 10051
7
+
8
+ attr_reader :configured
9
+
10
+ def initialize(opts={})
11
+ if opts[:config_file]
12
+ config = Zabbix::Agent::Configuration.read(opts[:config_file])
13
+ @host = config.server
14
+ @port = config.server_port || DEFAULT_SERVER_PORT
15
+ end
16
+
17
+ if opts[:host]
18
+ @host = opts[:host]
19
+ @port = opts[:port] || DEFAULT_SERVER_PORT
20
+ end
21
+
22
+ end
23
+
24
+
25
+ def configured?
26
+ @configured ||= !(@host.nil? and @port.nil?)
27
+ end
28
+
29
+
30
+ def connect
31
+ TCPSocket.new(@host, @port)
32
+ end
33
+
34
+
35
+ def send_start(key, opts={})
36
+ send_data("#{key}.start", 1, opts)
37
+ end
38
+
39
+
40
+ def send_stop(key, opts={})
41
+ send_data("#{key}.stop", 1, opts)
42
+ end
43
+
44
+
45
+ def send_heartbeat(key, msg="", opts={})
46
+ send_data("#{key}.heartbeat", msg, opts)
47
+ end
48
+
49
+ def send_data(key, value, opts={})
50
+ return false unless configured?
51
+ host = opts[:host] || Socket.gethostname
52
+ ts = opts[:ts] || Time.now.to_i
53
+ socket = connect
54
+
55
+ # construct json
56
+ json = Yajl::Encoder.encode({
57
+ :request => "agent data",
58
+ :clock => ts,
59
+ :data => [{
60
+ :host => host,
61
+ :key => key,
62
+ :value => value,
63
+ :clock => ts
64
+ }]
65
+ })
66
+
67
+ # send the data
68
+ socket.write "ZBXD\x01"
69
+ socket.write [json.size].pack('q')
70
+ socket.write json
71
+ socket.flush
72
+
73
+ # read the response message if desired
74
+ #header = socket.read(5)
75
+ #len = socket.read(8)
76
+ #len = len.unpack('q').shift
77
+ #msg = socket.read(len)
78
+ socket.close
79
+
80
+ return true
81
+ end
82
+ end
@@ -0,0 +1,8 @@
1
+ class Zabbix::Sender::Easy
2
+ def self.run (task, opts={}, &block)
3
+ zbx = Zabbix::Sender.new(opts)
4
+ zbx.send_start
5
+ block.call(zbx)
6
+ zbx.send_stop
7
+ end
8
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'zabbix'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,32 @@
1
+ require 'helper'
2
+
3
+ class TestZabbix < Test::Unit::TestCase
4
+
5
+ should "not be configured" do
6
+ zbx = Zabbix::Sender.new
7
+ assert_equal false, zbx.configured?
8
+ end
9
+
10
+ should "be configured by args" do
11
+ zbx = Zabbix::Sender.new :host => 'localhost'
12
+ assert_equal true, zbx.configured?
13
+ end
14
+
15
+ should "be configured by zabbix_agentd.conf" do
16
+ config_file = "#{File.dirname(__FILE__)}/zabbix_agentd.conf"
17
+ zbx = Zabbix::Sender.new :config_file => config_file
18
+ assert_equal true, zbx.configured?
19
+ end
20
+
21
+ should "send some data" do
22
+ config_file = "#{File.dirname(__FILE__)}/zabbix_agentd.conf"
23
+ zbx = Zabbix::Sender.new :config_file => config_file
24
+ key = :"foo.bar.baz"
25
+ host = "testhost.example.com"
26
+ assert_equal true, zbx.send_start(key, :host => host)
27
+ assert_equal true, zbx.send_heartbeat(key, "8==D~~~ ADAM", :host => host)
28
+ assert_equal true, zbx.send_stop(key, :host => host)
29
+ end
30
+
31
+
32
+ end
@@ -0,0 +1,2 @@
1
+ Server=localhost
2
+ ServerPort=10051
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zabbix
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Matthew Knopp
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-11-30 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: thoughtbot-shoulda
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: send data to zabbix from ruby
36
+ email: mknopp@yammer-inc.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.rdoc
44
+ files:
45
+ - .document
46
+ - .gitignore
47
+ - LICENSE
48
+ - README.rdoc
49
+ - Rakefile
50
+ - VERSION
51
+ - lib/zabbix.rb
52
+ - lib/zabbix/agent/configuration.rb
53
+ - lib/zabbix/sender.rb
54
+ - lib/zabbix/sender/easy.rb
55
+ - test/helper.rb
56
+ - test/test_zabbix.rb
57
+ - test/zabbix_agentd.conf
58
+ has_rdoc: true
59
+ homepage: http://github.com/mhat/zabbix
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options:
64
+ - --charset=UTF-8
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ hash: 3
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ requirements: []
86
+
87
+ rubyforge_project:
88
+ rubygems_version: 1.3.7
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: send data to zabbix from ruby
92
+ test_files:
93
+ - test/helper.rb
94
+ - test/test_zabbix.rb