zabbix-rails 0.1.8 → 0.2.2

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: 57a01f1a90fa92149ff18d714662c27729794976268f1d8b74beb0a2e9acaba5
4
- data.tar.gz: 7331a58248988f77994103d9be4dd5e0d33f65c2a8319e48549ac988b08a6ce7
3
+ metadata.gz: a35e9d3bc2ccbf034fa95b86940f7b9ce66b9d8fd3317ff98b3f002976f8b8e3
4
+ data.tar.gz: 802e68ec3f3284fbb86161fdfc5c426a34fba94469d35e1690ec8ff6427bea86
5
5
  SHA512:
6
- metadata.gz: 9b7cce1c75bc1dcc16206d522bef8b96975f242ad68cbb272ac4fd06144436eec293a27f21539d89dc08cca9b7fc2c784dea3a44dce2005b8bcedead0f730c8c
7
- data.tar.gz: 0a9eaf9aea738353c907607b36a90df624e12b14272adde4b50569d78b188410929b30d63373beb5a28854edfa3f766a23ffafefacc90f6b44a498c80c503d44
6
+ metadata.gz: 198fade3a060207b1c2e401640d796c1e1e6985bc21dbccba8c716f066874bdfe4f5302e33faf45bd1c74354597e70d8870872e54abc761cf58d24649c9ba1ee
7
+ data.tar.gz: 02e31303134e3afd75f87e5c409371e948f7db3d15401d21b519b248bde323ddc77d700b0e2d8d3e666768bccbdca88c3e5817e189bd8ea3ef62502f7fcdfb8f
@@ -0,0 +1,73 @@
1
+ require "active_support/concern"
2
+
3
+ module Zabbix
4
+ module Chart
5
+ extend ActiveSupport::Concern
6
+ include Zabbix::Connector
7
+
8
+ # 请求生成 graph_chart 对象
9
+ def graph_chart(graphid, height = 400, width = 900, start_at = "now-6h", end_at = "now")
10
+ # 构造数据结构
11
+ data = {
12
+ from: start_at,
13
+ to: end_at,
14
+ graphid: graphid,
15
+ height: height,
16
+ width: width
17
+ }
18
+ # 请求后端返回图片对象
19
+ _item_chart("/chart2.php", data)
20
+ end
21
+
22
+ # 请求生成 item_chart 对象
23
+ def item_chart(itemid, height = 400, width = 900, start_at = "now-6h", end_at = "now")
24
+ # 构造数据结构
25
+ data = {
26
+ from: start_at,
27
+ to: end_at,
28
+ itemids: [itemid.to_i],
29
+ height: height,
30
+ width: width
31
+ }
32
+ # 请求后端返回图片对象
33
+ _item_chart(data)
34
+ end
35
+
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)
41
+ # 请求接口
42
+ conn.get "#{url}" do |r|
43
+ r.params.merge!(data)
44
+ r.headers["Host"] = host
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"
46
+ r.headers["Cookie"] = zabbix_token
47
+ end.body
48
+ end
49
+
50
+ # 保存图片并返回文件名
51
+ def save_chart_graph(itemid, height = 400, width = 900, start_at = "now-6h", end_at = "now")
52
+ filename = generate_name(itemid)
53
+ save_file(filename, item_chart(itemid, height, width, start_at, end_at))
54
+ "chart/#{File.basename(filename)}"
55
+ end
56
+
57
+ # 保存图片
58
+ def save_file(filename, content)
59
+ # 生成绝对路径地址
60
+ filename.prepend("app/assets/images/chart/")
61
+
62
+ # 存储数据
63
+ File.open(filename, "wb") do |chart|
64
+ chart.write(content)
65
+ end
66
+ end
67
+
68
+ def generate_name(itemid)
69
+ require "uuidtools"
70
+ "#{UUIDTools::UUID.timestamp_create}-#{itemid}.png"
71
+ end
72
+ end
73
+ end
@@ -1,16 +1,17 @@
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: 2.hours) do
14
15
  Rails.logger.warn("Zabbix 正在请求 API 鉴权")
15
16
  ZabbixManager.connect(url: Config.url, user: Config.user, password: Config.password, debug: Config.debug)
16
17
  rescue => e
@@ -18,6 +19,14 @@ module Zabbix
18
19
  end
19
20
  end
20
21
 
22
+ # 获取 zabbix 登录凭证缓存
23
+ def zabbix_token
24
+ Rails.cache.fetch("zabbix_token", expires_in: 1.hours) 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 = Config.url
59
+ host = URI(base_url).host
60
+ conn = Faraday::Connection.new(base_url)
61
+
62
+ # 登录权限凭证
63
+ data = {
64
+ name: Config.user,
65
+ password: Config.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.8"
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.1.8
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - WENWU.YAN
@@ -24,7 +24,7 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
- description: Description of Zabbix-rails.
27
+ description: 借助 zabbix_manager 实现自动维护 zabbix 监控对象
28
28
  email:
29
29
  - careline@foxmail.com
30
30
  executables: []
@@ -35,6 +35,7 @@ files:
35
35
  - Rakefile
36
36
  - lib/tasks/zabbix_tasks.rake
37
37
  - lib/zabbix-rails.rb
38
+ - lib/zabbix/chart.rb
38
39
  - lib/zabbix/config.rb
39
40
  - lib/zabbix/connector.rb
40
41
  - lib/zabbix/dns_monitor.rb
@@ -42,10 +43,12 @@ files:
42
43
  - lib/zabbix/host_monitor.rb
43
44
  - lib/zabbix/item_trigger.rb
44
45
  - lib/zabbix/version.rb
45
- homepage: https://rubygem.org
46
+ homepage: https://github.com/ciscolive/zabbix-rails
46
47
  licenses: []
47
48
  metadata:
48
- homepage_uri: https://rubygem.org
49
+ homepage_uri: https://github.com/ciscolive/zabbix-rails
50
+ source_code_uri: https://github.com/ciscolive/zabbix-rails
51
+ changelog_uri: https://github.com/ciscolive/zabbix-rails/blob/main/README.md
49
52
  post_install_message:
50
53
  rdoc_options: []
51
54
  require_paths:
@@ -64,5 +67,5 @@ requirements: []
64
67
  rubygems_version: 3.3.3
65
68
  signing_key:
66
69
  specification_version: 4
67
- summary: Summary of Zabbix-rails.
70
+ summary: 主要实现 zabbix_connector 对象缓存,加速 API 交互效率
68
71
  test_files: []