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
117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/zabbix/api/client.rb', line 117 def initialize(url: nil,timeout: 60) Faraday.default_adapter = :net_http @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.
204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/zabbix/api/client.rb', line 204 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.
86 87 88 |
# File 'lib/zabbix/api/client.rb', line 86 def last @last end |
Instance Attribute Details
#conn ⇒ Object (readonly)
Returns the value of attribute conn.
109 110 111 |
# File 'lib/zabbix/api/client.rb', line 109 def conn @conn end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
109 110 111 |
# File 'lib/zabbix/api/client.rb', line 109 def token @token end |
#zabobject ⇒ Object
Returns the value of attribute zabobject.
110 111 112 |
# File 'lib/zabbix/api/client.rb', line 110 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.
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/zabbix/api/client.rb', line 170 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
190 191 192 |
# File 'lib/zabbix/api/client.rb', line 190 def known_objects @@zabbix_objects end |
#last ⇒ Object
returns the last response the client got from the server
221 222 223 |
# File 'lib/zabbix/api/client.rb', line 221 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
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/zabbix/api/client.rb', line 142 def login(user: nil,pass: nil) version = call('apiinfo.version',{}).result if version < UsernameChangeVersion res =post(method: 'user.login', params: {user: user, password:pass}, auth:nil) else res =post(method: 'user.login', params: {username: user, password:pass}, auth:nil) end @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.
158 159 160 |
# File 'lib/zabbix/api/client.rb', line 158 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.
132 133 134 135 136 137 |
# File 'lib/zabbix/api/client.rb', line 132 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 |