zabbix-rails 0.2.0 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/zabbix/chart.rb +8 -4
- data/lib/zabbix/connector.rb +24 -22
- data/lib/zabbix/dns_monitor.rb +2 -2
- data/lib/zabbix/host_monitor.rb +2 -2
- data/lib/zabbix/item_trigger.rb +2 -2
- data/lib/zabbix/version.rb +1 -1
- data/lib/zabbix-rails.rb +2 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a35e9d3bc2ccbf034fa95b86940f7b9ce66b9d8fd3317ff98b3f002976f8b8e3
|
4
|
+
data.tar.gz: 802e68ec3f3284fbb86161fdfc5c426a34fba94469d35e1690ec8ff6427bea86
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
42
|
+
conn.get "#{url}" do |r|
|
39
43
|
r.params.merge!(data)
|
40
|
-
r.headers["Host"] =
|
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
|
data/lib/zabbix/connector.rb
CHANGED
@@ -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:
|
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
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
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
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
62
|
+
# 登录权限凭证
|
63
|
+
data = {
|
64
|
+
name: Config.user,
|
65
|
+
password: Config.password,
|
66
|
+
autologin: 1,
|
67
|
+
enter: "Sign in",
|
68
|
+
}
|
68
69
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
70
|
+
# 请求认证
|
71
|
+
ret = conn.post "/index.php" do |r|
|
72
|
+
r.headers["Host"] = host
|
73
|
+
r.body = data
|
74
|
+
end
|
74
75
|
|
75
|
-
|
76
|
+
ret.headers["set-cookie"].presence || nil
|
77
|
+
end
|
76
78
|
end
|
77
79
|
end
|
data/lib/zabbix/dns_monitor.rb
CHANGED
data/lib/zabbix/host_monitor.rb
CHANGED
data/lib/zabbix/item_trigger.rb
CHANGED
data/lib/zabbix/version.rb
CHANGED
data/lib/zabbix-rails.rb
CHANGED