rbZabbix 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .idea
6
+ .yardoc
7
+ /doc
@@ -0,0 +1 @@
1
+ lib/**/*.rb - LICENSE TODO.rdoc ChangeLog.rdoc
File without changes
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in zabby.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,49 @@
1
+ Copyright 2011-2012 Farzad FARID / Pragmatic Source. All rights reserved.
2
+
3
+ Redistribution and use in source and binary forms, with or without modification, are
4
+ permitted provided that the following conditions are met:
5
+
6
+ 1. Redistributions of source code must retain the above copyright notice, this list of
7
+ conditions and the following disclaimer.
8
+
9
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list
10
+ of conditions and the following disclaimer in the documentation and/or other materials
11
+ provided with the distribution.
12
+
13
+ THIS SOFTWARE IS PROVIDED BY PRAGMATIC SOURCE ''AS IS'' AND ANY EXPRESS OR IMPLIED
14
+ WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
15
+ FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PRAGMATIC SOURCE OR
16
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
17
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
18
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
19
+ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
20
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
21
+ ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22
+
23
+ The views and conclusions contained in the software and documentation are those of the
24
+ authors and should not be interpreted as representing official policies, either expressed
25
+ or implied, of Pragmatic Source.
26
+
27
+ OTHER LICENSES COVERING PORTIONS OF CODE
28
+ ========================================
29
+
30
+ Portions of code in "Zabbix::Running" were inspired by Rush (https://github.com/adamwiggins/rush):
31
+
32
+ Copyright (c) 2010 Adam Wiggins
33
+
34
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this
35
+ software and associated documentation files (the "Software"), to deal in the Software
36
+ without restriction, including without limitation the rights to use, copy, modify,
37
+ merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
38
+ permit persons to whom the Software is furnished to do so, subject to the following
39
+ conditions:
40
+
41
+ The above copyright notice and this permission notice shall be included in all copies
42
+ or substantial portions of the Software.
43
+
44
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
45
+ INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
46
+ PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
47
+ FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
48
+ OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
49
+ DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,246 @@
1
+ = Zabby
2
+
3
+ Zabby[http://zabby.org] is a Ruby library and client for Zabbix[http://www.zabbix.com/]. It provides a simple and
4
+ clean interface to the {Zabbix Web Services}[http://www.zabbix.com/documentation/1.8/api].
5
+
6
+ == Goals
7
+
8
+ Zabby has the following caracteristics:
9
+ - Works both as a Ruby API and a command line tool.
10
+ - Uses an internal Ruby DSL (Domain Specific Langage) that give access to the full power of
11
+ Ruby scripting in your configurarion files. No need for a specific configuration language.
12
+ - Simple design: avoids spaghetti code, WTF and facepalm, unlike some other
13
+ Ruby Zabbix libraries.
14
+
15
+ There already are Ruby Zabbix libraries but some of them are unfinished (and apparently
16
+ abandonned) and some other's code is too complicated or poorly designed. This is why I decided
17
+ to write this tool for which my primary goal is to keep the code design the user interaction
18
+ and application interface clean and simple.
19
+
20
+ == Features
21
+
22
+ - Low level Ruby API that maps every Zabbix API (JSON RPC) to Ruby hashes.
23
+ - Higher level Ruby DSL, both available in pure Ruby and for writing configuration files.
24
+ - Supports SSL and authenticated HTTP proxies.
25
+ - Full featured IRB like shell included with Readline support. You can mix Zabbix instructions
26
+ with Ruby calls transparently.
27
+ - Reads a configuration file on startup.
28
+
29
+ == Changes
30
+
31
+ See the [ChangeLog.rdoc] file.
32
+
33
+ == Configuration
34
+
35
+ All the settings can be used interactively or written in a configuration file. The file
36
+ <tt>~/.zabbyrc</tt> is read on startup unless the option "<tt>--no-rcfile</tt>" is
37
+ used.
38
+
39
+ === Command line
40
+
41
+ The main configuration parameters are:
42
+
43
+ --[no-]rcfile [CONFIG FILE]: Configuration file to load on startup. Default is '~/.zabbyrc'. With the 'no' prefix no configuration file will be loaded.
44
+ -f, --extra-file COMMAND_FILE: Execute the instructions in COMMAND_FILE
45
+
46
+ === Zabby configuration
47
+
48
+ The following settings can be configured with the '+set+' command:
49
+ [server] Zabbix webservice URL. If the URL does not end with <tt>.php</tt> then
50
+ <tt>/api_jsonrpc.php</tt> is appended automatically.
51
+ [user] User login with +API+ rights on the Zabbix Server. If the account does not have
52
+ +API+ access the connection will be refused by Zabbix.
53
+ [password] User password.
54
+ [proxy_host] HTTP proxy.SSL is supported and the certificate validity is not checked. *Optional*
55
+ [proxy_user] User authentication on the proxy. *Optional*
56
+ [proxy_password] User password on the proxy. *Optional*
57
+
58
+ === Sample ".zabbyrc"
59
+
60
+ This is a configuration <tt>.zabbyrc</tt> file example, it will be loaded at startup:
61
+
62
+ # Zabby configuration
63
+ set :server => "https://monitoring.example.com"
64
+ set :user => "api_user"
65
+ set :password => "s3cr3t"
66
+ set :proxy_host => "http://10.10.10.10"
67
+ set :proxy_user => "john"
68
+ set :proxy_password => "D0e"
69
+ login
70
+
71
+ == Examples
72
+
73
+ === Zabby on the Command Line
74
+
75
+ *zabbysh* is an simple Zabbix Shell. It runs either interactively or
76
+ can be used to execute a command file with the "-f CMD_FILE" option.
77
+
78
+ If the command file does not end with "exit" then the program drops into
79
+ the shell after executing the file.
80
+
81
+ *zabbysh* provides Readline support but does no support advanced IRB features likes
82
+ multi-lines instructions.
83
+
84
+ *Note*: *zabbyrb* is another Zabbix Shell based on *IRB*. It provides the whole power of IRB
85
+ but may be less stable than zabbysh because the IRB API is poorly documented and hard
86
+ to use.
87
+
88
+ ==== Interactive shell
89
+
90
+ Provided you created a <tt>~/.zabbyrc</tt> configuration file with authentication settings
91
+ you can launch the interactive shell and start talking to the Zabbix server:
92
+
93
+ # zabbysh
94
+ Zabby Shell 0.1.0
95
+
96
+ ** This is a simple irb like Zabbix Shell. Multiline commands do not work for e.g. **
97
+ Type "help" for online documentation.
98
+ zabby> logged_in?
99
+ => true
100
+ zabby> Host.get "filter" => { "host" => "monitor.example.com" }, "output" => "extend"
101
+ => [{"snmp_errors_from"=>"0", "ipmi_port"=>"623", "outbytes"=>"0", "proxy_hostid"=>"0",
102
+ "maintenance_status"=>"0", "ipmi_password"=>"", "ipmi_privilege"=>"2",
103
+ "ipmi_disable_until"=>"0", "available"=>"1", "maintenances"=>[{"maintenanceid"=>"0"}],
104
+ "ipmi_error"=>"", "maintenanceid"=>"0", "useipmi"=>"0", "port"=>"10050", "useip"=>"1",
105
+ "ipmi_errors_from"=>"0", "maintenance_from"=>"0", "ipmi_ip"=>"", "hostid"=>"10017",
106
+ "ipmi_available"=>"0", "disable_until"=>"0", "ip"=>"127.0.0.1", "maintenance_type"=>"0",
107
+ "ipmi_authtype"=>"0", "inbytes"=>"0", "lastaccess"=>"0",
108
+ "host"=>"monitor.example.com", "snmp_error"=>"", "ipmi_username"=>"", "status"=>"0",
109
+ "dns"=>"monitor.example.com", "snmp_available"=>"0", "snmp_disable_until"=>"0",
110
+ "errors_from"=>"0", "error"=>""}]
111
+ zabby> Item.get "host" => "monitor.example.com", "output" => "refer"
112
+ => [{"itemid"=>"18435", "host"=>"monitor.example.com"},
113
+ {"itemid"=>"18436", "host"=>"monitor.example.com"},
114
+ {"itemid"=>"18438", "host"=>"monitor.example.com"},
115
+ {"itemid"=>"18439",....]
116
+ zabby> [...]
117
+ zabby> logout
118
+ => nil
119
+ zabby> exit
120
+
121
+ To execute a script file before dropping into the shell type:
122
+
123
+ # zabbysh --extra-file some_zabbix_cmds.zby
124
+
125
+ ===== Online help
126
+
127
+ Type *help* to get a brief description of available commands.
128
+
129
+ ==== Execute a command script
130
+
131
+ To execute a script non interactively create a file and put the same
132
+ instructions as above in it then execute *zabbysh*:
133
+
134
+ # zabbysh my_zabbix_commands.zby
135
+
136
+ You can even create auto-executable scripts! Just add "<tt>#!/usr/bin/env zabbysh</tt>"
137
+ on the first line of the script and make it executable with "<tt>chmod +x my_zabby_commands.zby</tt>".
138
+
139
+ For example create the script "my_zabbix_command.zby":
140
+
141
+ #!/usr/bin/env zabbysh
142
+ #
143
+ # Zabby configuration
144
+ set :server => "https://monitoring.example.com"
145
+ set :user => "api_user"
146
+ set :password => "s3cr3t"
147
+ set :proxy_host => "http://10.10.10.10"
148
+ set :proxy_user => "john"
149
+ set :proxy_password => "D0e"
150
+ login
151
+
152
+ hosts = Host.get "filter" => { "host" => "monitor.example.com" }, "output" => "extend"
153
+ h = hosts.first
154
+ puts "Host #{h['host']} has IP address #{h['ip']}"
155
+
156
+ exit 0
157
+
158
+ Now to execute the script you just have to type:
159
+
160
+ # ./my_zabbix_commands.zby
161
+
162
+ Zabbysh script can have their own command line parameters (with libs like 'optparse').
163
+
164
+ === API use
165
+
166
+ The barebone API can be used in Ruby like this:
167
+
168
+ require "rubygems"
169
+ require "zabby"
170
+ require "pp"
171
+
172
+ z = Zabby.init do
173
+ set :server => "https://monitoring.example.com"
174
+ set :user => "api_user"
175
+ set :password => "s3cr3t"
176
+ # The proxy settings are of coure optional
177
+ set :proxy_host => "http://10.10.10.10"
178
+ set :proxy_user => "john"
179
+ set :proxy_password => "D0e"
180
+ login
181
+ end
182
+
183
+ pp z.run { Zabby::Host.get "filter" => { "host" => "master.example.com" } }
184
+ pp z.run { Zabby::Item.get "host" => "master.example.com", "output" => "shorten" }
185
+
186
+
187
+ - The _server_, _user_ and _passord_ are necessary.
188
+ - If you don't provide the path to the JSON-RPC script "/api_jsonrpc.php" is appended
189
+ to the URI automatically.
190
+ - The <em>proxy_*</em> settings are optionnal.
191
+
192
+ ==== Note
193
+
194
+ When using the barebone API you have to prefix the Zabbix classes (Host, Item, Trigger, etc.) with the "Zabby::"
195
+ namespace, otherwise Ruby won't find the classes. This is apparently the way Ruby 1.8.x looks up constants, the
196
+ problem should not happen with Ruby 1.9.x...
197
+
198
+ == Requirements
199
+
200
+ - Ruby 1.8.5. <b>IT BASICALLY RUNS ANYWHERE!</b>
201
+ - JSON library: 'json' or 'json_pure'
202
+ - OptionParser
203
+ - Readline (optionnal, used by the interactive shell)
204
+
205
+ == References
206
+
207
+ - Main web site: http://zabby.org
208
+ - GitHub projet: https://github.com/Pragmatic-Source/zabby
209
+ - RubyGems.org homepage: https://rubygems.org/gems/zabby
210
+ - Author's company: http://www.pragmatic-source.com
211
+
212
+ == Contributing
213
+
214
+ All contributions are welcome. You can either
215
+ {open a ticket on Github}[https://github.com/Pragmatic-Source/zabby/issues], send a pull request
216
+ with a patch or send an email to the author.
217
+
218
+ == Author
219
+
220
+ Farzad FARID mailto:ffarid@pragmatic-source.com
221
+
222
+ === Thanks to
223
+
224
+ - Jean-Hadrien Chabran (jhchabran): Advices on code design.
225
+ - Nicolas Blanco (slainer68): Advices on code design and Triple Facepalming after reading other Ruby/Zabbix code.
226
+ - Renaud Chaput (renchap): For suggesting the "zabby" name.
227
+ - Folks on the irc channel #rubyonrails.fr@Freenode
228
+ - Beta testers: A-Kaser, gaetronik.
229
+
230
+ === Sources of inspiration
231
+
232
+ - Adam Wiggins's Rush[https://github.com/adamwiggins/rush] for the interactive shell.
233
+ - Other Zabbix Ruby libraries (only the good parts ;) ):
234
+
235
+ zabcon[http://trac.red-tux.net/]::
236
+ Zabcon is a command line interface for Zabbix written in Ruby
237
+
238
+ rzabbix[http://github.com/neerfri/rzabbix]::
239
+ Zabbix API client for Ruby
240
+
241
+ rubix[https://github.com/dhruvbansal/rubix]::
242
+ Rubix is a Ruby client for Zabbix
243
+
244
+ == License
245
+
246
+ This software is release under the <b>Simplified BSD license</b>. See the LICENSE file.
@@ -0,0 +1,11 @@
1
+ require 'bundler'
2
+ require 'yard'
3
+ Bundler::GemHelper.install_tasks
4
+
5
+ YARD::Rake::YardocTask.new do |t|
6
+ # File list is defined in '.yardopts'
7
+ #t.files = ['lib/**/*.rb', '-', 'LICENSE', 'TODO.rdoc', 'ChangeLog.rdoc']
8
+ #t.options = ['--any', '--extra', '--opts'] # optional
9
+ end
10
+
11
+ task :default => [ :yard, :install ]
data/TODO.rdoc CHANGED
File without changes
@@ -0,0 +1,86 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- encoding: utf-8 -*-
3
+ # Author:: Farzad FARID (<ffarid@pragmatic-source.com>)
4
+ # Copyright:: Copyright (c) 2011-2012 Farzad FARID
5
+ # License:: Simplified BSD License
6
+
7
+ # This is an experimental Zabby Shell based on IRB. It is still buggy and the code
8
+ # is very nasty because IRB is poorly documented and hard to use as a library. Use
9
+ # with care.
10
+ #
11
+ # Ugly IRB initialisation inspired by Shef (Chef's CLI) and a article on
12
+ # StackOverflow : http://stackoverflow.com/questions/4189818/how-to-run-irb-start-in-context-of-current-class
13
+
14
+ ZABBY_ROOT = File.expand_path('../../lib', __FILE__)
15
+ $: << ZABBY_ROOT unless $:.include?(ZABBY_ROOT)
16
+
17
+ require 'rbZabbix'
18
+ require 'optparse'
19
+ require 'ostruct'
20
+ require 'irb'
21
+ require 'irb/completion'
22
+
23
+
24
+ module ZabbyRB
25
+ def self.irb_conf
26
+ @irb_conf || IRB.conf
27
+ end
28
+
29
+ def self.configure_irb
30
+ irb_conf[:MAIN_CONTEXT] = IRB::Irb.new.context
31
+ irb_conf[:HISTORY_FILE] = "~/.zabby_history"
32
+ irb_conf[:SAVE_HISTORY] = 1000
33
+
34
+ irb_conf[:IRB_RC] = lambda do |conf|
35
+ m = conf.main
36
+ leader = "(#{m.class.to_s})"
37
+
38
+ def m.help
39
+ puts "Help??"
40
+ end
41
+
42
+ # Hack without which "exit" brings us back to the main IRB context instead
43
+ # of exiting.
44
+ def m.exit
45
+ Kernel.exit
46
+ end
47
+
48
+ conf.prompt_c = "Zabby#{leader} > "
49
+ conf.return_format = " => %s \n"
50
+ conf.prompt_i = "Zabby#{leader} > "
51
+ conf.prompt_n = "Zabby#{leader} ?> "
52
+ conf.prompt_s = "Zabby#{leader}%l> "
53
+ end
54
+ end
55
+
56
+ def self.init
57
+ configure_irb
58
+
59
+ puts <<EOT
60
+ Zabby Shell #{Zabby::VERSION}, irb based shell.
61
+
62
+ run `help' for help, `exit' or ^D to quit.
63
+
64
+ ** THIS IS AN EXPERIMENTAL SHELL, IT MAY BE UNSTABLE. USE WITH CARE. **
65
+ Please use 'zabbysh' instead and report a bug if 'zabbyrb' does not
66
+ work correctly.
67
+ EOT
68
+ end
69
+ end
70
+
71
+ module IRB
72
+ class << self
73
+ alias :run_original_config :run_config
74
+
75
+ def run_config
76
+ run_original_config
77
+ ZabbyRB.init
78
+ end
79
+ end
80
+ end
81
+
82
+ IRB.setup nil
83
+ #IRB.conf[:MAIN_CONTEXT] = IRB::Irb.new.context
84
+ require 'irb/ext/multi-irb'
85
+ IRB.irb nil, Zabby.init
86
+ IRB.start
@@ -0,0 +1,109 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- encoding: utf-8 -*-
3
+ # Author:: Farzad FARID (<ffarid@pragmatic-source.com>)
4
+ # Copyright:: Copyright (c) 2011-2012 Farzad FARID
5
+ # License:: Simplified BSD License
6
+
7
+
8
+ ZABBY_ROOT = File.expand_path('../../lib', __FILE__)
9
+ $: << ZABBY_ROOT unless $:.include?(ZABBY_ROOT)
10
+
11
+ require 'rbZabbix'
12
+ require 'optparse'
13
+ require 'ostruct'
14
+
15
+ options = OpenStruct.new
16
+ options.rcfile = File.expand_path('~/.zabbyrc')
17
+ options.extra_file = nil
18
+
19
+ opts = OptionParser.new do |opts|
20
+ opts.banner = <<'EOT'
21
+ Usage: zabbysh [zabbysh options] [script [script options]]
22
+
23
+ if no 'script' file is provided an interactive shell is started.
24
+ Otherwise the script file is executed. To load and execute a
25
+ supplementary script file before the shell use the '--extra-file'
26
+ option.
27
+
28
+ HOW TO PASS ARGUMENTS TO THE SCRIPT
29
+ -----------------------------------
30
+
31
+ The Zabbysh script can have its own command line arguments. Just prepend
32
+ '--' before the arguments to separate them from Zabbysh's own arguments.
33
+ EOT
34
+ opts.separator ""
35
+ opts.separator "Zabbysh specific options:"
36
+
37
+ opts.on("--[no-]rcfile [CONFIG FILE]",
38
+ "Configuration file to load on startup.\n\tDefault is '~/.zabbyrc'." +
39
+ " With the 'no' prefix no configuration file will be loaded.") do |file|
40
+ options.rcfile = file || nil
41
+ end
42
+ opts.on("-f", "--extra-file CMD_FILE",
43
+ "Execute the instructions in CMD_FILE") do |file|
44
+ options.extra_file = file
45
+ end
46
+
47
+ opts.on_tail("-h", "--help", "Show this message") do
48
+ puts opts
49
+ exit
50
+ end
51
+
52
+ opts.on_tail("--version", "Show version") do
53
+ puts <<EOT
54
+ Zabby #{Zabby::VERSION}
55
+ Copyright (c) 2011-2012-2012 Farzad FARID / Pragmatic Source
56
+ Home page: http://www.zabby.org/
57
+ License: Simplified BSD
58
+ EOT
59
+ exit
60
+ end
61
+ end
62
+
63
+ # Parse options, extract optional script to execute and initialize Zabby.
64
+ # Do not reorder commandline argument otherwise zabbysh's arguments will
65
+ # get mixed up with the script's argument.
66
+ begin
67
+ ENV['POSIXLY_CORRECT'] = '1'
68
+ opts.parse!(ARGV)
69
+ rescue OptionParser::ParseError => e
70
+ STDERR.puts("ERROR: #{e.message}.")
71
+ STDERR.puts opts
72
+ exit 1
73
+ ensure
74
+ ENV['POSIXLY_CORRECT'] = nil
75
+ end
76
+
77
+ # Now ARGV only contains the script's optional arguments.
78
+ script = ARGV.shift
79
+ z = Zabby.init
80
+
81
+ # Read & execute startup file and/or command file. The true/false indicates if
82
+ # we should fail in case the file does not exist.
83
+ [ [ options.rcfile, false ], [ options.extra_file, true ], [ script, true ] ].each do |file, fail_on_missing|
84
+ if file.nil?
85
+ # Do nothing
86
+ elsif File.exist?(file)
87
+ begin
88
+ z.run(file)
89
+ rescue SystemExit
90
+ exit 0
91
+ rescue Exception => e
92
+ puts "Exception #{e.class} -> #{e.message}"
93
+ e.backtrace.each do |t|
94
+ puts " #{::File.expand_path(t)}"
95
+ end
96
+ exit 1
97
+ end
98
+ elsif fail_on_missing # File is missing but do not fail for '.zabbyrc'
99
+ raise RuntimeError, "Missing Zabby script '#{file}'."
100
+ end
101
+ end
102
+
103
+
104
+ # We start the shell if a command file is not provided
105
+ if script.nil?
106
+ z.shell
107
+ end
108
+
109
+ exit 0