Skip to content

Instantly share code, notes, and snippets.

@nod0n
Forked from anonymous/zabbixsync.rb
Last active September 29, 2015 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nod0n/e9db3cb04b4eb5557027 to your computer and use it in GitHub Desktop.
Save nod0n/e9db3cb04b4eb5557027 to your computer and use it in GitHub Desktop.
the complete class
require 'zabbixapi'
require 'json'
require 'yaml'
require 'nokogiri'
require 'fileutils'
class ZabbixSync < ZabbixApi
RULES = {
templates: {createMissing: true, updateExisting: true},
applications: {
createMissing: true,
updateExisting: true,
deleteMissing: true
},
items: {
createMissing: true,
updateExisting: true,
deleteMissing: true
},
triggers: {
createMissing: true,
updateExisting: true,
deleteMissing: true
},
graphs: {
createMissing: true,
updateExisting: true,
deleteMissing: true
},
templateScreens: {createMissing: true, updateExisting: true},
discoveryRules: {
createMissing: true,
updateExisting: true,
deleteMissing: true
},
templateLinkage: {createMissing: true},
groups: {createMissing: true}
}
TEMPLATES_DIR = '/srv/zabbixsync_data'
TEMPLATES_INDEX_FILE = "templates.yaml"
CONNECTION = {
url: "http://localhost/zabbix/api_jsonrpc.php",
user: "Admin",
password: "zabbix"
}
TEMPLATES_LIST_FILE = '/usr/local/etc/zabbix_sync.conf'
EXPORT_TYPE = 'xml'
attr_reader(
:connection,
:templates_index,
:templates_index_file,
:templates_dir,
:templates_list_file,
:templates_white_list,
:templates_black_list,
:rules,
:templates_data
)
def initialize(options = {})
@export_format = EXPORT_TYPE
@imported_templates = []
@connection = CONNECTION
@templates_index_file = TEMPLATES_INDEX_FILE
@templates_dir = TEMPLATES_DIR
@templates_list_file = TEMPLATES_LIST_FILE
@rules = RULES
set_connection options
set_templates_index options
set_templates options
set_templates_list options
set_rules options
super @connection
end
### check if object is like it should (ex: not empty, has key)
def check_hash(hash)
hash.is_a?(Hash) and not hash.empty?
end
def check_string(string)
string.is_a?(String) and not string.empty?
end
def check_array(array)
array.is_a?(Array) and not array.empty?
end
def check_key(hash, key)
check_hash(hash) and hash.key?(key)
end
def check_key_string(hash, key)
check_key(hash, key) and check_string(hash[key])
end
def check_key_hash(hash, key)
check_key(hash, key) and check_hash(hash[key])
end
def check_key_array(hash, key)
check_key(hash, key) and check_array(hash[key])
end
def make_filename(name)
### filenames contain the number of a template ( <= @templates_index)
if @templates_index.key?(name)
"#{@templates_dir}/%06d" % @templates_index[name]
else
abort("template #{name} not found in templates index")
end
end
def set_connection(options)
if check_key(options, :connect)
connect = options[:connect]
if check_key_string(connect, :file)
@connection = YAML.load(File.read connect[:file])
end
@connection[:url] = connect[:url] if check_key_string(connect, :url)
@connection[:user] = connect[:user] if check_key_string(connect, :user)
if check_key_string(connect, :password)
@connection[:password] = connect[:password]
end
end
end
def set_templates_index(options)
if check_key(options, :templates_index)
index = options[:templates_index]
@templates_index_file = index[:file] if check_key_string(index, :file)
@templates_index = index[:index] if check_key_hash(index, :index)
end
@templates_index = {} unless @templates_index
end
def set_templates(options)
if check_key(options, :templates)
templates = options[:templates]
if check_key_string(templates, :directory)
@templates_dir = templates[:directory]
end
if check_key_array(templates, :templates)
@templates_data = templates[:templates]
end
end
@templates_data = [] unless @templates_data
end
def set_templates_list(options)
if check_key(options, :templates_list)
templates_names = options[:templates_list]
if check_key_string(templates_names, :file)
@templates_list_file = templates_names[:file]
end
if check_key_array(templates_names, :white_list)
@templates_white_list = templates_names[:white_list]
end
if check_key_array(templates_names, :black_list)
@templates_black_list = templates_names[:black_list]
end
end
@templates_list_file = '' unless @templates_list_file
@templates_white_list = [] unless @templates_white_list
@templates_black_list = [] unless @templates_black_list
end
def set_rules(options)
@rules = options[:rules] if check_key(options, :rules)
end
def list_templates(ids = templates.get_ids_by_host(filter: ""))
client.api_request(
method: "template.get",
params: {templateids: ids}
).map do |template|
template['host']
end
end
def export_templates(templates_names = list_templates)
@templates_data = {}
templates_names.each do |name|
id = [templates.get_id(host: name)]
@templates_data[name] = configurations.export(
format: @export_format,
options: {templates: id}
)
end
@templates_data.length
end
def gen_templates_index(templates_names = list_templates)
@templates_index = {}
number = 0
templates_names.each do |name|
@templates_index[name] = number
number += 1
end
@templates_index
end
def write_templates_index(
templates_dir = @templates_dir,
templates_index_file = @templates_index_file,
templates_index = @templates_index
)
FileUtils.mkdir_p(templates_dir)
File.write(
"#{templates_dir}/#{templates_index_file}",
templates_index.to_yaml
)
end
def write_templates_data
FileUtils.mkdir_p(@templates_dir)
@templates_index.each do |name, number|
if @templates_data.key? name
doc = Nokogiri::XML.parse(@templates_data[name])
File.write(make_filename(name), doc)
else
abort "Template name not found"
end
end
end
def read_templates_list(file = @templates_list_file)
if File.exists? file
content = YAML.load(File.read file)
if check_key_array(content, 'blacklist')
@templates_black_list += content['blacklist']
end
if check_key_array(content, 'whitelist')
@templates_white_list += content['whitelist']
end
else
abort("templates list file not found")
end
{
'blacklist' => @templates_black_list,
'whitelist' => @templates_white_list
}
end
def read_templates_index(file = "#{@templates_dir}/#{@templates_index_file}")
if File.exists?(file)
content = YAML.load(File.read file)
@templates_index = content.merge(@templates_index) if check_hash(content)
else
abort("templates index file not found")
end
end
def import_template(name)
read_templates_index if @templates_index.empty?
if @imported_templates.include?(name)
abort("template alredy imported")
else
data = File.read(make_filename name)
configurations.import(
format: @export_format,
rules: @rules,
source: data
)
@imported_templates << name
end
{'imported templates' => @imported_templates}
end
def list_dependencies(name)
read_templates_index if @templates_index.empty?
file = File.new(make_filename name)
Nokogiri::XML(file)
.xpath("//zabbix_export//templates//template//templates//template//name")
.map(&:text)
end
def import_recursive(name)
list_dependencies(name).each do |dependency|
unless @imported_templates.include?(dependency)
import_recursive(dependency)
end
end
import_template(name) unless @imported_templates.include?(name)
end
def gen_import_list
read_templates_index if @templates_index.empty?
if @templates_white_list.empty?
if @templates_black_list.empty?
@templates_index.keys
else
@templates_index.keys - @templates_black_list
end
else
@templates_white_list & @templates_index.keys
end
end
def import_templates(templates = gen_import_list)
@imported_templates = []
templates.each { |name| import_recursive(name) }
end
def import(options = {})
unless options.empty?
set_templates_index options
set_templates options
set_templates_list options
set_rules options
end
read_templates_list
read_templates_index
import_templates
end
def export(options = {})
unless options.empty?
set_templates_list options
end
gen_templates_index
write_templates_index
export_templates
write_templates_data
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment