Class: Zabbix::Api::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/zabbix/api/client.rb

Overview

Each instance of this class acts as a connection Zabbix's API

Constant Summary collapse

@@apiurl =
'api_jsonrpc.php'
@@zabbix_objects =

This is a list (as of zabbix 5.0) of top-level zabbix API calls that the client will understand via method_missing. If they add new things to the api they need to be added here as well if you intend on using the method_missing syntax.

[:action,:alert,:apiinfo,:application,:configuration,
:correlation,:dashboard,:dhost,:dservice,:dcheck,
:drule,:event,:graph,:graphitem,:graphprototype,
:history,:host,:hostgroup,:hostinterface,
:hostprototype,:iconmap,:image,:item,:itemprototype,
:discoveryrule,:maintenance,:map,:mediatype,:problem,
:proxy,:screen,:screenitem,:script,:service,:task,
:template,:templatescreen,:templatescreenitem,
:trend,:trigger,:triggerprototype,:user,:usergroup,
:usermacro,:valuemap,:httptest].to_set

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url: nil, timeout: 60) ⇒ Client

:url is required. You do not need to add 'api_jsonrpc.php' - this will happen automagically. You can alter request timeout if needed by passing :timeout - the default is 60 secs



75
76
77
78
79
80
81
82
83
84
# File 'lib/zabbix/api/client.rb', line 75

def initialize(url: nil,timeout: 60)
  @conn = Faraday.new(
    url: url,
    headers: {'Content-Type' => 'application/json-rpc'},
    request: { timeout: timeout }
  ) do |conn|
    conn.request :zabbix_api_request
  end
  @zabobject = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

this override of method_missing is the trick that lets zabbix-api-simple look quite a lot like the zabbix api documentation. If it finds that the method name you were trying to call is in @@zabbix_objects, then it constructs a call to that top level api entity using the parameters as arguments to the call.

this is really just here as syntactical sugar - you don't have to use it, but if you do you'll find that you need do essentially zero mental translation between the zabbix api documentation and your code.



143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/zabbix/api/client.rb', line 143

def method_missing(name, *args, &block)
  if @@zabbix_objects.include?(name)
    # Clone self cuz we want to be thread safe/recursable.  This will pop off the
    # stack after it's no longer referenced (and @zabobject will never change in the
    # original client instance)
    newcli = self.clone
    newcli.zabobject = name
    return newcli
  elsif @zabobject
    return call("#{@zabobject}.#{name}",args.first)
  else
    raise "Unknown zabbix object given: #{name}"
  end
end

Class Attribute Details

.lastObject

Returns the value of attribute last.



44
45
46
# File 'lib/zabbix/api/client.rb', line 44

def last
  @last
end

Instance Attribute Details

#connObject (readonly)

Returns the value of attribute conn.



67
68
69
# File 'lib/zabbix/api/client.rb', line 67

def conn
  @conn
end

#tokenObject (readonly)

Returns the value of attribute token.



67
68
69
# File 'lib/zabbix/api/client.rb', line 67

def token
  @token
end

#zabobjectObject

Returns the value of attribute zabobject.



68
69
70
# File 'lib/zabbix/api/client.rb', line 68

def zabobject
  @zabobject
end

Instance Method Details

#call(name, *args, &block) ⇒ Object

this is the method that method_missing calls to peform the actual work. The first parameter is the top-level api call (e.g. those listed in @@zabbix_objects. Args is a hash containing the particulars for the call. You can call this directly if you don't want to rely on method_missing.

results are returned as instances of OpenStruct. If you need this to be a hash just do to_h to the returned object.



122
123
124
125
126
127
128
129
130
131
# File 'lib/zabbix/api/client.rb', line 122

def call(name, *args, &block)
    res = post(method: "#{name}", params: args.first, id: '1', auth: @token).body
    raise res['error'].awesome_inspect(plain: true)  if res.has_key?('error')
    if res.has_key?('result') and res['result'].class == Array
      res = res['result'].collect{|each| OpenStruct.new(each)}
    else
      res = OpenStruct.new(res)
    end
    return res
end

#lastObject

returns the last response the client got from the server



160
161
162
# File 'lib/zabbix/api/client.rb', line 160

def last
  Client.last
end

#login(user: nil, pass: nil) ⇒ Object

both :user and :pass are required. This method calls user.logic abd stores the returned auth token for future calls to the api



99
100
101
102
103
# File 'lib/zabbix/api/client.rb', line 99

def (user: nil,pass: nil)
  res =post(method: 'user.login', params: {user: user, password:pass}, auth:nil)
  @token = res.body['result']
  OpenStruct.new(res.body)
end

#logoutObject

calls user.logout for the @token session. You really should pay attention to ensuring that this gets called, else you'll find over time that your sessions table is getting quite large and will start slowing down lots of stuff in zabbix.



110
111
112
# File 'lib/zabbix/api/client.rb', line 110

def logout
  OpenStruct.new(post(method: 'user.logout', params: [], auth: @token).body)
end

#post(args) ⇒ Object

This method posts a list of params to @conn/@@apiurl. You likely won't have need to call this directly.



89
90
91
92
93
94
# File 'lib/zabbix/api/client.rb', line 89

def post(args)
  args[:params] = [] if not args.has_key?(:params) or args[:params].nil?
  last = @conn.post(@@apiurl, args)
  Client.last = last
  return last
end