zabbix-api-simple 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +47 -0
- data/README.md +113 -0
- data/Rakefile +4 -0
- data/bin/bundle +114 -0
- data/bin/coderay +29 -0
- data/bin/console +15 -0
- data/bin/irb +29 -0
- data/bin/pry +29 -0
- data/bin/rake +29 -0
- data/bin/setup +8 -0
- data/bin/zapishell.rb +29 -0
- data/exe/zapishell.rb +23 -0
- data/lib/zabbix/api.rb +11 -0
- data/lib/zabbix/api/client.rb +109 -0
- data/lib/zabbix/api/version.rb +7 -0
- data/zabbix-api.gemspec +39 -0
- metadata +108 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 145656e58234b5023b8a15965919b2bbd0710aa2017982853a9dfb8d839d5d38
|
4
|
+
data.tar.gz: b3510b1c067cea088f1941924049d0b776019f55388884f1e49b72977db9319a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6688d815a3c0e48b7cfb44fada47f15c5058f9c81b2397cf6829b4c5a95eb129028bf043051ba4250e83b96990534aac5cceb6fdadc31d7c46504fbd4797079d
|
7
|
+
data.tar.gz: 4cde20d69dd48841abdf02faa92a259474866e67314fd93fa060b0aa47816c398970d00c91f12121009a2f734616d52ecda3169323d41e673704a707978b312b
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
zabbix-api (0.1.0)
|
5
|
+
amazing_print
|
6
|
+
faraday
|
7
|
+
optimist
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
amazing_print (1.3.0)
|
13
|
+
coderay (1.1.3)
|
14
|
+
faraday (1.4.1)
|
15
|
+
faraday-excon (~> 1.1)
|
16
|
+
faraday-net_http (~> 1.0)
|
17
|
+
faraday-net_http_persistent (~> 1.1)
|
18
|
+
multipart-post (>= 1.2, < 3)
|
19
|
+
ruby2_keywords (>= 0.0.4)
|
20
|
+
faraday-excon (1.1.0)
|
21
|
+
faraday-net_http (1.0.1)
|
22
|
+
faraday-net_http_persistent (1.1.0)
|
23
|
+
io-console (0.5.9)
|
24
|
+
irb (1.3.5)
|
25
|
+
reline (>= 0.1.5)
|
26
|
+
method_source (1.0.0)
|
27
|
+
multipart-post (2.1.1)
|
28
|
+
optimist (3.0.1)
|
29
|
+
pry (0.14.1)
|
30
|
+
coderay (~> 1.1)
|
31
|
+
method_source (~> 1.0)
|
32
|
+
rake (13.0.3)
|
33
|
+
reline (0.2.5)
|
34
|
+
io-console (~> 0.5)
|
35
|
+
ruby2_keywords (0.0.4)
|
36
|
+
|
37
|
+
PLATFORMS
|
38
|
+
ruby
|
39
|
+
|
40
|
+
DEPENDENCIES
|
41
|
+
irb
|
42
|
+
pry
|
43
|
+
rake (~> 13.0)
|
44
|
+
zabbix-api!
|
45
|
+
|
46
|
+
BUNDLED WITH
|
47
|
+
2.2.16
|
data/README.md
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
# Zabbix::Api
|
2
|
+
|
3
|
+
This zabbix api implementation for Ruby strives for three things:
|
4
|
+
|
5
|
+
- To the greatest extent possible, be syntactically & structurally identical to the API as it is described in the Zabbix documentation
|
6
|
+
- Be API version agnostic
|
7
|
+
- Be internally simple, unobtrusive, and low-maintenance
|
8
|
+
|
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
|
+
|
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.
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'zabbix-api'
|
19
|
+
```
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle install
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install zabbix-api
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
Once you have an authenticated instance of the client, syntax is basically always this:
|
32
|
+
|
33
|
+
`clientinstance.object.verb(<param kv pairs>)`
|
34
|
+
|
35
|
+
If "object" is unknown, "Unknown zabbix object given" will be raised.
|
36
|
+
|
37
|
+
If this call results in an error from the API, a RuntimeError will be raised indicating the specifics.
|
38
|
+
|
39
|
+
Complete detail about the last transaction you attempted is available via `clientinstance.last`
|
40
|
+
|
41
|
+
e.g.:
|
42
|
+
|
43
|
+
```
|
44
|
+
#!/usr/bin/env ruby
|
45
|
+
require 'zabbix/api'
|
46
|
+
require 'optimist'
|
47
|
+
require 'amazing_print'
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
opts = Optimist::options do
|
52
|
+
opt :url, "URL up to but no including api_jsonrpc.php",type: :string,default:'http://localhost'
|
53
|
+
opt :user, "User name to authenticate", type: :string, required: true
|
54
|
+
opt :pass, "Pass to auth user with", type: :string, required: true
|
55
|
+
opt :hostname, "Host name to search for", type: :string, required: true
|
56
|
+
end
|
57
|
+
|
58
|
+
api = Zabbix::Api::Client.new(opts[:url])
|
59
|
+
|
60
|
+
api.login(user: opts[:user],pass:opts[:pass])
|
61
|
+
|
62
|
+
hosts = api.host.get(search: {host: opts[:hostname]}) # return value is an OpenStruct
|
63
|
+
|
64
|
+
hosts.each {|host|
|
65
|
+
puts host.name
|
66
|
+
puts host.hostid
|
67
|
+
|
68
|
+
ap host.to_h
|
69
|
+
}
|
70
|
+
|
71
|
+
|
72
|
+
api.host.create(
|
73
|
+
host: "mynewhost",
|
74
|
+
interfaces: {
|
75
|
+
type: 1,
|
76
|
+
main: 1,
|
77
|
+
useip: 1,
|
78
|
+
ip: "1.2.3.4",
|
79
|
+
dns: "",
|
80
|
+
port: "10050"
|
81
|
+
},
|
82
|
+
groups: {groupid: "42"}
|
83
|
+
)
|
84
|
+
|
85
|
+
ap api.last # shows detail of the last transaction
|
86
|
+
|
87
|
+
ap api.logout
|
88
|
+
```
|
89
|
+
|
90
|
+
There's a little cli program that gets installed with the gem called zapishell.rb:
|
91
|
+
|
92
|
+
```
|
93
|
+
# zapishell.rb --help
|
94
|
+
Options:
|
95
|
+
-u, --url=<s> URL up to but no including api_jsonrpc.php (default: http://localhost)
|
96
|
+
-s, --user=<s> User name to authenticate
|
97
|
+
-p, --pass=<s> Pass to auth user with
|
98
|
+
-h, --help Show this message
|
99
|
+
```
|
100
|
+
|
101
|
+
It attempts to authenticate you to the given server, and then drops you to a pry prompt. You can play around with the api in that by doing `api.object.verb(key: 'value pairs')`
|
102
|
+
|
103
|
+
Refer to the API documentation for your version of Zabbix for the particulars.
|
104
|
+
|
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
|
+
## Contributing
|
112
|
+
|
113
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/zabbix-api.
|
data/Rakefile
ADDED
data/bin/bundle
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
#!/usr/bin/env ruby2.7
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
#
|
5
|
+
# This file was generated by Bundler.
|
6
|
+
#
|
7
|
+
# The application 'bundle' is installed as part of a gem, and
|
8
|
+
# this file is here to facilitate running it.
|
9
|
+
#
|
10
|
+
|
11
|
+
require "rubygems"
|
12
|
+
|
13
|
+
m = Module.new do
|
14
|
+
module_function
|
15
|
+
|
16
|
+
def invoked_as_script?
|
17
|
+
File.expand_path($0) == File.expand_path(__FILE__)
|
18
|
+
end
|
19
|
+
|
20
|
+
def env_var_version
|
21
|
+
ENV["BUNDLER_VERSION"]
|
22
|
+
end
|
23
|
+
|
24
|
+
def cli_arg_version
|
25
|
+
return unless invoked_as_script? # don't want to hijack other binstubs
|
26
|
+
return unless "update".start_with?(ARGV.first || " ") # must be running `bundle update`
|
27
|
+
bundler_version = nil
|
28
|
+
update_index = nil
|
29
|
+
ARGV.each_with_index do |a, i|
|
30
|
+
if update_index && update_index.succ == i && a =~ Gem::Version::ANCHORED_VERSION_PATTERN
|
31
|
+
bundler_version = a
|
32
|
+
end
|
33
|
+
next unless a =~ /\A--bundler(?:[= ](#{Gem::Version::VERSION_PATTERN}))?\z/
|
34
|
+
bundler_version = $1
|
35
|
+
update_index = i
|
36
|
+
end
|
37
|
+
bundler_version
|
38
|
+
end
|
39
|
+
|
40
|
+
def gemfile
|
41
|
+
gemfile = ENV["BUNDLE_GEMFILE"]
|
42
|
+
return gemfile if gemfile && !gemfile.empty?
|
43
|
+
|
44
|
+
File.expand_path("../../Gemfile", __FILE__)
|
45
|
+
end
|
46
|
+
|
47
|
+
def lockfile
|
48
|
+
lockfile =
|
49
|
+
case File.basename(gemfile)
|
50
|
+
when "gems.rb" then gemfile.sub(/\.rb$/, gemfile)
|
51
|
+
else "#{gemfile}.lock"
|
52
|
+
end
|
53
|
+
File.expand_path(lockfile)
|
54
|
+
end
|
55
|
+
|
56
|
+
def lockfile_version
|
57
|
+
return unless File.file?(lockfile)
|
58
|
+
lockfile_contents = File.read(lockfile)
|
59
|
+
return unless lockfile_contents =~ /\n\nBUNDLED WITH\n\s{2,}(#{Gem::Version::VERSION_PATTERN})\n/
|
60
|
+
Regexp.last_match(1)
|
61
|
+
end
|
62
|
+
|
63
|
+
def bundler_version
|
64
|
+
@bundler_version ||=
|
65
|
+
env_var_version || cli_arg_version ||
|
66
|
+
lockfile_version
|
67
|
+
end
|
68
|
+
|
69
|
+
def bundler_requirement
|
70
|
+
return "#{Gem::Requirement.default}.a" unless bundler_version
|
71
|
+
|
72
|
+
bundler_gem_version = Gem::Version.new(bundler_version)
|
73
|
+
|
74
|
+
requirement = bundler_gem_version.approximate_recommendation
|
75
|
+
|
76
|
+
return requirement unless Gem::Version.new(Gem::VERSION) < Gem::Version.new("2.7.0")
|
77
|
+
|
78
|
+
requirement += ".a" if bundler_gem_version.prerelease?
|
79
|
+
|
80
|
+
requirement
|
81
|
+
end
|
82
|
+
|
83
|
+
def load_bundler!
|
84
|
+
ENV["BUNDLE_GEMFILE"] ||= gemfile
|
85
|
+
|
86
|
+
activate_bundler
|
87
|
+
end
|
88
|
+
|
89
|
+
def activate_bundler
|
90
|
+
gem_error = activation_error_handling do
|
91
|
+
gem "bundler", bundler_requirement
|
92
|
+
end
|
93
|
+
return if gem_error.nil?
|
94
|
+
require_error = activation_error_handling do
|
95
|
+
require "bundler/version"
|
96
|
+
end
|
97
|
+
return if require_error.nil? && Gem::Requirement.new(bundler_requirement).satisfied_by?(Gem::Version.new(Bundler::VERSION))
|
98
|
+
warn "Activating bundler (#{bundler_requirement}) failed:\n#{gem_error.message}\n\nTo install the version of bundler this project requires, run `gem install bundler -v '#{bundler_requirement}'`"
|
99
|
+
exit 42
|
100
|
+
end
|
101
|
+
|
102
|
+
def activation_error_handling
|
103
|
+
yield
|
104
|
+
nil
|
105
|
+
rescue StandardError, LoadError => e
|
106
|
+
e
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
m.load_bundler!
|
111
|
+
|
112
|
+
if m.invoked_as_script?
|
113
|
+
load Gem.bin_path("bundler", "bundle")
|
114
|
+
end
|
data/bin/coderay
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env ruby2.7
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
#
|
5
|
+
# This file was generated by Bundler.
|
6
|
+
#
|
7
|
+
# The application 'coderay' is installed as part of a gem, and
|
8
|
+
# this file is here to facilitate running it.
|
9
|
+
#
|
10
|
+
|
11
|
+
require "pathname"
|
12
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
13
|
+
Pathname.new(__FILE__).realpath)
|
14
|
+
|
15
|
+
bundle_binstub = File.expand_path("../bundle", __FILE__)
|
16
|
+
|
17
|
+
if File.file?(bundle_binstub)
|
18
|
+
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
|
19
|
+
load(bundle_binstub)
|
20
|
+
else
|
21
|
+
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
|
22
|
+
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
require "rubygems"
|
27
|
+
require "bundler/setup"
|
28
|
+
|
29
|
+
load Gem.bin_path("coderay", "coderay")
|
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "zabbix/api"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "pry"
|
15
|
+
Pry.start
|
data/bin/irb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env ruby2.7
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
#
|
5
|
+
# This file was generated by Bundler.
|
6
|
+
#
|
7
|
+
# The application 'irb' is installed as part of a gem, and
|
8
|
+
# this file is here to facilitate running it.
|
9
|
+
#
|
10
|
+
|
11
|
+
require "pathname"
|
12
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
13
|
+
Pathname.new(__FILE__).realpath)
|
14
|
+
|
15
|
+
bundle_binstub = File.expand_path("../bundle", __FILE__)
|
16
|
+
|
17
|
+
if File.file?(bundle_binstub)
|
18
|
+
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
|
19
|
+
load(bundle_binstub)
|
20
|
+
else
|
21
|
+
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
|
22
|
+
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
require "rubygems"
|
27
|
+
require "bundler/setup"
|
28
|
+
|
29
|
+
load Gem.bin_path("irb", "irb")
|
data/bin/pry
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env ruby2.7
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
#
|
5
|
+
# This file was generated by Bundler.
|
6
|
+
#
|
7
|
+
# The application 'pry' is installed as part of a gem, and
|
8
|
+
# this file is here to facilitate running it.
|
9
|
+
#
|
10
|
+
|
11
|
+
require "pathname"
|
12
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
13
|
+
Pathname.new(__FILE__).realpath)
|
14
|
+
|
15
|
+
bundle_binstub = File.expand_path("../bundle", __FILE__)
|
16
|
+
|
17
|
+
if File.file?(bundle_binstub)
|
18
|
+
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
|
19
|
+
load(bundle_binstub)
|
20
|
+
else
|
21
|
+
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
|
22
|
+
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
require "rubygems"
|
27
|
+
require "bundler/setup"
|
28
|
+
|
29
|
+
load Gem.bin_path("pry", "pry")
|
data/bin/rake
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env ruby2.7
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
#
|
5
|
+
# This file was generated by Bundler.
|
6
|
+
#
|
7
|
+
# The application 'rake' is installed as part of a gem, and
|
8
|
+
# this file is here to facilitate running it.
|
9
|
+
#
|
10
|
+
|
11
|
+
require "pathname"
|
12
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
13
|
+
Pathname.new(__FILE__).realpath)
|
14
|
+
|
15
|
+
bundle_binstub = File.expand_path("../bundle", __FILE__)
|
16
|
+
|
17
|
+
if File.file?(bundle_binstub)
|
18
|
+
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
|
19
|
+
load(bundle_binstub)
|
20
|
+
else
|
21
|
+
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
|
22
|
+
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
require "rubygems"
|
27
|
+
require "bundler/setup"
|
28
|
+
|
29
|
+
load Gem.bin_path("rake", "rake")
|
data/bin/setup
ADDED
data/bin/zapishell.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env ruby2.7
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
#
|
5
|
+
# This file was generated by Bundler.
|
6
|
+
#
|
7
|
+
# The application 'zapishell.rb' is installed as part of a gem, and
|
8
|
+
# this file is here to facilitate running it.
|
9
|
+
#
|
10
|
+
|
11
|
+
require "pathname"
|
12
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
13
|
+
Pathname.new(__FILE__).realpath)
|
14
|
+
|
15
|
+
bundle_binstub = File.expand_path("../bundle", __FILE__)
|
16
|
+
|
17
|
+
if File.file?(bundle_binstub)
|
18
|
+
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
|
19
|
+
load(bundle_binstub)
|
20
|
+
else
|
21
|
+
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
|
22
|
+
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
require "rubygems"
|
27
|
+
require "bundler/setup"
|
28
|
+
|
29
|
+
load Gem.bin_path("zabbix-api", "zapishell.rb")
|
data/exe/zapishell.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'zabbix/api'
|
3
|
+
require 'optimist'
|
4
|
+
require 'amazing_print'
|
5
|
+
require 'pry'
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
opts = Optimist::options do
|
10
|
+
opt :url, "URL up to but no including api_jsonrpc.php",type: :string,default:'http://localhost'
|
11
|
+
opt :user, "User name to authenticate", type: :string, required: true
|
12
|
+
opt :pass, "Pass to auth user with", type: :string, required: true
|
13
|
+
end
|
14
|
+
|
15
|
+
print "user.login: "
|
16
|
+
api = Zabbix::Api::Client.new(opts[:url])
|
17
|
+
|
18
|
+
ap api.login(user: opts[:user],pass:opts[:pass])
|
19
|
+
|
20
|
+
binding.pry
|
21
|
+
|
22
|
+
print "user.logout: "
|
23
|
+
ap api.logout
|
data/lib/zabbix/api.rb
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
module Zabbix
|
2
|
+
module Api
|
3
|
+
|
4
|
+
require 'faraday'
|
5
|
+
require 'json'
|
6
|
+
require 'set'
|
7
|
+
require 'pry'
|
8
|
+
|
9
|
+
module FaradayMiddleware
|
10
|
+
|
11
|
+
class ZabbixApiRequest < Faraday::Middleware
|
12
|
+
|
13
|
+
def initialize(app)
|
14
|
+
super(app)
|
15
|
+
@app=app
|
16
|
+
end
|
17
|
+
|
18
|
+
def on_request(env)
|
19
|
+
env[:request_body][:jsonrpc] = "2.0"
|
20
|
+
env[:request_body][:id] = "1"
|
21
|
+
env[:request_body] = env[:request_body].to_json
|
22
|
+
end
|
23
|
+
|
24
|
+
def on_complete(env)
|
25
|
+
env[:response_body] = JSON.parse(env[:response_body])
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
Faraday::Request.register_middleware zabbix_api_request: -> { ZabbixApiRequest }
|
31
|
+
|
32
|
+
end # module FaradayMiddleware
|
33
|
+
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
class Client
|
38
|
+
|
39
|
+
@@apiurl = 'api_jsonrpc.php'
|
40
|
+
|
41
|
+
@@zabbix_objects = [:action,:alert,:apiinfo,:application,:configuration,
|
42
|
+
:correlation,:dashboard,:dhost,:dservice,:dcheck,
|
43
|
+
:drule,:event,:graph,:graphitem,:graphprototype,
|
44
|
+
:history,:host,:hostgroup,:hostinterface,
|
45
|
+
:hostprototype,:iconmap,:image,:item,:itemprototype,
|
46
|
+
:discoveryrule,:maintenance,:map,:mediatype,:problem,
|
47
|
+
:proxy,:screen,:screenitem,:script,:service,:task,
|
48
|
+
:template,:templatescreen,:templatescreenitem,
|
49
|
+
:trend,:trigger,:triggerprototype,:user,:usergroup,
|
50
|
+
:usermacro,:valuemap,:httptest].to_set
|
51
|
+
|
52
|
+
|
53
|
+
attr_reader :conn,:token,:last
|
54
|
+
attr_accessor :zabobject
|
55
|
+
|
56
|
+
|
57
|
+
def initialize(url)
|
58
|
+
@conn = Faraday.new(
|
59
|
+
url: url,
|
60
|
+
headers: {'Content-Type' => 'application/json-rpc'}
|
61
|
+
) do |conn|
|
62
|
+
conn.request :zabbix_api_request
|
63
|
+
end
|
64
|
+
@zabobject = nil
|
65
|
+
end
|
66
|
+
|
67
|
+
def post(args)
|
68
|
+
args[:params] = [] if not args.has_key?(:params) or args[:params].nil?
|
69
|
+
@last = @conn.post(@@apiurl, args)
|
70
|
+
end
|
71
|
+
|
72
|
+
def login(user: nil,pass: nil)
|
73
|
+
res =post(method: 'user.login', params: {user: user, password:pass}, auth:nil)
|
74
|
+
@token = res.body['result']
|
75
|
+
OpenStruct.new(res.body)
|
76
|
+
end
|
77
|
+
|
78
|
+
def logout
|
79
|
+
OpenStruct.new(post(method: 'user.logout', params: [], auth: @token).body)
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
def method_missing(name, *args, &block)
|
84
|
+
if @@zabbix_objects.include?(name)
|
85
|
+
# Clone self cuz we want to be thread safe/recursable. This will pop off the
|
86
|
+
# stack after it's no longer referenced (and @zabobject will never change in the
|
87
|
+
# original client instance)
|
88
|
+
newcli = self.clone
|
89
|
+
newcli.zabobject = name
|
90
|
+
return newcli
|
91
|
+
elsif @zabobject
|
92
|
+
object = @zabobject
|
93
|
+
res = post(method: "#{object}.#{name}", params: args.first, id: '1', auth: @token).body
|
94
|
+
raise res['error'].awesome_inspect(plain: true) if res.has_key?('error')
|
95
|
+
if res.has_key?('result') and res['result'].class == Array
|
96
|
+
res = res['result'].collect{|each| OpenStruct.new(each)}
|
97
|
+
else
|
98
|
+
res = OpenStruct.new(res)
|
99
|
+
end
|
100
|
+
return res
|
101
|
+
else
|
102
|
+
raise "Unknown zabbix object given: #{name}"
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
107
|
+
|
108
|
+
end # module Api
|
109
|
+
end # module Zabbix
|
data/zabbix-api.gemspec
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/zabbix/api/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "zabbix-api-simple"
|
7
|
+
spec.version = Zabbix::Api::VERSION
|
8
|
+
spec.authors = ["David Parker"]
|
9
|
+
spec.email = ["daveparker01@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "Zabbix API wrapper"
|
12
|
+
spec.description = "This Zabbix API library strives to be syntactically as similar as possible to the API as described by the Zabbix documentation"
|
13
|
+
spec.homepage = "https://gitlab.com/svdasein/zabbix-api"
|
14
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
|
15
|
+
|
16
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
17
|
+
|
18
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
19
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
20
|
+
#spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
21
|
+
|
22
|
+
# Specify which files should be added to the gem when it is released.
|
23
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
24
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
25
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
|
26
|
+
end
|
27
|
+
spec.bindir = "exe"
|
28
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
29
|
+
spec.require_paths = ["lib"]
|
30
|
+
|
31
|
+
# Uncomment to register a new dependency of your gem
|
32
|
+
# spec.add_dependency "example-gem", "~> 1.0"
|
33
|
+
|
34
|
+
# For more information and examples about making a new gem, checkout our
|
35
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
36
|
+
spec.add_dependency 'faraday'
|
37
|
+
spec.add_dependency 'optimist'
|
38
|
+
spec.add_dependency 'amazing_print'
|
39
|
+
end
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: zabbix-api-simple
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David Parker
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-04-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: optimist
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: amazing_print
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: This Zabbix API library strives to be syntactically as similar as possible
|
56
|
+
to the API as described by the Zabbix documentation
|
57
|
+
email:
|
58
|
+
- daveparker01@gmail.com
|
59
|
+
executables:
|
60
|
+
- zapishell.rb
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- ".gitignore"
|
65
|
+
- Gemfile
|
66
|
+
- Gemfile.lock
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/bundle
|
70
|
+
- bin/coderay
|
71
|
+
- bin/console
|
72
|
+
- bin/irb
|
73
|
+
- bin/pry
|
74
|
+
- bin/rake
|
75
|
+
- bin/setup
|
76
|
+
- bin/zapishell.rb
|
77
|
+
- exe/zapishell.rb
|
78
|
+
- lib/zabbix/api.rb
|
79
|
+
- lib/zabbix/api/client.rb
|
80
|
+
- lib/zabbix/api/version.rb
|
81
|
+
- zabbix-api.gemspec
|
82
|
+
homepage: https://gitlab.com/svdasein/zabbix-api
|
83
|
+
licenses: []
|
84
|
+
metadata:
|
85
|
+
allowed_push_host: https://rubygems.org
|
86
|
+
homepage_uri: https://gitlab.com/svdasein/zabbix-api
|
87
|
+
source_code_uri: https://gitlab.com/svdasein/zabbix-api
|
88
|
+
post_install_message:
|
89
|
+
rdoc_options: []
|
90
|
+
require_paths:
|
91
|
+
- lib
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 2.4.0
|
97
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
requirements: []
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 2.7.6
|
105
|
+
signing_key:
|
106
|
+
specification_version: 4
|
107
|
+
summary: Zabbix API wrapper
|
108
|
+
test_files: []
|