Class: Zabbix::ZabbixApi

Inherits:
Object
  • Object
show all
Defined in:
lib/zabbixapi/base.rb,
lib/zabbixapi/host.rb,
lib/zabbixapi/item.rb,
lib/zabbixapi/graph.rb,
lib/zabbixapi/group.rb,
lib/zabbixapi/screen.rb,
lib/zabbixapi/trigger.rb,
lib/zabbixapi/template.rb,
lib/zabbixapi/mediatype.rb,
lib/zabbixapi/usermacro.rb,
lib/zabbixapi/application.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_url, api_user, api_password) ⇒ ZabbixApi

Returns a new instance of ZabbixApi



28
29
30
31
32
33
34
# File 'lib/zabbixapi/base.rb', line 28

def initialize (api_url, api_user, api_password)
  @api_url = api_url
  @api_user = api_user
  @api_password = api_password

  @debug = false # Disable debug by default
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug



26
27
28
# File 'lib/zabbixapi/base.rb', line 26

def debug
  @debug
end

Instance Method Details

#add_application(app_options) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/zabbixapi/application.rb', line 4

def add_application(app_options)
  app_options_default = {
      'hostid' => nil,
      'name' => nil
  }
  application = merge_opt(app_options_default, app_options)
  message = {
      'method' => 'application.create',
      'params' => application
  }
  responce = send_request(message)
  responce.empty? ? nil : responce['applicationids'][0].to_i
end

#add_graph(graph) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/zabbixapi/graph.rb', line 4

def add_graph(graph)
  message = {
      'method' => 'graph.create',
      'params' => graph
  }
  response = send_request(message)
  response.empty? ? nil : response['graphids'][0]
end

#add_graph_to_screen(screen_id, graph_id, x, y) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/zabbixapi/screen.rb', line 76

def add_graph_to_screen(screen_id, graph_id, x, y)
  message = {
      'method' => 'screen.addItems',
      'params' => {
          'screenids' => [screen_id],
          'screenitems' => [
              {
                  'resourcetype' => 'graph',
                  'resourceid' => graph_id,
                  'width' => '800',
                  'height' => '200',
                  'x' => x,
                  'y' => y,
                  'valign' => 'Middle',
                  'halign' => 'Centre',
                  'colspan' => '0',
                  'rowspan' => '0',
                  'elements' => '0',
                  'dynamic' => '0',
                  'url' => '0',
                  'style' => '0'
              }
          ]
      }
  }
  response = send_request(message)
  return response
end

#add_group(groupname) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/zabbixapi/group.rb', line 22

def add_group(groupname)
  message = {
      'method' => 'hostgroup.create',
      'params' => {
          'name' => groupname
      }
  }
  response = send_request(message)
  response ? response['groupids'][0].to_i : nil
end

#add_host(host_options) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/zabbixapi/host.rb', line 15

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'].nil? || host_options['groups'].map! { |group_id| {'groupid' => group_id} }
  host = merge_opt(host_default, host_options)
  message = {
      'method' => 'host.create',
      'params' => host
  }
  response = send_request(message)
  response.empty? ? nil : response['hostids'][0].to_i
end

#add_host_to_group(host_id, group_id) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/zabbixapi/group.rb', line 53

def add_host_to_group(host_id, group_id)
  message = {
      'method' => 'hostgroup.massAdd',
      'params' => {
          'groups' => [group_id],
          'hosts' => [host_id]
      }
  }
  response = send_request(message)
  response ? response[0]['groupid'].to_i : nil
end

#add_item(item) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
# File 'lib/zabbixapi/item.rb', line 4

