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 +4 -4
- data/.gitignore +1 -1
- data/.gitlab-ci.yml +12 -0
- data/CHANGELOG.md +9 -0
- data/Gemfile +1 -0
- data/LICENSE.txt +21 -0
- data/README.md +7 -3
- data/Rakefile +8 -1
- data/bin/zapishell.rb +1 -1
- data/exe/zapishell.rb +16 -2
- data/lib/zabbix/api/client.rb +112 -12
- data/lib/zabbix/api/version.rb +1 -1
- data/lib/zabbix/api.rb +0 -6
- data/public/Zabbix/Api/Client.html +1102 -0
- data/public/Zabbix/Api/FaradayMiddleware/ZabbixApiRequest.html +341 -0
- data/public/Zabbix/Api/FaradayMiddleware.html +115 -0
- data/public/Zabbix/Api/OpenStruct.html +384 -0
- data/public/Zabbix/Api/ZabbixStruct.html +384 -0
- data/public/Zabbix/Api.html +135 -0
- data/public/Zabbix.html +117 -0
- data/public/_index.html +180 -0
- data/public/class_list.html +51 -0
- data/public/css/common.css +1 -0
- data/public/css/full_list.css +58 -0
- data/public/css/style.css +497 -0
- data/public/file.CHANGELOG.html +89 -0
- data/public/file.LICENSE.html +70 -0
- data/public/file.README.html +190 -0
- data/public/file_list.html +66 -0
- data/public/frames.html +17 -0
- data/public/index.html +190 -0
- data/public/js/app.js +314 -0
- data/public/js/full_list.js +216 -0
- data/public/js/jquery.js +4 -0
- data/public/method_list.html +195 -0
- data/public/top-level-namespace.html +110 -0
- data/zabbix-api.gemspec +4 -3
- metadata +46 -6
- data/Gemfile.lock +0 -47
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 29edc33eb38ffc5637044d089db8de87c61188c9b939610f35fa17371cd68e54
|
4
|
+
data.tar.gz: 8695fad3a891752d7baae725742f816614e11259cc30977c1278db86c4598299
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 43e8c89766180de14705412c8c4983fe941d26782d05ccca9b215d7511e093f82ea0f16416e4025db9165cc40bd418d218f0e17882fbcdf0d03dd6e907a97b84
|
7
|
+
data.tar.gz: 78b1efab5a179280b9372758e70d9d0a040dbbb746c39b2e28b2c65d1069d16273ef47da075fbcf37d67cb4136c320058be621ec5e0f123c1ef3c5e989a4ce46
|
data/.gitignore
CHANGED
data/.gitlab-ci.yml
ADDED
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
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
|
-
**
|
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
|
-
|
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
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
|
-
|
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
|
data/lib/zabbix/api/client.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
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
|
-
|
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
|
data/lib/zabbix/api/version.rb
CHANGED