zabbix-rails 0.2.0 → 0.2.2

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: d72e0a2099b3dd87de6d9a38ec3514bc2e8f6fa7720804a202457a278af39987
4
- data.tar.gz: d92f35f0c3497663edc9f50f5a6fbf4815bde298fb50fd2eb2921ffa367077ed
3
+ metadata.gz: a35e9d3bc2ccbf034fa95b86940f7b9ce66b9d8fd3317ff98b3f002976f8b8e3
4
+ data.tar.gz: 802e68ec3f3284fbb86161fdfc5c426a34fba94469d35e1690ec8ff6427bea86
5
5
  SHA512:
6
- metadata.gz: 239955a4196412900aad1f05e4b455274355960d3643ef1fc6572f16b16b158f7ec7b856aa069046c857774c284eaf7ef9d2b6a8f60e9727b2a4bbef1dd674c9
7
- data.tar.gz: 6d3b8303de360e70c3c590faa7ed39b3b22fab9a198fd552dbe150ebf5a4a4eec090110c8b199db4c16409d54ffdbd7d143aebb4d1e372ae671435e4621b856e
6
+ metadata.gz: 198fade3a060207b1c2e401640d796c1e1e6985bc21dbccba8c716f066874bdfe4f5302e33faf45bd1c74354597e70d8870872e54abc761cf58d24649c9ba1ee
7
+ data.tar.gz: 02e31303134e3afd75f87e5c409371e948f7db3d15401d21b519b248bde323ddc77d700b0e2d8d3e666768bccbdca88c3e5817e189bd8ea3ef62502f7fcdfb8f
data/lib/zabbix/chart.rb CHANGED
@@ -1,9 +1,10 @@
1
1
  require "active_support/concern"
2
2
 
3
3
  module Zabbix
4
- extend ActiveSupport::Concern
5
-
6
4
  module Chart
5
+ extend ActiveSupport::Concern
6
+ include Zabbix::Connector
7
+
7
8
  # 请求生成 graph_chart 对象
8
9
  def graph_chart(graphid, height = 400, width = 900, start_at = "now-6h", end_at = "now")
9
10
  # 构造数据结构
@@ -34,10 +35,13 @@ module Zabbix
34
35
 
35
36
  # 请求后端返回图形
36
37
  def _item_chart(url = "/chart.php", data)
38
+ base_url = Config.url
39
+ host = URI(base_url).host
40
+ conn = Faraday::Connection.new(base_url)
37
41
  # 请求接口
38
- @conn.get "#{url}" do |r|
42
+ conn.get "#{url}" do |r|
39
43
  r.params.merge!(data)
40
- r.headers["Host"] = Config.url.gsub!(%r{http(s)?://}, "")
44
+ r.headers["Host"] = host
41
45
  r.headers["Accept"] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
42
46
  r.headers["Cookie"] = zabbix_token
43
47
  end.body
@@ -1,11 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "active_support/concern"
4
+ require "faraday"
4
5
 
5
6
  module Zabbix
6
- extend ::ActiveSupport::Concern
7
-
8
7
  module Connector
8
+ extend ::ActiveSupport::Concern
9
+
9
10
  # 开发测试环境需要单独开启缓存功能:bin/rails dev:cache
10
11
  # 将鉴权后的 zabbix-rails 对象缓存到 Rails,减少不必要的认证动作加速执行
11
12
  # https://guides.rubyonrails.org/caching_with_rails.html
@@ -20,7 +21,7 @@ module Zabbix
20
21
 
21
22
  # 获取 zabbix 登录凭证缓存
22
23
  def zabbix_token
23
- Rails.cache.fetch("zabbix_token", expires_in: 2.hours) do
24
+ Rails.cache.fetch("zabbix_token", expires_in: 1.hours) do
24
25
  Rails.logger.warn("正在请求 zabbix/index.php 登录认证")
25
26
  authenticate_with_cookie
26
27
  end
@@ -50,28 +51,29 @@ module Zabbix
50
51
  rescue => e
51
52
  Rails.logger.warn("调用 delete_trigger_monitor 接口异常,原始报错信息: #{e}")
52
53
  end
53
- end
54
54
 
55
- # 请求 zabbix 后端认证,返回 Cookie
56
- def authenticate_with_cookie
57
- # 初始化会话
58
- base_url = Config.url
59
- @conn = Faraday::Connection.new(base_url)
55
+ # 请求 zabbix 后端认证,返回 Cookie
56
+ def authenticate_with_cookie
57
+ # 初始化会话
58
+ base_url = Config.url
59
+ host = URI(base_url).host
60
+ conn = Faraday::Connection.new(base_url)
60
61
 
61
- # 登录权限凭证
62
- data = {
63
- name: Config.user,
64
- password: Config.password,
65
- autologin: 1,
66
- enter: "Sign in",
67
- }
62
+ # 登录权限凭证
63
+ data = {
64
+ name: Config.user,
65
+ password: Config.password,
66
+ autologin: 1,
67
+ enter: "Sign in",
68
+ }
68
69
 
69
- # 请求认证
70
- ret = @conn.post "/index.php" do |r|
71
- r.headers["Host"] = base_url.gsub!(%r{http(s)?://}, "")
72
- r.body = data
73
- end
70
+ # 请求认证
71
+ ret = conn.post "/index.php" do |r|
72
+ r.headers["Host"] = host
73
+ r.body = data
74
+ end
74
75
 
75
- ret.headers["set-cookie"].presence || nil
76
+ ret.headers["set-cookie"].presence || nil
77
+ end
76
78
  end
77
79
  end
@@ -3,9 +3,9 @@
3
3
  require "active_support/concern"
4
4
 
5
5
  module Zabbix
6
- extend ::ActiveSupport::Concern
7
-
8
6
  module DnsMonitor
7
+ extend ::ActiveSupport::Concern
8
+
9
9
  include Zabbix::Connector
10
10
 
11
11
  # 生成 DNS 触发器表达式
@@ -3,9 +3,9 @@
3
3
  require "active_support/concern"
4
4
 
5
5
  module Zabbix
6
- extend ::ActiveSupport::Concern
7
-
8
6
  module HostMonitor
7
+ extend ::ActiveSupport::Concern
8
+
9
9
  include Zabbix::Connector
10
10
 
11
11
  # 创建或更新 HostMonitor主机监控对象
@@ -3,9 +3,9 @@
3
3
  require "active_support/concern"
4
4
 
5
5
  module Zabbix
6
- extend ::ActiveSupport::Concern
7
-
8
6
  module ItemTrigger
7
+ extend ::ActiveSupport::Concern
8
+
9
9
  include Zabbix::Connector
10
10
 
11
11
  # 创建或新增触发器,返回触发器ID | 触发器的 DATA_STRUCTURE
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Zabbix
4
- VERSION = "0.2.0"
4
+ VERSION = "0.2.2"
5
5
  end
data/lib/zabbix-rails.rb CHANGED
@@ -1,8 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # 注意加载顺序
3
4
  require "zabbix/version"
4
5
  require "zabbix/config"
5
6
  require "zabbix/connector"
7
+ require "zabbix/chart"
6
8
  require "zabbix/host_monitor"
7
9
  require "zabbix/item_trigger"
8
10
  require "zabbix/dns_monitor"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zabbix-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - WENWU.YAN