#!/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_Memory"

# 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.get_group_id(group_name)

options = {
  'groups' => [ g_id.to_i ],
  'host' => template_name
}

t_id = zbx.add_template(options)

# Create application #{app_name}
app_name = "Memory"
p " ** Create application #{app_name}."
application = {
  'hostid' => t_id.to_i,
  'name' => app_name
}
a_id = zbx.add_application(application)

# 'Buffers memory'
options = {
  'description' => "Buffers memory",
  'key_' => "vm.memory.size[buffers]",
  'hostid' => t_id.to_i,
  'applications' => [ a_id.to_i ],
  'units' => 'B',
  'history' => 7,
  'trends' => 30,
  'value_type' => 0
}
p " ** Add 'Buffers memory' to #{template_name}."

buffers_in_b_item_id = zbx.add_item(options)

# 'Cached memory'
options = {
  'description' => "Cached memory",
  'key_' => "vm.memory.size[cached]",
  'hostid' => t_id.to_i,
  'applications' => [ a_id.to_i ],
  'units' => 'B',
  'history' => 7,
  'trends' => 30,
  'value_type' => 0
}
p " ** Add 'Cached memory' to #{template_name}."

cached_in_b_item_id = zbx.add_item(options)

# 'Total memory'
options = {
  'description' => "Total memory",
  'key_' => "vm.memory.size[total]",
  'hostid' => t_id.to_i,
  'applications' => [ a_id.to_i ],
  'delay' => '300',
  'units' => 'B',
  'history' => 7,
  'trends' => 30,
  'value_type' => 0
}
p " ** Add 'Total memory' to #{template_name}."

total_in_b_item_id = zbx.add_item(options)

# 'Used memory with buffers and cache'
options = {
  'description' => "Used memory",
  'key_' => "vm.memory.size[used]",
  'hostid' => t_id.to_i,
  'applications' => [ a_id.to_i ],
  'delay' => '300',
  'units' => 'B',
  'history' => 7,
  'trends' => 30,
  'value_type' => 0
}
p " ** Add 'Used memory' to #{template_name}."

used_in_b_item_id = zbx.add_item(options)

# 'Free memory'
options = {
  'description' => "Free memory",
  'key_' => "vm.memory.size[free]",
  'hostid' => t_id.to_i,
  'applications' => [ a_id.to_i ],
  'units' => 'B',
  'history' => 7,
  'trends' => 30,
  'value_type' => 0
}
p " ** Add 'Free memory' to #{template_name}."

i_id = zbx.add_item(options)

# 'Free mmeory with buffers and cache'
options = {
  'description' => "Free memory with buffers and cache",
  'key_' => "vm.memory.size[free_with_buffer_cache]",
  'params' => "last(\"vm.memory.size[free]\") + last(\"vm.memory.size[cached]\") + last(\"vm.memory.size[buffers]\")",
  'hostid' => t_id.to_i,
  'type' => '15', #calculated
  'applications' => [ a_id.to_i ],
  'units' => 'B',
  'history' => 7,
  'trends' => 30,
  'value_type' => 0
}
p " ** Add 'Free memory with buffers and cache' to #{template_name}."

i_id = zbx.add_item(options)

# 'Swap in all'
options = {
  'description' => "Swap in all",
  'key_' => "system.swap.in[]",
  'hostid' => t_id.to_i,
  'applications' => [ a_id.to_i ],
  'multiplier' => 1,
  'formula' => 512,
  'units' => 'B',
  'history' => 7,
  'trends' => 30,
  'value_type' => 0
}
p " ** Add 'Swap in all' to #{template_name}."

i_id = zbx.add_item(options)

# 'Swap out all'
options = {
  'description' => "Swap out all",
  'key_' => "system.swap.out[]",
  'hostid' => t_id.to_i,
  'applications' => [ a_id.to_i ],
  'multiplier' => 1,
  'formula' => 512,
  'units' => 'B',
  'history' => 7,
  'trends' => 30,
  'value_type' => 0
}
p " ** Add 'Swap out all' to #{template_name}."

i_id = zbx.add_item(options)

# TRIGGERS
options = {
  'description' => "Free memory without cache, %",
  'expression' => "{#{template_name}:vm.memory.size[free].last(0)}<15",
  'priority' => 2, # warning
  'templateid' => 0,
  'comments' => "Free memory without cache, % warning trigger",
  'type' => 0,
  'status' => '0'
}

p " ** Add 'Free memory without cache, %'"
tr_id = zbx.add_trigger(options)

options = {
  'description' => "Free memory without cache, %",
  'expression' => "{#{template_name}:vm.memory.size[free].last(0)}<10",
  'priority' => 5, # disaster
  'templateid' => 0,
  'commenty' => "Free memory without cache, %",
  'type' => 0,
  'status' => '0'
}

p " ** Add 'Free memory without cache, % disaster trigger'"
tr_id = zbx.add_trigger(options)

options = {
  'description' => "Swap usage in, B",
  'expression' => "{#{template_name}:system.swap.in[].last(0)}#0",
  'priority' => 2, # warning
  'templateid' => 0,
  'comments' => "Swap usage in, B",
  'type' => 0,
  'status' => '0'
}

p " ** Add 'Swap usage in, B warning trigger'"
tr_id = zbx.add_trigger(options)

options = {
  'description' => "Swap usage out, B",
  'expression' => "{#{template_name}:system.swap.out[].last(0)}#0",
  'priority' => 2, # warning
  'templateid' => 0,
  'comments' => "Swap usage out, B",
  'type' => 0,
  'status' => '0'
}

p " ** Add 'Swap usage out, B warning trigger'"
tr_id = zbx.add_trigger(options)

options = {
    'gitems' => [
    {   
      "itemid" => total_in_b_item_id,
      "drawtype" => "0",
      "sortorder" => "0",
      "color" => "AA0000",
      "yaxisside" => "0",
      "calc_fnc" => "2",
      "type" => "0",
      "periods_cnt" => "5" 
    },  
    {   
      "itemid" => used_in_b_item_id,
      "drawtype" => "0",
      "sortorder" => "0",
      "color" => "009900",
      "yaxisside" => "0",
      "calc_fnc" => "2",
      "type" => "0",
      "periods_cnt" => "5" 
    },
    {   
      "itemid" => cached_in_b_item_id,
      "drawtype" => "0",
      "sortorder" => "0",
      "color" => "0000AA",
      "yaxisside" => "0",
      "calc_fnc" => "2",
      "type" => "0",
      "periods_cnt" => "5" 
    },
    {   
      "itemid" => buffers_in_b_item_id,
      "drawtype" => "0",
      "sortorder" => "0",
      "color" => "AA00AA",
      "yaxisside" => "0",
      "calc_fnc" => "2",
      "type" => "0",
      "periods_cnt" => "5" 
    }
  ],  
  "show_triggers" => "1",
  "name" => "Memory usage",
  "width" => "900",
  "height" => "200",
  "templateid" =>  "0" 
}

g_id = zbx.add_graph(options)