Class: Zabby::Connection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConnection

Returns a new instance of Connection.



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

def initialize
  reset
end

Instance Attribute Details

#authObject (readonly)

Returns the value of attribute auth.



9
10
11
# File 'lib/zabby/connection.rb', line 9

def auth
  @auth
end

#passwordObject (readonly)

Returns the value of attribute password.



8
9
10
# File 'lib/zabby/connection.rb', line 8

def password
  @password
end

#proxy_hostObject (readonly)

Returns the value of attribute proxy_host.



8
9
10
# File 'lib/zabby/connection.rb', line 8

def proxy_host
  @proxy_host
end

#proxy_passwordObject (readonly)

Returns the value of attribute proxy_password.



8
9
10
# File 'lib/zabby/connection.rb', line 8

def proxy_password
  @proxy_password
end

#proxy_userObject (readonly)

Returns the value of attribute proxy_user.



8
9
10
# File 'lib/zabby/connection.rb', line 8

def proxy_user
  @proxy_user
end

#request_idObject (readonly)

Returns the value of attribute request_id.



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

def request_id
  @request_id
end

#request_pathObject (readonly)

Returns the value of attribute request_path.



8
9
10
# File 'lib/zabby/connection.rb', line 8

def request_path
  @request_path
end

#uriObject (readonly)

Returns the value of attribute uri.



8
9
10
# File 'lib/zabby/connection.rb', line 8

def uri
  @uri
end

#userObject (readonly)

Returns the value of attribute user.



8
9
10
# File 'lib/zabby/connection.rb', line 8

def user
  @user
end

Instance Method Details

#authenticateAuthentication key

Returns:

  • (Authentication key)


50
51
52
53
54
55
56
57
58
# File 'lib/zabby/connection.rb', line 50

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_message(element, action, params = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/zabby/connection.rb', line 60

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



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/zabby/connection.rb', line 85

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)


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

def logged_in?
  !@auth.nil?
end

#login(config) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/zabby/connection.rb', line 22

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.empty? ? "/api_jsonrpc.php" : @uri.path
  authenticate
end

#logoutObject



37
38
39
# File 'lib/zabby/connection.rb', line 37

def logout
  reset
end

#next_request_idObject



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

def next_request_id
  @request_id += 1
end

#perform_request(element, action, params) ⇒ Object



70
71
72
73
74
75
# File 'lib/zabby/connection.rb', line 70

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



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/zabby/connection.rb', line 98

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

  if response.code != "200" then
    raise ResponseCodeError.new("Response code from [#{@uri}] is #{response.code}): #{response.body}")
  end

  zabbix_response = JSON.parse(response.body)

  #if not ( responce_body_hash['id'] == id ) then
  # raise Zabbix::InvalidAnswerId.new("Wrong ID in zabbix answer")
  #end

  # Check errors in zabbix answer. If error exist - raise exception Zabbix::Error
  if (error = zabbix_response['error']) then
    error_message = error['message']
    error_data = error['data']
    error_code = error['code']

    e_message = "Code: [" + error_code.to_s + "]. Message: [" + error_message +
            "]. Data: [" + error_data + "]."

    if error_data == "Login name or password is incorrect"
      raise AuthenticationError.new(e_message)
    else
      raise StandardError.new(e_message)
    end
  end

  zabbix_response['result']
end

#request(message) ⇒ Object



77
78
79
80
81
82
# File 'lib/zabby/connection.rb', line 77

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



16
17
18
19
20
# File 'lib/zabby/connection.rb', line 16

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