zabbix-rails 0.1.3 → 0.1.8
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/config.rb +13 -0
- data/lib/zabbix/connector.rb +4 -7
- data/lib/zabbix/dns_monitor.rb +5 -1
- data/lib/zabbix/engine.rb +3 -1
- data/lib/zabbix/host_monitor.rb +33 -34
- data/lib/zabbix/item_trigger.rb +5 -1
- data/lib/zabbix/version.rb +3 -1
- data/lib/zabbix-rails.rb +5 -3
- metadata +5 -5
- 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: 57a01f1a90fa92149ff18d714662c27729794976268f1d8b74beb0a2e9acaba5
|
4
|
+
data.tar.gz: 7331a58248988f77994103d9be4dd5e0d33f65c2a8319e48549ac988b08a6ce7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9b7cce1c75bc1dcc16206d522bef8b96975f242ad68cbb272ac4fd06144436eec293a27f21539d89dc08cca9b7fc2c784dea3a44dce2005b8bcedead0f730c8c
|
7
|
+
data.tar.gz: 0a9eaf9aea738353c907607b36a90df624e12b14272adde4b50569d78b188410929b30d63373beb5a28854edfa3f766a23ffafefacc90f6b44a498c80c503d44
|
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/connector.rb
CHANGED
@@ -1,21 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
|
2
3
|
require "active_support/concern"
|
3
4
|
|
4
5
|
module Zabbix
|
5
|
-
|
6
|
-
extend ActiveSupport::Concern
|
6
|
+
extend ::ActiveSupport::Concern
|
7
7
|
|
8
|
+
module Connector
|
8
9
|
# 开发测试环境需要单独开启缓存功能:bin/rails dev:cache
|
9
10
|
# 将鉴权后的 zabbix-rails 对象缓存到 Rails,减少不必要的认证动作加速执行
|
10
11
|
# https://guides.rubyonrails.org/caching_with_rails.html
|
11
12
|
def zabbix_connector
|
12
|
-
url = "http://zabbix-rails.local/api_jsonrpc.php"
|
13
|
-
user = "Admin"
|
14
|
-
password = "zabbix-rails"
|
15
|
-
|
16
13
|
Rails.cache.fetch("zabbix_connector", expires_in: 4.hours) do
|
17
14
|
Rails.logger.warn("Zabbix 正在请求 API 鉴权")
|
18
|
-
ZabbixManager.connect(url: url, user: user, password: password, debug:
|
15
|
+
ZabbixManager.connect(url: Config.url, user: Config.user, password: Config.password, debug: Config.debug)
|
19
16
|
rescue => e
|
20
17
|
Rails.logger.warn("登录 ZABBIX 异常,请检查网络或登录凭证。原始报错信息: #{e}")
|
21
18
|
end
|
data/lib/zabbix/dns_monitor.rb
CHANGED
@@ -1,8 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "active_support/concern"
|
4
|
+
|
3
5
|
module Zabbix
|
6
|
+
extend ::ActiveSupport::Concern
|
7
|
+
|
4
8
|
module DnsMonitor
|
5
|
-
include
|
9
|
+
include Zabbix::Connector
|
6
10
|
|
7
11
|
# 生成 DNS 触发器表达式
|
8
12
|
def dns_trigger_expression(name)
|
data/lib/zabbix/engine.rb
CHANGED
data/lib/zabbix/host_monitor.rb
CHANGED
@@ -1,49 +1,48 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "active_support/concern"
|
4
|
+
|
3
5
|
module Zabbix
|
6
|
+
extend ::ActiveSupport::Concern
|
7
|
+
|
4
8
|
module HostMonitor
|
5
|
-
|
6
|
-
include ZabbixConnectConcern
|
9
|
+
include Zabbix::Connector
|
7
10
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
hostid = item[:hostid]
|
11
|
+
# 创建或更新 HostMonitor主机监控对象
|
12
|
+
def refresh_zabbix_host(item)
|
13
|
+
if zabbix_connector.present? && (zabbix_connector.is_a? ZabbixManager)
|
14
|
+
# 查询已关联的 hostid
|
15
|
+
hostid = item[:hostid]
|
14
16
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
else
|
27
|
-
Rails.logger.warn("正在更新监控对象: #{item.sn} - #{item.name}")
|
28
|
-
zabbix_connector.hosts.create_or_update(item.host_params)
|
29
|
-
end
|
17
|
+
# 封装数据之前确保设备属组已经存在
|
18
|
+
search_id = zabbix_connector.hosts.get_id(host: item.sn)
|
19
|
+
# 构建 zabbix-rails 监控对象数据结构,创建或者更新数据
|
20
|
+
if hostid.present?
|
21
|
+
if search_id.blank?
|
22
|
+
Rails.logger.warn("监控主机信息发生变化,正在创建监控对象: #{item}.inspect")
|
23
|
+
Zabbix::DeleteMonitor.perform_now(item.sn)
|
24
|
+
zabbix_connector.hosts.create_or_update(item.host_params)
|
25
|
+
elsif hostid != search_id
|
26
|
+
Rails.logger.warn("ZABBIX已有监控项,正在刷新数据 #{hostid} - #{search_id}")
|
27
|
+
search_id
|
30
28
|
else
|
31
|
-
Rails.logger.warn("
|
29
|
+
Rails.logger.warn("正在更新监控对象: #{item.sn} - #{item.name}")
|
32
30
|
zabbix_connector.hosts.create_or_update(item.host_params)
|
33
31
|
end
|
34
|
-
|
35
|
-
# 返回监控对象的 host_id
|
36
|
-
Rails.logger.warn("成功执行创建或更新监控主机: #{item.name} #{item.sn}")
|
37
|
-
zabbix_connector.hosts.get_id(host: item.sn).presence || nil
|
38
32
|
else
|
39
|
-
Rails.logger.warn("
|
40
|
-
|
33
|
+
Rails.logger.warn("正在新增监控对象: #{item.sn} - #{item.name}")
|
34
|
+
zabbix_connector.hosts.create_or_update(item.host_params)
|
41
35
|
end
|
42
|
-
rescue => e
|
43
|
-
Rails.logger.warn("无法执行创建或更新监控主机: #{item.name} #{item.sn} #{e}")
|
44
|
-
end
|
45
36
|
|
37
|
+
# 返回监控对象的 host_id
|
38
|
+
Rails.logger.warn("成功执行创建或更新监控主机: #{item.name} #{item.sn}")
|
39
|
+
zabbix_connector.hosts.get_id(host: item.sn).presence || nil
|
40
|
+
else
|
41
|
+
Rails.logger.warn("未获取 ZABBIX_MGMT 对象,无法执行创建或更新监控主机: #{item.name} #{item.sn}")
|
42
|
+
nil
|
43
|
+
end
|
44
|
+
rescue => e
|
45
|
+
Rails.logger.warn("无法执行创建或更新监控主机: #{item.name} #{item.sn} #{e}")
|
46
46
|
end
|
47
|
-
|
48
47
|
end
|
49
48
|
end
|
data/lib/zabbix/item_trigger.rb
CHANGED
@@ -1,8 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "active_support/concern"
|
4
|
+
|
3
5
|
module Zabbix
|
6
|
+
extend ::ActiveSupport::Concern
|
7
|
+
|
4
8
|
module ItemTrigger
|
5
|
-
include
|
9
|
+
include Zabbix::Connector
|
6
10
|
|
7
11
|
# 创建或新增触发器,返回触发器ID | 触发器的 DATA_STRUCTURE
|
8
12
|
def refresh_trigger(data)
|
data/lib/zabbix/version.rb
CHANGED
data/lib/zabbix-rails.rb
CHANGED
@@ -1,16 +1,18 @@
|
|
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
|
-
require "active_support/concern"
|
8
9
|
|
9
10
|
# 加载外部依赖
|
11
|
+
require "active_support/concern"
|
10
12
|
require "zabbix_manager"
|
11
13
|
|
12
14
|
module Zabbix
|
13
|
-
extend ActiveSupport::Concern
|
15
|
+
extend ::ActiveSupport::Concern
|
14
16
|
|
15
17
|
class << self
|
16
18
|
attr_accessor :url, :user, :password, :debug
|
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.
|
4
|
+
version: 0.1.8
|
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-
|
11
|
+
date: 2022-03-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: '0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: '0'
|
27
27
|
description: Description of Zabbix-rails.
|
28
28
|
email:
|
29
29
|
- careline@foxmail.com
|
@@ -35,12 +35,12 @@ files:
|
|
35
35
|
- Rakefile
|
36
36
|
- lib/tasks/zabbix_tasks.rake
|
37
37
|
- lib/zabbix-rails.rb
|
38
|
+
- lib/zabbix/config.rb
|
38
39
|
- lib/zabbix/connector.rb
|
39
40
|
- lib/zabbix/dns_monitor.rb
|
40
41
|
- lib/zabbix/engine.rb
|
41
42
|
- lib/zabbix/host_monitor.rb
|
42
43
|
- lib/zabbix/item_trigger.rb
|
43
|
-
- lib/zabbix/railtie.rb
|
44
44
|
- lib/zabbix/version.rb
|
45
45
|
homepage: https://rubygem.org
|
46
46
|
licenses: []
|
data/lib/zabbix/railtie.rb
DELETED