zabbix_manager 5.0.1 → 5.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/.idea/workspace.xml +69 -0
  3. data/lib/zabbix_manager/basic/basic_alias.rb +37 -0
  4. data/lib/zabbix_manager/basic/basic_extend.rb +38 -0
  5. data/lib/zabbix_manager/basic/basic_func.rb +103 -0
  6. data/lib/zabbix_manager/basic/basic_init.rb +46 -0
  7. data/lib/zabbix_manager/basic/basic_logic.rb +227 -0
  8. data/lib/zabbix_manager/classes/actions.rb +41 -0
  9. data/lib/zabbix_manager/classes/applications.rb +43 -0
  10. data/lib/zabbix_manager/classes/configurations.rb +42 -0
  11. data/lib/zabbix_manager/classes/drules.rb +55 -0
  12. data/lib/zabbix_manager/classes/errors.rb +28 -0
  13. data/lib/zabbix_manager/classes/events.rb +18 -0
  14. data/lib/zabbix_manager/classes/graphs.rb +111 -0
  15. data/lib/zabbix_manager/classes/hostgroups.rb +58 -0
  16. data/lib/zabbix_manager/classes/hosts.rb +218 -0
  17. data/lib/zabbix_manager/classes/httptests.rb +54 -0
  18. data/lib/zabbix_manager/classes/items.rb +146 -0
  19. data/lib/zabbix_manager/classes/maintenance.rb +17 -0
  20. data/lib/zabbix_manager/classes/mediatypes.rb +109 -0
  21. data/lib/zabbix_manager/classes/problems.rb +133 -0
  22. data/lib/zabbix_manager/classes/proxies.rb +68 -0
  23. data/lib/zabbix_manager/classes/roles.rb +114 -0
  24. data/lib/zabbix_manager/classes/screens.rb +94 -0
  25. data/lib/zabbix_manager/classes/scripts.rb +35 -0
  26. data/lib/zabbix_manager/classes/server.rb +16 -0
  27. data/lib/zabbix_manager/classes/templates.rb +123 -0
  28. data/lib/zabbix_manager/classes/triggers.rb +166 -0
  29. data/lib/zabbix_manager/classes/unusable.rb +8 -0
  30. data/lib/zabbix_manager/classes/usergroups.rb +73 -0
  31. data/lib/zabbix_manager/classes/usermacros.rb +228 -0
  32. data/lib/zabbix_manager/classes/users.rb +64 -0
  33. data/lib/zabbix_manager/classes/valuemaps.rb +50 -0
  34. data/lib/zabbix_manager/client.rb +158 -0
  35. data/lib/zabbix_manager/version.rb +1 -1
  36. data/zabbix_manager-5.0.1.gem +0 -0
  37. data/zabbix_manager-5.0.2.gem +0 -0
  38. metadata +81 -4
