zabbix-rails 0.1.5 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +24 -4
- data/lib/tasks/zabbix_tasks.rake +1 -0
- data/lib/zabbix/chart.rb +69 -0
- data/lib/zabbix/config.rb +13 -0
- data/lib/zabbix/connector.rb +36 -6
- data/lib/zabbix/dns_monitor.rb +2 -0
- data/lib/zabbix/engine.rb +2 -0
- data/lib/zabbix/host_monitor.rb +2 -1
- data/lib/zabbix/item_trigger.rb +2 -0
- data/lib/zabbix/version.rb +3 -1
- data/lib/zabbix-rails.rb +4 -1
- metadata +9 -6
- data/lib/zabbix/railtie.rb +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d72e0a2099b3dd87de6d9a38ec3514bc2e8f6fa7720804a202457a278af39987
|
4
|
+
data.tar.gz: d92f35f0c3497663edc9f50f5a6fbf4815bde298fb50fd2eb2921ffa367077ed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 239955a4196412900aad1f05e4b455274355960d3643ef1fc6572f16b16b158f7ec7b856aa069046c857774c284eaf7ef9d2b6a8f60e9727b2a4bbef1dd674c9
|
7
|
+
data.tar.gz: 6d3b8303de360e70c3c590faa7ed39b3b22fab9a198fd552dbe150ebf5a4a4eec090110c8b199db4c16409d54ffdbd7d143aebb4d1e372ae671435e4621b856e
|
data/README.md
CHANGED
@@ -1,10 +1,17 @@
|
|
1
|
-
# Zabbix
|
2
|
-
|
1
|
+
# Zabbix-Rails
|
2
|
+
主要实现 zabbix_connector 对象缓存,加速 API 交互效率。 依赖 zabbix_manager 作为后端请求模块
|
3
3
|
|
4
4
|
## Usage
|
5
|
-
|
5
|
+
```
|
6
|
+
1、include Zabbix::Connector 到模块或者类对象;
|
7
|
+
2、调用 zabbix_connector 方法自动实现后端鉴权,当前前提是配置好 'zabbix_rails.rb in config/initializers'
|
8
|
+
3、调用 Zabbix::HostMonitor 实现自动维护监控对象
|
9
|
+
4、调用 Zabbix::ItemTrigger 实现自动维护触发器对象
|
10
|
+
5、调用 Zabbix::Dns 实现自动维护 DNS 监控
|
11
|
+
```
|
6
12
|
|
7
13
|
## Installation
|
14
|
+
|
8
15
|
Add this line to your application's Gemfile:
|
9
16
|
|
10
17
|
```ruby
|
@@ -12,17 +19,30 @@ gem "zabbix-rails"
|
|
12
19
|
```
|
13
20
|
|
14
21
|
And then execute:
|
22
|
+
|
15
23
|
```bash
|
16
24
|
$ bundle
|
17
25
|
```
|
18
26
|
|
19
27
|
Or install it yourself as:
|
28
|
+
|
20
29
|
```bash
|
21
30
|
$ gem install zabbix-rails
|
22
31
|
```
|
23
32
|
|
33
|
+
add zabbix_rails.rb in config/initializers
|
34
|
+
|
35
|
+
```
|
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
|
42
|
+
```
|
43
|
+
|
24
44
|
## Contributing
|
25
|
-
Contribution directions go here.
|
26
45
|
|
27
46
|
## License
|
47
|
+
|
28
48
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/lib/tasks/zabbix_tasks.rake
CHANGED
data/lib/zabbix/chart.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
require "active_support/concern"
|
2
|
+
|
3
|
+
module Zabbix
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
module Chart
|
7
|
+
# 请求生成 graph_chart 对象
|
8
|
+
def graph_chart(graphid, height = 400, width = 900, start_at = "now-6h", end_at = "now")
|
9
|
+
# 构造数据结构
|
10
|
+
data = {
|
11
|
+
from: start_at,
|
12
|
+
to: end_at,
|
13
|
+
graphid: graphid,
|
14
|
+
height: height,
|
15
|
+
width: width
|
16
|
+
}
|
17
|
+
# 请求后端返回图片对象
|
18
|
+
_item_chart("/chart2.php", data)
|
19
|
+
end
|
20
|
+
|
21
|
+
# 请求生成 item_chart 对象
|
22
|
+
def item_chart(itemid, height = 400, width = 900, start_at = "now-6h", end_at = "now")
|
23
|
+
# 构造数据结构
|
24
|
+
data = {
|
25
|
+
from: start_at,
|
26
|
+
to: end_at,
|
27
|
+
itemids: [itemid.to_i],
|
28
|
+
height: height,
|
29
|
+
width: width
|
30
|
+
}
|
31
|
+
# 请求后端返回图片对象
|
32
|
+
_item_chart(data)
|
33
|
+
end
|
34
|
+
|
35
|
+
# 请求后端返回图形
|
36
|
+
def _item_chart(url = "/chart.php", data)
|
37
|
+
# 请求接口
|
38
|
+
@conn.get "#{url}" do |r|
|
39
|
+
r.params.merge!(data)
|
40
|
+
r.headers["Host"] = Config.url.gsub!(%r{http(s)?://}, "")
|
41
|
+
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
|
+
r.headers["Cookie"] = zabbix_token
|
43
|
+
end.body
|
44
|
+
end
|
45
|
+
|
46
|
+
# 保存图片并返回文件名
|
47
|
+
def save_chart_graph(itemid, height = 400, width = 900, start_at = "now-6h", end_at = "now")
|
48
|
+
filename = generate_name(itemid)
|
49
|
+
save_file(filename, item_chart(itemid, height, width, start_at, end_at))
|
50
|
+
"chart/#{File.basename(filename)}"
|
51
|
+
end
|
52
|
+
|
53
|
+
# 保存图片
|
54
|
+
def save_file(filename, content)
|
55
|
+
# 生成绝对路径地址
|
56
|
+
filename.prepend("app/assets/images/chart/")
|
57
|
+
|
58
|
+
# 存储数据
|
59
|
+
File.open(filename, "wb") do |chart|
|
60
|
+
chart.write(content)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def generate_name(itemid)
|
65
|
+
require "uuidtools"
|
66
|
+
"#{UUIDTools::UUID.timestamp_create}-#{itemid}.png"
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/lib/zabbix/connector.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/concern"
|
1
4
|
|
2
5
|
module Zabbix
|
3
6
|
extend ::ActiveSupport::Concern
|
@@ -7,18 +10,22 @@ module Zabbix
|
|
7
10
|
# 将鉴权后的 zabbix-rails 对象缓存到 Rails,减少不必要的认证动作加速执行
|
8
11
|
# https://guides.rubyonrails.org/caching_with_rails.html
|
9
12
|
def zabbix_connector
|
10
|
-
|
11
|
-
user = "Admin"
|
12
|
-
password = "zabbix-rails"
|
13
|
-
|
14
|
-
Rails.cache.fetch("zabbix_connector", expires_in: 4.hours) do
|
13
|
+
Rails.cache.fetch("zabbix_connector", expires_in: 2.hours) do
|
15
14
|
Rails.logger.warn("Zabbix 正在请求 API 鉴权")
|
16
|
-
ZabbixManager.connect(url: url, user: user, password: password, debug:
|
15
|
+
ZabbixManager.connect(url: Config.url, user: Config.user, password: Config.password, debug: Config.debug)
|
17
16
|
rescue => e
|
18
17
|
Rails.logger.warn("登录 ZABBIX 异常,请检查网络或登录凭证。原始报错信息: #{e}")
|
19
18
|
end
|
20
19
|
end
|
21
20
|
|
21
|
+
# 获取 zabbix 登录凭证缓存
|
22
|
+
def zabbix_token
|
23
|
+
Rails.cache.fetch("zabbix_token", expires_in: 2.hours) do
|
24
|
+
Rails.logger.warn("正在请求 zabbix/index.php 登录认证")
|
25
|
+
authenticate_with_cookie
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
22
29
|
# 删除主机监控
|
23
30
|
def delete_host_monitor(name)
|
24
31
|
id = zabbix_connector.hosts.get_host_id(name)
|
@@ -44,4 +51,27 @@ module Zabbix
|
|
44
51
|
Rails.logger.warn("调用 delete_trigger_monitor 接口异常,原始报错信息: #{e}")
|
45
52
|
end
|
46
53
|
end
|
54
|
+
|
55
|
+
# 请求 zabbix 后端认证,返回 Cookie
|
56
|
+
def authenticate_with_cookie
|
57
|
+
# 初始化会话
|
58
|
+
base_url = Config.url
|
59
|
+
@conn = Faraday::Connection.new(base_url)
|
60
|
+
|
61
|
+
# 登录权限凭证
|
62
|
+
data = {
|
63
|
+
name: Config.user,
|
64
|
+
password: Config.password,
|
65
|
+
autologin: 1,
|
66
|
+
enter: "Sign in",
|
67
|
+
}
|
68
|
+
|
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
|
74
|
+
|
75
|
+
ret.headers["set-cookie"].presence || nil
|
76
|
+
end
|
47
77
|
end
|
data/lib/zabbix/dns_monitor.rb
CHANGED
data/lib/zabbix/engine.rb
CHANGED
data/lib/zabbix/host_monitor.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "active_support/concern"
|
4
|
+
|
3
5
|
module Zabbix
|
4
6
|
extend ::ActiveSupport::Concern
|
5
7
|
|
@@ -42,6 +44,5 @@ module Zabbix
|
|
42
44
|
rescue => e
|
43
45
|
Rails.logger.warn("无法执行创建或更新监控主机: #{item.name} #{item.sn} #{e}")
|
44
46
|
end
|
45
|
-
|
46
47
|
end
|
47
48
|
end
|
data/lib/zabbix/item_trigger.rb
CHANGED
data/lib/zabbix/version.rb
CHANGED
data/lib/zabbix-rails.rb
CHANGED
@@ -1,11 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "zabbix/version"
|
4
|
+
require "zabbix/config"
|
2
5
|
require "zabbix/connector"
|
3
6
|
require "zabbix/host_monitor"
|
4
7
|
require "zabbix/item_trigger"
|
5
8
|
require "zabbix/dns_monitor"
|
6
|
-
require "zabbix/railtie"
|
7
9
|
|
8
10
|
# 加载外部依赖
|
11
|
+
require "active_support/concern"
|
9
12
|
require "zabbix_manager"
|
10
13
|
|
11
14
|
module Zabbix
|
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.
|
4
|
+
version: 0.2.0
|
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:
|
27
|
+
description: 借助 zabbix_manager 实现自动维护 zabbix 监控对象
|
28
28
|
email:
|
29
29
|
- careline@foxmail.com
|
30
30
|
executables: []
|
@@ -35,17 +35,20 @@ files:
|
|
35
35
|
- Rakefile
|
36
36
|
- lib/tasks/zabbix_tasks.rake
|
37
37
|
- lib/zabbix-rails.rb
|
38
|
+
- lib/zabbix/chart.rb
|
39
|
+
- lib/zabbix/config.rb
|
38
40
|
- lib/zabbix/connector.rb
|
39
41
|
- lib/zabbix/dns_monitor.rb
|
40
42
|
- lib/zabbix/engine.rb
|
41
43
|
- lib/zabbix/host_monitor.rb
|
42
44
|
- lib/zabbix/item_trigger.rb
|
43
|
-
- lib/zabbix/railtie.rb
|
44
45
|
- lib/zabbix/version.rb
|
45
|
-
homepage: https://
|
46
|
+
homepage: https://github.com/ciscolive/zabbix-rails
|
46
47
|
licenses: []
|
47
48
|
metadata:
|
48
|
-
homepage_uri: https://
|
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:
|
70
|
+
summary: 主要实现 zabbix_connector 对象缓存,加速 API 交互效率
|
68
71
|
test_files: []
|
data/lib/zabbix/railtie.rb
DELETED