#!/usr/bin/env ruby require 'rubygems' require 'yaml' require 'getopt/std' require 'zabbixapi' opt = Getopt::Std.getopts("g:E:n:") group_name = opt["g"] zabbix_env = opt["E"] raid_name = opt["n"] template_name = "TMPL_mdadm_#{raid_name}" # read config config = YAML::load(open('./config.yml')) api_url = config[zabbix_env]["api_url"] api_login = config[zabbix_env]["api_login"] api_password = config[zabbix_env]["api_password"] # Esablish new connection zbx = Zabbix::ZabbixApi.new(api_url, api_login, api_password) # Create new template p " * Creating template #{template_name}." g_id = zbx.add_or_get_group(group_name) options = { 'groups' => [ g_id.to_i ], 'host' => template_name } t_id = zbx.add_or_get_template(options) # Create application #{app_name} app_name = "mdadm_#{raid_name}" p " ** Create application #{app_name}." application = { 'hostid' => t_id.to_i, 'name' => app_name } a_id = zbx.add_or_get_application(t_id, application) # 'state of #{raid_name}' options = { 'description' => "state of #{raid_name}", 'key_' => "mdadm.state[#{raid_name}]", 'hostid' => t_id.to_i, 'applications' => [ a_id.to_i ], 'history' => 7, 'trends' => 30, 'delay' => 60, 'value_type' => 0 } p " ** Add 'mdadm.state[#{raid_name}]' to #{template_name}." item_id = zbx.add_or_get_item(t_id, options) # TRIGGERS options = { 'description' => "#{raid_name} is faulty", 'expression' => "{#{template_name}:mdadm.state[#{raid_name}].last(0)}>0", 'priority' => 5, # disaster 'templateid' => 0, 'comments' => "#{raid_name} is faulty (disaster)", 'type' => 0, 'status' => '0' } p " ** Add '#{raid_name} disaster' trigger'" zbx.debug = true tr_id = zbx.add_or_get_trigger(t_id, options)