zabbix_api_gem 0.3.3 → 0.3.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7f320a0b0f09957b5daca1b574e6b47665823214b2bc3df38e1c09dc6baf84be
4
- data.tar.gz: 528c6c270141c9662417247fb72c013818c841ac92cbc5474d68bd49d31d815b
3
+ metadata.gz: 1d9caf35864059444cdcda6d50357ea93aec46aca168097f25664c870d1b1b29
4
+ data.tar.gz: 1defae29de7348948fa323a99476748902456512e479b6c704cc0e926d377b15
5
5
  SHA512:
6
- metadata.gz: 918cf731282b13be7f4624e36d6ec635bc43386e5cf33785cd3c09c8c1b09b15849c108390f481874ad5731734b2c1b9ad675c3c566bd78c3195b24cb82b9e9a
7
- data.tar.gz: 89fc1ff2554b76f98f2605e26cce055d4e4fbc46658b134e01ff2d26663b4b71a57357cdc9d2fb35b1c86fbd200eff35c748b364e463652daadc1f5a15036cdc
6
+ metadata.gz: cac93c6741f01358190714aa44f78f9929851bc7ddb758789c5e4cd47ca0506e850cc4dfa4471202d70c6a3ac26b16075263c7b629c6afcaedf38ad9bde26e1e
7
+ data.tar.gz: 35c41b3d7b640a9f71bfea83dd4b5afc439cdbff5d1c0bac9f56139986c9bc2864d0d4cd4bbd619efeee8a3db683af342f68ddef2ca228d26bec8fc375ae59d2
data/CHANGELOG.md CHANGED
@@ -1,20 +1,29 @@
1
- ## [Unreleased]
1
+ # Changelog
2
2
 
3
3
  ## [0.1.0] - 2024-02-09
4
+
4
5
  - Initial release
5
6
 
6
7
  ## [0.2.0] - 2024-02-12
8
+
7
9
  - Include events and method to get object by id
8
10
 
9
11
  ## [0.3.0] - 2024-02-20
12
+
10
13
  - ConfigurationError raised if not configured properly
11
14
 
12
15
  ## [0.3.1] - 2024-02-22
16
+
13
17
  - Fix problem with entity creation
14
18
 
15
19
  ## [0.3.2] - 2024-03-21
20
+
16
21
  - Refactory extract method
17
22
 
18
23
  ## [0.3.3] - 2024-03-21
24
+
19
25
  - Fix issues with extract method
20
26
 
27
+ ## [0.3.4] - 2024-03-21
28
+
29
+ - Fix issues with problems api and use eventids parameter
data/Gemfile CHANGED
@@ -8,4 +8,4 @@ gemspec
8
8
  gem 'rake', '~> 13.0'
9
9
  gem 'rubocop', '~> 1.7'
10
10
  gem 'simplecov', require: false, group: :test
11
- gem 'wrapi'
11
+ gem 'wrapi'
data/README.md CHANGED
@@ -1,9 +1,11 @@
1
1
  # Zabbix API
