zabby 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,6 +2,14 @@
2
2
 
3
3
  List of visible changes:
4
4
 
5
+ == v0.1.2 (2012/03/27)
6
+
7
+ * Older Zabbix 1.8.x used "user.authenticate" instead of "user.login", we must try both methods.
8
+ * Improve command line parsing. It is now easier to pass parameters to a "zabbysh" script withtout
9
+ having to prepend "--".
10
+ * Make some methods private.
11
+ * Internal: add an primary key to Zabby classes (not used yet).
12
+
5
13
  == v0.1.1 (2012/03/14)
6
14
 
7
15
  * Initialization code (the argument given to "Zabby.init") was not executed.
@@ -159,6 +159,8 @@ Now to execute the script you just have to type:
159
159
 
160
160
  # ./my_zabbix_commands.zby
161
161
 
162
+ Zabbysh script can have their own command line parameters (with libs like 'optparse').
163
+
162
164
  === API use
163
165
 
164
166
  The barebone API can be used in Ruby like this:
@@ -171,6 +173,7 @@ The barebone API can be used in Ruby like this:
171
173
  set :server => "https://monitoring.example.com"
172
174
  set :user => "api_user"
173
175
  set :password => "s3cr3t"
176
+ # The proxy settings are of coure optional
174
177
  set :proxy_host => "http://10.10.10.10"
175
178
  set :proxy_user => "john"
176
179
  set :proxy_password => "D0e"
@@ -222,6 +225,7 @@ Farzad FARID mailto:ffarid@pragmatic-source.com
222
225
  - Nicolas Blanco (slainer68): Advices on code design and Triple Facepalming after reading other Ruby/Zabbix code.
223
226
  - Renaud Chaput (renchap): For suggesting the "zabby" name.
224
227
  - Folks on the irc channel #rubyonrails.fr@Freenode
228
+ - Beta testers: A-Kaser, gaetronik.
225
229
 
226
230
  === Sources of inspiration
227
231
 
@@ -17,30 +17,33 @@ options.rcfile = File.expand_path('~/.zabbyrc')
17
17
  options.extra_file = nil
18
18
 
19
19
  opts = OptionParser.new do |opts|
20
- opts.banner = <<EOT
21
- Usage: zabbysh [options] [script]
20
+ opts.banner = <<'EOT'
21
+ Usage: zabbysh [zabbysh options] [script [script options]]
22
22
 
23
- If no 'script' file is provided an interactive shell is started. Otherwise the script file
24
- is executed. To load and execute a supplementary script file before the shell use the
25
- '--extra-file' option.
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.
26
33
  EOT
27
34
  opts.separator ""
28
- opts.separator "Specific options:"
35
+ opts.separator "Zabbysh specific options:"
29
36
 
30
37
  opts.on("--[no-]rcfile [CONFIG FILE]",
31
- "Configuration file to load on startup. Default is '~/.zabbyrc'." +
38
+ "Configuration file to load on startup.\n\tDefault is '~/.zabbyrc'." +
32
39
  " With the 'no' prefix no configuration file will be loaded.") do |file|
33
40
  options.rcfile = file || nil
34
41
  end
35
- opts.on("-f", "--extra-file COMMAND_FILE",
36
- "Execute the instructions in COMMAND_FILE") do |file|
42
+ opts.on("-f", "--extra-file CMD_FILE",
43
+ "Execute the instructions in CMD_FILE") do |file|
37
44
  options.extra_file = file
38
45
  end
39
46
 
40
- opts.separator ""
41
- opts.separator "Common options:"
42
-
43
-
44
47
  opts.on_tail("-h", "--help", "Show this message") do
45
48
  puts opts
46
49
  exit
@@ -57,15 +60,22 @@ EOT
57
60
  end
58
61
  end
59
62
 
60
- # Parse options, extract script to execute and initialize Zabby.
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.
61
66
  begin
67
+ ENV['POSIXLY_CORRECT'] = '1'
62
68
  opts.parse!(ARGV)
63
69
  rescue OptionParser::ParseError => e
64
70
  STDERR.puts("ERROR: #{e.message}.")
65
71
  STDERR.puts opts
66
72
  exit 1
73
+ ensure
74
+ ENV['POSIXLY_CORRECT'] = nil
67
75
  end
68
- script = ARGV.first
76
+
77
+ # Now ARGV only contains the script's optional arguments.
78
+ script = ARGV.shift
69
79
  z = Zabby.init
