#!/usr/bin/env ruby require 'rubygems' require 'getopt/std' require 'yaml' require 'zabbixapi' opt = Getopt::Std.getopts("g:i:E:") group_name = opt["g"] zabbix_env = opt["E"] template_name = "TMPL_nginx" app_name = "Nginx" # 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} p " ** Create application #{app_name}." application = { 'hostid' => t_id.to_i, 'name' => app_name } a_id = zbx.add_or_get_application(t_id, application) # 'nginx service status' options = { 'description' => "nginx service status", 'key_' => "runit_service[nginx]", 'hostid' => t_id.to_i, 'applications' => [ a_id.to_i ], 'history' => 7, 'trends' => 30, 'delay' => 60, 'value_type' => 0 } p " ** Add 'nginx service status' to #{template_name}." item_id = zbx.add_or_get_item(t_id, options) store = Hash.new() # 'nginx active_connections' options = { 'description' => "nginx active_connections", 'key_' => "nginx_status[http://127.0.0.1:8010/status,active_connections]", 'hostid' => t_id.to_i, 'applications' => [ a_id.to_i ], 'history' => 7, 'trends' => 30, 'delay' => 60, 'value_type' => 0 } p " ** Add 'nginx active_connections' to #{template_name}." item_id = zbx.add_or_get_item(t_id, options) store["active_connections"] = item_id ["writing", "reading", "waiting"].each do |param| # 'nginx #{param}' options = { 'description' => "nginx #{param}", 'key_' => "nginx_status[http://127.0.0.1:8010/status,#{param}]", 'hostid' => t_id.to_i, 'applications' => [ a_id.to_i ], 'history' => 7, 'trends' => 30, 'delay' => 60, 'value_type' => 0 } p " ** Add 'nginx #{param}' to #{template_name}." item_id = zbx.add_or_get_item(t_id, options) store[param] = item_id end ["handled", "accepts", "requests"].each do |param| # 'nginx #{param}' options = { 'description' => "nginx #{param}", 'key_' => "nginx_status[http://127.0.0.1:8010/status,#{param}]", 'hostid' => t_id.to_i, 'applications' => [ a_id.to_i ], 'history' => 7, 'trends' => 30, 'delay' => 60, 'value_type' => '3', # Numeric (unsigned) 'delta' => 1 # Store as delta (Speed per second) } p " ** Add 'nginx #{param}' to #{template_name}." item_id = zbx.add_or_get_item(t_id, options) store[param] = item_id end # TRIGGERS options = { 'description' => "nginx service status", 'expression' => "{#{template_name}:runit_service[nginx].last(0)}#0", 'priority' => 5, # disaster 'templateid' => 0, 'comments' => "nginx service status (disaster)", 'type' => 0, 'status' => '0' } p " ** Add 'nginx service status' trigger" tr_id = zbx.add_or_get_trigger(t_id, options) # GRAPHS options = { 'gitems' => [ { "itemid" => store["active_connections"], "drawtype" => "0", "sortorder" => "0", "color" => "AA0000", "yaxisside" => "0", "calc_fnc" => "2", "type" => "0", "periods_cnt" => "5" }, { "itemid" => store["writing"], "drawtype" => "0", "sortorder" => "0", "color" => "009900", "yaxisside" => "0", "calc_fnc" => "2", "type" => "0", "periods_cnt" => "5" }, { "itemid" => store["waiting"], "drawtype" => "0", "sortorder" => "0", "color" => "0000AA", "yaxisside" => "0", "calc_fnc" => "2", "type" => "0", "periods_cnt" => "5" } ], "show_triggers" => "1", "name" => "Nginx status", "width" => "900", "height" => "200", "templateid" => "0" } g_id = zbx.add_or_get_graph(t_id, options)