@@ -0,0 +1,50 @@
1
+ class ZabbixManager
2
+ class ValueMaps < Basic
3
+ # The method name used for interacting with ValueMaps via Zabbix API
4
+ #
5
+ # @return [String]
6
+ def method_name
7
+ 'valuemap'
8
+ end
9
+
10
+ # The key field name used for ValueMap objects via Zabbix API
11
+ #
12
+ # @return [String]
13
+ def key
14
+ 'valuemapid'
15
+ end
16
+
17
+ # The id field name used for identifying specific ValueMap objects via Zabbix API
18
+ #
19
+ # @return [String]
20
+ def identify
21
+ 'name'
22
+ end
23
+
24
+ # Get or Create ValueMap object using Zabbix API
25
+ #
26
+ # @param data [Hash] Needs to include valuemapids [List] to properly identify ValueMaps via Zabbix API
27
+ # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
28
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
29
+ # @return [Integer] Zabbix object id
30
+ def get_or_create(data)
31
+ log "[DEBUG] Call get_or_create with parameters: #{data.inspect}"
32
+
33
+ unless (id = get_id(valuemapids: data[:valuemapids]))
34
+ id = create(data)
35
+ end
36
+ id
37
+ end
38
+
39
+ # Create or update Item object using Zabbix API
40
+ #
41
+ # @param data [Hash] Needs to include valuemapids to properly identify ValueMaps via Zabbix API
42
+ # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
43
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
44
+ # @return [Integer] Zabbix object id
45
+ def create_or_update(data)
46
+ valuemapid = get_id(name: data[:name])
47
+ valuemapid ? update(data.merge(valuemapids: [:valuemapid])) : create(data)
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,158 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'openssl'
4
+
5
+ class ZabbixManager
6
+ class Client
7
+ # @param (see ZabbixManager::Client#initialize)
8
+ # @return [Hash]
9
+ attr_reader :options
10
+
11
+ # @return [Integer]
12
+ def id
13
+ rand(800_000)
14
+ end
15
+
16
+ # Returns the API version from the Zabbix Server
17
+ #
18
+ # @return [String, Hash]
19
+ def api_version
20
+ api_request(method: 'apiinfo.version', params: {})
21
+ end
22
+
23
+ # Log in to the Zabbix Server and generate an auth token using the API
24
+ #
25
+ # @return [Hash, String]
26
+ def auth
27
+ api_request(
28
+ method: 'user.login',
29
+ params: {
30
+ user: @options[:user],
31
+ password: @options[:password]
32
+ }
33
+ )
34
+ end
35
+
36
+ # ZabbixManager::Basic.log does not like @client.options[:debug]
37
+ #
38
+ # @return [boolean]
39
+ def debug?
40
+ !@options || @options[:debug]
41
+ end
42
+
43
+ # Initializes a new Client object
44
+ #
45
+ # @param options [Hash]
46
+ # @option opts [String] :url The url of zabbixapi(example: 'http://localhost/zabbix/api_jsonrpc.php')
47
+ # @option opts [String] :user
48
+ # @option opts [String] :password
49
+ # @option opts [String] :http_user A user for basic auth.(optional)
50
+ # @option opts [String] :http_password A password for basic auth.(optional)
51
+ # @option opts [Integer] :timeout Set timeout for requests in seconds.(default: 60)
52
+ #
53
+ # @return [ZabbixManager::Client]
54
+ def initialize(options = {})
55
+ @options = options
56
+ if !ENV['http_proxy'].nil? && options[:no_proxy] != true
57
+ @proxy_uri = URI.parse(ENV['http_proxy'])
58
+ @proxy_host = @proxy_uri.host
59
+ @proxy_port = @proxy_uri.port
60
+ @proxy_user, @proxy_pass = @proxy_uri.userinfo&.split(/:/) if @proxy_uri.userinfo
61
+ end
62
+
63
+ if api_version.match? /^7\.\d+\.\d+$/
64
+ message = "Zabbix API version: #{api_version} is not supported by this version of zabbixapi"
65
+ if @options[:ignore_version]
66
+ puts "[WARNING] #{message}" if @options[:debug]
67
+ else
68
+ raise ZabbixManager::ManagerError.new(message)
69
+ end
70
+ end
71
+
72
+ @auth_hash = auth
73
+ end
74
+
75
+ # Convert message body to JSON string for the Zabbix API
76
+ #
77
+ # @param body [Hash]
78
+ # @return [String]
79
+ def message_json(body)
80
+ message = {
81
+ method: body[:method],
82
+ params: body[:params],
83
+ id: id,
84
+ jsonrpc: '2.0'
85
+ }
86
+
87
+ message[:auth] = @auth_hash unless body[:method] == 'apiinfo.version' || body[:method] == 'user.login'
88
+
89
+ JSON.generate(message)
90
+ end
91
+
92
+ # @param body [String]
93
+ # @return [String]
94
+ def http_request(body)
95
+ uri = URI.parse(@options[:url])
96
+
97
+ # set the time out the default (60) or to what the user passed
98
+ timeout = @options[:timeout].nil? ? 60 : @options[:timeout]
99
+ puts "[DEBUG] Timeout for request set to #{timeout} seconds" if @options[:debug]
100
+
101
+ http =
102
+ if @proxy_uri
103
+ Net::HTTP.Proxy(@proxy_host, @proxy_port, @proxy_user, @proxy_pass).new(uri.host, uri.port)
104
+ else
105
+ Net::HTTP.new(uri.host, uri.port)
106
+ end
107
+
108
+ if uri.scheme == 'https'
109
+ http.use_ssl = true
110
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
111
+ end
112
+
113
+ http.open_timeout = timeout
114
+ http.read_timeout = timeout
115
+
116
+ request = Net::HTTP::Post.new(uri.request_uri)
117
+ request.basic_auth @options[:http_user], @options[:http_password] if @options[:http_user]
118
+ request.add_field('Content-Type', 'application/json-rpc')
119
+ request.body = body
120
+
121
+ response = http.request(request)
122
+
123
+ raise HttpError.new("HTTP Error: #{response.code} on #{@options[:url]}", response) unless response.code == '200'
124
+
125
+ puts "[DEBUG] Get answer: #{response.body}" if @options[:debug]
126
+ response.body
127
+ end
128
+
129
+ # @param body [String]
130
+ # @return [Hash, String]
131
+ def _request(body)
132
+ puts "[DEBUG] Send request: #{body}" if @options[:debug]
133
+ result = JSON.parse(http_request(body))
134
+ # 异常信息抛出
135
+ raise ManagerError.new("Server answer API error\n #{JSON.pretty_unparse(result['error'])}\n on request:\n #{pretty_body(body)}", result) if result['error']
136
+
137
+ result['result']
138
+ end
139
+
140
+ def pretty_body(body)
141
+ parsed_body = JSON.parse(body)
142
+
143
+ # If password is in body hide it
144
+ parsed_body['params']['password'] = '***' if parsed_body['params'].is_a?(Hash) && parsed_body['params'].key?('password')
145
+
146
+ JSON.pretty_unparse(parsed_body)
147
+ end
148
+
149
+ # Execute Zabbix API requests and return response
150
+ #
151
+ # @param body [Hash]
152
+ # @return [Hash, String]
153
+ def api_request(body)
154
+ # ap body
155
+ _request message_json(body)
156
+ end
157
+ end
158
+ end
@@ -1,3 +1,3 @@
1
1
  class ZabbixManager
2
- VERSION = '5.0.1'.freeze
2
+ VERSION = '5.0.4'.freeze
3
3
  end
Binary file
Binary file
metadata CHANGED
@@ -1,15 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zabbix_manager
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.1
4
+ version: 5.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - WENWU YAN
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-12-18 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2022-03-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: http
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '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: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
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'
13
55
  description: Write a longer description or delete this line.
14
56
  email:
15
57
  - careline@foxmail.com
@@ -17,6 +59,7 @@ executables: []
17
59
  extensions: []
18
60
  extra_rdoc_files: []
19
61
  files:
62
+ - ".idea/workspace.xml"
20
63
  - ".rubocop.yml"
21
64
  - CHANGELOG.md
22
65
  - CODE_OF_CONDUCT.md
@@ -27,7 +70,41 @@ files:
27
70
  - bin/console
28
71
  - bin/setup
29
72
  - lib/zabbix_manager.rb
73
+ - lib/zabbix_manager/basic/basic_alias.rb
74
+ - lib/zabbix_manager/basic/basic_extend.rb
75
+ - lib/zabbix_manager/basic/basic_func.rb
76
+ - lib/zabbix_manager/basic/basic_init.rb
77
+ - lib/zabbix_manager/basic/basic_logic.rb
78
+ - lib/zabbix_manager/classes/actions.rb
79
+ - lib/zabbix_manager/classes/applications.rb
80
+ - lib/zabbix_manager/classes/configurations.rb
81
+ - lib/zabbix_manager/classes/drules.rb
82
+ - lib/zabbix_manager/classes/errors.rb
83
+ - lib/zabbix_manager/classes/events.rb
84
+ - lib/zabbix_manager/classes/graphs.rb
85
+ - lib/zabbix_manager/classes/hostgroups.rb
86
+ - lib/zabbix_manager/classes/hosts.rb
87
+ - lib/zabbix_manager/classes/httptests.rb
88
+ - lib/zabbix_manager/classes/items.rb
89
+ - lib/zabbix_manager/classes/maintenance.rb
90
+ - lib/zabbix_manager/classes/mediatypes.rb
91
+ - lib/zabbix_manager/classes/problems.rb
92
+ - lib/zabbix_manager/classes/proxies.rb
93
+ - lib/zabbix_manager/classes/roles.rb
94
+ - lib/zabbix_manager/classes/screens.rb
95
+ - lib/zabbix_manager/classes/scripts.rb
96
+ - lib/zabbix_manager/classes/server.rb
97
+ - lib/zabbix_manager/classes/templates.rb
98
+ - lib/zabbix_manager/classes/triggers.rb
99
+ - lib/zabbix_manager/classes/unusable.rb
100
+ - lib/zabbix_manager/classes/usergroups.rb
101
+ - lib/zabbix_manager/classes/usermacros.rb
102
+ - lib/zabbix_manager/classes/users.rb
103
+ - lib/zabbix_manager/classes/valuemaps.rb
104
+ - lib/zabbix_manager/client.rb
30
105
  - lib/zabbix_manager/version.rb
106
+ - zabbix_manager-5.0.1.gem
107
+ - zabbix_manager-5.0.2.gem
31
108
  homepage: https://github.com/snmpd/zabbix_manager
32
109
  licenses:
33
110
  - MIT
@@ -51,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
51
128
  - !ruby/object:Gem::Version
52
129
  version: '0'
53
130
  requirements: []
54
- rubygems_version: 3.2.22
131
+ rubygems_version: 3.3.3
55
132
  signing_key:
56
133
  specification_version: 4
57
134
  summary: Write a short summary, because RubyGems requires one.