Class: Zabbix::Api::Client
- Inherits:
-
Object
- Object
- Zabbix::Api::Client
- 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
-
.last ⇒ Object
Returns the value of attribute last.
Instance Attribute Summary collapse
-
#conn ⇒ Object
readonly
Returns the value of attribute conn.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
-
#zabobject ⇒ Object
Returns the value of attribute zabobject.
Instance Method Summary collapse
-
#call(name, *args, &block) ⇒ Object
this is the method that method_missing calls to peform the actual work.
-
#initialize(url: nil, timeout: 60) ⇒ Client
constructor
:url is required.
-
#known_objects ⇒ Object
just handy if you're doing a REPL (e.g. zapishell.rb) Returns list of zabbix objects the api currently is aware of.
-
#last ⇒ Object
returns the last response the client got from the server.
-
#login(user: nil, pass: nil) ⇒ Object
both :user and :pass are required.
-
#logout ⇒ Object
calls user.logout for the @token session.
-
#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.
-
#post(args) ⇒ Object
This method posts a list of params to @conn/@@apiurl.
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
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/zabbix/api/client.rb', line 115 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.
196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/zabbix/api/client.rb', line 196 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
.last ⇒ Object
Returns the value of attribute last.
84 85 86 |
# File 'lib/zabbix/api/client.rb', line 84 def last @last end |
Instance Attribute Details
#conn ⇒ Object (readonly)
Returns the value of attribute conn.
107 108 109 |
# File 'lib/zabbix/api/client.rb', line 107 def conn @conn end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
107 108 109 |
# File 'lib/zabbix/api/client.rb', line 107 def token @token end |
#zabobject ⇒ Object
Returns the value of attribute zabobject.
108 109 110 |
# File 'lib/zabbix/api/client.rb', line 108 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.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/zabbix/api/client.rb', line 162 def call(name, *args, &block) res = nil if name == 'apiinfo.version' res = post(method: "#{name}", params: args.first, id: '1').body else res = post(method: "#{name}", params: args.first, id: '1', auth: @token).body end 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 |
#known_objects ⇒ Object
just handy if you're doing a REPL (e.g. zapishell.rb) Returns list of zabbix objects the api currently is aware of
182 183 184 |
# File 'lib/zabbix/api/client.rb', line 182 def known_objects @@zabbix_objects end |
#last ⇒ Object
returns the last response the client got from the server
213 214 215 |
# File 'lib/zabbix/api/client.rb', line 213 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
139 140 141 142 143 |
# File 'lib/zabbix/api/client.rb', line 139 def login(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 |
#logout ⇒ Object
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.
150 151 152 |
# File 'lib/zabbix/api/client.rb', line 150 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.
129 130 131 132 133 134 |
# File 'lib/zabbix/api/client.rb', line 129 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 |