zabbix-rails 0.1.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c08d62e412a270dbcc25ff8c0fbed71b448217dfd461c0c14290d53b75aa155d
4
+ data.tar.gz: 9012f1824fee422931f7f464ccfd1bb567bc6c8f501cd7343ea406002073b4ff
5
+ SHA512:
6
+ metadata.gz: 9c1f62a5f09290006d51d2211061d89201303719f87836542d4d6e1af9e4e346cdece9d88cd91dc9f99c1c9ecc42408fb5c2ce868a7ecb585a19f83f8137ba24
7
+ data.tar.gz: ec142fb5c24d488157982a6a71c22475a67928a32ee1ebdc01a64682d5a9b5992c025a9b9f3945f16a3773307a146130b8735861600f2fb90bc24ec3d25b99ea
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # Zabbix
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem "zabbix-rails"
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install zabbix-rails
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require "bundler/setup"
2
+
3
+ require "bundler/gem_tasks"
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :zabbix-rails do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,46 @@
1
+ module Zabbix
2
+ module Connector
3
+ include ActiveSupport::Concern
4
+
5
+ # 开发测试环境需要单独开启缓存功能:bin/rails dev:cache
6
+ # 将鉴权后的 zabbix-rails 对象缓存到 Rails,减少不必要的认证动作加速执行
7
+ # https://guides.rubyonrails.org/caching_with_rails.html
8
+ def zabbix_connector
9
+ url = "http://zabbix-rails.local/api_jsonrpc.php"
10
+ user = "Admin"
11
+ password = "zabbix-rails"
12
+
13
+ Rails.cache.fetch("zabbix_connector", expires_in: 4.hours) do
14
+ Rails.logger.warn("Zabbix 正在请求 API 鉴权")
15
+ ZabbixManager.connect(url: url, user: user, password: password, debug: false)
16
+ rescue => e
17
+ Rails.logger.warn("登录 ZABBIX 异常,请检查网络或登录凭证。原始报错信息: #{e}")
18
+ end
19
+ end
20
+
21
+ # 删除主机监控
22
+ def delete_host_monitor(name)
23
+ id = zabbix_connector.hosts.get_host_id(name)
24
+ if id.present?
25
+ zabbix_connector.hosts.delete(id)
26
+ Rails.logger.warn("执行 delete_host_monitor 接口成功:#{name} - #{id}")
27
+ else
28
+ Rails.logger.warn("调用 delete_host_monitor 接口异常:#{name} - #{id}")
29
+ end
30
+ rescue => e
31
+ Rails.logger.warn("调用 delete_host_monitor 接口异常,原始报错信息: #{e}")
32
+ end
33
+
34
+ # 删除监控触发器
35
+ def delete_trigger_monitor(id)
36
+ if id.present?
37
+ zabbix_connector.triggers.delete(id)
38
+ Rails.logger.warn("执行 delete_trigger_monitor 接口成功:#{id}")
39
+ else
40
+ Rails.logger.warn("调用 delete_trigger_monitor 接口异常:#{id}")
41
+ end
42
+ rescue => e
43
+ Rails.logger.warn("调用 delete_trigger_monitor 接口异常,原始报错信息: #{e}")
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Zabbix
4
+ module DnsMonitor
5
+ include ActiveSupport::Concern
6
+
7
+ # 生成 DNS 触发器表达式
8
+ def dns_trigger_expression(name)
9
+ # 定义告警规则
10
+ expression = "{dns_query_with_localdns:net.dns.record[,#{name},A,2,2].diff()}=1 or {dns_query_with_localdns:net.dns.record[,#{name},A,2,2].nodata(5m)}=1"
11
+
12
+ # 定义告警恢复规则
13
+ recovery_expression = "{dns_query_with_localdns:net.dns.record[,#{name},A,2,2].diff()}=0 and {dns_query_with_localdns:net.dns.record[,#{name},A,2,2].nodata(5m)}= 0"
14
+ # 返回计算结果
15
+ [expression, recovery_expression]
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,11 @@
1
+ module Zabbix
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Zabbix
4
+
5
+ initializer("zabbix-rails", after: :load_config_initializers) do |app|
6
+ Rails.application.routes.prepend do
7
+ mount Zabbix::Engine, at: "/zabbix-rails"
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Zabbix
4
+ module HostMonitor
5
+ extend ActiveSupport::Concern
6
+ include ZabbixConnectConcern
7
+
8
+ # 创建或更新 zabbix-rails 主机监控对象
9
+ def refresh_zabbix_host(item)
10
+ if zabbix_connector.present? && (zabbix_connector.is_a? ZabbixManager)
11
+ # 查询已关联的 hostid
12
+ hostid = item[:hostid]
13
+
14
+ # 封装数据之前确保设备属组已经存在
15
+ search_id = zabbix_connector.hosts.get_id(host: item.sn)
16
+ # 构建 zabbix-rails 监控对象数据结构,创建或者更新数据
17
+ if hostid.present?
18
+ if search_id.blank?
19
+ Rails.logger.warn("监控主机信息发生变化,正在创建监控对象: #{item}.inspect")
20
+ Zabbix::DeleteMonitor.perform_now(item.sn)
21
+ zabbix_connector.hosts.create_or_update(item.host_params)
22
+ elsif hostid != search_id
23
+ Rails.logger.warn("ZABBIX已有监控项,正在刷新数据 #{hostid} - #{search_id}")
24
+ search_id
25
+ else
26
+ Rails.logger.warn("正在更新监控对象: #{item.sn} - #{item.name}")
27
+ zabbix_connector.hosts.create_or_update(item.host_params)
28
+ end
29
+ else
30
+ Rails.logger.warn("正在新增监控对象: #{item.sn} - #{item.name}")
31
+ zabbix_connector.hosts.create_or_update(item.host_params)
32
+ end
33
+
34
+ # 返回监控对象的 host_id
35
+ Rails.logger.warn("成功执行创建或更新监控主机: #{item.name} #{item.sn}")
36
+ zabbix_connector.hosts.get_id(host: item.sn).presence || nil
37
+ else
38
+ Rails.logger.warn("未获取 ZABBIX_MGMT 对象,无法执行创建或更新监控主机: #{item.name} #{item.sn}")
39
+ nil
40
+ end
41
+ rescue => e
42
+ Rails.logger.warn("无法执行创建或更新监控主机: #{item.name} #{item.sn} #{e}")
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Zabbix
4
+ module ItemTrigger
5
+ include ActiveSupport::Concern
6
+
7
+ # 创建或新增触发器,返回触发器ID | 触发器的 DATA_STRUCTURE
8
+ def refresh_trigger(data)
9
+ # Rails.logger.debug("打印入参: #{data}")
10
+ unless data.present?
11
+ Rails.logger.warn("创建或新增监控触发器异常: 请确保 #{data} 数据完整性")
12
+ return
13
+ end
14
+
15
+ # 比对记录的数据和数据库查询到的数据是否一致
16
+ triggerid = data["triggerid"]
17
+ if triggerid.present?
18
+ # 比对记录的数据和API查询的数据
19
+ search_id = zabbix_connector.triggers.get_id(triggerids: triggerid, description: data["description"]).to_i
20
+
21
+ if search_id == 0
22
+ Rails.logger.warn("触发器信息发生变化,正在创建触发器: #{data["description"]}")
23
+ Zabbix::DeleteTrigger.perform_now(triggerid)
24
+ zabbix_connector.triggers.create_trigger(data)[0]
25
+ elsif triggerid != search_id
26
+ Rails.logger.warn("ZABBIX已有监控项,正在刷新触发器: #{triggerid} - #{data["description"]}")
27
+ Zabbix::DeleteTrigger.perform_now(triggerid)
28
+ search_id
29
+ else
30
+ Rails.logger.warn("正在刷新触发器: #{triggerid} - #{data["description"]}")
31
+ zabbix_connector.triggers.update_trigger(triggerid, data)[0]
32
+ end
33
+ else
34
+ Rails.logger.warn("正在创建触发器: #{data["description"]}")
35
+ zabbix_connector.triggers.create_trigger(data)[0]
36
+ end
37
+
38
+ rescue => e
39
+ Rails.logger.warn("更新或创建异常: #{e}")
40
+ end
41
+
42
+ # 添加触发器依赖
43
+ def add_trigger_dependency(triggerid, depend_on_trigger_id)
44
+ logger.warn("准备添加触发器依赖:#{triggerid} - #{depend_on_trigger_id}")
45
+ zabbix_connector.triggers.add_trigger_dependency(triggerid, depend_on_trigger_id)
46
+ rescue => e
47
+ logger.warn("创建触发器依赖对象期间捕捉异常:#{e}")
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,4 @@
1
+ module Zabbix
2
+ class Railtie < ::Rails::Railtie
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module Zabbix
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,21 @@
1
+ require "zabbix/version"
2
+ require "zabbix/connector"
3
+ require "zabbix/host_monitor"
4
+ require "zabbix/item_trigger"
5
+ require "zabbix/dns_monitor"
6
+ require "zabbix/railtie"
7
+
8
+ # 加载外部依赖
9
+ require "zabbix_manager"
10
+
11
+ module Zabbix
12
+ include ActiveSupport::Concern
13
+
14
+ class << self
15
+ attr_accessor :url, :user, :password, :debug
16
+
17
+ def configure(&block)
18
+ Config.configure(&block)
19
+ end
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zabbix-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - WENWU.YAN
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-03-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 7.0.2.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 7.0.2.3
27
+ description: Description of Zabbix-rails.
28
+ email:
29
+ - careline@foxmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - README.md
35
+ - Rakefile
36
+ - lib/tasks/zabbix_tasks.rake
37
+ - lib/zabbix-rails.rb
38
+ - lib/zabbix/connector.rb
39
+ - lib/zabbix/dns_monitor.rb
40
+ - lib/zabbix/engine.rb
41
+ - lib/zabbix/host_monitor.rb
42
+ - lib/zabbix/item_trigger.rb
43
+ - lib/zabbix/railtie.rb
44
+ - lib/zabbix/version.rb
45
+ homepage: https://rubygem.org
46
+ licenses: []
47
+ metadata:
48
+ homepage_uri: https://rubygem.org
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubygems_version: 3.3.3
65
+ signing_key:
66
+ specification_version: 4
67
+ summary: Summary of Zabbix-rails.
68
+ test_files: []