zobbix 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +27 -1
- data/lib/zobbix/api_request.rb +6 -0
- data/lib/zobbix/api_response.rb +7 -0
- data/lib/zobbix/version.rb +1 -1
- data/lib/zobbix.rb +13 -33
- metadata +1 -3
- data/lib/zobbix/apiinfo/version_request.rb +0 -11
- data/lib/zobbix/user/login_request.rb +0 -14
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 29150be35da6782e1024701d4d292405f81c5bf7
         | 
| 4 | 
            +
              data.tar.gz: 8837a76838b79f9d5015f5fe656be5d32993bf88
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 6cc7318b64d3256ad64e4fa9100c44fb23279b550e550b184db77245c83679b36d1886b9e8f390174bc8bcdbcd666138ba56dee6bb568e6507cc324f3d36fb97
         | 
| 7 | 
            +
              data.tar.gz: 1c40af5a0a5232f757369870f5d0fadda8fd4e4bce304ba179c2ac9f56fbd951c79801d5cc26c3d4c10c2fa4bedd9511787046e0f9ed2bb271ecf072a81c07ba
         | 
    
        data/README.md
    CHANGED
    
    | @@ -22,9 +22,35 @@ params = { | |
| 22 22 | 
             
              expression: 'Linux Template:system.cpu.util[all,idle,avg1].avg(120)}<5'
         | 
| 23 23 | 
             
            }
         | 
| 24 24 |  | 
| 25 | 
            -
            zbx.request(methods, params)
         | 
| 25 | 
            +
            response = zbx.request(methods, params)
         | 
| 26 | 
            +
             | 
| 27 | 
            +
            response.class #=> Zobbix::ApiResponse
         | 
| 28 | 
            +
             | 
| 29 | 
            +
            version = zbx.request('apiinfo.version')
         | 
| 30 | 
            +
             | 
| 31 | 
            +
            version.result #=> "2.4.6"
         | 
| 32 | 
            +
            version.success? #=> true
         | 
| 33 | 
            +
             | 
| 34 | 
            +
            error = zbx.request('unknown.method')
         | 
| 35 | 
            +
            error.error? #=> true
         | 
| 36 | 
            +
            error.result #=> nil
         | 
| 37 | 
            +
            error.error_code #=> -32602
         | 
| 38 | 
            +
            error.error_message #=> "Invalid params."
         | 
| 39 | 
            +
            error.error_data #=> 'Incorrect API "unknown".'
         | 
| 40 | 
            +
            error.raise_exception #=> Zobbix::Error: API returned error. Code: -32602 Message: Invalid params. Data: Incorrect API "unknown".
         | 
| 26 41 | 
             
            ``` 
         | 
| 27 42 |  | 
| 43 | 
            +
            ## API Exceptions
         | 
| 44 | 
            +
            ```ruby
         | 
| 45 | 
            +
            # Enable API exceptions
         | 
| 46 | 
            +
             | 
| 47 | 
            +
            zbx = Zobbix.connect(uri:      'http://localhost/zabbix',
         | 
| 48 | 
            +
                                 user:     'Admin',
         | 
| 49 | 
            +
                                 password: 'zabbix',
         | 
| 50 | 
            +
                                 raise_exceptions: true)
         | 
| 51 | 
            +
            zbx.request('unknown.method') #=> Zobbix::Error: API returned error. Code: -32602 Message: Invalid params. Data: Incorrect API "unknown".
         | 
