Class: Zabbix::API
- Inherits:
-
Object
- Object
- Zabbix::API
- Defined in:
- lib/zmonitor/api.rb,
lib/zmonitor/api.rb
Defined Under Namespace
Classes: NotAuthorisedError, ResponseCodeError, ResponseError
Instance Attribute Summary collapse
-
#event ⇒ Object
API classes.
-
#server ⇒ Object
Returns the value of attribute server.
-
#token ⇒ Object
Returns the value of attribute token.
-
#trigger ⇒ Object
API classes.
-
#user ⇒ Object
API classes.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
-
#whoami ⇒ Object
Returns the value of attribute whoami.
Instance Method Summary collapse
- #call_api(message) ⇒ Object
-
#initialize(server = "http://localhost", verbose = false) ⇒ API
constructor
A new instance of API.
Constructor Details
#initialize(server = "http://localhost", verbose = false) ⇒ API
Returns a new instance of API.
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/zmonitor/api.rb', line 26 def initialize( server = "http://localhost", verbose = false) # Parse the URL beforehand @server = URI.parse(server) @verbose = verbose # set up API class methods @user = Zabbix::User.new(self) @event = Zabbix::Event.new(self) @trigger = Zabbix::Trigger.new(self) end |
Instance Attribute Details
#event ⇒ Object
API classes
24 25 26 |
# File 'lib/zmonitor/api.rb', line 24 def event @event end |
#server ⇒ Object
Returns the value of attribute server.
22 23 24 |
# File 'lib/zmonitor/api.rb', line 22 def server @server end |
#token ⇒ Object
Returns the value of attribute token.
22 23 24 |
# File 'lib/zmonitor/api.rb', line 22 def token @token end |
#trigger ⇒ Object
API classes
24 25 26 |
# File 'lib/zmonitor/api.rb', line 24 def trigger @trigger end |
#user ⇒ Object
API classes
24 25 26 |
# File 'lib/zmonitor/api.rb', line 24 def user @user end |
#verbose ⇒ Object
Returns the value of attribute verbose.
22 23 24 |
# File 'lib/zmonitor/api.rb', line 22 def verbose @verbose end |
#whoami ⇒ Object
Returns the value of attribute whoami.
22 23 24 |
# File 'lib/zmonitor/api.rb', line 22 def whoami @whoami end |
Instance Method Details
#call_api(message) ⇒ Object
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 |
# File 'lib/zmonitor/api.rb', line 45 def call_api() # Finish preparing the JSON call ['id'] = rand 65536 if ['id'].nil? ['jsonrpc'] = '2.0' # Check if we have authorization token if we're not logging in if @token.nil? && ['method'] != 'user.login' puts "[ERROR] Authorisation Token not initialised. message => #{}" raise NotAuthorisedError.new() else ['auth'] = @token if ['method'] != 'user.login' end = JSON.generate() # Create a JSON string # Open TCP connection to Zabbix master connection = Net::HTTP.new(@server.host, @server.port) connection.read_timeout = 300 # Check to see if we're connecting via SSL if @server.scheme == 'https' then connection.use_ssl = true connection.verify_mode = OpenSSL::SSL::VERIFY_NONE end # Prepare POST request for sending request = Net::HTTP::Post.new(@server.request_uri) request.add_field('Content-Type', 'application/json-rpc') request.body = # Send request begin puts "[INFO] Attempting to send request => #{request}, request body => #{request.body}" if @verbose response = connection.request(request) rescue ::SocketError => e puts "[ERROR] Could not complete request: SocketError => #{e.}" if @verbose raise SocketError.new(e.) rescue Timeout::Error => e puts "[ERROR] Timed out from Zabbix master. Is it being funky? => #{e.}" exit end puts "[INFO] Received response: #{response}" if @verbose raise ResponseCodeError.new("[ERROR] Did not receive 200 OK, but HTTP code #{response.code}") if response.code != "200" # Check for an error, and return the parsed result if everything's fine parsed_response = JSON.parse(response.body) if error = parsed_response['error'] raise ResponseError.new("[ERROR] Received error response: code => #{error['code'].to_s}; message => #{error['message']}; data => #{error['data']}") end return parsed_response['result'] end |