def add_item(item)
  # Default item options
  # See: http://www.zabbix.com/documentation/1.8/api/item
  ## Item types (see ./frontends/php/include/defines.inc.php in zabbix source)
  # ITEM_TYPE_ZABBIX              0
  # ITEM_TYPE_SNMPV1              1
  # ITEM_TYPE_TRAPPER             2
  # ITEM_TYPE_SIMPLE              3
  # ITEM_TYPE_SNMPV2C             4
  # ITEM_TYPE_INTERNAL            5
  # ITEM_TYPE_SNMPV3              6
  # ITEM_TYPE_ZABBIX_ACTIVE       7
  # ITEM_TYPE_AGGREGATE           8
  # ITEM_TYPE_HTTPTEST            9
  # ITEM_TYPE_EXTERNAL            10
  # ITEM_TYPE_DB_MONITOR          11
  # ITEM_TYPE_IPMI                12
  # ITEM_TYPE_SSH                 13
  # ITEM_TYPE_TELNET              14
  # ITEM_TYPE_CALCULATED          15
  item_options = {
      'description' => nil,
      'key_' => nil,
      'hostid' => nil,
      'delay' => 60,
      'history' => 60,
      'status' => 0,
      'type' => 7,
      'snmp_community' => '',
      'snmp_oid' => '',
      'value_type' => 3,
      'data_type' => 0,
      'trapper_hosts' => 'localhost',
      'snmp_port' => 161,
      'units' => '',
      'multiplier' => 0,
      'delta' => 0,
      'snmpv3_securityname' => '',
      'snmpv3_securitylevel' => 0,
      'snmpv3_authpassphrase' => '',
      'snmpv3_privpassphrase' => '',
      'formula' => 0,
      'trends' => 365,
      'logtimefmt' => '',
      'valuemapid' => 0,
      'delay_flex' => '',
      'authtype' => 0,
      'username' => '',
      'password' => '',
      'publickey' => '',
      'privatekey' => '',
      'params' => '',
      'ipmi_sensor' => '',
      'applications' => '',
      'templateid' => 0
  }
  item_options.merge!(item)
  message = {
      'method' => 'item.create',
      'params' => [item_options]
  }
  response = send_request(message)
  response.empty? ? nil : response['itemids'][0]
end

#add_macro(host_id, macro_name, macro_value) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/zabbixapi/usermacro.rb', line 4

def add_macro(host_id, macro_name, macro_value)
  message = {
      'method' => 'Usermacro.create',
      'params' => {
          'hostid' => host_id,
          'macro' => macro_name,
          'value' => macro_value
      }
  }
  response = send_request(message)
  hostmacroids == response['hostmacroids'] ? hostmacroids : nil
end

#add_mediatype(mediatype_options) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/zabbixapi/mediatype.rb', line 18

def add_mediatype(mediatype_options)
  mediatype_default = {
    'type' => '0',
    'description' => '',
    'smtp_server' => '',
    'smtp_helo'   => '',
    'smtp_email'  => '',
    'exec_path'   => '',
    'gsm_modem'   => '',
    'username'    => '',
    'passwd'      => ''
  }
  mediatype = merge_opt(mediatype_default, mediatype_options)
  message = {
    'method' => 'mediatype.create',
    'params' => mediatype
  }
  response = send_request(message)
  response.empty? ? nil : response['mediatypeids'][0].to_i
end

#add_or_get_application(host_id, app_options) ⇒ Object



18
19
20
21
22
23
# File 'lib/zabbixapi/application.rb', line 18

def add_or_get_application(host_id, app_options)
  unless a_id = get_app_id(host_id, app_options['name'])
    a_id = add_application(app_options)
  end
  return a_id
end

#add_or_get_graph(host_id, graph) ⇒ Object



27
28
29
30
31
32
# File 'lib/zabbixapi/graph.rb', line 27

def add_or_get_graph(host_id, graph)
  unless g_id = get_graph_id(host_id, graph['name'])
    g_id = add_graph(graph)
  end
  return g_id
end

#add_or_get_group(groupname) ⇒ Object



33
34
35
36
37
38
# File 'lib/zabbixapi/group.rb', line 33

def add_or_get_group(groupname)
  unless g_id = get_group_id([groupname])
    g_id = add_group(groupname)
  end
  return g_id
end

#add_or_get_item(host_id, item_options) ⇒ Object



83
84
85
86
87
88
# File 'lib/zabbixapi/item.rb', line 83

def add_or_get_item(host_id, item_options)
  unless i_id = get_item_id(host_id, item_options['description'])
    i_id = add_item(item_options)
  end
  return i_id
end

#add_or_get_template(template_options) ⇒ Object



19
20
21
22
23
24
# File 'lib/zabbixapi/template.rb', line 19

def add_or_get_template(template_options)
  unless t_id = get_template_id(template_options['host'])
    t_id = add_template(template_options)
  end
  return t_id