2
+
2
3
  [![Version](https://img.shields.io/gem/v/zabbix_api_gem.svg)](https://rubygems.org/gems/zabbix_api_gem)
3
4
  [![Maintainability](https://api.codeclimate.com/v1/badges/de2999161abdc8179fde/maintainability)](https://codeclimate.com/github/jancotanis/zabbix/maintainability)
4
5
  [![Test Coverage](https://api.codeclimate.com/v1/badges/de2999161abdc8179fde/test_coverage)](https://codeclimate.com/github/jancotanis/zabbix/test_coverage)
5
6
 
6
- 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/
7
+ This is a wrapper for the Zabix rest API. You can see the
8
+ API endpoints [here](https://www.zabbix.com/documentation/current/en/manual/api/reference/)
7
9
 
8
10
  Currently only the GET requests to get a list of hosts, host groups and problems are implemented.
9
11
 
@@ -17,15 +19,20 @@ gem 'zabbix_api_gem'
17
19
 
18
20
  And then execute:
19
21
 
20
- $ bundle install
22
+ ```console
23
+ > bundle install
24
+ ```
21
25
 
22
26
  Or install it yourself as:
23
27
 
24
- $ gem install zabbix_api_gem
28
+ ```console
29
+ > gem install zabbix_api_gem
30
+ ```
25
31
 
26
32
  ## Usage
27
33
 
28
- Before you start making the requests to API provide the client id and client secret and email/password using the configuration wrapping.
34
+ Before you start making the requests to API provide the client id and client secret and
35
+ email/password using the configuration wrapping.
29
36
 
30
37
  ```ruby
31
38
  require 'zabbix_api_gem'
@@ -49,19 +56,23 @@ end
49
56
  ```
50
57
 
51
58
  ## Resources
59
+
52
60
  ### Authentication
61
+
53
62
  ```ruby
54
63
  # setup configuration
55
64
  #
56
65
  client.login
57
66
  ```
67
+
58
68
  |Resource|API endpoint|Description|
59
69
  |:--|:--|:--|
60
70
  |.login| none |uses settings.get to check if credentials are correct. Raises Zabbix:AuthenticationError incase this fails|
61
71
 
62
-
63
72
  ### Server settings
73
+
64
74
  Return zabbix server settings
75
+
65
76
  ```ruby
66
77
  puts client.settings.default_theme
67
78
  ```
@@ -70,10 +81,10 @@ puts client.settings.default_theme
70
81
  |:--|:--|
71
82
  |.settings|settings.get|
72
83
 
73
-
74
-
75
84
  ### Data resourcews
85
+
76
86
  Endpoint for data related requests
87
+
77
88
  ```
78
89
  groups = client.hostgroups
79
90
  group_hosts = client.hosts({groupids:[groups.first.groupid]})
@@ -92,11 +103,9 @@ end
92
103
  |.problems, problem(event_id)|problems.get|
93
104
  |.events, event(event_id)|problems.get|
94
105
 
95
-
96
-
97
106
  ## Contributing
98
107
 
99
- Bug reports and pull requests are welcome on GitHub at https://github.com/jancotanis/zabbix.
108
+ Bug reports and pull requests are welcome on [GitHub](https://github.com/jancotanis/zabbix).
100
109
 
101
110
  ## License
102
111
 
data/lib/zabbix/api.rb CHANGED
@@ -1,35 +1,71 @@
1
- require "wrapi"
1
+ # frozen_string_literal: true
2
+
3
+ require 'wrapi'
2
4
  require File.expand_path('request', __dir__)
3
5
  require File.expand_path('authentication', __dir__)
4
6
 
5
7
  module Zabbix
6
- # @private
8
+ # This class is the core API client that interfaces with Zabbix.
9
+ # It manages configuration options, API connections, and helper utilities.
10
+ #
11
+ # @example Create a new Zabbix API client:
12
+ # api = Zabbix::API.new(endpoint: 'https://zabbix.example.com/api_jsonrpc.php', api_key: 'your_key')
13
+ #
14
+ # @note This class includes connection, request handling, JSON-RPC 2.0 support, and authentication from WrAPI.
7
15
  class API
16
+ # Allow read/write access to configuration keys like endpoint, user_agent, and others.
17
+ # These keys are inherited from `WrAPI::Configuration::VALID_OPTIONS_KEYS`.
18
+ #
19
+ # @!attribute [rw] endpoint
20
+ # @return [String] The base URL of the Zabbix API.
21
+ # @!attribute [rw] user_agent
22
+ # @return [String] The user agent used in API requests.
23
+ attr_accessor(*WrAPI::Configuration::VALID_OPTIONS_KEYS)
8
24
 
9
- # @private
10
- attr_accessor *WrAPI::Configuration::VALID_OPTIONS_KEYS
11
-
12
- # Creates a new API and copies settings from singleton
25
+ # Initialize a new Zabbix::API instance and copy settings from the singleton configuration.
26
+ #
27
+ # @param options [Hash] Configuration options for the Zabbix API client.
28
+ # Options override any default values set in the singleton Zabbix configuration.
29
+ #
30
+ # @option options [String] :endpoint The base URL of the Zabbix API.
31
+ # @option options [String] :user_agent The User-Agent header sent with requests.
32
+ # @option options [Integer] :timeout Request timeout in seconds.
13
33
  def initialize(options = {})
14
- options = Zabbix.options.merge(options)
34
+ options = Zabbix.options.merge(options) # Merge provided options with default options
15
35
  WrAPI::Configuration::VALID_OPTIONS_KEYS.each do |key|
16
- send("#{key}=", options[key])
36
+ send("#{key}=", options[key]) # Assign each configuration option dynamically
17
37
  end
18
38
  end
19
39
 
40
+ # Retrieve the current configuration for the API client.
41
+ #
42
+ # @return [Hash] A hash containing the current configuration options.
43
+ # @example Get the client configuration:
44
+ # api = Zabbix::API.new
45
+ # puts api.config # => { endpoint: "https://...", user_agent: "...", timeout: 60 }
20
46
  def config
21
47
  conf = {}
22
48
  WrAPI::Configuration::VALID_OPTIONS_KEYS.each do |key|
23
- conf[key] = send key
49
+ conf[key] = send(key) # Build a hash of current configuration values
24
50
  end
25
51
  conf
26
52
  end
27
53
 
28
- # Convert zabbix clock to datetime
29
- def zabbix_clock secs
30
- Time.at( secs.to_i ).to_datetime
54
+ # Convert a Zabbix clock timestamp to a Ruby DateTime object.
55
+ #
56
+ # @param secs [String, Integer] The Zabbix clock time in seconds since the Unix epoch.
57
+ # @return [DateTime] A DateTime object representing the input time.
58
+ #
59
+ # @example Convert Zabbix clock to DateTime:
60
+ # api.zabbix_clock(1684500000) # => #<DateTime: 2023-05-19T09:20:00+00:00>
61
+ def zabbix_clock(secs)
62
+ Time.at(secs.to_i).to_datetime # Convert seconds since Unix epoch to DateTime
31
63
  end
32
64
 
65
+ # Includes core modules from WrAPI and Zabbix:
66
+ # - Connection: For managing HTTP connections.
67
+ # - Request: For handling JSON-RPC 2.0 requests.
68
+ # - Authentication: For managing API authentication.
33
69
  include WrAPI::Connection
34
70
  include WrAPI::Request
35
71
  include Request::JSONRPC2
@@ -1,18 +1,18 @@
1
+ # frozen_string_literal: true
1
2
 
2
3
  module Zabbix
3
4
  # Deals with authentication flow and stores it within global configuration
4
5
  module Authentication
5
-
6
6
  # Authorize to the Zabbix portal using the access_token
7
7
  # @see https://www.zabbix.com/documentation/current/en/manual/api
8
- def login(options = {})
9
- raise ConfigurationError, "Accesstoken/api-key not set" unless access_token
10
- # only bearer token needed
8
+ def login(_options = {})
9
+ raise ConfigurationError, 'Accesstoken/api-key not set' unless access_token
10
+
11
+ # only bearer token needed
11
12
  # will do sanity check if token if valid
12
13
  rpc_call('settings.get')
13
14
  rescue RPCError => e
14
15
  raise AuthenticationError, e
15
16
  end
16
-
17
17
  end
18
18
  end
data/lib/zabbix/client.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require File.expand_path('api', __dir__)
2
4
 
3
5
  module Zabbix
@@ -6,34 +8,30 @@ module Zabbix
6
8
  # @note All methods have been separated into modules and follow the same grouping used in api docs
7
9
  # @see https://www.zabbix.com/documentation/current/en/manual/api
8
10
  class Client < API
9
- private
10
11
  def self.def_rpc_call(method, rpc_method, id_field = nil)
11
12
  self.send(:define_method, method) do |params = nil|
12
13
  rpc_call(rpc_method, params)
13
14
  end
14
-
15
15
  # singular by stripping last trailing 's'
16
16
  self.singular_method(method.to_s.chop.to_sym, rpc_method, id_field) if id_field
17
17
  end
18
-
19
- def self.singular_method method, rpc_method, id_field
20
- self.send(:define_method, method) do |ids,params = nil|
18
+
19
+ def self.singular_method(method, rpc_method, id_field)
20
+ self.send(:define_method, method) do |ids, params = nil|
21
21
  if ids.is_a?(Array)
22
- rpc_call(rpc_method, {"#{id_field}":ids}.merge(params || {}))
22
+ rpc_call(rpc_method, { "#{id_field}": ids}.merge(params || { }))
23
23
  else
24
- rpc_call(rpc_method, {"#{id_field}":[ids]}.merge(params || {})).first
24
+ rpc_call(rpc_method, { "#{id_field}": [ids]}.merge(params || { })).first
25
25
  end
26
26
  end
27
27
  end
28
28
 
29
- public
30
29
  def_rpc_call :api_info, 'api.info'
31
30
  def_rpc_call :settings, 'settings.get'
32
31
  def_rpc_call :hostgroups, 'hostgroup.get', 'groupids'
33
32
  def_rpc_call :hosts, 'host.get', 'hostids'
34
- def_rpc_call :problems, 'problem.get', 'problemids'
33
+ def_rpc_call :problems, 'problem.get', 'eventids'
35
34
  def_rpc_call :events, 'event.get', 'eventids'
36
35
  def_rpc_call :acknowledge_events, 'event.acknowledge'
37
-
38
36
  end
39
37
  end
data/lib/zabbix/error.rb CHANGED
@@ -1,5 +1,6 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Zabbix
2
-
3
4
  # Generic error to be able to rescue all Zabbix errors
4
5
  class ZabbixError < StandardError; end
5
6
 
@@ -11,4 +12,4 @@ module Zabbix
11
12
 
12
13
  # Error when authentication fails
13
14
  class AuthenticationError < ZabbixError; end
14
- end
15
+ end
@@ -1,8 +1,8 @@
1
+ # frozen_string_literal: true
1
2
 
2
3
  module Zabbix
3
4
  # Deals with requests
4
5
  module Request
5
-
6
6
  # JSON-RPC is a stateless, light-weight remote procedure call (RPC) protocol. Primarily this
7
7
  # specification defines several data structures and the rules around their processing. It is
8
8
  # transport agnostic in that the concepts can be used within the same process, over sockets, over
@@ -16,21 +16,20 @@ module Zabbix
16
16
 
17
17
  def rpc_call(method, params = nil)
18
18
  options = {
19
- "jsonrpc": "2.0",
20
- "method": method,
21
- "params": {
22
- "output": "extend"
19
+ 'jsonrpc': '2.0',
20
+ 'method': method,
21
+ 'params': {
22
+ 'output': 'extend'
23
23
  },
24
- "id": @@id += 1
24
+ 'id': @@id += 1
25
25
  }
26
26
 
27
27
  options[:params] = params if params
28
- result = post( ZABBIX_ENDPOINT, options )
28
+ result = post(ZABBIX_ENDPOINT, options)
29
29
  raise RPCError, result.body['error'] if result.body['error']
30
30
 
31
31
  WrAPI::Request::Entity.create(result.body['result'])
32
32
  end
33
-
34
33
  end
35
34
  end
36
35
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Zabbix
4
- VERSION = '0.3.3'
4
+ VERSION = '0.3.4'
5
5
  end
data/lib/zabbix.rb CHANGED
@@ -1,25 +1,51 @@
1
- require "wrapi"
1
+ # frozen_string_literal: true
2
+
3
+ require 'wrapi'
2
4
  require File.expand_path('zabbix/version', __dir__)
3
5
  require File.expand_path('zabbix/error', __dir__)
4
6
  require File.expand_path('zabbix/client', __dir__)
5
7
 
8
+ # This module provides a Ruby API wrapper for Zabbix.
9
+ # It includes configuration options and creates client instances for interacting with the Zabbix API.
10
+ #
11
+ # @example Initialize a Zabbix client:
12
+ # client = Zabbix.client(endpoint: 'https://your-zabbix-server/api_jsonrpc.php')
13
+ #
6
14
  module Zabbix
7
15
  extend WrAPI::Configuration
8
16
  extend WrAPI::RespondTo
9
17
 
10
- DEFAULT_UA = "Ruby Zabbix API wrapper #{Zabbix::VERSION}".freeze
18
+ # Default User-Agent string for API requests.
19
+ DEFAULT_UA = "Ruby Zabbix API wrapper #{Zabbix::VERSION}"
11
20
 
21
+ # Creates and returns a new Zabbix client instance.
22
+ # Merges default client settings with any user-provided options.
23
+ #
24
+ # @param options [Hash] Optional settings to configure the client.
25
+ # - `:user_agent` (String) – Custom User-Agent string for API requests.
26
+ # - `:endpoint` (String) – The API endpoint URL (e.g., 'https://zabbix.example.com/api_jsonrpc.php').
27
+ #
28
+ # @return [Zabbix::Client] A configured client instance.
29
+ #
30
+ # @example Create a client with a custom User-Agent and endpoint:
31
+ # client = Zabbix.client(user_agent: "MyApp Zabbix Client", endpoint: "https://zabbix.example.com/api_jsonrpc.php")
12
32
  #
13
- # @return [Zabbix::Client]
14
33
  def self.client(options = {})
34
+ # Ensure that the options argument is a Hash to prevent merge errors
35
+ options = options.is_a?(Hash) ? options : {}
15
36
  Zabbix::Client.new({
16
37
  user_agent: DEFAULT_UA
17
38
  }.merge(options))
18
39
  end
19
40
 
41
+ # Resets the Zabbix module configuration to its default state.
42
+ #
43
+ # This method sets the default endpoint and User-Agent string.
44
+ #
45
+ # @return [void]
20
46
  def self.reset
21
47
  super
22
- self.endpoint = nil
23
- self.user_agent = DEFAULT_UA
48
+ self.endpoint = nil
49
+ self.user_agent = DEFAULT_UA
24
50
  end
25
51
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
 
2
3
  # place holder to use unique gem name
3
4
  require File.expand_path('zabbix', __dir__)
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zabbix_api_gem
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Janco Tanis
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2024-07-18 00:00:00.000000000 Z
10
+ date: 2025-03-21 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: faraday
@@ -94,7 +93,6 @@ dependencies:
94
93
  - - ">="
95
94
  - !ruby/object:Gem::Version
96
95
  version: '0'
97
- description:
98
96
  email: gems@jancology.com
99
97
  executables: []
100
98
  extensions: []
@@ -122,7 +120,6 @@ licenses:
122
120
  metadata:
123
121
  homepage_uri: https://rubygems.org/gems/zabbix
124
122
  source_code_uri: https://github.com/jancotanis/zabbix
125
- post_install_message:
126
123
  rdoc_options: []
127
124
  require_paths:
128
125
  - lib
@@ -137,8 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
137
134
  - !ruby/object:Gem::Version
138
135
  version: '0'
139
136
  requirements: []
140
- rubygems_version: 3.2.3
141
- signing_key:
137
+ rubygems_version: 3.6.2
142
138
  specification_version: 4
143
139
  summary: A Ruby wrapper for the Zabbix APIs (readonly)
144
140
  test_files: []