Created
September 16, 2015 07:33
-
-
Save anonymous/e8e67cc91e932aaf2f00 to your computer and use it in GitHub Desktop.
class ZabbixSync
This file contains 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
require 'zabbixapi' | |
require 'json' | |
require 'yaml' | |
require 'nokogiri' | |
require 'fileutils' | |
class ZabbixSync < ZabbixApi | |
RULES_DEFAULT = { | |
: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_FILE_NAME_DEFAULT = 'data/templates.json' | |
EXPORT_DIR_NAME_DEFAULT = 'data' | |
EXPORT_TYPE_DEFAULT = 'xml' | |
OPTIONS_DEFAULT = { | |
:url => "http://localhost/zabbix/api_jsonrpc.php", | |
:user => "Admin", | |
:password => "zabbix" | |
} | |
CONFIG_FILE_DEFAULT = '/usr/local/etc/zabbix_sync.conf' | |
attr_accessor(:export_format, :export_dir_name, :templates_file_name, :rules, | |
:imported_templates, :template_list, :template_name_list, | |
:templates_export, :config_file_name, :config_file, :blacklist) | |
def initialize( options = OPTIONS_DEFAULT, config_file_name = CONFIG_FILE_DEFAULT ) | |
super options | |
@templates_file_name = TEMPLATES_FILE_NAME_DEFAULT | |
@rules = RULES_DEFAULT | |
@export_dir_name = EXPORT_DIR_NAME_DEFAULT | |
@export_format = EXPORT_TYPE_DEFAULT | |
@imported_templates = [] | |
@config_file_name = config_file_name | |
@config_file = Hash.new | |
@blacklist = Array.new | |
end | |
def make_filename(template) | |
"#{export_dir_name}/%06d" % template_list[template] | |
end | |
def create_dir(path) | |
dirname = File.dirname(path) | |
unless File.directory?(dirname) | |
FileUtils.mkdir_p(dirname) | |
end | |
end | |
def get_template_list(ids = []) | |
if ids.empty? | |
ids += templates.get_ids_by_host(:filter => "") | |
end | |
@template_name_list = client.api_request( | |
:method => "template.get", | |
:params => {:templateids => ids} | |
).map do |template| | |
template['host'] | |
end | |
end | |
def export_templates | |
@templates_export = Hash.new | |
template_name_list.each do |name| | |
id = [templates.get_id(:host => name)] | |
@templates_export[name] = configurations.export( | |
:format => export_format, | |
:options => {:templates => id} | |
) | |
end | |
end | |
def write_template_list | |
create_dir(templates_file_name) | |
@template_list = Hash.new | |
number = 0 | |
template_name_list.each do |name| | |
@template_list[name] = number | |
number += 1 | |
end | |
File.write(templates_file_name, JSON.pretty_generate(template_list)) | |
end | |
def write_templates_export | |
create_dir(export_dir_name) | |
template_list.each do |template, file_number| | |
doc = Nokogiri::XML.parse(templates_export[template]) | |
File.write(make_filename(template), doc) | |
end | |
end | |
def read_template_list | |
@template_list = JSON.parse(File.read(templates_file_name)) | |
read_config_file | |
blacklist.each do |t| | |
template_list.delete(t) | |
end | |
end | |
def read_config_file | |
if File.exists?(config_file_name) | |
config_file = YAML.load(File.read config_file_name) | |
end | |
if @config_file['blacklist'].class == Array | |
blacklist = @config_file['blacklist'] | |
end | |
end | |
def list_dependencies(template) | |
file = File.new(make_filename(template)) | |
Nokogiri::XML(file) | |
.xpath("//zabbix_export//templates//template//templates//template//name") | |
.map(&:text) | |
end | |
def import_template(template) | |
unless imported_templates.include?(template) | |
data = File.read(make_filename(template)) | |
configurations.import( | |
:format => export_format, | |
:rules => rules, | |
:source => data | |
) | |
imported_templates << template | |
end | |
end | |
def import_template_recursiv(template) | |
# template -> parent | |
# dependencies -> child | |
dependencies = list_dependencies(template) | |
dependencies.each do |dependency| | |
# unless child already imorted | |
unless imported_templates.include?(dependency) | |
import_template_recursiv(dependency) | |
end | |
end | |
import_template(template) | |
end | |
def import_templates | |
@imported_templates = [] | |
template_list.keys.each do |template| | |
import_template_recursiv(template) | |
end | |
end | |
def export_auto | |
get_template_list | |
export_templates | |
write_template_list | |
write_templates_export | |
end | |
def import_auto | |
read_template_list | |
import_templates | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment