zabbix-rails 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a35e9d3bc2ccbf034fa95b86940f7b9ce66b9d8fd3317ff98b3f002976f8b8e3
4
- data.tar.gz: 802e68ec3f3284fbb86161fdfc5c426a34fba94469d35e1690ec8ff6427bea86
3
+ metadata.gz: e91a0b65f3d8c11a21c801e896756b8b33c2a3ac0cbcdb5aaadc51ff77347a3f
4
+ data.tar.gz: 0cb4b629c690749a26f01ff2d6c3dad7ecb1aa78b17882e38b119333de361803
5
5
  SHA512:
6
- metadata.gz: 198fade3a060207b1c2e401640d796c1e1e6985bc21dbccba8c716f066874bdfe4f5302e33faf45bd1c74354597e70d8870872e54abc761cf58d24649c9ba1ee
7
- data.tar.gz: 02e31303134e3afd75f87e5c409371e948f7db3d15401d21b519b248bde323ddc77d700b0e2d8d3e666768bccbdca88c3e5817e189bd8ea3ef62502f7fcdfb8f
6
+ metadata.gz: dab95a7df10f9833babcbe70aea7077df0c3b57bdc4ac3479a643d4c7450e16ebb80e216390fcf35c3c8a0b7cbdc05706634e469c43b0871b714fda6b7483283
7
+ data.tar.gz: 357db5c2018f7edf22d3066712fd47159eefc8a272474cddf4c5a6ef0fa4ea7cc596db6ce775f5c1b3b585c2aae5306eadd9c319e1da4746bf457805e5f4bb29
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "bundler/setup"
2
4
 
3
5
  require "bundler/gem_tasks"
data/lib/zabbix/chart.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/concern"
2
4
 
3
5
  module Zabbix
@@ -35,9 +37,9 @@ module Zabbix
35
37
 
36
38
  # 请求后端返回图形
37
39
  def _item_chart(url = "/chart.php", data)
38
- base_url = Config.url
40
+ base_url = Zabbix.url
39
41
  host = URI(base_url).host
40
- conn = Faraday::Connection.new(base_url)
42
+ conn = Faraday::Connection.new(base_url)
41
43
  # 请求接口
42
44
  conn.get "#{url}" do |r|
43
45
  r.params.merge!(data)
@@ -51,13 +53,15 @@ module Zabbix
51
53
  def save_chart_graph(itemid, height = 400, width = 900, start_at = "now-6h", end_at = "now")
52
54
  filename = generate_name(itemid)
53
55
  save_file(filename, item_chart(itemid, height, width, start_at, end_at))
54
- "chart/#{File.basename(filename)}"
56
+ "/assets/chart/#{File.basename(filename)}"
55
57
  end
56
58
 
57
59
  # 保存图片
58
60
  def save_file(filename, content)
61
+ directory_base = "app/assets/images/chart/"
59
62
  # 生成绝对路径地址
60
- filename.prepend("app/assets/images/chart/")
63
+ FileUtils.mkdir_p("#{Rails.root}/#{directory_base}") unless Dir.exist?("#{Rails.root}/#{directory_base}")
64
+ filename.prepend(directory_base)
61
65
 
62
66
  # 存储数据
63
67
  File.open(filename, "wb") do |chart|
@@ -65,9 +69,9 @@ module Zabbix
65
69
  end
66
70
  end
67
71
 
72
+ # 生成随机名称
68
73
  def generate_name(itemid)
69
- require "uuidtools"
70
- "#{UUIDTools::UUID.timestamp_create}-#{itemid}.png"
74
+ "#{SecureRandom.hex(8)}-#{itemid}.png"
71
75
  end
72
76
  end
73
77
  end
@@ -11,9 +11,9 @@ module Zabbix
11
11
  # 将鉴权后的 zabbix-rails 对象缓存到 Rails,减少不必要的认证动作加速执行
12
12
  # https://guides.rubyonrails.org/caching_with_rails.html
13
13
  def zabbix_connector
14
- Rails.cache.fetch("zabbix_connector", expires_in: 2.hours) do
14
+ Rails.cache.fetch("zabbix_connector", expires_in: 30.minutes) do
15
15
  Rails.logger.warn("Zabbix 正在请求 API 鉴权")
16
- ZabbixManager.connect(url: Config.url, user: Config.user, password: Config.password, debug: Config.debug)
16
+ ZabbixManager.connect(url: Zabbix.url, user: Zabbix.user, password: Zabbix.password, debug: Zabbix.debug)
17
17
  rescue => e
18
18
  Rails.logger.warn("登录 ZABBIX 异常,请检查网络或登录凭证。原始报错信息: #{e}")
19
19
  end
@@ -21,7 +21,7 @@ module Zabbix
21
21
 
22
22
  # 获取 zabbix 登录凭证缓存
23
23
  def zabbix_token
24
- Rails.cache.fetch("zabbix_token", expires_in: 1.hours) do
24
+ Rails.cache.fetch("zabbix_token", expires_in: 30.minutes) do
25
25
  Rails.logger.warn("正在请求 zabbix/index.php 登录认证")
26
26
  authenticate_with_cookie
27
27
  end
@@ -55,14 +55,14 @@ module Zabbix
55
55
  # 请求 zabbix 后端认证,返回 Cookie
56
56
  def authenticate_with_cookie
57
57
  # 初始化会话
58
- base_url = Config.url
58
+ base_url = Zabbix.url
59
59
  host = URI(base_url).host
60
60
  conn = Faraday::Connection.new(base_url)
61
61
 
62
62
  # 登录权限凭证
63
63
  data = {
64
- name: Config.user,
65
- password: Config.password,
64
+ name: Zabbix.user,
65
+ password: Zabbix.password,
66
66
  autologin: 1,
67
67
  enter: "Sign in",
68
68
  }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Zabbix
4
- VERSION = "0.2.2"
4
+ VERSION = "0.2.3"
5
5
  end
data/lib/zabbix-rails.rb CHANGED
@@ -2,12 +2,11 @@
2
2
 
3
3
  # 注意加载顺序
4
4
  require "zabbix/version"
5
- require "zabbix/config"
6
5
  require "zabbix/connector"
7
- require "zabbix/chart"
8
6
  require "zabbix/host_monitor"
9
7
  require "zabbix/item_trigger"
10
8
  require "zabbix/dns_monitor"
9
+ require "zabbix/chart"
11
10
 
12
11
  # 加载外部依赖
13
12
  require "active_support/concern"
@@ -19,8 +18,8 @@ module Zabbix
19
18
  class << self
20
19
  attr_accessor :url, :user, :password, :debug
21
20
 
22
- def configure(&block)
23
- Config.configure(&block)
21
+ def config
22
+ yield self
24
23
  end
25
24
  end
26
25
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zabbix-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - WENWU.YAN
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-26 00:00:00.000000000 Z
11
+ date: 2022-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -36,7 +36,6 @@ files:
36
36
  - lib/tasks/zabbix_tasks.rake
37
37
  - lib/zabbix-rails.rb
38
38
  - lib/zabbix/chart.rb
39
- - lib/zabbix/config.rb
40
39
  - lib/zabbix/connector.rb
41
40
  - lib/zabbix/dns_monitor.rb
42
41
  - lib/zabbix/engine.rb
data/lib/zabbix/config.rb DELETED
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Zabbix
4
- class Config
5
- class << self
6
- attr_accessor :url, :user, :password, :debug
7
-
8
- def configure
9
- yield self
10
- end
11
- end
12
- end
13
- end