zabbix-api-simple 0.1.2 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a8f4e84a0886f658f05bd62ffbe03f0586006a7a1aff9b97a7915c900e2edc6e
4
- data.tar.gz: d0cd327492b60c398a23d4e4885fa6445e9c4ea1e6b4aff02b264d9c529a8072
3
+ metadata.gz: a22800f0fc86c622d9aed2347de3474493f11063afe93574722d9775d3e183a7
4
+ data.tar.gz: 3e2d7da66ec9eec2a62b793016af6190b3682e23151599a0e5b92bf336b0ac51
5
5
  SHA512:
6
- metadata.gz: a997c5ceaa9730a349965dab37bf09435d723641ad2a8649a848e347673ff317ae90f8eaa1ceb65378b54a1e3eacf7974d2ec6565bd1edb64f458e9a30f43351
7
- data.tar.gz: 7bc895f73dd85e5a2e1e9ebcd4774d1ba9b4ca911c1abf394825d267dbb27ed2690993c6afcd0fee5d02617959b1bab042403edf306c0aa3a220ee62b4bcee47
6
+ metadata.gz: eff8065f139dbeefff931f0939b65ef5e1205e0d8d2869e5c6fecff3582b4f4c12182ff0084bbc3a40f8d9f702d31d382eda3585fe609c6509b7c8a7de6c3487
7
+ data.tar.gz: 07c4520f7944ec3a6d5253af1416c78fb62419fe885cae49db5e9409ecb7ef7b208fb56e69e69abd6918d9e69d0151beae9b2105410566bf712b462d018fbb5e
data/.gitignore CHANGED
@@ -1,7 +1,7 @@
1
1
  /.bundle/
2
2
  /.yardoc
3
3
  /_yardoc/
4
- /Gemfile.lock/
4
+ Gemfile.lock
5
5
  /coverage/
6
6
  /doc/
7
7
  /pkg/
data/.gitlab-ci.yml ADDED
@@ -0,0 +1,12 @@
1
+ image: alpine:latest
2
+
3
+ pages:
4
+ stage: deploy
5
+ script:
6
+ - echo 'Nothing to do...'
7
+ artifacts:
8
+ paths:
9
+ - public
10
+ expire_in: never
11
+ only:
12
+ - master
data/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ # 0.1.4
2
+ - Just doc changes
3
+ # 0.1.5
4
+ - deps fix for pry
5
+ - add known_objects method
6
+ - add exception trap for json parse issues w/ note about php memory
7
+ - change zapishell.rb to start in the api context
8
+ - add exception for apiinfo.version (refuses auth token for some reason)
9
+ - add pretty inspection mode to zapishell.rb
10
+ # 0.1.6
11
+ - force faraday 1.8 - 2.0 appears to break things
data/Gemfile CHANGED
@@ -8,6 +8,7 @@ gemspec
8
8
  group :development do
9
9
  gem 'irb'
10
10
  gem 'pry'
11
+ gem 'yard'
11
12
  end
12
13
 
13
14
  gem "rake", "~> 13.0"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 David Parker
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Zabbix::Api
1
+ # Zabbix::Api [![Gem Version](https://badge.fury.io/rb/zabbix-api-simple.svg)](https://badge.fury.io/rb/zabbix-api-simple)
2
2
 
3
3
  This zabbix api implementation for Ruby strives for three things:
4
4
 
@@ -8,7 +8,11 @@ This zabbix api implementation for Ruby strives for three things:
8
8
 
9
9
  It accomplishes these goals primarily by deferring (via Faraday) to the Zabbix API itself for just about everything - it does not attempt to re-create or otherwise "fancy-up" the API. The reasoning behind this is that the zabbix api itself is complex enough, and the Zabbix project can alter the specifics of the api at any time. This library just provides Ruby semantics that closely follow the API specification. The advantage to you is that you do not need to wrap your head around two APIs (the Zabbix API proper, and the API library's API)
10
10
 
11
- **NOTE:** This thing is brand new, and it works for me. I'm certain I'm far from done adding stuff to it, but as it is at this moment it's good enough for me. While I doubt it, there may be breaking changes going forward, so just "buyer beware". I'll remove this note once I feel it's more or less stable.
11
+ **Detailed documentation for this library is [>>HERE<<](https://svdasein.gitlab.io/zabbix-api)**
12
+
13
+ **Source repository is [>>HERE<<](https://gitlab.com/svdasein/zabbix-api)**
14
+
15
+ If you need to send data into Zabbix via the "Zabbix trapper" protocol, you might also be interested in [zabbix_sender_api](https://svdasein.gitlab.io/zabbix_sender_api)
12
16
 
13
17
  ## Installation
14
18
 
@@ -32,6 +36,8 @@ Once you have an authenticated instance of the client, syntax is basically alway
32
36
 
33
37
  `clientinstance.object.verb(<param kv pairs>)`
34
38
 
39
+ ![simple translation](images/zabbix-api-simple-simplicity.png)
40
+
35
41
  If "object" is unknown, "Unknown zabbix object given" will be raised.
36
42
 
37
43
  If this call results in an error from the API, a RuntimeError will be raised indicating the specifics.
@@ -55,7 +61,7 @@ opts = Optimist::options do
55
61
  opt :hostname, "Host name to search for", type: :string, required: true
56
62
  end
57
63
 
58
- api = Zabbix::Api::Client.new(opts[:url])
64
+ api = Zabbix::Api::Client.new(url: opts[:url])
59
65
 
60
66
  api.login(user: opts[:user],pass:opts[:pass])
61
67
 
@@ -102,12 +108,6 @@ It attempts to authenticate you to the given server, and then drops you to a pry
102
108
 
103
109
  Refer to the API documentation for your version of Zabbix for the particulars.
104
110
 
105
- ## Development
106
-
107
- After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
108
-
109
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
110
-
111
111
  ## Contributing
112
112
 
113
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/zabbix-api.
113
+ Bug reports and pull requests are welcome on GitLab at https://gitlab.com/svdasein/zabbix-api
data/Rakefile CHANGED
@@ -1,4 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "bundler/gem_tasks"
4
- task default: %i[]
4
+ desc "Generate yard docs in public"
5
+ task :gendocs do
6
+ puts %x(yard -o public --files CHANGELOG.md,LICENSE.txt)
7
+ puts %x(git add public/*)
8
+ end
9
+ desc "Generate docs and run :build"
10
+ task :buildall => [ :gendocs, :build ]
11
+ task default: :buildall
data/bin/zapishell.rb CHANGED
@@ -26,4 +26,4 @@ end
26
26
  require "rubygems"
27
27
  require "bundler/setup"
28
28
 
29
- load Gem.bin_path("zabbix-api", "zapishell.rb")
29
+ load Gem.bin_path("zabbix-api-simple", "zapishell.rb")
data/exe/zapishell.rb CHANGED
@@ -12,12 +12,26 @@ opts = Optimist::options do
12
12
  opt :pass, "Pass to auth user with", type: :string, required: true
13
13
  end
14
14
 
15
+
16
+
17
+ Zabbix::Api::OpenStruct.prettymode = true
18
+
15
19
  print "user.login: "
16
20
  api = Zabbix::Api::Client.new(url: opts[:url])
17
21
 
18
22
  ap api.login(user: opts[:user],pass:opts[:pass])
19
23
 
20
- binding.pry
24
+ puts <<-END
25
+
26
+ Enter api commands e.g. host.get
27
+
28
+ execute known_objects for a list of known zabbix object types
29
+
30
+ quit to exit - your session will be logged out for you
31
+
32
+ END
33
+
34
+ api.pry
21
35
 
22
36
  print "user.logout: "
23
37
  ap api.logout
Binary file
Binary file
@@ -4,10 +4,15 @@ module Zabbix
4
4
  require 'faraday'
5
5
  require 'json'
6
6
  require 'set'
7
+ require 'amazing_print'
7
8
  require 'pry'
8
9
 
9
10
  module FaradayMiddleware
10
11
 
12
+ ##
13
+ # this middleware adapter does zabbix api essential things
14
+ # at the REST level, and hendles serializing/de-serializing
15
+ # ruby objects.
11
16
  class ZabbixApiRequest < Faraday::Middleware
12
17
 
13
18
  def initialize(app)
@@ -22,7 +27,11 @@ module Zabbix
22
27
  end
23
28
 
24
29
  def on_complete(env)
25
- env[:response_body] = JSON.parse(env[:response_body])
30
+ begin
31
+ env[:response_body] = JSON.parse(env[:response_body])
32
+ rescue JSON::ParserError => e
33
+ env[:response_body] = {error: e,note: 'Check api web configuration (e.g. url, PHP memory, etc)'}
34
+ end
26
35
  end
27
36
 
28
37
  end
@@ -33,7 +42,42 @@ module Zabbix
33
42
 
34
43
 
35
44
 
45
+ ##
46
+ # Subclass of OpenStruct adds some niceties for REPL etc
47
+ #
48
+ class OpenStruct < OpenStruct
49
+
50
+ ##
51
+ # If true, inspect returns awesome_inspect instead of default
52
+ @@prettymode = false
36
53
 
54
+ class << self
55
+ ##
56
+ # returns current value of prettymode
57
+ #
58
+ def prettymode
59
+ @@prettymode
60
+ end
61
+ ##
62
+ # sets prettymod to aBool
63
+ #
64
+ def prettymode=(aBool)
65
+ @@prettymode = aBool
66
+ end
67
+ end
68
+
69
+ def inspect
70
+ if @@prettymode
71
+ awesome_inspect
72
+ else
73
+ super
74
+ end
75
+ end
76
+
77
+ end
78
+
79
+ ##
80
+ # Each instance of this class acts as a connection Zabbix's API
37
81
  class Client
38
82
 
39
83
  class << self
@@ -43,6 +87,11 @@ module Zabbix
43
87
 
44
88
  @@apiurl = 'api_jsonrpc.php'
45
89
 
90
+ ##
91
+ # This is a list (as of zabbix 5.0) of top-level zabbix API calls
92
+ # that the client will understand via method_missing. If they
93
+ # add new things to the api they need to be added here as well
94
+ # *if* you intend on using the method_missing syntax.
46
95
  @@zabbix_objects = [:action,:alert,:apiinfo,:application,:configuration,
47
96
  :correlation,:dashboard,:dhost,:dservice,:dcheck,
48
97
  :drule,:event,:graph,:graphitem,:graphprototype,
@@ -59,6 +108,10 @@ module Zabbix
59
108
  attr_accessor :zabobject
60
109
 
61
110
 
111
+ ##
112
+ # :url is required. You do not need to add 'api_jsonrpc.php' - this will
113
+ # happen automagically. You can alter request timeout if needed by passing
114
+ # :timeout - the default is 60 secs
62
115
  def initialize(url: nil,timeout: 60)
63
116
  @conn = Faraday.new(
64
117
  url: url,
@@ -70,6 +123,9 @@ module Zabbix
70
123
  @zabobject = nil
71
124
  end
72
125
 
126
+ ##
127
+ # This method posts a list of params to @conn/@@apiurl. You likely won't
128
+ # have need to call this directly.
73
129
  def post(args)
74
130
  args[:params] = [] if not args.has_key?(:params) or args[:params].nil?
75
131
  last = @conn.post(@@apiurl, args)
@@ -77,17 +133,66 @@ module Zabbix
77
133
  return last
78
134
  end
79
135
 
136
+ ##
137
+ # both :user and :pass are required. This method calls user.logic
138
+ # abd stores the returned auth token for future calls to the api
80
139
  def login(user: nil,pass: nil)
81
140
  res =post(method: 'user.login', params: {user: user, password:pass}, auth:nil)
82
141
  @token = res.body['result']
83
142
  OpenStruct.new(res.body)
84
143
  end
85
144
 
145
+ ##
146
+ # calls user.logout for the @token session. You really should pay
147
+ # attention to ensuring that this gets called, else you'll find over time
148
+ # that your sessions table is getting quite large and will start slowing
149
+ # down lots of stuff in zabbix.
86
150
  def logout
87
151
  OpenStruct.new(post(method: 'user.logout', params: [], auth: @token).body)
88
152
  end
89
153
 
154
+ ##
155
+ # this is the method that method_missing calls to peform the actual work.
156
+ # The first parameter is the top-level api call (e.g. those listed in
157
+ # @@zabbix_objects. Args is a hash containing the particulars for the call.
158
+ # You can call this directly if you don't want to rely on method_missing.
159
+ #
160
+ # results are returned as instances of OpenStruct. If you need this to be
161
+ # a hash just do to_h to the returned object.
162
+ def call(name, *args, &block)
163
+ res = nil
164
+ if name == 'apiinfo.version'
165
+ res = post(method: "#{name}", params: args.first, id: '1').body
166
+ else
167
+ res = post(method: "#{name}", params: args.first, id: '1', auth: @token).body
168
+ end
169
+ raise res['error'].awesome_inspect(plain: true) if res.has_key?('error')
170
+ if res.has_key?('result') and res['result'].class == Array
171
+ res = res['result'].collect{|each| OpenStruct.new(each)}
172
+ else
173
+ res = OpenStruct.new(res)
174
+ end
175
+ return res
176
+ end
177
+
178
+ ##
179
+ # just handy if you're doing a REPL (e.g. zapishell.rb) Returns
180
+ # list of zabbix objects the api currently is aware of
181
+ #
182
+ def known_objects
183
+ @@zabbix_objects
184
+ end
90
185
 
186
+
187
+ ##
188
+ # this override of method_missing is the trick that lets zabbix-api-simple
189
+ # look quite a lot like the zabbix api documentation. If it finds that the
190
+ # method name you were trying to call is in @@zabbix_objects, then it constructs
191
+ # a call to that top level api entity using the parameters as arguments to the call.
192
+ #
193
+ # this is really just here as syntactical sugar - you don't *have* to use it, but
194
+ # if you do you'll find that you need do essentially zero mental translation between
195
+ # the zabbix api documentation and your code.
91
196
  def method_missing(name, *args, &block)
92
197
  if @@zabbix_objects.include?(name)
93
198
  # Clone self cuz we want to be thread safe/recursable. This will pop off the
@@ -97,20 +202,14 @@ module Zabbix
97
202
  newcli.zabobject = name
98
203
  return newcli
99
204
  elsif @zabobject
100
- object = @zabobject
101
- res = post(method: "#{object}.#{name}", params: args.first, id: '1', auth: @token).body
102
- raise res['error'].awesome_inspect(plain: true) if res.has_key?('error')
103
- if res.has_key?('result') and res['result'].class == Array
104
- res = res['result'].collect{|each| OpenStruct.new(each)}
105
- else
106
- res = OpenStruct.new(res)
107
- end
108
- return res
205
+ return call("#{@zabobject}.#{name}",args.first)
109
206
  else
110
207
  raise "Unknown zabbix object given: #{name}"
111
208
  end
112
209
  end
113
210
 
211
+ ##
212
+ # returns the last response the client got from the server
114
213
  def last
115
214
  Client.last
116
215
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Zabbix
4
4
  module Api
5
- VERSION = "0.1.2"
5
+ VERSION = "0.1.6"
6
6
  end
7
7
  end
data/lib/zabbix/api.rb CHANGED
@@ -3,9 +3,3 @@
3
3
  require_relative "api/version"
4
4
  require_relative "api/client"
5
5
 
6
- module Zabbix
7
- module Api
8
- class Error < StandardError; end
9
- # Your code goes here...
10
- end
11
- end