zabbix-rails 0.1.9 → 0.2.3

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: aa818a8ba839cf3da0d79d2bd76a5bce34c9b153408320c57e87aa8c62c28700
4
- data.tar.gz: bf33db9b88d475e5ebf71caa669efe7be39a5976a0f1a844978a157181a89a58
3
+ metadata.gz: e91a0b65f3d8c11a21c801e896756b8b33c2a3ac0cbcdb5aaadc51ff77347a3f
4
+ data.tar.gz: 0cb4b629c690749a26f01ff2d6c3dad7ecb1aa78b17882e38b119333de361803
5
5
  SHA512:
6
- metadata.gz: 20114878297c5b7113f204881462c466b45e8512e500f5e3e6ede5a56ddc0869872bfdf7446142948133467c3a53e021f0a2b5a4d0d5aaf06a3e2574940448e4
7
- data.tar.gz: 1d3a1613710a430c2485e2354776df1e6cf8121ce203241a980c51ecd24c7363c3cb92d78d635ecfe83531927c4a02ea5f90e1eefb7147d674cc2d71b1d5b16d
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"
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/concern"
4
+
5
+ module Zabbix
6
+ module Chart
7
+ extend ActiveSupport::Concern
8
+ include Zabbix::Connector
9
+
10
+ # 请求生成 graph_chart 对象
11
+ def graph_chart(graphid, height = 400, width = 900, start_at = "now-6h", end_at = "now")
12
+ # 构造数据结构
13
+ data = {
14
+ from: start_at,
15
+ to: end_at,
16
+ graphid: graphid,
17
+ height: height,
18
+ width: width
19
+ }
20
+ # 请求后端返回图片对象
21
+ _item_chart("/chart2.php", data)
22
+ end
23
+
24
+ # 请求生成 item_chart 对象
25
+ def item_chart(itemid, height = 400, width = 900, start_at = "now-6h", end_at = "now")
26
+ # 构造数据结构
27
+ data = {
28
+ from: start_at,
29
+ to: end_at,
30
+ itemids: [itemid.to_i],
31
+ height: height,
32
+ width: width
33
+ }
34
+ # 请求后端返回图片对象
35
+ _item_chart(data)
36
+ end
37
+
38
+ # 请求后端返回图形
39
+ def _item_chart(url = "/chart.php", data)
40
+ base_url = Zabbix.url
41
+ host = URI(base_url).host
42
+ conn = Faraday::Connection.new(base_url)
43
+ # 请求接口
44
+ conn.get "#{url}" do |r|
45
+ r.params.merge!(data)
46
+ r.headers["Host"] = host
47
+ 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"
48
+ r.headers["Cookie"] = zabbix_token
49
+ end.body
50
+ end
51
+
52
+ # 保存图片并返回文件名
53
+ def save_chart_graph(itemid, height = 400, width = 900, start_at = "now-6h", end_at = "now")
54
+ filename = generate_name(itemid)
55
+ save_file(filename, item_chart(itemid, height, width, start_at, end_at))
56
+ "/assets/chart/#{File.basename(filename)}"
57
+ end
58
+
59
+ # 保存图片
60
+ def save_file(filename, content)
61
+ directory_base = "app/assets/images/chart/"
62
+ # 生成绝对路径地址
63
+ FileUtils.mkdir_p("#{Rails.root}/#{directory_base}") unless Dir.exist?("#{Rails.root}/#{directory_base}")
64
+ filename.prepend(directory_base)
65
+
66
+ # 存储数据
67
+ File.open(filename, "wb") do |chart|
68
+ chart.write(content)
69
+ end
70
+ end
71
+
72
+ # 生成随机名称
73
+ def generate_name(itemid)
74
+ "#{SecureRandom.hex(8)}-#{itemid}.png"
75
+ end
76
+ end
77
+ end
@@ -1,23 +1,32 @@
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
12
13
  def zabbix_connector
13
- Rails.cache.fetch("zabbix_connector", expires_in: 4.hours) do
14
+ Rails.cache.fetch("zabbix_connector", expires_in: 30.minutes) do
14
15
  Rails.logger.warn("Zabbix 正在请求 API 鉴权")
15
- 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)
16
17
  rescue => e
17
18
  Rails.logger.warn("登录 ZABBIX 异常,请检查网络或登录凭证。原始报错信息: #{e}")
18
19
  end
19
20
  end
20
21
 
22
+ # 获取 zabbix 登录凭证缓存
23
+ def zabbix_token
24
+ Rails.cache.fetch("zabbix_token", expires_in: 30.minutes) do
25
+ Rails.logger.warn("正在请求 zabbix/index.php 登录认证")
26
+ authenticate_with_cookie
27
+ end
28
+ end
29
+
21
30
  # 删除主机监控
22
31
  def delete_host_monitor(name)
23
32
  id = zabbix_connector.hosts.get_host_id(name)
@@ -42,5 +51,29 @@ module Zabbix
42
51
  rescue => e
43
52
  Rails.logger.warn("调用 delete_trigger_monitor 接口异常,原始报错信息: #{e}")
44
53
  end
54
+
55
+ # 请求 zabbix 后端认证,返回 Cookie
56
+ def authenticate_with_cookie
57
+ # 初始化会话
58
+ base_url = Zabbix.url
59
+ host = URI(base_url).host
60
+ conn = Faraday::Connection.new(base_url)
61
+
62
+ # 登录权限凭证
63
+ data = {
64
+ name: Zabbix.user,
65
+ password: Zabbix.password,
66
+ autologin: 1,
67
+ enter: "Sign in",
68
+ }
69
+
70
+ # 请求认证
71
+ ret = conn.post "/index.php" do |r|
72
+ r.headers["Host"] = host
73
+ r.body = data
74
+ end
75
+
76
+ ret.headers["set-cookie"].presence || nil
77
+ end
45
78
  end
46
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.1.9"
4
+ VERSION = "0.2.3"
5
5
  end
data/lib/zabbix-rails.rb CHANGED
@@ -1,11 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # 注意加载顺序
3
4
  require "zabbix/version"
4
- require "zabbix/config"
5
5
  require "zabbix/connector"
6
6
  require "zabbix/host_monitor"
7
7
  require "zabbix/item_trigger"
8
8
  require "zabbix/dns_monitor"
9
+ require "zabbix/chart"
9
10
 
10
11
  # 加载外部依赖
11
12
  require "active_support/concern"
@@ -17,8 +18,8 @@ module Zabbix
17
18
  class << self
18
19
  attr_accessor :url, :user, :password, :debug
19
20
 
20
- def configure(&block)
21
- Config.configure(&block)
21
+ def config
22
+ yield self
22
23
  end
23
24
  end
24
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.1.9
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
@@ -35,7 +35,7 @@ files:
35
35
  - Rakefile
36
36
  - lib/tasks/zabbix_tasks.rake
37
37
  - lib/zabbix-rails.rb
38
- - lib/zabbix/config.rb
38
+ - lib/zabbix/chart.rb
39
39
  - lib/zabbix/connector.rb
40
40
  - lib/zabbix/dns_monitor.rb
41
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