70
80
 
71
81
  # Read & execute startup file and/or command file. The true/false indicates if
@@ -55,6 +55,12 @@ module Zabby
55
55
  'user' => @user,
56
56
  'password' => @password)
57
57
  @auth = query_zabbix_rpc(auth_message)
58
+ rescue Zabby::APIError
59
+ # Older Zabbix 1.8.x used a different authentication method. You have to guess...
60
+ auth_message = format_message('user', 'authenticate',
61
+ 'user' => @user,
62
+ 'password' => @password)
63
+ @auth = query_zabbix_rpc(auth_message)
58
64
  rescue Exception => e
59
65
  @auth = nil
60
66
  raise e
@@ -35,7 +35,7 @@ module Zabby
35
35
  end
36
36
 
37
37
  # Show the Shell helpers documentation
38
- def self.show_helpers_doc
38
+ def self.helpers_doc
39
39
  help = <<EOT
40
40
  Available commands:
41
41
  ==================
@@ -96,7 +96,7 @@ The parameters 'server', 'user' and 'password' must be defined.}
96
96
 
97
97
  desc 'Show this help text.'
98
98
  def help
99
- puts Zabby::ShellHelpers.show_helpers_doc
99
+ puts Zabby::ShellHelpers.helpers_doc
100
100
  end
101
101
  end
102
102
  end
@@ -4,5 +4,5 @@
4
4
  # License:: Simplified BSD License
5
5
 
6
6
  module Zabby
7
- VERSION = "0.1.1"
7
+ VERSION = "0.1.2"
8
8
  end
@@ -24,13 +24,31 @@ module Zabby
24
24
  module ClassMethods
25
25
  # List of valid Web Service methods for the current Zabbix Object
26
26
  attr_reader :zmethods
27
+ # The id field name for the model ("actionid", "hostid", etc.)
28
+ attr_reader :id
27
29
 
28
30
  # Name of the current class without the namespace
29
31
  # @return [String]
30
32
  # @example
31
- # Zabby::Host.object_name => "host"
32
- def object_name
33
- @object_name ||= self.name.gsub(/^.*::/, '').downcase
33
+ # Zabby::Host.object_name => "Host"
34
+ def class_name
35
+ @class_name ||= self.name.split(/::/).last.downcase
36
+ end
37
+
38
+ # Human representation of the Zabbix Class
39
+ # @return [String] Class representation
40
+ # @example
41
+ # Host.inspect => "<Zabby::Host methods=(create, delete, exists, get, update)>"
42
+ def inspect
43
+ "<#{name} methods=(#{@zmethods.join(', ')})>"
44
+ end
45
+
46
+ private
47
+
48
+ # Set the name of the primary key
49
+ # @param key [String] Primary key name
50
+ def primary_key(key)
51
+ @id = key.to_sym
34
52
  end
35
53
 
36
54
  # Add the list of Web Service methods to the current class.
@@ -52,19 +70,11 @@ module Zabby
52
70
  # @raise [NoMethodError] Raised on invalid method names.
53
71
  def method_missing(zmethod, *args, &block)
54
72
  if @zmethods.include? zmethod
55
- Zabby::Runner.instance.connection.perform_request(object_name, zmethod, args.first)
73
+ Zabby::Runner.instance.connection.perform_request(class_name, zmethod, args.first)
56
74
  else
57
75
  super
58
76
  end
59
77
  end
60
-
61
- # Human representation of the Zabbix Class
62
- # @return [String] Class representation
63
- # @example
64
- # Host.inspect => "<Zabbix Object 'host', methods: create, delete, exists, get, update>"
65
- def inspect
66
- "<Zabbix Object '#{object_name}', methods: #{@zmethods.join(', ')}>"
67
- end
68
78
  end
69
79
  end
70
80
 
@@ -72,11 +82,13 @@ module Zabby
72
82
 
73
83
  class Action
74
84
  include ZClass
85
+ primary_key :actionid
75
86
  add_zmethods :create, :delete, :exists, :get, :update
76
87
  end
77
88
 
78
89
  class Alert
79
90
  include ZClass
91
+ primary_key :alertid
80
92
  add_zmethods :create, :delete, :get
81
93
  end
82
94
 
@@ -87,106 +99,127 @@ module Zabby
87
99
 
88
100
  class Application
89
101
  include ZClass