end

#add_or_get_trigger(host_id, trigger) ⇒ Object



13
14
15
16
17
18
# File 'lib/zabbixapi/trigger.rb', line 13

def add_or_get_trigger(host_id, trigger)
  unless tr_id = get_trigger_id(host_id, trigger['description'])
    tr_id = add_trigger(trigger)
  end
  return tr_id
end

#add_screen(screen_name, hsize, vsize) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/zabbixapi/screen.rb', line 105

def add_screen(screen_name, hsize, vsize)
  message = {
      'method' => 'screen.create',
      'params' => {
          'name' => screen_name,
          'hsize' => hsize,
          'vsize' => vsize
      }
  }
  response = send_request(message)
  response.empty? ? nil : response['screenids'][0]
end

#add_template(template_options) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/zabbixapi/template.rb', line 4

def add_template(template_options)
  template_default = {
      'host' => nil,
      'groups' => [],
  }
  template_options['groups'].map! { |group_id| {'groupid' => group_id} }
  template = merge_opt(template_default, template_options)
  message = {
      'method' => 'template.create',
      'params' => template
  }
  response = send_request(message)
  response.empty? ? nil : response['templateids'][0].to_i
end

#add_trigger(trigger) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/zabbixapi/trigger.rb', line 4

def add_trigger(trigger)
  message = {
      'method' => 'trigger.create',
      'params' => [ trigger ]
  }
  response = send_request(message)
  response.empty? ? nil : response['triggerids'][0]
end

#authObject



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/zabbixapi/base.rb', line 108

def auth()
  auth_message = {
      'auth' => nil,
      'method' => 'user.authenticate',
      'params' => {
          'user' => @api_user,
          'password' => @api_password,
          '0' => '0'
      }
  }
do_request(auth_message)
end

#del_all_graphs_from_screen(screen_id) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/zabbixapi/screen.rb', line 65

def del_all_graphs_from_screen(screen_id)
  message = {
      'method' => 'screen.deleteItems',
      'params' => {
          'screenids' => [screen_id],
      }
  }
  response = send_request(message)
  response ? response[0]['screenids'].to_i : nil
end

#delete_group(groupname) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/zabbixapi/group.rb', line 40

def delete_group(groupname)
  if group_id = get_group_id(groupname)
    message = {
        'method' => 'hostgroup.delete',
        'params' => {
            'groupid' => group_id
        }
    }
    response = send_request(message)
    response ? response['groupids'][0].to_i : nil
  end
end

#delete_host(hostname) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/zabbixapi/host.rb', line 56

def delete_host(hostname)
  if host_id = get_host_id(hostname)
    message = {
        'method' => 'host.delete',
        'params' => {
            'hostid' => host_id
        }
    }
    response = send_request(message)
    response.empty? ?  nil :  response['hostids'][0].to_i
  end
end

#delete_item(item_ids) ⇒ Object

Don't work with api < 1.8.4



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/zabbixapi/item.rb', line 106

def delete_item(item_ids)
  if item_ids.kind_of? Array
    message = {
        'method' => 'item.delete',
        'params' => item_ids
    }
  elsif item_ids.kind_of? Fixnum or item_ids.kind_of? String
    message = {
        'method' => 'item.delete',
        'params' => [item_ids]
    }
  else
    raise Zabbix::ArgumentError.new("Zabbix::ZabbixApi.delete_item() argument error. item_ids => #{item_ids.inspect}")
  end
  response = send_request(message)
  if response.empty?
    result = nil
  else
    response['itemids'].count == 1 ? result = response['itemids'][0] : result = response['itemids']
  end
  return result
end

#delete_mediatype(mediatype) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/zabbixapi/mediatype.rb', line 39

def delete_mediatype(mediatype)
  if mediatype_id = get_mediatype_id(mediatype)
    message = {
        'method' => 'mediatype.delete',
        'params' => 
            [mediatype_id]
    }
    response = send_request(message)
    response.empty? ? nil : response['mediatypeids'][0]
  end
end

#delete_template(template_name) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/zabbixapi/template.rb', line 114