| 52 | 
            +
            ```
         | 
| 53 | 
            +
             | 
| 28 54 | 
             
            ## Zabbix Server API
         | 
| 29 55 |  | 
| 30 56 | 
             
            [API reference (2.2)](https://www.zabbix.com/documentation/2.2/manual/api/reference)
         | 
    
        data/lib/zobbix/api_request.rb
    CHANGED
    
    | @@ -20,6 +20,9 @@ class Zobbix | |
| 20 20 | 
             
                  new(*args).perform
         | 
| 21 21 | 
             
                end
         | 
| 22 22 |  | 
| 23 | 
            +
                # @param [String] uri Zabbix Server uri
         | 
| 24 | 
            +
                # @param [String] method API method
         | 
| 25 | 
            +
                # @param [Hash] params API method params
         | 
| 23 26 | 
             
                def initialize(uri, method, params)
         | 
| 24 27 | 
             
                  @uri    = uri.sub(/\/$/, '')
         | 
| 25 28 | 
             
                  @method = method
         | 
| @@ -27,6 +30,9 @@ class Zobbix | |
| 27 30 | 
             
                  @params = params
         | 
| 28 31 | 
             
                end
         | 
| 29 32 |  | 
| 33 | 
            +
                # Execute http request
         | 
| 34 | 
            +
                #
         | 
| 35 | 
            +
                # @return [Zobbix::ApiResponse]
         | 
| 30 36 | 
             
                def perform
         | 
| 31 37 | 
             
                  ApiResponse.new { self.class.post("#{@uri}#{self.class.path}", body: payload) }
         | 
| 32 38 | 
             
                end
         | 
    
        data/lib/zobbix/api_response.rb
    CHANGED
    
    | @@ -11,30 +11,37 @@ class Zobbix | |
| 11 11 | 
             
                  @exception = ex
         | 
| 12 12 | 
             
                end
         | 
| 13 13 |  | 
| 14 | 
            +
                # @return [Boolean] True, if no HTTP or API errors happened
         | 
| 14 15 | 
             
                def success?
         | 
| 15 16 | 
             
                  @exception.nil? && @response['error'].nil?
         | 
| 16 17 | 
             
                end
         | 
| 17 18 |  | 
| 19 | 
            +
                # @return [Boolean] Opposite of #success?
         | 
| 18 20 | 
             
                def error?
         | 
| 19 21 | 
             
                  !success?
         | 
| 20 22 | 
             
                end
         | 
| 21 23 |  | 
| 24 | 
            +
                # @return [String, Hash] API result. Depends on API method
         | 
| 22 25 | 
             
                def result
         | 
| 23 26 | 
             
                  @response['result']
         | 
| 24 27 | 
             
                end
         | 
| 25 28 |  | 
| 29 | 
            +
                # @return [Fixnum, nil] Zabbix API error code
         | 
| 26 30 | 
             
                def error_code
         | 
| 27 31 | 
             
                  @response['error'] && @response['error']['code']
         | 
| 28 32 | 
             
                end
         | 
| 29 33 |  | 
| 34 | 
            +
                # @return [String, nil] Zabbix API error message
         | 
| 30 35 | 
             
                def error_message
         | 
| 31 36 | 
             
                  @response['error'] && @response['error']['message']
         | 
| 32 37 | 
             
                end
         | 
| 33 38 |  | 
| 39 | 
            +
                # @return [String, nil] Zabbix API error description
         | 
| 34 40 | 
             
                def error_data
         | 
| 35 41 | 
             
                  @response['error'] && @response['error']['data']
         | 
| 36 42 | 
             
                end
         | 
| 37 43 |  | 
| 44 | 
            +
                # @return [Fixnum] Request id
         | 
| 38 45 | 
             
                def id
         | 
| 39 46 | 
             
                  @response['id']
         | 
| 40 47 | 
             
                end
         | 
    
        data/lib/zobbix/version.rb
    CHANGED
    
    
    
        data/lib/zobbix.rb
    CHANGED
    
    | @@ -34,6 +34,11 @@ class Zobbix | |
| 34 34 | 
             
                end
         | 
| 35 35 | 
             
              end
         | 
| 36 36 |  | 
| 37 | 
            +
              #
         | 
| 38 | 
            +
              # @param [Hash] credentials Connection credentials
         | 
| 39 | 
            +
              # @option opts [String] :uri Zabbix Server URI
         | 
| 40 | 
            +
              # @option opts [String] :user
         | 
| 41 | 
            +
              # @option opts [String] :password
         | 
| 37 42 | 
             
              def self.connect(credentials)
         | 
| 38 43 | 
             
                new(credentials)
         | 
| 39 44 | 
             
                  .tap(&:check_version!)
         | 
| @@ -45,9 +50,6 @@ class Zobbix | |
| 45 50 | 
             
              end
         | 
| 46 51 |  | 
| 47 52 | 
             
              attr_reader :credentials, :auth
         | 
| 48 | 
            -
             | 
| 49 | 
            -
              #
         | 
| 50 | 
            -
              # @param Zobbix::Credentials
         | 
| 51 53 | 
             
              def initialize(credentials)
         | 
| 52 54 | 
             
                @raise_exceptions = credentials.delete(:raise_exceptions) || false
         | 
| 53 55 | 
             
                @credentials = Credentials.new(credentials)
         | 
| @@ -60,7 +62,7 @@ class Zobbix | |
| 60 62 | 
             
              # @raise Zobbix::ConnectionError Can't establish connection
         | 
| 61 63 | 
             
              # @raise Zobbix::UnsupportedVersionError Bad API version
         | 
| 62 64 | 
             
              def check_version!
         | 
| 63 | 
            -
                version =  | 
| 65 | 
            +
                version = request('apiinfo.version').result
         | 
| 64 66 |  | 
| 65 67 | 
             
                if version.nil?
         | 
| 66 68 | 
             
                  raise ConnectionError.new(credentials)
         | 
| @@ -80,9 +82,8 @@ class Zobbix | |
| 80 82 | 
             
              #
         | 
| 81 83 | 
             
              # @raise Zobbix::AuthenticationError
         | 
| 82 84 | 
             
              def authenticate!
         | 
| 83 | 
            -
                response =  | 
| 84 | 
            -
             | 
| 85 | 
            -
                                                      credentials.password)
         | 
| 85 | 
            +
                response = request('user.login', user: credentials.user,
         | 
| 86 | 
            +
                                                 password: credentials.password)
         | 
| 86 87 |  | 
| 87 88 | 
             
                unless response.success?
         | 
| 88 89 | 
             
                  raise AuthenticationError.new(credentials)
         | 
| @@ -93,42 +94,21 @@ class Zobbix | |
| 93 94 |  | 
| 94 95 | 
             
              # Makes API request
         | 
| 95 96 | 
             
              #
         | 
| 96 | 
            -
              # @param [String]  | 
| 97 | 
            -
              # @param [Hash]  | 
| 98 | 
            -
              # @return [Zobbix::ApiResponse]  | 
| 97 | 
            +
              # @param [String] method Request method
         | 
| 98 | 
            +
              # @param [Hash] params Request params
         | 
| 99 | 
            +
              # @return [Zobbix::ApiResponse] Response object
         | 
| 99 100 | 
             
              #
         | 
| 100 101 | 
             
              # @see https://www.zabbix.com/documentation/2.4/manual/api/reference
         | 
| 101 102 | 
             
              def request(method, params = {})
         | 
| 102 | 
            -
                 | 
| 103 | 
            -
             | 
| 104 | 
            -
                response =
         | 
| 105 | 
            -
                  if request
         | 
| 106 | 
            -
                    if requires_auth?(method)
         | 
| 107 | 
            -
                      request.perform(credentials.uri, @auth, params)
         | 
| 108 | 
            -
                    else
         | 
| 109 | 
            -
                      request.perform(credentials.uri, params)
         | 
| 110 | 
            -
                    end
         | 
| 111 | 
            -
                  else
         | 
| 112 | 
            -
                    raw_request(method, params)
         | 
| 113 | 
            -
                  end
         | 
| 103 | 
            +
                params = params.merge(auth: @auth) if requires_auth?(method)
         | 
| 114 104 |  | 
| 105 | 
            +
                response = ApiRequest.perform(credentials.uri, method, params)
         | 
| 115 106 | 
             
                response.raise_exception if @raise_exceptions && response.error?
         | 
| 116 | 
            -
             | 
| 117 107 | 
             
                response
         | 
| 118 108 | 
             
              end
         | 
| 119 109 |  | 
| 120 110 | 
             
              private
         | 
| 121 111 |  | 
| 122 | 
            -
              def resolve_class(method)
         | 
| 123 | 
            -
                namespace, mtd = method.split('.').map(&:capitalize)
         | 
| 124 | 
            -
                self.class.const_get(namespace).const_get("#{mtd}Request")
         | 
| 125 | 
            -
              rescue NameError
         | 
| 126 | 
            -
              end
         | 
| 127 | 
            -
             | 
| 128 | 
            -
              def raw_request(method, params)
         | 
| 129 | 
            -
                ApiRequest.perform(credentials.uri, method, params.merge(auth: @auth))
         | 
| 130 | 
            -
              end
         | 
| 131 | 
            -
             | 
| 132 112 | 
             
              def requires_auth?(method)
         | 
| 133 113 | 
             
                method = method.to_s
         | 
| 134 114 | 
             
                method != 'apiinfo.version' && method != 'user.login'
         | 
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: zobbix
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.0. | 
| 4 | 
            +
              version: 0.0.2
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Michael Lutsiuk
         | 
| @@ -109,10 +109,8 @@ files: | |
| 109 109 | 
             
            - lib/zobbix.rb
         | 
| 110 110 | 
             
            - lib/zobbix/api_request.rb
         | 
| 111 111 | 
             
            - lib/zobbix/api_response.rb
         | 
| 112 | 
            -
            - lib/zobbix/apiinfo/version_request.rb
         | 
| 113 112 | 
             
            - lib/zobbix/core_ext/hash.rb
         | 
| 114 113 | 
             
            - lib/zobbix/credentials.rb
         | 
| 115 | 
            -
            - lib/zobbix/user/login_request.rb
         | 
| 116 114 | 
             
            - lib/zobbix/version.rb
         | 
| 117 115 | 
             
            - test/api_request_test.rb
         | 
| 118 116 | 
             
            - test/api_response_test.rb
         |