Class: Zabbix::API

Inherits:
Object
  • Object
show all
Defined in:
lib/zmonitor/api.rb,
lib/zmonitor/api.rb

Direct Known Subclasses

Event, Trigger, User

Defined Under Namespace

Classes: NotAuthorisedError, ResponseCodeError, ResponseError

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#eventObject

API classes



24
25
26
# File 'lib/zmonitor/api.rb', line 24

def event
  @event
end

#serverObject

Returns the value of attribute server.



22
23
24
# File 'lib/zmonitor/api.rb', line 22

def server
  @server
end

#tokenObject

Returns the value of attribute token.



22
23
24
# File 'lib/zmonitor/api.rb', line 22

def token
  @token
end

#triggerObject

API classes



24
25
26
# File 'lib/zmonitor/api.rb', line 24

def trigger
  @trigger
end

#userObject

API classes



24
25
26
# File 'lib/zmonitor/api.rb', line 24

def user
  @user
end

#verboseObject

Returns the value of attribute verbose.



22
23
24
# File 'lib/zmonitor/api.rb', line 22

def verbose
  @verbose
end

#whoamiObject

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

Raises:



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(message)
  # Finish preparing the JSON call
  message['id'] = rand 65536 if message['id'].nil?
  message['jsonrpc'] = '2.0'
  # Check if we have authorization token if we're not logging in
  if @token.nil? && message['method'] != 'user.login'
    puts "[ERROR] Authorisation Token not initialised. message => #{message}"
    raise NotAuthorisedError.new()
  else
    message['auth'] = @token if message['method'] != 'user.login'
  end

  json_message = JSON.generate(message) # 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 = json_message

  # 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.message}" if @verbose
    raise SocketError.new(e.message)
  rescue Timeout::Error => e
    puts "[ERROR] Timed out from Zabbix master. Is it being funky? => #{e.message}"
    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