#!/usr/bin/env ruby require 'rubygems' require 'yaml' require 'getopt/std' require 'zabbixapi' opt = Getopt::Std.getopts("g:E:") group_name = opt["g"] zabbix_env = opt["E"] template_name = "TMPL_mailer" # 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 = "mailer" p " ** Create application #{app_name}." application = { 'hostid' => t_id.to_i, 'name' => app_name } a_id = zbx.add_or_get_application(t_id, application) # 'mailq_size' options = { 'description' => "mailq size", 'key_' => "mailer.mailq", 'hostid' => t_id.to_i, 'applications' => [ a_id.to_i ], 'history' => 7, 'trends' => 30, 'delay' => 60, 'value_type' => 0 } p " ** Add 'mailer.mailq' to #{template_name}." mailq = zbx.add_or_get_item(t_id, options) # Exim options = { 'description' => "num of exim", 'key_' => "proc.num[exim4]", 'hostid' => t_id.to_i, 'applications' => [ a_id.to_i ], 'history' => 7, 'trends' => 30, 'delay' => 120, 'value_type' => 0 } p " ** Add 'num of exim' to #{template_name}." item_id = zbx.add_or_get_item(t_id, options) # TRIGGERS options = { 'description' => "mailq size", 'expression' => "{#{template_name}:mailer.mailq.last(0)}>100", 'priority' => 2, # warning 'templateid' => 0, 'comments' => "Mailq size too big", 'type' => 0, 'status' => '0' } p " ** Add 'mailq warning trigger'" tr_id = zbx.add_or_get_trigger(t_id, options) options = { 'description' => "exim is running", 'expression' => "{#{template_name}:proc.num[exim4].last(0)}<1", 'priority' => 5, # disaster 'templateid' => 0, 'comments' => "Exim is down, call Stepan or Skipka", 'type' => 0, 'status' => '0' } p " ** Add 'Exim is down' trigger" tr_id = zbx.add_or_get_trigger(t_id, options) # GRAPHS options = { 'gitems' => [ { "itemid" => mailq, "drawtype" => "0", "sortorder" => "0", "color" => "AA0000", "yaxisside" => "0", "calc_fnc" => "2", "type" => "0", "periods_cnt" => "5" }, ], "show_triggers" => "1", "name" => "mailq size", "width" => "900", "height" => "200", "templateid" => "0" } p " ** Add 'mailq size graph'" g_id = zbx.add_or_get_graph(t_id, options)