zabbix-api-simple 0.1.3 → 0.2.0

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: 35647dac01d85faed96daeacd718c183ffb90ee374f97e93a1b8108408def350
4
- data.tar.gz: 6ad7fc1f2fa168f35fce665da63d156a163e81705c42a33059081b96adb72e15
3
+ metadata.gz: 767eed1d8880dfac1dcae338914a324ead075de7502f9899ab15088715757c78
4
+ data.tar.gz: 9709424f2953c6485e75c9b859db99b7ecba9ddc57600490c19e596c7a750276
5
5
  SHA512:
6
- metadata.gz: 187014b38f74a272f2f90a7fae4dfe2a17bb772f842a2f12001bfca4eba135e0f4edebfa5f361a3e131361597d1181f7cee402e09bb525f6ea038fc52e6f816b
7
- data.tar.gz: 94a0eae1c0d62056128f6c2a55eee1418c5e516ff475fda580a941a8ba9c0e0a5da4402db3eb6c1cc0f365d1782305765ed666749d81328b968bc0d636353f54
6
+ metadata.gz: bedb840a9e1a83a21f635a3818fc4569168521bbec3f96e186262f3c107d8a04e402d9cf44eed52dd6d547ef17ed0f2cb018d59f4bea2166a77eb309e311ad27
7
+ data.tar.gz: 40eb31a36188f50aa05e6a8fdd0559f026a240c10eefb4aec560d3be49ce324832a28291719ce04289d03ad852023cb18efe0cb8efdf2840e0cc1d70ba529b7e
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.
@@ -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/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
@@ -1,13 +1,18 @@
1
1
  module Zabbix
2
2
  module Api
3
3
 
4
- require 'faraday'
4
+ require 'faraday/net_http'
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,18 +27,57 @@ 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
29
38
 
30
- Faraday::Request.register_middleware zabbix_api_request: -> { ZabbixApiRequest }
39
+ Faraday::Request.register_middleware(zabbix_api_request: ZabbixApiRequest)
31
40
 
32
41
  end # module FaradayMiddleware
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,7 +108,12 @@ 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)
116
+ Faraday.default_adapter = :net_http
63
117
  @conn = Faraday.new(
64
118
  url: url,
65
119
  headers: {'Content-Type' => 'application/json-rpc'},
@@ -70,6 +124,9 @@ module Zabbix
70
124
  @zabobject = nil
71
125
  end
72
126
 
127
+ ##
128
+ # This method posts a list of params to @conn/@@apiurl. You likely won't
129
+ # have need to call this directly.
73
130
  def post(args)
74
131
  args[:params] = [] if not args.has_key?(:params) or args[:params].nil?
75
132
  last = @conn.post(@@apiurl, args)
@@ -77,18 +134,39 @@ module Zabbix
77
134
  return last
78
135
  end
79
136
 
137
+ ##
138
+ # both :user and :pass are required. This method calls user.logic
139
+ # abd stores the returned auth token for future calls to the api
80
140
  def login(user: nil,pass: nil)
81
141
  res =post(method: 'user.login', params: {user: user, password:pass}, auth:nil)
82
142
  @token = res.body['result']
83
143
  OpenStruct.new(res.body)
84
144
  end
85
145
 
146
+ ##
147
+ # calls user.logout for the @token session. You really should pay
148
+ # attention to ensuring that this gets called, else you'll find over time
149
+ # that your sessions table is getting quite large and will start slowing
150
+ # down lots of stuff in zabbix.
86
151
  def logout
87
152
  OpenStruct.new(post(method: 'user.logout', params: [], auth: @token).body)
88
153
  end
89
154
 
155
+ ##
156
+ # this is the method that method_missing calls to peform the actual work.
157
+ # The first parameter is the top-level api call (e.g. those listed in
158
+ # @@zabbix_objects. Args is a hash containing the particulars for the call.
159
+ # You can call this directly if you don't want to rely on method_missing.
160
+ #
161
+ # results are returned as instances of OpenStruct. If you need this to be
162
+ # a hash just do to_h to the returned object.
90
163
  def call(name, *args, &block)
164
+ res = nil
165
+ if name == 'apiinfo.version'
166
+ res = post(method: "#{name}", params: args.first, id: '1').body
167
+ else
91
168
  res = post(method: "#{name}", params: args.first, id: '1', auth: @token).body
169
+ end
92
170
  raise res['error'].awesome_inspect(plain: true) if res.has_key?('error')
93
171
  if res.has_key?('result') and res['result'].class == Array
94
172
  res = res['result'].collect{|each| OpenStruct.new(each)}
@@ -98,7 +176,24 @@ module Zabbix
98
176
  return res
99
177
  end
100
178
 
179
+ ##
180
+ # just handy if you're doing a REPL (e.g. zapishell.rb) Returns
181
+ # list of zabbix objects the api currently is aware of
182
+ #
183
+ def known_objects
184
+ @@zabbix_objects
185
+ end
186
+
101
187
 
188
+ ##
189
+ # this override of method_missing is the trick that lets zabbix-api-simple
190
+ # look quite a lot like the zabbix api documentation. If it finds that the
191
+ # method name you were trying to call is in @@zabbix_objects, then it constructs
192
+ # a call to that top level api entity using the parameters as arguments to the call.
193
+ #
194
+ # this is really just here as syntactical sugar - you don't *have* to use it, but
195
+ # if you do you'll find that you need do essentially zero mental translation between
196
+ # the zabbix api documentation and your code.
102
197
  def method_missing(name, *args, &block)
103
198
  if @@zabbix_objects.include?(name)
104
199
  # Clone self cuz we want to be thread safe/recursable. This will pop off the
@@ -114,6 +209,8 @@ module Zabbix
114
209
  end
115
210
  end
116
211
 
212
+ ##
213
+ # returns the last response the client got from the server
117
214
  def last
118
215
  Client.last
119
216
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Zabbix
4
4
  module Api
5
- VERSION = "0.1.3"
5
+ VERSION = "0.2.0"
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