graylogapi 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9be5fd32aab8bdf033a166ea8f8a2bbcbc37d922
4
- data.tar.gz: 4b1c4e462cbe2ef62b37c3d8c260eac5563f1eb7
3
+ metadata.gz: d11f3ff96af6158de0d004545af1fd99f2fd6b23
4
+ data.tar.gz: 77936ef677d557d8237fc3cba8f074127afc8765
5
5
  SHA512:
6
- metadata.gz: 6a1f053a8900febc23289bce905b860b411b3ae8239cd14d5621236a5434f7b1e3bceb6c3a743636c20e181626a96571991cc64c218194fe8ff5f9235bc3a13e
7
- data.tar.gz: 594d5f6b37bae202ec9b79d668c991badc2473bbebbddd1b2a3f01275857f90eee243081bfd17bedb3100f08b00579e0322fca3b5441db516162436fdc3fcda7
6
+ metadata.gz: f7f0b8293f39e8e5076041c2767acd5c4b6287c482335ea6c46dfa81bdb3825f0550fa06805661fd5bb563a6823fa8ad5a81b92a0c1d91f39e6d109bf2a34b99
7
+ data.tar.gz: 558faa1e0cb74d393b4aa79d12294bd9f0f5e4b7f8e3efa933bcb23914d146f9bd2da0f9f9cc596edf65d7333189d148449f1180107f78b79e4b0f510b99e0cf
data/README.md CHANGED
@@ -29,11 +29,34 @@ Ruby gem for working with [Graylog](https://www.graylog.org/) via the [Graylog R
29
29
  Structure of gem looks like Graylog REST Api or navigation menu in UI.
30
30
  For example, you can find *Inputs* in *System/Inputs* in the UI and you can find *Inputs* in `GraylogAPI.new(...).system.inputs` in the gem.
31
31
 
32
- ### get Input by id
33
32
 
34
- graylogapi = Graylog.new('http://localhost:9000/api', 'username', 'password')
33
+
34
+ ### Authorization
35
+
36
+ #### user and password
37
+
38
+ ```
39
+ graylogapi = GraylogAPI.new(base_url: 'http://localhost:9000/api', user: 'username', pass: 'password')
40
+ ```
41
+
42
+ #### token
43
+
44
+ ```
45
+ graylogapi = GraylogAPI.new(base_url: 'http://localhost:9000/api', token: 'token')
46
+ ```
47
+
48
+
49
+
50
+ ### Examples
51
+
52
+ #### get Input by id
53
+
54
+ graylogapi = GraylogAPI.new(base_url: 'http://localhost:9000/api', user: 'username', pass: 'password')
55
+
35
56
  graylogapi.system.inputs.by_id('5947d3840b5712166af25009')
36
57
 
58
+ You can find more examples [here](./examples/)
59
+
37
60
  ## Supported methods
38
61
 
39
62
  * **Alerts**: Manage stream alerts for all streams
@@ -69,7 +92,10 @@ For example, you can find *Inputs* in *System/Inputs* in the UI and you can find
69
92
  * node_by_id — Infromation about a node.
70
93
  * **System/IndexSets**: Index sets
71
94
  * all — Get a list of all index sets.
95
+ * default — Get default index set.
72
96
  * create(params) — Create index set.
97
+ * update(params) — Update index set.
98
+ * make_default(id) — Set default index set.
73
99
  * by_id(id) — Get index set by id.
74
100
  * delete(id) — Delete index set.
75
101
  * **System/Inputs**: Message inputs
@@ -78,6 +104,16 @@ For example, you can find *Inputs* in *System/Inputs* in the UI and you can find
78
104
  * create(params) — Launch input on this node.
79
105
  * update(params) — Update input on this node.
80
106
  * delete(id) — Terminate input on this node.
107
+ * **System/Inputs/Types**: Message input types of this node.
108
+ * node — Get all available input types of this node.
109
+ * all — Get informatino about all input types.
110
+ * by_type(type) — Get information about a single input type.
111
+ * name_to_type(name) — Convert name of type to type.
112
+ * **Users**: User accounts.
113
+ * tokens(username) — Retrieves the list of access tokens for a user.
114
+ * create_token(username, name) — Generate a new access token for a user.
115
+ * delete_token(username, name) — Removes a token for a user.
116
+
81
117
 
82
118
  ## Copyright
83
119
 
@@ -37,11 +37,7 @@ class GraylogAPI
37
37
  if body.nil? || body.empty?
38
38
  {}
39
39
  else
40
- begin
41
- JSON.parse(body)
42
- rescue
43
- body
44
- end
40
+ JSON.parse(body)
45
41
  end
46
42
  end
47
43
  end
@@ -18,11 +18,16 @@ class GraylogAPI
18
18
  # Initializes a new Client object
19
19
  #
20
20
  # @param options [Hash]
21
+ # * :base_url [String] Endpoint of graylog API
22
+ # * :user [String] Username
23
+ # * :pass [String] Password
24
+ # * :token [String] Token
25
+ # * :read_timeout [Integer] Read timeout of http request in seconds (60 default)
26
+ # * :open_timeout [Integer] Open timeout of http request in secods (60 default)
21
27
  # @return [GraylogAPI::Client]
22
28
  def initialize(options = {})
23
29
  @options = options
24
- uri = URI.parse(options[:base_url])
25
- @http = Net::HTTP.new(uri.host, uri.port)
30
+ @http = http_client(URI.parse(options[:base_url]), options)
26
31
  end
27
32
 
28
33
  # Make request to the API
@@ -30,21 +35,33 @@ class GraylogAPI
30
35
  # @param method [Symbol] can be :get, :post, :delete, :put
31
36
  # @param path [String] url
32
37
  # @param params [Hash] request params
33
- # @return Struct
34
- def json_request(method, path, params = {})
35
- request = request(method, path, params)
36
- request.basic_auth(options[:user], options[:pass])
38
+ # @return [GraylogAPI::Client::Response]
39
+ def request(method, path, params = {})
40
+ request = make_request(method, path, params)
37
41
  request.add_field('Content-Type', 'application/json')
42
+ request.basic_auth(username, password)
38
43
  response = @http.request(request)
39
44
 
40
45
  Response.new(response)
41
- rescue
42
- response
43
46
  end
44
47
 
45
48
  private
46
49
 
47
- def request(method, path, params = {})
50
+ def http_client(uri, options)
51
+ http = Net::HTTP.new(uri.host, uri.port)
52
+
53
+ if uri.scheme == 'https'
54
+ http.use_ssl = true
55
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
56
+ end
57
+
58
+ http.read_timeout = options[:read_timeout].nil? ? 60 : options[:read_timeout]
59
+ http.open_timeout = options[:open_timeout].nil? ? 60 : options[:open_timeout]
60
+
61
+ http
62
+ end
63
+
64
+ def make_request(method, path, params = {})
48
65
  case method
49
66
  when :get, :delete
50
67
  full_path = [options[:base_url] + path, URI.encode_www_form(params)]
@@ -55,5 +72,13 @@ class GraylogAPI
55
72
  req
56
73
  end
57
74
  end
75
+
76
+ def username
77
+ options[:token].nil? ? options[:user] : options[:token]
78
+ end
79
+
80
+ def password
81
+ options[:token].nil? ? options[:pass] : 'token'
82
+ end
58
83
  end
59
84
  end
@@ -10,14 +10,14 @@ class GraylogAPI
10
10
  # @param params [Hash]
11
11
  # @return [GraylogAPI::Client::Response]
12
12
  def create(params)
13
- @client.json_request(:post, '/dashboards', params)
13
+ @client.request(:post, '/dashboards', params)
14
14
  end
15
15
 
16
16
  # get list of all dashboards
17
17
  #
18
18
  # @return [GraylogAPI::Client::Response]
19
19
  def all
20
- @client.json_request(:get, '/dashboards')
20
+ @client.request(:get, '/dashboards')
21
21
  end
22
22
 
23
23
  # get dashboard by id
@@ -25,7 +25,7 @@ class GraylogAPI
25
25
  # @param id [Integer] id of a dashboard
26
26
  # @return [GraylogAPI::Client::Response]
27
27
  def by_id(id)
28
- @client.json_request(:get, "/dashboards/#{id}")
28
+ @client.request(:get, "/dashboards/#{id}")
29
29
  end
30
30
 
31
31
  # update dashboard
@@ -34,7 +34,7 @@ class GraylogAPI
34
34
  # @param params [Hash]
35
35
  # @return [GraylogAPI::Client::Response]
36
36
  def update(id, params)
37
- @client.json_request(:put, "/dashboards/#{id}", params)
37
+ @client.request(:put, "/dashboards/#{id}", params)
38
38
  end
39
39
 
40
40
  # delete dashboard
@@ -42,7 +42,7 @@ class GraylogAPI
42
42
  # @param id [Integer] id of a dashboard
43
43
  # @return [GraylogAPI::Client::Response]
44
44
  def delete(id)
45
- @client.json_request(:delete, "/dashboards/#{id}")
45
+ @client.request(:delete, "/dashboards/#{id}")
46
46
  end
47
47
  end
48
48
  end
@@ -11,7 +11,7 @@ class GraylogAPI
11
11
  # @param params [Hash] hash with key and value of new static field
12
12
  # @return [GraylogAPI::Client::Response]
13
13
  def create(input_id, params)
14
- @client.json_request(:post, "/system/inputs/#{input_id}/staticfields", params)
14
+ @client.request(:post, "/system/inputs/#{input_id}/staticfields", params)
15
15
  end
16
16
 
17
17
  # delete key of an input
@@ -20,7 +20,7 @@ class GraylogAPI
20
20
  # @param key [String] key of static field
21
21
  # @return [GraylogAPI::Client::Response]
22
22
  def delete(input_id, key)
23
- @client.json_request(:delete, "/system/inputs/#{input_id}/staticfields/#{key}")
23
+ @client.request(:delete, "/system/inputs/#{input_id}/staticfields/#{key}")
24
24
  end
25
25
  end
26
26
  end
@@ -6,39 +6,39 @@ class GraylogAPI
6
6
  end
7
7
 
8
8
  def all
9
- @client.json_request(:get, '/streams')
9
+ @client.request(:get, '/streams')
10
10
  end
11
11
 
12
12
  def create(params = {})
13
- @client.json_request(:post, '/streams', params)
13
+ @client.request(:post, '/streams', params)
14
14
  end
15
15
 
16
16
  def enabled
17
- @client.json_request(:get, '/streams/enabled')
17
+ @client.request(:get, '/streams/enabled')
18
18
  end
19
19
 
20
20
  def by_id(id)
21
- @client.json_request(:get, "/streams/#{id}")
21
+ @client.request(:get, "/streams/#{id}")
22
22
  end
23
23
 
24
24
  def update(id, params = {})
25
- @client.json_request(:put, "/streams/#{id}", params)
25
+ @client.request(:put, "/streams/#{id}", params)
26
26
  end
27
27
 
28
28
  def delete(id, params = {})
29
- @client.json_request(:delete, "/streams/#{id}", params)
29
+ @client.request(:delete, "/streams/#{id}", params)
30
30
  end
31
31
 
32
32
  def clone(id, params = {})
33
- @client.json_request(:post, "/streams/#{id}/clone", params)
33
+ @client.request(:post, "/streams/#{id}/clone", params)
34
34
  end
35
35
 
36
36
  def pause(id)
37
- @client.json_request(:post, "/streams/#{id}/pause")
37
+ @client.request(:post, "/streams/#{id}/pause")
38
38
  end
39
39
 
40
40
  def resume(id)
41
- @client.json_request(:post, "/streams/#{id}/resume")
41
+ @client.request(:post, "/streams/#{id}/resume")
42
42
  end
43
43
  end
44
44
  end
@@ -7,15 +7,15 @@ class GraylogAPI
7
7
  end
8
8
 
9
9
  def node
10
- @client.json_request(:get, '/system/cluster/node')
10
+ @client.request(:get, '/system/cluster/node')
11
11
  end
12
12
 
13
13
  def nodes
14
- @client.json_request(:get, '/system/cluster/nodes')
14
+ @client.request(:get, '/system/cluster/nodes')
15
15
  end
16
16
 
17
17
  def node_by_id(id)
18
- @client.json_request(:get, "/system/cluster/nodes/#{id}")
18
+ @client.request(:get, "/system/cluster/nodes/#{id}")
19
19
  end
20
20
  end
21
21
  end
@@ -6,28 +6,59 @@ class GraylogAPI
6
6
  @client = client
7
7
  end
8
8
 
9
+ # Get a list of all index sets.
10
+ #
11
+ # @return [GraylogAPI::Client::Response]
9
12
  def all
10
- @client.json_request(:get, '/system/indices/index_sets')
13
+ @client.request(:get, '/system/indices/index_sets')
11
14
  end
12
15
 
16
+ # Get default index set.
17
+ #
18
+ # @return [Hash]
19
+ def default
20
+ all['index_sets'].find { |i| i['default'] == true }
21
+ end
22
+
23
+ # Create index set.
24
+ #
25
+ # @param params [Hash] title, description, etc.
26
+ # @return [GraylogAPI::Client::Response]
13
27
  def create(params)
14
- @client.json_request(:post, '/system/indices/index_sets', params)
28
+ @client.request(:post, '/system/indices/index_sets', params)
15
29
  end
16
30
 
31
+ # Get index set by id.
32
+ #
33
+ # @param id [String]
34
+ # @return [GraylogAPI::Client::Response]
17
35
  def by_id(id)
18
- @client.json_request(:get, "/system/indices/index_sets/#{id}")
36
+ @client.request(:get, "/system/indices/index_sets/#{id}")
19
37
  end
20
38
 
39
+ # Delete index set.
40
+ #
41
+ # @param id [String]
42
+ # @return [GraylogAPI::Client::Response]
21
43
  def delete(id)
22
- @client.json_request(:delete, "/system/indices/index_sets/#{id}")
44
+ @client.request(:delete, "/system/indices/index_sets/#{id}")
23
45
  end
24
46
 
47
+ # Set default index set.
48
+ #
49
+ # @param id [String]
50
+ # @return [GraylogAPI::Client::Response]
25
51
  def make_default(id)
26
- @client.json_request(:put, "/system/indices/index_sets/#{id}/default")
52
+ @client.request(:put, "/system/indices/index_sets/#{id}/default")
27
53
  end
28
54
 
55
+ # Update index set.
56
+ #
57
+ # @param id [String]
58
+ # @param params [Hash]
59
+ # @return [GraylogAPI::Client::Response]
29
60
  def update(id, params)
30
- @client.json_request(:put, "/system/indices/index_sets/#{id}", params)
61
+ @client.request(:put, "/system/indices/index_sets/#{id}", params)
31
62
  end
32
63
  end
33
64
  end
@@ -0,0 +1,40 @@
1
+ class GraylogAPI
2
+ class System
3
+ class Inputs
4
+ # class for getting info about input types
5
+ class Types
6
+ def initialize(client)
7
+ @client = client
8
+ end
9
+
10
+ # get input types of current node
11
+ #
12
+ # @return [GraylogAPI::Client::Response]
13
+ def node
14
+ @client.request(:get, '/system/inputs/types')
15
+ end
16
+
17
+ # get all input types
18
+ #
19
+ # @return [GraylogAPI::Client::Response]
20
+ def all
21
+ @client.request(:get, '/system/inputs/types/all')
22
+ end
23
+
24
+ # get info about input type
25
+ #
26
+ # @return [GraylogAPI::Client::Response]
27
+ def by_type(type)
28
+ @client.request(:get, "/system/inputs/types/#{type}")
29
+ end
30
+
31
+ # convert type name to type
32
+ #
33
+ # @return [String]
34
+ def name_to_type(name)
35
+ all.body.find { |_, type| type['name'].casecmp(name).zero? }.first
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -1,3 +1,5 @@
1
+ require 'graylogapi/system/inputs/types'
2
+
1
3
  class GraylogAPI
2
4
  class System
3
5
  # class for manage inputs
@@ -8,40 +10,68 @@ class GraylogAPI
8
10
 
9
11
  # get all inputs
10
12
  #
11
- # @return [Struct]
13
+ # @return [GraylogAPI::Client::Response]
12
14
  def all
13
- @client.json_request(:get, '/system/inputs')
15
+ @client.request(:get, '/system/inputs')
14
16
  end
15
17
 
16
18
  # get input by input id
17
19
  #
18
- # @return [Struct]
20
+ # @return [GraylogAPI::Client::Response]
19
21
  def by_id(id)
20
- @client.json_request(:get, "/system/inputs/#{id}")
22
+ @client.request(:get, "/system/inputs/#{id}")
21
23
  end
22
24
 
23
25
  # create input
24
26
  #
25
27
  # @param params [Hash]
26
- # @return [Struct]
28
+ # @return [GraylogAPI::Client::Response]
27
29
  def create(params = {})
28
- @client.json_request(:post, '/system/inputs', params)
30
+ @client.request(:post, '/system/inputs', parse(params))
29
31
  end
30
32
 
31
33
  # update input
32
34
  #
33
35
  # @param params [Hash]
34
- # @return [Struct]
36
+ # @return [GraylogAPI::Client::Response]
35
37
  def update(id, params = {})
36
- @client.json_request(:put, "/system/inputs/#{id}", params)
38
+ @client.request(:put, "/system/inputs/#{id}", parse(params))
37
39
  end
38
40
 
39
41
  # delete input
40
42
  #
41
43
  # @param params [Hash]
42
- # @return [Struct]
43
- def delete(id, params = {})
44
- @client.json_request(:delete, "/system/inputs/#{id}", params)
44
+ # @return [GraylogAPI::Client::Response]
45
+ def delete(id)
46
+ @client.request(:delete, "/system/inputs/#{id}")
47
+ end
48
+
49
+ # object for get information about input types
50
+ #
51
+ # @return [GraylogAPI::System::Inputs::Types]
52
+ def types
53
+ @types ||= Types.new(@client)
54
+ end
55
+
56
+ private
57
+
58
+ # parase params
59
+ #
60
+ # @return [Hash]
61
+ def parse(params)
62
+ params = type_name_to_type(params) if params.key? :type_name
63
+
64
+ params
65
+ end
66
+
67
+ # convert type name to type
68
+ #
69
+ # @return [Hash]
70
+ def type_name_to_type(params)
71
+ type = types.name_to_type(params[:type_name])
72
+ params[:type] = type
73
+ params.delete(:type_name)
74
+ params
45
75
  end
46
76
  end
47
77
  end
@@ -10,15 +10,15 @@ class GraylogAPI
10
10
  end
11
11
 
12
12
  def overview
13
- @client.json_request(:get, '/system')
13
+ @client.request(:get, '/system')
14
14
  end
15
15
 
16
16
  def jvm
17
- @client.json_request(:get, '/system/jvm')
17
+ @client.request(:get, '/system/jvm')
18
18
  end
19
19
 
20
20
  def thread_dump
21
- @client.json_request(:get, '/system/threaddump')
21
+ @client.request(:get, '/system/threaddump')
22
22
  end
23
23
 
24
24
  # object for manage System/Inputs
@@ -0,0 +1,23 @@
1
+ class GraylogAPI
2
+ # class for manage users
3
+ class Users
4
+ def initialize(client)
5
+ @client = client
6
+ end
7
+
8
+ # Retrieves the list of access tokens for a user
9
+ def tokens(username)
10
+ @client.request(:get, "/users/#{username}/tokens")
11
+ end
12
+
13
+ # Generate a new access token for a user
14
+ def create_token(username, name)
15
+ @client.request(:post, "/users/#{username}/tokens/#{name}", {})
16
+ end
17
+
18
+ # Removes a token for a user
19
+ def delete_token(username, name)
20
+ @client.request(:delete, "/users/#{username}/tokens/#{name}")
21
+ end
22
+ end
23
+ end
@@ -1,3 +1,3 @@
1
1
  class GraylogAPI
2
- VERSION = '1.1.0'.freeze
2
+ VERSION = '1.2.0'.freeze
3
3
  end
data/lib/graylogapi.rb CHANGED
@@ -1,13 +1,14 @@
1
1
  libdir = File.dirname(__FILE__)
2
2
  $LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
3
3
 
4
- require 'graylogapi/version'
5
4
  require 'graylogapi/alerts'
6
5
  require 'graylogapi/client'
7
6
  require 'graylogapi/dashboards'
8
7
  require 'graylogapi/static_fields'
9
8
  require 'graylogapi/streams'
10
9
  require 'graylogapi/system'
10
+ require 'graylogapi/users'
11
+ require 'graylogapi/version'
11
12
 
12
13
  # class for work with graylog api
13
14
  class GraylogAPI
@@ -46,4 +47,9 @@ class GraylogAPI
46
47
  def static_fields
47
48
  @static_fields ||= StaticFields.new(@client)
48
49
  end
50
+
51
+ # @return [GraylogAPI::Users]
52
+ def users
53
+ @users ||= Users.new(@client)
54
+ end
49
55
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graylogapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey Aleksandrov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-14 00:00:00.000000000 Z
11
+ date: 2017-09-18 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Allows you to work with graylog api from ruby
14
14
  email:
@@ -31,6 +31,8 @@ files:
31
31
  - lib/graylogapi/system/cluster.rb
32
32
  - lib/graylogapi/system/index_sets.rb
33
33
  - lib/graylogapi/system/inputs.rb
34
+ - lib/graylogapi/system/inputs/types.rb
35
+ - lib/graylogapi/users.rb
34
36
  - lib/graylogapi/version.rb
35
37
  homepage: https://github.com/postgred/graylogapi
36
38
  licenses: