zabbix_manager 5.0.1 → 5.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/lib/zabbix_manager/basic/basic_alias.rb +37 -0
  3. data/lib/zabbix_manager/basic/basic_extend.rb +38 -0
  4. data/lib/zabbix_manager/basic/basic_func.rb +103 -0
  5. data/lib/zabbix_manager/basic/basic_init.rb +46 -0
  6. data/lib/zabbix_manager/basic/basic_logic.rb +227 -0
  7. data/lib/zabbix_manager/classes/actions.rb +41 -0
  8. data/lib/zabbix_manager/classes/applications.rb +43 -0
  9. data/lib/zabbix_manager/classes/configurations.rb +42 -0
  10. data/lib/zabbix_manager/classes/drules.rb +55 -0
  11. data/lib/zabbix_manager/classes/errors.rb +28 -0
  12. data/lib/zabbix_manager/classes/events.rb +18 -0
  13. data/lib/zabbix_manager/classes/graphs.rb +111 -0
  14. data/lib/zabbix_manager/classes/hostgroups.rb +58 -0
  15. data/lib/zabbix_manager/classes/hosts.rb +218 -0
  16. data/lib/zabbix_manager/classes/httptests.rb +54 -0
  17. data/lib/zabbix_manager/classes/items.rb +145 -0
  18. data/lib/zabbix_manager/classes/maintenance.rb +17 -0
  19. data/lib/zabbix_manager/classes/mediatypes.rb +109 -0
  20. data/lib/zabbix_manager/classes/problems.rb +133 -0
  21. data/lib/zabbix_manager/classes/proxies.rb +68 -0
  22. data/lib/zabbix_manager/classes/roles.rb +114 -0
  23. data/lib/zabbix_manager/classes/screens.rb +94 -0
  24. data/lib/zabbix_manager/classes/scripts.rb +35 -0
  25. data/lib/zabbix_manager/classes/server.rb +16 -0
  26. data/lib/zabbix_manager/classes/templates.rb +123 -0
  27. data/lib/zabbix_manager/classes/triggers.rb +166 -0
  28. data/lib/zabbix_manager/classes/unusable.rb +8 -0
  29. data/lib/zabbix_manager/classes/usergroups.rb +73 -0
  30. data/lib/zabbix_manager/classes/usermacros.rb +228 -0
  31. data/lib/zabbix_manager/classes/users.rb +64 -0
  32. data/lib/zabbix_manager/classes/valuemaps.rb +50 -0
  33. data/lib/zabbix_manager/client.rb +158 -0
  34. data/lib/zabbix_manager/version.rb +1 -1
  35. data/zabbix_manager-5.0.1.gem +0 -0
  36. metadata +77 -2
@@ -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(100_000)
14
+ end
15
+
16
+ # Returns the API version from the Zabbix Server
17
+ #
18
+ # @return [String]
19
+ def api_version
20
+ @api_version ||= api_request(method: 'apiinfo.version', params: {})
21
+ @api_version
22
+ end
23
+
24
+ # Log in to the Zabbix Server and generate an auth token using the API
25
+ #
26
+ # @return [Hash]
27
+ def auth
28
+ api_request(
29
+ method: 'user.login',
30
+ params: {
31
+ user: @options[:user],
32
+ password: @options[:password]
33
+ }
34
+ )
35
+ end
36
+
37
+ # ZabbixManager::Basic.log does not like @client.options[:debug]
38
+ #
39
+ # @return [boolean]
40
+ def debug?
41
+ return !@options || @options[:debug]
42
+ end
43
+
44
+ # Initializes a new Client object
45
+ #
46
+ # @param options [Hash]
47
+ # @option opts [String] :url The url of zabbixapi(example: 'http://localhost/zabbix/api_jsonrpc.php')
48
+ # @option opts [String] :user
49
+ # @option opts [String] :password
50
+ # @option opts [String] :http_user A user for basic auth.(optional)
51
+ # @option opts [String] :http_password A password for basic auth.(optional)
52
+ # @option opts [Integer] :timeout Set timeout for requests in seconds.(default: 60)
53
+ #
54
+ # @return [ZabbixManager::Client]
55
+ def initialize(options = {})
56
+ @options = options
57
+ if !ENV['http_proxy'].nil? && options[:no_proxy] != true
58
+ @proxy_uri = URI.parse(ENV['http_proxy'])
59
+ @proxy_host = @proxy_uri.host
60
+ @proxy_port = @proxy_uri.port
61
+ @proxy_user, @proxy_pass = @proxy_uri.userinfo.split(/:/) if @proxy_uri.userinfo
62
+ end
63
+ unless api_version =~ %r{^5.[0|2]\.\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.2'.freeze
3
3
  end
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
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.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - WENWU YAN
@@ -9,7 +9,49 @@ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
11
  date: 2021-12-18 00:00:00.000000000 Z
12
- dependencies: []
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
@@ -27,7 +69,40 @@ files:
27
69
  - bin/console
28
70
  - bin/setup
29
71
  - lib/zabbix_manager.rb
72
+ - lib/zabbix_manager/basic/basic_alias.rb
73
+ - lib/zabbix_manager/basic/basic_extend.rb
74
+ - lib/zabbix_manager/basic/basic_func.rb
75
+ - lib/zabbix_manager/basic/basic_init.rb
76
+ - lib/zabbix_manager/basic/basic_logic.rb
77
+ - lib/zabbix_manager/classes/actions.rb
78
+ - lib/zabbix_manager/classes/applications.rb
79
+ - lib/zabbix_manager/classes/configurations.rb
80
+ - lib/zabbix_manager/classes/drules.rb
81
+ - lib/zabbix_manager/classes/errors.rb
82
+ - lib/zabbix_manager/classes/events.rb
83
+ - lib/zabbix_manager/classes/graphs.rb
84
+ - lib/zabbix_manager/classes/hostgroups.rb
85
+ - lib/zabbix_manager/classes/hosts.rb
86
+ - lib/zabbix_manager/classes/httptests.rb
87
+ - lib/zabbix_manager/classes/items.rb
88
+ - lib/zabbix_manager/classes/maintenance.rb
89
+ - lib/zabbix_manager/classes/mediatypes.rb
90
+ - lib/zabbix_manager/classes/problems.rb
91
+ - lib/zabbix_manager/classes/proxies.rb
92
+ - lib/zabbix_manager/classes/roles.rb
93
+ - lib/zabbix_manager/classes/screens.rb
94
+ - lib/zabbix_manager/classes/scripts.rb
95
+ - lib/zabbix_manager/classes/server.rb
96
+ - lib/zabbix_manager/classes/templates.rb
97
+ - lib/zabbix_manager/classes/triggers.rb
98
+ - lib/zabbix_manager/classes/unusable.rb
99
+ - lib/zabbix_manager/classes/usergroups.rb
100
+ - lib/zabbix_manager/classes/usermacros.rb
101
+ - lib/zabbix_manager/classes/users.rb
102
+ - lib/zabbix_manager/classes/valuemaps.rb
103
+ - lib/zabbix_manager/client.rb
30
104
  - lib/zabbix_manager/version.rb
105
+ - zabbix_manager-5.0.1.gem
31
106
  homepage: https://github.com/snmpd/zabbix_manager
32
107
  licenses:
33
108
  - MIT