102
+ primary_key :applicationid
90
103
  add_zmethods :create, :delete, :exists, :get, :massAdd, :update
91
104
  end
92
105
 
93
106
  class Event
94
107
  include ZClass
108
+ primary_key :eventid
95
109
  add_zmethods :acknowledge, :create, :delete, :get
96
110
  end
97
111
 
98
112
  class Graph
99
113
  include ZClass
114
+ primary_key :graphid
100
115
  add_zmethods :create, :delete, :exists, :get, :update
101
116
  end
102
117
 
103
118
  class Graphitem
104
119
  include ZClass
120
+ primary_key :gitemid
105
121
  add_zmethods :get
106
122
  end
107
123
 
108
124
  class History
109
125
  include ZClass
126
+ primary_key :id # TODO Verify. The online documentation is not clear.
110
127
  add_zmethods :delete, :get
111
128
  end
112
129
 
113
130
  class Host
114
131
  include ZClass
132
+ primary_key :hostid
115
133
  add_zmethods :create, :delete, :exists, :get, :update
116
134
  end
117
135
 
118
136
  class Hostgroup
119
137
  include ZClass
138
+ primary_key :groupid
120
139
  add_zmethods :create, :delete, :exists, :get, :massAdd, :massRemove, :massUpdate, :update
121
140
  end
122
141
 
123
142
  class Image
124
143
  include ZClass
144
+ primary_key :imageid
125
145
  add_zmethods :create, :delete, :exists, :get, :update
126
146
  end
127
147
 
128
148
  class Item
129
149
  include ZClass
150
+ primary_key :itemid
130
151
  add_zmethods :create, :delete, :exists, :get, :update
131
152
  end
132
153
 
133
154
  class Maintenance
134
155
  include ZClass
156
+ primary_key :maintenanceid
135
157
  add_zmethods :create, :delete, :exists, :get, :update
136
158
  end
137
159
 
138
160
  class Map
139
161
  include ZClass
162
+ primary_key :sysmapid
140
163
  add_zmethods :create, :delete, :exists, :get, :update
141
164
  end
142
165
 
143
166
  class Mediatype
144
167
  include ZClass
168
+ primary_key :mediatypeid
145
169
  add_zmethods :create, :delete, :get, :update
146
170
  end
147
171
 
148
172
  class Proxy
149
173
  include ZClass
174
+ primary_key :proxyid
150
175
  add_zmethods :get
151
176
  end
152
177
 
153
178
  class Screen
154
179
  include ZClass
180
+ primary_key :screenid
155
181
  add_zmethods :create, :delete, :get, :update
156
182
  end
157
183
 
158
184
  class Script
159
185
  include ZClass
186
+ primary_key :scriptid
160
187
  add_zmethods :create, :delete, :execute, :get, :update
161
188
  end
162
189
 
163
190
  class Template
164
191
  include ZClass
192
+ primary_key :templateid
165
193
  add_zmethods :create, :delete, :exists, :get, :massAdd, :massRemove, :massUpdate, :update
166
194
  end
167
195
 
168
196
  class Trigger
169
197
  include ZClass
198
+ primary_key :triggerid
170
199
  add_zmethods :addDependencies, :create, :delete, :deleteDependencies, :exists, :get, :update
171
200
  end
172
201
 
173
202
  class User
174
203
  include ZClass
204
+ primary_key :userid
175
205
  add_zmethods :addMedia, :authenticate, :create, :delete, :deleteMedia, :get, :login, :logout, :update, :updateMedia, :updateProfile
176
206
  end
177
207
 
178
208
  class Usergroup
179
209
  include ZClass
210
+ primary_key :usrgrpid
180
211
  add_zmethods :create, :delete, :exists, :get, :massAdd, :massRemove, :massUpdate, :update
181
212
  end
182
213
 
183
214
  class Usermacro
184
215
  include ZClass
216
+ primary_key :hostmacroid
185
217
  add_zmethods :createGlobal, :deleteGlobal, :deleteHostMacro, :get, :massAdd, :massRemove, :massUpdate, :updateGlobal
186
218
  end
187
219
 
188
220
  class Usermedia
189
221
  include ZClass
222
+ primary_key :mediatypeid
190
223
  add_zmethods :get
191
224
  end
192
225
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zabby
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Farzad FARID
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-03-14 00:00:00 Z
18
+ date: 2012-03-27 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: json