def delete_template(template_name)
  message = {
      'method' => 'template.delete',
      'params' => [
          {
            "templateid" => get_template_id(template_name)
          }
        ]
  }
  response = send_request(message)
  response.empty? ? nil : response['templateids'][0].to_i
end

#do_request(message) ⇒ Object



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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/zabbixapi/base.rb', line 36

def do_request(message)

  id = rand 100_000

  message['id'] = id
  message['jsonrpc'] = '2.0'

  message_json = JSON.generate(message)

  uri = URI.parse(@api_url)
  http = Net::HTTP.new(uri.host, uri.port)

  if uri.scheme == "https"
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  request = Net::HTTP::Post.new(uri.request_uri)
  request.add_field('Content-Type', 'application/json-rpc')
  request.body=(message_json)

  begin
    puts "[ZBXAPI] : #{Time.now()} : INFO : Do request. Body => #{request.body}" if @debug
    response = http.request(request)
  rescue ::SocketError => e
    puts "[ZBXAPI] : #{Time.now()} : ERROR : SocketError => #{e.message}" if @debug
    raise Zabbix::SocketError.new(e.message)
  end

  if @debug
    puts "[ZBXAPI] : #{Time.now()} : INFO : Response start"
    puts "[ZBXAPI] : #{Time.now()} : INFO : Response body: #{response.inspect}"
    puts "[ZBXAPI] : #{Time.now()} : INFO : Response end"
  end

  if response.code != "200"
    raise Zabbix::ResponseCodeError.new("Responce code from [" + @api_url + "] is #{response.code}")
  end

  response_body_hash = JSON.parse(response.body)

  if @debug
    puts "[ZBXAPI] : #{Time.now()} : INFO : Response body"
    puts response_body_hash.inspect
  end

  if error = response_body_hash['error']
    error_message = error['message']
    error_data = error['data']
    error_code = error['code']

    e_message = "Code: [" + error_code.to_s + "]. Message: [" + error_message +\
          "]. Data: [" + error_data + "]."

    case error_code.to_s
      when '-32602'
        raise Zabbix::AlreadyExist.new(e_message)
      when '-32500'
        raise  Zabbix::ArgumentError.new(e_message)
      else
        raise Zabbix::ResponseError.new(e_message)
    end
  end

  return response_body_hash['result']
end

#get_app_id(host_id, app_name) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/zabbixapi/application.rb', line 25

def get_app_id(host_id, app_name)
  message = {
      'method' => 'application.get',
      'params' => {
          'filter' => {
              'name' => app_name,
              'hostid' => host_id
          }
      }
  }
  responce = send_request(message)
  responce.empty? ? nil : responce[0]['applicationid']
end

#get_graph_id(host_id, graph_name) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/zabbixapi/graph.rb', line 13

def get_graph_id(host_id, graph_name)
  message = {
      'method' => 'graph.get',
      'params' => {
          'filter' => {
              'name' => graph_name,
              'hostid' => host_id
          }
      }
  }
  response = send_request(message)
  response.empty? ? nil : response[0]['graphid']
end

#get_graphs(host_id) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/zabbixapi/graph.rb', line 34

def get_graphs(host_id)
  message = {
      'method' => 'graph.get',
      'params' => {
          'extendoutput' => '1',
          'filter' => {
              'hostid' => host_id
          }
      }
  }
  response = send_request(message)
  if response.empty?
    result = nil
  else
    result = {}
    response.each() do |graph|
      graph_id = graph['graphid']
      graph_name = graph['name']
      result[graph_id] = graph_name
    end
  end
  return result
end

#get_group_id(pattern) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/zabbixapi/group.rb', line 4

def get_group_id(pattern)
  message = {
      'method' => 'hostgroup.get',
      'params' => {
          'filter' => {
              'name' => pattern
          }
      }
  }
  response = send_request(message)
  response.empty? ? nil : response[0]['groupid'].to_i
end

#get_host_id(hostname) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/zabbixapi/host.rb', line 43

def get_host_id(hostname)
  message = {
      'method' => 'host.get',
      'params' => {
          'filter' => {
              'host' => hostname
          }
      }
  }
  response = send_request(message)
  response.empty? ?  nil : response[0]['hostid'].to_i
end

#get_item_id(host_id, item_name) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/zabbixapi/item.rb', line 69

def get_item_id(host_id, item_name)
  message = {
      'method' => 'item.get',
      'params' => {
          'filter' => {
              'hostid' => host_id,
              'description' => item_name
          }
      }
  }
  response = send_request(message)
  response.empty? ? nil : response[0]['itemid']
end

#get_macro(host_id, macro_name) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/zabbixapi/usermacro.rb', line 17

def get_macro(host_id, macro_name)
  message = {
      'method' => 'Usermacro.get',
      'params' => {
          'hostids' => host_id,
          'macros' => macro_name,
          'extendoutput' => '1'
      }
  }
  response = send_request(message)
  if response.empty?
    result = nil
  else
    if hostmacroid == response[0]['hostmacroid']
      macro_id = hostmacroid
      macro_value = response[0]['value']

      result = {
          'id' => macro_id,
          'value' => macro_value
      }
    else
      result = nil
    end
  end
  return result
end

#get_mediatype_id(mediatype) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/zabbixapi/mediatype.rb', line 4

def get_mediatype_id(mediatype)
  message = {
    'method' => 'mediatype.get',
    'params' => {
    'search' => {
      'description' => mediatype
      },
    'output' => 'extend',
    }
  }
  response = send_request(message)
  response.empty? ?  nil : response[0]['mediatypeid'].to_i
end

#get_screen_graph_ids(screen_id) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/zabbixapi/screen.rb', line 31

def get_screen_graph_ids(screen_id)
  message = {
      'method' => 'screen.get',
      'params' => {
          'extendoutput' => '1',
          'select_screenitems' => '1',
          'screenids' => [screen_id]
      }
  }
  response = send_request(message)
  if response.empty?
    result = nil
  else
    result = []
    screenitems = response[0]['screenitems']
    screenitems.each() do |item|
      result << item['resourceid'] if item['resourcetype'].to_i == 0
    end
  end
  return result
end

#get_screen_id(screen_name) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/zabbixapi/screen.rb', line 4

def get_screen_id(screen_name)
  message = {
      'method' => 'screen.get',
      'params' => {
          'filter' => {
              'name' => screen_name
          }
      }
  }
  response = send_request(message)
  response.empty? ? nil : response[0]['screenid']
end

#get_screen_parameter(screen_name, param_name) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/zabbixapi/screen.rb', line 17

def get_screen_parameter(screen_name, param_name)
  message = {
      'method' => 'screen.get',
      'params' => {
          'extendoutput' => '1',
          'filter' => {
              'name' => screen_name
          }
      }
  }
  response = send_request(message)
  response.empty? ? nil : response[0][param_name]
end

#get_template_id(template_name) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/zabbixapi/template.rb', line 72

def get_template_id(template_name)
  message = {
      'method' => 'template.get',
      'params' => {
          'filter' => {
              'host' => template_name
          }
      }
  }
  response = send_request(message)
  response.empty? ? nil : response[0]['templateid'].to_i
end

#get_template_ids_by_host(host_id) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/zabbixapi/template.rb', line 26

def get_template_ids_by_host(host_id)
  message = {
      'method' => 'template.get',
      'params' => {
          'hostids' => [host_id]
      }
  }
  response = send_request(message)
  if response.empty?
    result = nil
  else
    result = []
    response.each_key() do |template_id|
      result << template_id
    end
  end
  return result
end

#get_templatesObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/zabbixapi/template.rb', line 45

def get_templates()
  message = {
      'method' => 'template.get',
      'params' => {
          'extendoutput' => '0'
      }
  }
  response = send_request(message)
  if response.empty?
    result = nil
  else
    result = {}
    if response.kind_of? Hash
      template_ids = response.keys()
      template_ids.each() do |template_id|
        template_name = response[template_id]['host']
        result[template_id] = template_name
      end
    elsif response.kind_of? Array
      response.each do |template_info|
        result[template_info['hostid']] = template_info['host']
      end
    end
  end
  return result
end

#get_trigger_id(host_id, trigger_name) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/zabbixapi/trigger.rb', line 20

def get_trigger_id(host_id, trigger_name)
  message = {
      'method' => 'trigger.get',
      'params' => {
          'filter' => {
              'hostid' => host_id,
              'description' => [ trigger_name ]
          }
      }
  }
  response = send_request(message)
  response.empty? ? nil : response[0]['triggerid']
