zabbix-api-simple 0.1.1 → 0.1.5

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: 6e4b7542ca901c9a7b69721322dfa5cac8294a10c5af3c55fb1e3064f19df156
4
- data.tar.gz: 076dd443693be95e728b601cda49dbc34a93191b62b9b14ea07b351c3cb6158a
3
+ metadata.gz: 29edc33eb38ffc5637044d089db8de87c61188c9b939610f35fa17371cd68e54
4
+ data.tar.gz: 8695fad3a891752d7baae725742f816614e11259cc30977c1278db86c4598299
5
5
  SHA512:
6
- metadata.gz: 45debfd58a35cc793df8fedf2eded4886f4c085bfdff418c35f3f35b8b2ba10973a1305eecef0650f81b4a991e3687efb86a7a1a35aa16695369a219ccd1b484
7
- data.tar.gz: 0aebc9edd72680f02eca0b7373b2fea2877019cacf39bca631c308cb5639ea711476bba2acc332d20bfc1051a36bc1ff4636606e59cb60a10adbf89c080c7b00
6
+ metadata.gz: 43e8c89766180de14705412c8c4983fe941d26782d05ccca9b215d7511e093f82ea0f16416e4025db9165cc40bd418d218f0e17882fbcdf0d03dd6e907a97b84
7
+ data.tar.gz: 78b1efab5a179280b9372758e70d9d0a040dbbb746c39b2e28b2c65d1069d16273ef47da075fbcf37d67cb4136c320058be621ec5e0f123c1ef3c5e989a4ce46
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,9 @@
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
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
 
@@ -55,7 +59,7 @@ opts = Optimist::options do
55
59
  opt :hostname, "Host name to search for", type: :string, required: true
56
60
  end
57
61
 
58
- api = Zabbix::Api::Client.new(opts[:url])
62
+ api = Zabbix::Api::Client.new(url: opts[:url])
59
63
 
60
64
  api.login(user: opts[:user],pass:opts[:pass])
61
65
 
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
- api = Zabbix::Api::Client.new(opts[:url])
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
@@ -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,16 +108,24 @@ module Zabbix
59
108
  attr_accessor :zabobject
60
109
 
61
110
 
62
- def initialize(url)
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
115
+ def initialize(url: nil,timeout: 60)
63
116
  @conn = Faraday.new(
64
117
  url: url,
65
- headers: {'Content-Type' => 'application/json-rpc'}
118
+ headers: {'Content-Type' => 'application/json-rpc'},
119
+ request: { timeout: timeout }
66
120
  ) do |conn|
67
121
  conn.request :zabbix_api_request
68
122
  end
69
123
  @zabobject = nil
70
124
  end
71
125
 
126
+ ##
127
+ # This method posts a list of params to @conn/@@apiurl. You likely won't
128
+ # have need to call this directly.
72
129
  def post(args)
73
130
  args[:params] = [] if not args.has_key?(:params) or args[:params].nil?
74
131
  last = @conn.post(@@apiurl, args)
@@ -76,17 +133,66 @@ module Zabbix
76
133
  return last
77
134
  end
78
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
79
139
  def login(user: nil,pass: nil)
80
140
  res =post(method: 'user.login', params: {user: user, password:pass}, auth:nil)
81
141
  @token = res.body['result']
82
142
  OpenStruct.new(res.body)
83
143
  end
84
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.
85
150
  def logout
86
151
  OpenStruct.new(post(method: 'user.logout', params: [], auth: @token).body)
87
152
  end
88
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
89
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.
90
196
  def method_missing(name, *args, &block)
91
197
  if @@zabbix_objects.include?(name)
92
198
  # Clone self cuz we want to be thread safe/recursable. This will pop off the
@@ -96,20 +202,14 @@ module Zabbix
96
202
  newcli.zabobject = name
97
203
  return newcli
98
204
  elsif @zabobject
99
- object = @zabobject
100
- res = post(method: "#{object}.#{name}", params: args.first, id: '1', auth: @token).body
101
- raise res['error'].awesome_inspect(plain: true) if res.has_key?('error')
102
- if res.has_key?('result') and res['result'].class == Array
103
- res = res['result'].collect{|each| OpenStruct.new(each)}
104
- else
105
- res = OpenStruct.new(res)
106
- end
107
- return res
205
+ return call("#{@zabobject}.#{name}",args.first)
108
206
  else
109
207
  raise "Unknown zabbix object given: #{name}"
110
208
  end
111
209
  end
112
210
 
211
+ ##
212
+ # returns the last response the client got from the server
113
213
  def last
114
214
  Client.last
115
215
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Zabbix
4
4
  module Api
5
- VERSION = "0.1.1"
5
+ VERSION = "0.1.5"
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