zabbix-rails 0.2.0 → 0.2.4

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: d72e0a2099b3dd87de6d9a38ec3514bc2e8f6fa7720804a202457a278af39987
4
- data.tar.gz: d92f35f0c3497663edc9f50f5a6fbf4815bde298fb50fd2eb2921ffa367077ed
3
+ metadata.gz: d477e8482d7b336bc91a086a5b17d4623e4e6ecc3cc6e9ef86fc523fa57c97cb
4
+ data.tar.gz: 20ca6fb13b28defc342218df40a2a69d1b87b6a898bced5239935158659f527f
5
5
  SHA512:
6
- metadata.gz: 239955a4196412900aad1f05e4b455274355960d3643ef1fc6572f16b16b158f7ec7b856aa069046c857774c284eaf7ef9d2b6a8f60e9727b2a4bbef1dd674c9
7
- data.tar.gz: 6d3b8303de360e70c3c590faa7ed39b3b22fab9a198fd552dbe150ebf5a4a4eec090110c8b199db4c16409d54ffdbd7d143aebb4d1e372ae671435e4621b856e
6
+ metadata.gz: e0b5ad8ce0ca98cbde47f8d3c745c400644853e8bbc4ce4f8151991da97cd3ce92f4aab0a370674dae50b78f7dd0dc003d1d1f386410d4e82620a0b16a2290b1
7
+ data.tar.gz: 85a9134058479f03dd307caab468070c55d793a97efae018a0b919cca5f7de119ec97e2b6eddd623b923027e2a92f175ed6dfd85426b73ab37879c317a5d275a
data/README.md CHANGED
@@ -1,13 +1,16 @@
1
1
  # Zabbix-Rails
2
+
2
3
  主要实现 zabbix_connector 对象缓存,加速 API 交互效率。 依赖 zabbix_manager 作为后端请求模块
3
4
 
4
5
  ## Usage
6
+
5
7
  ```
6
8
  1、include Zabbix::Connector 到模块或者类对象;
7
9
  2、调用 zabbix_connector 方法自动实现后端鉴权,当前前提是配置好 'zabbix_rails.rb in config/initializers'
8
- 3、调用 Zabbix::HostMonitor 实现自动维护监控对象
9
- 4、调用 Zabbix::ItemTrigger 实现自动维护触发器对象
10
+ 3、调用 Zabbix::HostMonitor 实现自动维护监控对象,需要自行实现 [Zabbix::DeleteMonitor] 的JOB 消息队列函数;
11
+ 4、调用 Zabbix::ItemTrigger 实现自动维护触发器对象,需要自行实现 [Zabbix::DeleteTrigger] 的JOB 消息队列函数;
10
12
  5、调用 Zabbix::Dns 实现自动维护 DNS 监控
13
+ 6、调用 Zabbix::Chart 实现自动登录 zabbix 下载 item 的监控快照图片,同时兼容 V5 和 V6 版本,下载路径在 app/assets/images/chart;
11
14
  ```
12
15
 
13
16
  ## Installation
@@ -32,13 +35,19 @@ $ gem install zabbix-rails
32
35
 
33
36
  add zabbix_rails.rb in config/initializers
34
37
 
38
+ ```ruby
39
+ Zabbix.configure do |config|
40
+ config.url = Rails.application.credentials.zabbix.url
41
+ config.user = Rails.application.credentials.zabbix.username
42
+ config.password = Rails.application.credentials.zabbix.password
43
+ config.debug = false
44
+ end
35
45
  ```
36
- # Zabbix.configure do |config|
37
- # c.url = Rails.application.credentials.zabbix.url
38
- # c.user = Rails.application.credentials.zabbix.username
39
- # c.password = Rails.application.credentials.zabbix.password
40
- # c.debug = false
41
- # end
46
+
47
+ 下载 zabbix item 图片范例:
48
+ ```ruby
49
+ include Zabbix::Chart
50
+ save_chart_graph 37932
42
51
  ```
43
52
 
44
53
  ## Contributing
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,9 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/concern"
2
4
 
3
5
  module Zabbix
4
- extend ActiveSupport::Concern
5
-
6
6
  module Chart
7
+ extend ActiveSupport::Concern
8
+ include Zabbix::Connector
9
+
7
10
  # 请求生成 graph_chart 对象
8
11
  def graph_chart(graphid, height = 400, width = 900, start_at = "now-6h", end_at = "now")
9
12
  # 构造数据结构
@@ -34,10 +37,13 @@ module Zabbix
34
37
 
35
38
  # 请求后端返回图形
36
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)
37
43
  # 请求接口
38
- @conn.get "#{url}" do |r|
44
+ conn.get(url) do |r|
39
45
  r.params.merge!(data)
40
- r.headers["Host"] = Config.url.gsub!(%r{http(s)?://}, "")
46
+ r.headers["Host"] = host
41
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"
42
48
  r.headers["Cookie"] = zabbix_token
43
49
  end.body
@@ -47,23 +53,27 @@ module Zabbix
47
53
  def save_chart_graph(itemid, height = 400, width = 900, start_at = "now-6h", end_at = "now")
48
54
  filename = generate_name(itemid)
49
55
  save_file(filename, item_chart(itemid, height, width, start_at, end_at))
50
- "chart/#{File.basename(filename)}"
51
56
  end
52
57
 
53
58
  # 保存图片
54
59
  def save_file(filename, content)
60
+ directory_base = "app/assets/images/chart/"
61
+
55
62
  # 生成绝对路径地址
56
- 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)
57
65
 
58
66
  # 存储数据
59
67
  File.open(filename, "wb") do |chart|
60
68
  chart.write(content)
61
69
  end
70
+
71
+ "#{Rails.root}/#{filename}"
62
72
  end
63
73
 
74
+ # 生成随机名称
64
75
  def generate_name(itemid)
65
- require "uuidtools"
66
- "#{UUIDTools::UUID.timestamp_create}-#{itemid}.png"
76
+ "#{SecureRandom.hex(8)}-#{itemid}.png"
67
77
  end
68
78
  end
69
79
  end
@@ -1,18 +1,19 @@
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: 2.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
@@ -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: 30.minutes) do
24
25
  Rails.logger.warn("正在请求 zabbix/index.php 登录认证")
25
26
  authenticate_with_cookie
26
27
  end
@@ -50,28 +51,30 @@ 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 = Zabbix.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: Zabbix.user,
65
+ password: Zabbix.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
+ # 此处需要特殊处理,否则 V5 版本无法正常加载缓存
77
+ ret.headers["set-cookie"]&.gsub(/ HttpOnly,?/, "").presence || nil
78
+ end
76
79
  end
77
80
  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.4"
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.2.0
4
+ version: 0.2.4
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-05 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