zabbix-rails 0.2.2 → 0.2.5
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 +4 -4
- data/README.md +17 -8
- data/Rakefile +2 -0
- data/lib/zabbix/chart.rb +13 -7
- data/lib/zabbix/connector.rb +9 -8
- data/lib/zabbix/host_monitor.rb +3 -0
- data/lib/zabbix/version.rb +1 -1
- data/lib/zabbix-rails.rb +3 -4
- metadata +2 -3
- data/lib/zabbix/config.rb +0 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 83d29ce8108e3a961a912b374df2183d87ca82c3bc32a7740ac1776918fd1714
|
4
|
+
data.tar.gz: c95b0c117cbf172ea55798bcc49a0dec2459957a4c0060543fa0862d5ea7cd60
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 20370a5a28d224ab5de353f00b0ab5ed41df9392f248d446f0eeb9767dfb00b248927752b1ad4108ecab89817da74e89561450d3358beb3a268145b14527d302
|
7
|
+
data.tar.gz: 3198490f86bcfca1976932222c48b188751879104202752479146c9591dd3036f5dc03e9241127f6acad551ad373b19317980b6e74c12482defc13f3f714c825
|
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
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
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
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,11 +37,11 @@ module Zabbix
|
|
35
37
|
|
36
38
|
# 请求后端返回图形
|
37
39
|
def _item_chart(url = "/chart.php", data)
|
38
|
-
base_url =
|
40
|
+
base_url = Zabbix.url
|
39
41
|
host = URI(base_url).host
|
40
|
-
conn
|
42
|
+
conn = Faraday::Connection.new(base_url)
|
41
43
|
# 请求接口
|
42
|
-
conn.get
|
44
|
+
conn.get(url) do |r|
|
43
45
|
r.params.merge!(data)
|
44
46
|
r.headers["Host"] = host
|
45
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"
|
@@ -51,23 +53,27 @@ 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)}"
|
55
56
|
end
|
56
57
|
|
57
58
|
# 保存图片
|
58
59
|
def save_file(filename, content)
|
60
|
+
directory_base = "app/assets/images/chart/"
|
61
|
+
|
59
62
|
# 生成绝对路径地址
|
60
|
-
|
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|
|
64
68
|
chart.write(content)
|
65
69
|
end
|
70
|
+
|
71
|
+
"#{Rails.root}/#{filename}"
|
66
72
|
end
|
67
73
|
|
74
|
+
# 生成随机名称
|
68
75
|
def generate_name(itemid)
|
69
|
-
|
70
|
-
"#{UUIDTools::UUID.timestamp_create}-#{itemid}.png"
|
76
|
+
"#{SecureRandom.hex(8)}-#{itemid}.png"
|
71
77
|
end
|
72
78
|
end
|
73
79
|
end
|
data/lib/zabbix/connector.rb
CHANGED
@@ -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:
|
14
|
+
Rails.cache.fetch("zabbix_connector", expires_in: 30.minutes) do
|
15
15
|
Rails.logger.warn("Zabbix 正在请求 API 鉴权")
|
16
|
-
ZabbixManager.connect(url:
|
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:
|
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,25 +55,26 @@ module Zabbix
|
|
55
55
|
# 请求 zabbix 后端认证,返回 Cookie
|
56
56
|
def authenticate_with_cookie
|
57
57
|
# 初始化会话
|
58
|
-
base_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:
|
65
|
-
password:
|
64
|
+
name: Zabbix.user,
|
65
|
+
password: Zabbix.password,
|
66
66
|
autologin: 1,
|
67
67
|
enter: "Sign in",
|
68
68
|
}
|
69
69
|
|
70
70
|
# 请求认证
|
71
|
-
ret = conn.post
|
71
|
+
ret = conn.post("/index.php") do |r|
|
72
72
|
r.headers["Host"] = host
|
73
73
|
r.body = data
|
74
74
|
end
|
75
75
|
|
76
|
-
|
76
|
+
# 此处需要特殊处理,否则 V5 版本无法正常加载缓存
|
77
|
+
ret.headers["set-cookie"]&.gsub(/ HttpOnly,?/, "").presence || nil
|
77
78
|
end
|
78
79
|
end
|
79
80
|
end
|
data/lib/zabbix/host_monitor.rb
CHANGED
@@ -15,6 +15,9 @@ module Zabbix
|
|
15
15
|
hostid = item[:hostid]
|
16
16
|
|
17
17
|
# 封装数据之前确保设备属组已经存在
|
18
|
+
zabbix_connector.hostgroups.get_or_create_hostgroups(item.group) if item.group.present?
|
19
|
+
|
20
|
+
# 检查是否已存在监控对象
|
18
21
|
search_id = zabbix_connector.hosts.get_id(host: item.sn)
|
19
22
|
# 构建 zabbix-rails 监控对象数据结构,创建或者更新数据
|
20
23
|
if hostid.present?
|
data/lib/zabbix/version.rb
CHANGED
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
|
23
|
-
|
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.
|
4
|
+
version: 0.2.5
|
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-
|
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
|