zabbix_api_gem 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.env.template +2 -0
- data/.gitignore +44 -0
- data/CHANGELOG.md +4 -0
- data/Gemfile +10 -0
- data/README.md +99 -0
- data/Rakefile +14 -0
- data/lib/zabbix/api.rb +39 -0
- data/lib/zabbix/authentication.rb +18 -0
- data/lib/zabbix/client.rb +23 -0
- data/lib/zabbix/error.rb +11 -0
- data/lib/zabbix/request.rb +36 -0
- data/lib/zabbix/version.rb +5 -0
- data/lib/zabbix.rb +25 -0
- data/lib/zabbix_api_gem.rb +3 -0
- data/zabbix.gemspec +36 -0
- metadata +129 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a6c8f297f2139a9f7c49a789f9e1ffd4600e5912d9e0b1a63330f761752fcd1e
|
4
|
+
data.tar.gz: 50b8942c322c9875c94c28a75080feb1b72f93910511e2bfa10c3353740577d7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ba97284983467df1d8b2ead9f52b04883f5978bf78c38d0d0c3fc229cbc5a07ce81cb3ec8d79c5332ec591e6c0f7943f60d7cea4c29a57d0650c8e6a764bfc08
|
7
|
+
data.tar.gz: 28858aaa64c5fb2e9666949d83108e1751192d64042023afb6e7800ff037fe6e810f972692e8e5179c0fd89d0d5e506e03b56c762821a2162ab1f5a026f6a757
|
data/.env.template
ADDED
data/.gitignore
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/spec/examples.txt
|
9
|
+
/test/tmp/
|
10
|
+
/test/version_tmp/
|
11
|
+
/tmp/
|
12
|
+
/data/
|
13
|
+
*.log
|
14
|
+
*.txt
|
15
|
+
*.json
|
16
|
+
*.yml
|
17
|
+
|
18
|
+
# Used by dotenv library to load environment variables.
|
19
|
+
.env
|
20
|
+
|
21
|
+
|
22
|
+
## Documentation cache and generated files:
|
23
|
+
/.yardoc/
|
24
|
+
/_yardoc/
|
25
|
+
/doc/
|
26
|
+
/rdoc/
|
27
|
+
|
28
|
+
## Environment normalization:
|
29
|
+
/.bundle/
|
30
|
+
/vendor/bundle
|
31
|
+
/lib/bundler/man/
|
32
|
+
|
33
|
+
# for a library or gem, you might want to ignore these files since the code is
|
34
|
+
# intended to run in multiple environments; otherwise, check them in:
|
35
|
+
# Gemfile.lock
|
36
|
+
# .ruby-version
|
37
|
+
# .ruby-gemset
|
38
|
+
|
39
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
40
|
+
.rvmrc
|
41
|
+
|
42
|
+
# Used by RuboCop. Remote config files pulled in from inherit_from directive.
|
43
|
+
# .rubocop-https?--*
|
44
|
+
Gemfile.lock
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
# Zabbix API
|
2
|
+
|
3
|
+
This is a wrapper for the Zabix rest API. You can see the API endpoints here https://www.zabbix.com/documentation/current/en/manual/api/reference/
|
4
|
+
|
5
|
+
Currently only the GET requests to get a list of hosts, host groups and problems are implemented.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'zabbix_api_gem'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install zabbix_api_gem
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Before you start making the requests to API provide the client id and client secret and email/password using the configuration wrapping.
|
26
|
+
|
27
|
+
```
|
28
|
+
require 'zabbix_api_gem'
|
29
|
+
|
30
|
+
# use do block
|
31
|
+
Zabbix.configure do |config|
|
32
|
+
config.endpoint = ENV["ZABBIX_API_HOST"]
|
33
|
+
config.access_token = ENV["ZABBIX_API_KEY"]
|
34
|
+
end
|
35
|
+
|
36
|
+
# or configure with options hash
|
37
|
+
client = Zabbix.client({ logger: Logger.new(CLIENT_LOGGER) })
|
38
|
+
|
39
|
+
client = Zabbix.client
|
40
|
+
client.login
|
41
|
+
|
42
|
+
hostgroups = client.hostgroups
|
43
|
+
companies.each do |c|
|
44
|
+
puts "#{c.name}"
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
## Resources
|
49
|
+
### Authentication
|
50
|
+
```
|
51
|
+
# setup configuration
|
52
|
+
#
|
53
|
+
client.login
|
54
|
+
```
|
55
|
+
|Resource|API endpoint|Description|
|
56
|
+
|:--|:--|:--|
|
57
|
+
|.login| none |uses settings.get to check if credentials are correct. Raises Zabbix:AuthenticationError incase this fails|
|
58
|
+
|
59
|
+
|
60
|
+
### Server settings
|
61
|
+
Return zabbix server settings
|
62
|
+
```
|
63
|
+
puts client.settings.default_theme
|
64
|
+
```
|
65
|
+
|
66
|
+
|Resource|API endpoint|
|
67
|
+
|:--|:--|
|
68
|
+
|.settings|settings.get|
|
69
|
+
|
70
|
+
|
71
|
+
|
72
|
+
### Data resourcews
|
73
|
+
Endpoint for data related requests
|
74
|
+
```
|
75
|
+
groups = client.hostgroups
|
76
|
+
group_hosts = client.hosts({groupids:[groups.first.groupid]})
|
77
|
+
|
78
|
+
group_hosts.each do |host|
|
79
|
+
client.problems({hostids:[host.hostid]}).each do |problem|
|
80
|
+
puts problem.name
|
81
|
+
end
|
82
|
+
end
|
83
|
+
```
|
84
|
+
|
85
|
+
|Resource|API endpoint|
|
86
|
+
|:--|:--|
|
87
|
+
|.hostgroups|hostgroup.get|
|
88
|
+
|.hosts|hosts.get|
|
89
|
+
|.problems|problems.get|
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
## Contributing
|
94
|
+
|
95
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/jancotanis/veeam.
|
96
|
+
|
97
|
+
## License
|
98
|
+
|
99
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
require 'rake/testtask'
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << 'test'
|
8
|
+
t.libs << 'lib'
|
9
|
+
t.test_files = FileList['test/**/*_test.rb']
|
10
|
+
end
|
11
|
+
|
12
|
+
require 'rubocop/rake_task'
|
13
|
+
RuboCop::RakeTask.new
|
14
|
+
task default: %i[test rubocop]
|
data/lib/zabbix/api.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require "wrapi"
|
2
|
+
require File.expand_path('request', __dir__)
|
3
|
+
require File.expand_path('authentication', __dir__)
|
4
|
+
|
5
|
+
module Zabbix
|
6
|
+
# @private
|
7
|
+
class API
|
8
|
+
|
9
|
+
# @private
|
10
|
+
attr_accessor *WrAPI::Configuration::VALID_OPTIONS_KEYS
|
11
|
+
|
12
|
+
# Creates a new API and copies settings from singleton
|
13
|
+
def initialize(options = {})
|
14
|
+
options = Zabbix.options.merge(options)
|
15
|
+
WrAPI::Configuration::VALID_OPTIONS_KEYS.each do |key|
|
16
|
+
send("#{key}=", options[key])
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def config
|
21
|
+
conf = {}
|
22
|
+
WrAPI::Configuration::VALID_OPTIONS_KEYS.each do |key|
|
23
|
+
conf[key] = send key
|
24
|
+
end
|
25
|
+
conf
|
26
|
+
end
|
27
|
+
|
28
|
+
# Convert zabbix clock to datetime
|
29
|
+
def zabbix_clock secs
|
30
|
+
Time.at( secs.to_i ).to_datetime
|
31
|
+
end
|
32
|
+
|
33
|
+
include WrAPI::Connection
|
34
|
+
include WrAPI::Request
|
35
|
+
include Request::JSONRPC2
|
36
|
+
include WrAPI::Authentication
|
37
|
+
include Authentication
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
|
2
|
+
module Zabbix
|
3
|
+
# Deals with authentication flow and stores it within global configuration
|
4
|
+
module Authentication
|
5
|
+
|
6
|
+
# https://helpcenter.veeam.com/docs/vac/rest/reference/vspc-rest.html?ver=80#tag/Authentication
|
7
|
+
# Authorize to the Veeam portal and return access_token
|
8
|
+
def login(options = {})
|
9
|
+
raise ArgumentError, "Accesstoken/api-key not set" unless access_token
|
10
|
+
# only bearer token needed
|
11
|
+
# will do sanitty check if token if valid
|
12
|
+
rpc_call('settings.get')
|
13
|
+
rescue RPCError => e
|
14
|
+
raise AuthenticationError, e
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.expand_path('api', __dir__)
|
2
|
+
|
3
|
+
module Zabbix
|
4
|
+
# Wrapper for the Zabbix REST API
|
5
|
+
#
|
6
|
+
# @note All methods have been separated into modules and follow the same grouping used in api docs
|
7
|
+
# @see https://www.zabbix.com/documentation/current/en/manual/api
|
8
|
+
class Client < API
|
9
|
+
|
10
|
+
def self.def_rpc_call(method, rpc_method)
|
11
|
+
self.send(:define_method, method) do |params = nil|
|
12
|
+
rpc_call(rpc_method, params)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def_rpc_call(:api_info, 'api.info')
|
17
|
+
def_rpc_call(:settings, 'settings.get')
|
18
|
+
def_rpc_call(:hostgroups, 'hostgroup.get')
|
19
|
+
def_rpc_call(:hosts, 'host.get')
|
20
|
+
def_rpc_call(:problems, 'problem.get')
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
data/lib/zabbix/error.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
module Zabbix
|
2
|
+
|
3
|
+
# Generic error to be able to rescue all Zabbix errors
|
4
|
+
class ZabbixError < StandardError; end
|
5
|
+
|
6
|
+
# Raised when Zabbix RPC protocol returns error object
|
7
|
+
class RPCError < ZabbixError; end
|
8
|
+
|
9
|
+
# Error when authentication fails
|
10
|
+
class AuthenticationError < ZabbixError; end
|
11
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
|
2
|
+
module Zabbix
|
3
|
+
# Deals with requests
|
4
|
+
module Request
|
5
|
+
|
6
|
+
# JSON-RPC is a stateless, light-weight remote procedure call (RPC) protocol. Primarily this
|
7
|
+
# specification defines several data structures and the rules around their processing. It is
|
8
|
+
# transport agnostic in that the concepts can be used within the same process, over sockets, over
|
9
|
+
# http, or in many various message passing environments. It uses JSON (RFC 4627) as data format.
|
10
|
+
#
|
11
|
+
# https://www.jsonrpc.org/specification
|
12
|
+
module JSONRPC2
|
13
|
+
|
14
|
+
@@id = 1
|
15
|
+
ZABBIX_ENDPOINT = '/zabbix/api_jsonrpc.php'
|
16
|
+
|
17
|
+
def rpc_call(method, params = nil)
|
18
|
+
options = {
|
19
|
+
"jsonrpc": "2.0",
|
20
|
+
"method": method,
|
21
|
+
"params": {
|
22
|
+
"output": "extend"
|
23
|
+
},
|
24
|
+
"id": @@id += 1
|
25
|
+
}
|
26
|
+
|
27
|
+
options[:params] = params if params
|
28
|
+
result = post( ZABBIX_ENDPOINT, options )
|
29
|
+
raise RPCError, result.body['error'] if result.body['error']
|
30
|
+
|
31
|
+
WrAPI::Request::Entity.new(result.body['result'])
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/zabbix.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require "wrapi"
|
2
|
+
require File.expand_path('zabbix/version', __dir__)
|
3
|
+
require File.expand_path('zabbix/error', __dir__)
|
4
|
+
require File.expand_path('zabbix/client', __dir__)
|
5
|
+
|
6
|
+
module Zabbix
|
7
|
+
extend WrAPI::Configuration
|
8
|
+
extend WrAPI::RespondTo
|
9
|
+
|
10
|
+
DEFAULT_UA = "Ruby Zabbix API wrapper #{Zabbix::VERSION}".freeze
|
11
|
+
|
12
|
+
#
|
13
|
+
# @return [Zabbix::Client]
|
14
|
+
def self.client(options = {})
|
15
|
+
Zabbix::Client.new({
|
16
|
+
user_agent: DEFAULT_UA
|
17
|
+
}.merge(options))
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.reset
|
21
|
+
super
|
22
|
+
self.endpoint = nil
|
23
|
+
self.user_agent = DEFAULT_UA
|
24
|
+
end
|
25
|
+
end
|
data/zabbix.gemspec
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/zabbix/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'zabbix_api_gem'
|
7
|
+
s.version = Zabbix::VERSION
|
8
|
+
s.authors = ['Janco Tanis']
|
9
|
+
s.email = 'gems@jancology.com'
|
10
|
+
s.license = 'MIT'
|
11
|
+
|
12
|
+
s.summary = 'A Ruby wrapper for the Zabbix APIs (readonly)'
|
13
|
+
s.homepage = 'https://rubygems.org/gems/zabbix'
|
14
|
+
|
15
|
+
s.required_ruby_version = Gem::Requirement.new('>= 2.4.0')
|
16
|
+
|
17
|
+
s.metadata['homepage_uri'] = s.homepage
|
18
|
+
s.metadata['source_code_uri'] = 'https://github.com/jancotanis/zabbix'
|
19
|
+
|
20
|
+
# Specify which files should be added to the gem when it is released.
|
21
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
|
+
s.files = Dir.chdir(File.expand_path(__dir__)) do
|
23
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
|
24
|
+
end
|
25
|
+
s.bindir = 'exe'
|
26
|
+
s.executables = s.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
27
|
+
s.require_paths = ['lib']
|
28
|
+
|
29
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
30
|
+
s.platform = Gem::Platform::RUBY
|
31
|
+
s.add_runtime_dependency 'faraday'
|
32
|
+
s.add_runtime_dependency 'wrapi', ">= 0.2.0"
|
33
|
+
s.add_development_dependency 'dotenv'
|
34
|
+
s.add_development_dependency 'minitest'
|
35
|
+
s.add_development_dependency 'rubocop'
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: zabbix_api_gem
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Janco Tanis
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-02-12 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: wrapi
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.2.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.2.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: dotenv
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description:
|
84
|
+
email: gems@jancology.com
|
85
|
+
executables: []
|
86
|
+
extensions: []
|
87
|
+
extra_rdoc_files: []
|
88
|
+
files:
|
89
|
+
- ".env.template"
|
90
|
+
- ".gitignore"
|
91
|
+
- CHANGELOG.md
|
92
|
+
- Gemfile
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- lib/zabbix.rb
|
96
|
+
- lib/zabbix/api.rb
|
97
|
+
- lib/zabbix/authentication.rb
|
98
|
+
- lib/zabbix/client.rb
|
99
|
+
- lib/zabbix/error.rb
|
100
|
+
- lib/zabbix/request.rb
|
101
|
+
- lib/zabbix/version.rb
|
102
|
+
- lib/zabbix_api_gem.rb
|
103
|
+
- zabbix.gemspec
|
104
|
+
homepage: https://rubygems.org/gems/zabbix
|
105
|
+
licenses:
|
106
|
+
- MIT
|
107
|
+
metadata:
|
108
|
+
homepage_uri: https://rubygems.org/gems/zabbix
|
109
|
+
source_code_uri: https://github.com/jancotanis/zabbix
|
110
|
+
post_install_message:
|
111
|
+
rdoc_options: []
|
112
|
+
require_paths:
|
113
|
+
- lib
|
114
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: 2.4.0
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
requirements: []
|
125
|
+
rubygems_version: 3.2.12
|
126
|
+
signing_key:
|
127
|
+
specification_version: 4
|
128
|
+
summary: A Ruby wrapper for the Zabbix APIs (readonly)
|
129
|
+
test_files: []
|