end

#get_triggers_by_host(host_id) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/zabbixapi/trigger.rb', line 34

def get_triggers_by_host(host_id)
  message = {
      'method' => 'trigger.get',
      'params' => {
          'filter' => {
              'hostid' => host_id,
          },
          'extendoutput' => '1'
      }
  }
  response = send_request(message)
  if response.empty?
    result = {}
  else
    result = {}
    response.each do |trigger|
      trigger_id = trigger['triggerid']
      description = trigger['description']
      result[trigger_id] = description
    end
  end
  return result
end

#group_exist?(pattern) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
# File 'lib/zabbixapi/group.rb', line 17

def group_exist?(pattern)
  group_id = get_group_id(pattern)
  group_id ? true : false
end

#item_exist?(host_id, item_name) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
93
# File 'lib/zabbixapi/item.rb', line 90

def item_exist?(host_id, item_name)
  item_id = get_item_id(host_id, item_name)
  item_id ? true : false
end


85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/zabbixapi/template.rb', line 85

def link_templates_with_hosts(templates_id, hosts_id)
  templates_id.class == Array ? message_templates_id = templates_id : message_templates_id = [templates_id]
  hosts_id.class == Array ? message_hosts_id = hosts_id : message_hosts_id = [hosts_id]
  message = {
      'method' => 'template.massAdd',
      'params' => {
          'hosts' => message_hosts_id.map { |t| {"hostid" => t} },
          'templates' => message_templates_id.map { |t| {"templateid" => t} }
      }
  }
  response = send_request(message)
  response.empty? ? nil : response['templateid'][0].to_i
end

#merge_opt(a, b) ⇒ Object

Utils.



123
124
125
126
127
128
129
130
131
# File 'lib/zabbixapi/base.rb', line 123

def merge_opt(a, b)
  c = {}
  b.each_pair do |key, value|
    if a.has_key?(key) then
      c[key] = value
    end
  end
  return a.merge(c)
end

#send_request(message) ⇒ Object



103
104
105
106
# File 'lib/zabbixapi/base.rb', line 103

def send_request(message)
  message['auth'] = auth()
  do_request(message)
end

#set_screen_parameter(screen_id, param_name, param_value) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/zabbixapi/screen.rb', line 53

def set_screen_parameter(screen_id, param_name, param_value)
  message = {
      'method' => 'screen.update',
      'params' => {
          param_name => param_value,
          'screenid' => screen_id
      }
  }
  response = send_request(message)
  response.empty? ? nil : response[0]['screenids'].to_i
end


99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/zabbixapi/template.rb', line 99

def unlink_templates_from_hosts(templates_id, hosts_id)
  templates_id.class == Array ? message_templates_id = templates_id : message_templates_id = [templates_id]
  hosts_id.class == Array ? message_hosts_id = hosts_id : message_hosts_id = [hosts_id]
  message = {
      'method' => 'template.massRemove',
      'params' => {
          'hostids' => message_hosts_id,
          'templateids' => message_templates_id,
          'force' => '1'
      }
  }
  response = send_request(message)
  response.empty? ? nil : response['templateids'][0].to_i
end

#update_host(host_id, host_options) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/zabbixapi/host.rb', line 4

def update_host(host_id, host_options)
  host = host_options
  host['hostid'] = host_id
  message = {
      'method' => 'host.update',
      'params' => host
  }
  responce = send_request(message)
  responce.empty? ?  nil :  response['hostids'][0].to_i 
end

#update_item(item_id, options) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/zabbixapi/item.rb', line 95

def update_item(item_id, options)
  options["item_id"]
  message = {
      'method' => 'item.update',
      'params' => options
  }
  response = send_request(message)
  response.empty? ? nil : response['itemids'][0]
end

#update_trigger_status(trigger_id, status) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/zabbixapi/trigger.rb', line 58

def update_trigger_status(trigger_id, status)
  message = {
      'method' => 'trigger.update_status',
      'params' => {
          'triggerid' => trigger_id,
          'status' => status
      }
  }
  response = send_request(message)
  response.empty? ? nil : response['triggerids'][0]
end