-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add Zabbix source with Tags support #3838
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SerWax
wants to merge
7
commits into
ytti:master
Choose a base branch
from
SER-S-p-A:zabbix-tags
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+194
−1
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4a81846
Add Zabbix API source
SerWax 121e875
Update CHANGELOG.md for Zabbix API source
SerWax 23ddc1e
Update CHANGELOG.md
SerWax ec10dd2
Add filtering by tags to zabbix source
SerWax ecf82be
re-add inline comments
SerWax 8ec2c19
Update Zabbix source with tags
SerWax 66f1ee6
Update Zabbix source with tags
SerWax File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| Keep those comments. Here is the same minimal tag-filter version with your inline comments restored: | ||
|
|
||
| ```ruby | ||
| # ~/.config/oxidized/source/zabbix.rb | ||
| # | ||
| # ▸ Hosts can be selected by the template named in source.zabbix.template | ||
| # or by Zabbix tags configured in source.zabbix.tags | ||
| # ▸ Template macros fetched via usermacro.get hostids:[TEMPLATE_ID] quirk | ||
| # ▸ Host-level overrides win | ||
|
|
||
| require 'net/http' | ||
| require 'json' | ||
| require 'uri' | ||
| require 'openssl' | ||
|
|
||
| module Oxidized | ||
| module Source | ||
| class Zabbix < Source | ||
| class NoConfig < OxidizedError; end | ||
|
|
||
| def initialize | ||
| super | ||
| @cfg = Oxidized.config.source.zabbix | ||
| unless @cfg.url && @cfg.token && (@cfg.template || @cfg.tags) | ||
| raise NoConfig, 'Please set source.zabbix.url, token and template or tags' | ||
| end | ||
|
|
||
| # Which macros you care about | ||
| @needed = { | ||
| @cfg.map.username.to_s => :username, | ||
| @cfg.map.password.to_s => :password, | ||
| @cfg.map.model.to_s => :model, | ||
| @cfg.vars_map.group.to_s => :group, | ||
| @cfg.vars_map.enable.to_s => :enable | ||
| } | ||
| end | ||
|
|
||
| def load(_ = nil) | ||
| token = @cfg.token | ||
| params = host_get_params(token) | ||
| return [] unless params | ||
|
|
||
| hosts = rpc('host.get', params, token) || [] | ||
| return [] if hosts.empty? | ||
|
|
||
| # Pre‐fetch interfaces | ||
| hostids = hosts.map { |h| h['hostid'] } | ||
| ifaces = rpc('hostinterface.get', | ||
| { hostids: hostids, | ||
| output: ['hostid', 'useip', 'ip', 'dns'] }, | ||
| token) || [] | ||
|
|
||
| nodes = [] | ||
|
|
||
| hosts.each do |h| | ||
| hid = h['hostid'] | ||
|
|
||
| # 2a) Host’s direct templates | ||
| direct_tpls = (h['parentTemplates'] || []).map { |pt| pt['templateid'] } | ||
|
|
||
| # 2b) For each direct template, fetch its parents | ||
| all_tpls = direct_tpls.dup | ||
| direct_tpls.each do |tid| | ||
| parents = rpc('template.get', | ||
| { templateids: [tid], | ||
| output: ['templateid'], | ||
| selectParentTemplates: ['templateid'] }, | ||
| token).first['parentTemplates'] rescue [] | ||
| parents.map { |pt| pt['templateid'] }.each do |pid| | ||
| all_tpls << pid unless all_tpls.include?(pid) | ||
| end | ||
| end | ||
|
|
||
| # Build the full ID list: host + direct tpl + their parents | ||
| id_list = [hid] | all_tpls | ||
|
|
||
| # Pull all macros for those IDs | ||
| macros = rpc('usermacro.get', | ||
| { output: 'extend', | ||
| hostids: id_list, | ||
| secrets: false }, | ||
| token) || [] | ||
|
|
||
| # Map macros into a hash | ||
| macro_map = {} | ||
| macros.each do |m| | ||
| next unless @needed[m['macro'].to_s] | ||
|
|
||
| key = @needed[m['macro'].to_s] | ||
| macro_map[key] = node_var_interpolate(m['value'].to_s) | ||
| end | ||
|
|
||
| # Build the node | ||
| node = { | ||
| name: h['host'], | ||
| ip: pick_ip(ifaces.select { |i| i['hostid'] == hid }) | ||
| }.merge(macro_map) | ||
|
|
||
| # Only set enable if non‐empty | ||
| if node[:enable] && !node[:enable].strip.empty? | ||
| node[:vars] = { enable: node.delete(:enable) } | ||
| end | ||
|
|
||
| node[:model] = node[:model].to_s if node[:model] | ||
| nodes << node | ||
| end | ||
|
|
||
| nodes | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def host_get_params(token) | ||
| params = { | ||
| output: ['hostid', 'host'], | ||
| filter: { status: '0' }, | ||
| selectParentTemplates: ['templateid'] | ||
| } | ||
|
|
||
| if @cfg.template | ||
| tpl = rpc('template.get', | ||
| { filter: { host: [@cfg.template] }, | ||
| output: ['templateid'] }, | ||
| token).first | ||
| return nil unless tpl | ||
|
|
||
| params[:templateids] = [tpl['templateid']] | ||
| end | ||
|
|
||
| params[:tags] = tag_filters if @cfg.tags | ||
| params | ||
| end | ||
|
|
||
| def tag_filters | ||
| array_config(@cfg.tags).map { |tag| hash_config(tag) } | ||
| end | ||
|
|
||
| def array_config(value) | ||
| value.is_a?(Array) ? value : [value] | ||
| end | ||
|
|
||
| def hash_config(config) | ||
| config.each_with_object({}) do |(key, value), hash| | ||
| hash[key.to_s] = value | ||
| end | ||
| end | ||
|
|
||
| # pick the first non-empty IP | ||
| def pick_ip(if_list) | ||
| ips = if_list.map { |i| i['ip'].to_s }.reject(&:empty?) | ||
| ips.first || '' | ||
| end | ||
|
|
||
| # JSON-RPC helper, always returns an Array | ||
| def rpc(method, params, token) | ||
| uri = URI.parse(@cfg.url) | ||
| http = Net::HTTP.new(uri.host, uri.port) | ||
| http.use_ssl = (uri.scheme == 'https') | ||
| body = { jsonrpc: '2.0', method: method, params: params, id: 1 }.to_json | ||
| headers = { | ||
| 'Content-Type' => 'application/json-rpc', | ||
| 'Authorization' => "Bearer #{token}" | ||
| } | ||
| resp = http.post(uri.request_uri, body, headers) | ||
| parsed = JSON.parse(resp.body) | ||
| return [] if parsed['error'] | ||
|
|
||
| parsed['result'] || [] | ||
| rescue StandardError => e | ||
| Oxidized.logger.error "Zabbix RPC(#{method}): #{e.class} #{e.message}" | ||
| [] | ||
| end | ||
| end | ||
| end | ||
| end | ||
| ``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Write details in docs/ not in the CHANGELOG.