Class: Zabby::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/zabby/connection.rb

Constant Summary collapse

JSONRPC_SCRIPT =

Name of the Zabbix RPC script

"/api_jsonrpc.php"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConnection

Returns a new instance of Connection.



15
16
17
# File 'lib/zabby/connection.rb', line 15

def initialize
  reset
end

Instance Attribute Details

#authObject (readonly)

Returns the value of attribute auth.



12
13
14
# File 'lib/zabby/connection.rb', line 12

def auth
  @auth
end

#passwordObject (readonly)

Returns the value of attribute password.



11
12
13
# File 'lib/zabby/connection.rb', line 11

def password
  @password
end

#proxy_hostObject (readonly)

Returns the value of attribute proxy_host.



11
12
13
# File 'lib/zabby/connection.rb', line 11

def proxy_host
  @proxy_host
end

#proxy_passwordObject (readonly)

Returns the value of attribute proxy_password.



11
12
13
# File 'lib/zabby/connection.rb', line 11

def proxy_password
  @proxy_password
end

#proxy_userObject (readonly)

Returns the value of attribute proxy_user.



11
12
13
# File 'lib/zabby/connection.rb', line 11

def proxy_user
  @proxy_user
end

#request_idObject (readonly)

Returns the value of attribute request_id.



13
14
15
# File 'lib/zabby/connection.rb', line 13

def request_id
  @request_id
end

#request_pathObject (readonly)

Returns the value of attribute request_path.



11
12
13
# File 'lib/zabby/connection.rb', line 11

def request_path
  @request_path
end

#uriObject (readonly)

Returns the value of attribute uri.



11
12
13
# File 'lib/zabby/connection.rb', line 11

def uri
  @uri
end

#userObject (readonly)

Returns the value of attribute user.



11
12
13
# File 'lib/zabby/connection.rb', line 11

def user
  @user
end

Instance Method Details

#authenticateAuthentication key

Returns:

  • (Authentication key)


53
54
55
56
57
58
59
60
61
# File 'lib/zabby/connection.rb', line 53

def authenticate
  auth_message = format_message('user', 'login',
                                'user' => @user,
                                'password' => @password)
  @auth = query_zabbix_rpc(auth_message)
rescue Exception => e
  @auth = nil
  raise e
end

#format_exception(zabbix_response) ⇒ Object

Raise a Zabby exception

Parameters:

  • zabbix_response (Hash)

    JSON formatted Zabbix response.

Raises:



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/zabby/connection.rb', line 110

def format_exception(zabbix_response)
  error = zabbix_response['error']
  error_message = error['message']
  error_data = error['data']
  error_code = error['code']

  if error_data == "Login name or password is incorrect"
    raise AuthenticationError.new(error_message, error_code, error_data)
  else
    raise APIError.new(error_message, error_code, error_data)
  end
end

#format_message(element, action, params = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/zabby/connection.rb', line 63

def format_message(element, action, params = {})
  {
      'jsonrpc' => '2.0',
      'id' => next_request_id,
      'method' => "#{element}.#{action}",
      'params' => params,
      'auth' => @auth
  }
end

#httpObject

Prepare http object



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/zabby/connection.rb', line 93

def http
  if @proxy_host
    http = Net::HTTP::Proxy(@proxy_host.host, @proxy_host.port, @proxy_user, @proxy_password).new(@uri.host, @uri.port)
  else
    http = Net::HTTP.new(@uri.host, @uri.port)
  end
  if @uri.scheme == "https"
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  http
end

#logged_in?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/zabby/connection.rb', line 44

def logged_in?
  !@auth.nil?
end

#login(config) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/zabby/connection.rb', line 25

def (config)
  return @auth if @auth
  
  @uri = URI.parse(config.server)
  @user = config.user
  @password = config.password
  if config.proxy_host
    @proxy_host = URI.parse(config.proxy_host)
    @proxy_user = config.proxy_user
    @proxy_password = config.proxy_password
  end
  @request_path = @uri.path[-4,4] == '.php' ? @uri.path : @uri.path + JSONRPC_SCRIPT
  authenticate
end

#logoutObject



40
41
42
# File 'lib/zabby/connection.rb', line 40

def logout
  reset
end

#next_request_idObject



48
49
50
# File 'lib/zabby/connection.rb', line 48

def next_request_id
  @request_id += 1
end

#perform_request(element, action, params) ⇒ Object

Perform an authenticated request

Returns:

  • (Object)

    The Zabbix response (Hash, Boolean, etc.) in JSON format.

Raises:



75
76
77
78
79
80
# File 'lib/zabby/connection.rb', line 75

def perform_request(element, action, params)
  raise AuthenticationError.new("Not logged in") if !logged_in?

  message = format_message(element, action, params)
  query_zabbix_rpc(message)
end

#query_zabbix_rpc(message) ⇒ Object

Query the Zabbix Web Services and extract the JSON response.

Parameters:

  • message (Hash)

    request in JSON format.

Returns:

  • (Object)

    The Zabbix response (Hash, Boolean, etc.) in JSON format.

Raises:



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/zabby/connection.rb', line 127

def query_zabbix_rpc(message)
  # Send the request!
  http_response = http.request(request(message))

  # Check for HTTP errors.
  if http_response.code != "200"
    raise ResponseCodeError.new("Error from #{@uri}", http_response.code, http_response.body)
  end

  zabbix_response = JSON.parse(http_response.body)

  # Check for Zabbix errors.
  if zabbix_response['error']
    format_exception(zabbix_response)
  end

  zabbix_response['result']
end

#request(message) ⇒ Net::HTTP::Post

Prepare a JSON request HTTP Post format

Parameters:

  • message (Hash)

    A hash with all parameters for the Zabbix web service.

Returns:

  • (Net::HTTP::Post)

    Message ready to be POSTed.



85
86
87
88
89
90
# File 'lib/zabby/connection.rb', line 85

def request(message)
  req = Net::HTTP::Post.new(@request_path)
  req.add_field('Content-Type', 'application/json-rpc')
  req.body = JSON.generate(message)
  req
end

#resetObject



19
20
21
22
23
# File 'lib/zabby/connection.rb', line 19

def reset
  @uri = @user = @password = @proxy_host = @proxy_user = @proxy_password = nil
  @request_id = 0
  @auth = nil
end