Module: Zabbix

Defined in:
lib/host.rb,
lib/item.rb,
lib/graph.rb,
lib/group.rb,
lib/screen.rb,
lib/trigger.rb,
lib/template.rb,
lib/usermacro.rb

Defined Under Namespace

Classes: ZabbixApi

Instance Method Summary collapse

Instance Method Details

#add_host(host_options) ⇒ Object

Method for creation host in zabbix.

  • Input parameter - hash host_options. Available keys in hash:

    • host - hostname. Type: string. Default: nil;

    • port - zabbix agent pont. Type: int. Default: 10050;

    • status - host status. Type: int. Possible values: 0 - monitored, 1 - not monitored. Default: 0;

    • useip - use ip or dns name for monitoring host. Possible values: 0 - don’t use ip (use dns name), 1 - use ip (don’t use dns name);

    • ip - host’s ip address. Used for monitoring host if useip set to 1. Default: ‘0.0.0.0’;

    • proxy_hostid - host_id of zabbix proxy (if necessary). See get_host_id. Default: 0 (don’t use proxy server);

    • groups - array of groups that belong host. Default: [].

    • useipmi - Use or not ipmi. Default: 0 (don’t use ipmi);

    • ipmi_ip - Default: ”;

    • ipmi_port - Default: 623;

    • ipmi_authtype - Default: 0;

    • ipmi_privilege - Default: 0;

    • ipmi_username - Default: ”;

    • ipmi_password - Default: ”;



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/host.rb', line 30

def add_host(host_options)

  host_default = {
    'host' => nil,
    'port' => 10050,
    'status' => 0,
    'useip' => 0,
    'dns' => '',
    'ip' => '0.0.0.0',
    'proxy_hostid' => 0,
    'groups' => [],
    'useipmi' => 0,
    'ipmi_ip' => '',
    'ipmi_port' => 623,
    'ipmi_authtype' => 0,
    'ipmi_privilege' => 0,
    'ipmi_username' => '',
    'ipmi_password' => ''
  }

  host_options['groups'].map! { |group_id| {'groupid' => group_id} }

  host = merge_opt(host_default, host_options)

  message = {
    'method' => 'host.create',
    'params' => host
  }

  responce = send_request(message)

  if not ( responce.empty? ) then
    result = responce['hostids'][0].to_i
  else
    result = nil
  end

  return result
end

#get_host_id(hostname) ⇒ Object

Method for retrieving host id from zabbix by hostname.

  • Non optional input parameters:

    • hostname - Type: String.

  • Return:

    • host_id - Return finded host_id for passed hostname. If host not found in zabbix - return nil



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/host.rb', line 75

def get_host_id(hostname)
  
  message = {
    'method' => 'host.get',
    'params' => {
      'filter' => {
        'host' => hostname
      }
    }
  }

  responce = send_request(message)

  if not ( responce.empty? ) then
    result = responce[0]['hostid'].to_i
  else
    result = nil
  end

  return result
end