splunk-sdk-ruby 0.1.0 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MDQzNDZmNGE2M2MzNzFiZDJmODg4ZTNkZjU4MTdlZDdjMDUwMWRlNA==
5
+ data.tar.gz: !binary |-
6
+ NjI4ZDc1MGE0MWQ2ZDBkOGI0ZmZiOTAxMTI5NmEwMDgyNzU2YWFmOA==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ Njc3ODE4ZjBiY2FmMWJhMWRiZWU4MThkYTg4Y2FhYjU0OTc3MjI1NTg5ZTkw
10
+ OGFlMzJmODU3OTVhNjk5ZGZhOWExOWM4YzY2ODE4ZTBjMTU4OTdiZTcwZTAz
11
+ MWQwZWMyZjQ1OWM2YTc1MDE2ZWRhNDcyNGMzMTY1MWMyMDJlZmI=
12
+ data.tar.gz: !binary |-
13
+ MzRhMTdhYmNjZDkwYTgxNmVlYWMwZTA1MGE4YWIzNjY5YTUwYjMzMTU4MjY2
14
+ NzZhNjFkZDM1NGFhNjgyOWMwNWY3ODM5ZjJmODQyYmQ3Mzc1ZTgxOGEyZTAw
15
+ NTAxZjkyNTNiNGM2M2U1Y2FhYTdmN2ExOGU1ZDE0Mzk0OWRlNDE=
@@ -1,5 +1,33 @@
1
1
  # Splunk SDK for Ruby Changelog
2
2
 
3
+ ## Version 0.8.1 (beta)
4
+
5
+ ### Bugs fixed
6
+
7
+ * Fixed wrong version number in a few documentation files.
8
+
9
+ ## Version 0.8.0 (beta)
10
+
11
+ ### Breaking changes
12
+
13
+ * The _raw field in events is now returned as text, not XML. That is, all tags
14
+ such as the sg elements, are removed, and all characters are unescaped. The
15
+ XML is available from the segmented_raw method on the event, which returns a
16
+ string containing the raw XML of the _raw field returned by the server.
17
+ * The severities in messages in Atom feeds are now strings instead of symbols.
18
+
19
+ ### New features
20
+
21
+ * Added support for inputs via the Service#inputs method, and for modular
22
+ input kinds via the Service#modular_input_kinds method.
23
+ * Added segmented_raw method to events returned by ResultsReader.
24
+
25
+ ### Bugs fixed
26
+
27
+ * Added missing Splunk:: prefix in example in the docs for Service.
28
+ * Moved default "segmentation=none" option for asynchronous searches from
29
+ Job#initialize to Job#events, Job#preview, and Job#results.
30
+
3
31
  ## Version 0.1.0 (preview)
4
32
 
5
33
  ### Breaking changes
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
- # The Splunk Software Development Kit for Ruby (Preview Release)
1
+ # The Splunk Software Development Kit for Ruby (Beta Release)
2
2
 
3
- #### Version 0.1
4
- This Splunk Software Development Kit (SDK) contains library code and examples
5
- designed to enable developers to build applications using Splunk.
3
+ #### Version 0.8
4
+ This Splunk Software Development Kit (SDK) for Ruby contains library code and
5
+ examples designed to enable developers to build applications using Splunk.
6
6
 
7
7
  Splunk is a search engine and analytic environment that uses a distributed
8
8
  map-reduce architecture to efficiently index, search, and process large
@@ -17,15 +17,6 @@ The Splunk developer platform enables developers to take advantage of the
17
17
  same technology used by the Splunk product to build exciting new applications
18
18
  that are enabled by Splunk's unique capabilities.
19
19
 
20
- 1. This Preview release is pre-beta, and therefore is incomplete and may have
21
- bugs. A Beta release is planned prior to a general release.
22
-
23
- 2. The Apache license only applies to this SDK and no other software provided
24
- by Splunk.
25
-
26
- 3. Splunk, in using the Apache license, is not providing any warranties or
27
- indemnification, or accepting any liabilities with the Preview of this SDK.
28
-
29
20
  ## Getting started with the Splunk SDK for Ruby
30
21
 
31
22
  The Splunk SDK for Ruby contains code and some examples that show how to
@@ -131,9 +122,7 @@ line, the SDK examples and unit tests use the values from the .splunkrc file.
131
122
  # Splunk password
132
123
  password=changeme
133
124
  # Access scheme (default: https)
134
- scheme=https
135
- # Your version of Splunk (default: 5.0)
136
- version=5.0</pre>
125
+ scheme=https</pre>
137
126
 
138
127
  2. Save the file as .splunkrc in the current user's home directory.
139
128
 
@@ -172,7 +161,6 @@ Click **Yes**, then continue creating the file.
172
161
  running the examples. You can either update to the latest version
173
162
  of the SDK, or comment out the <tt>app</tt>, <tt>owner</tt>, and
174
163
  <tt>version</tt> fields.
175
- * The <tt>version</tt> field is only used by the Splunk SDK for JavaScript.
176
164
 
177
165
  #### Run the unit tests
178
166
 
@@ -0,0 +1,96 @@
1
+ #--
2
+ # Copyright 2011-2012 Splunk, Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License"): you may
5
+ # not use this file except in compliance with the License. You may obtain
6
+ # a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+ # License for the specific language governing permissions and limitations
14
+ # under the License.
15
+ #++
16
+
17
+ ##
18
+ # To make this example work, you need to install the example_modular_inputs.spl
19
+ # application in the examples directory in your Splunk instance. It provides
20
+ # two modular inputs called test1 and test2.
21
+
22
+ require 'splunk-sdk-ruby'
23
+
24
+ # How to get to the Splunk server. Edit this to match your
25
+ # own Splunk install.
26
+ config = {
27
+ :scheme => :https,
28
+ :host => "localhost",
29
+ :port => 8089,
30
+ :username => "admin",
31
+ :password => "changeme"
32
+ }
33
+
34
+ # First open a connection to Splunk.
35
+ service = Splunk::connect(config)
36
+
37
+ # List the modular inputs on the server.
38
+ puts "Modular inputs:"
39
+ service.modular_input_kinds.each do |mi|
40
+ puts " #{mi["title"]} (#{mi.name})"
41
+ end
42
+
43
+ # List the parameters to test1
44
+ puts
45
+ puts "Arguments to test1:"
46
+ puts
47
+ args = service.modular_input_kinds["test1"].arguments
48
+ args.each do |key, val|
49
+ puts " Arg: #{key}"
50
+ puts " Human-readable title: #{val["title"]}"
51
+ puts " Description: #{val["description"]}"
52
+ puts " Req on\tReq on"
53
+ puts " create\tedit\tOrder\tType"
54
+ puts " #{val["required_on_create"]}\t\t\t#{val["required_on_edit"]}\t\t#{val["order"]}\t#{val["data_type"]}"
55
+ puts
56
+ end
57
+
58
+ # Now we'll create an input of kind test1. The only field we must provide
59
+ # is 'required_on_create', which has the required_on_create=1 in its definition.
60
+ # All the other arguments are optional.
61
+ test1_inputs = service.inputs["test1"]
62
+
63
+ # Create an input of kind test1.
64
+ INPUT_NAME = "my_input"
65
+ if test1_inputs.has_key?(INPUT_NAME)
66
+ test1_inputs.delete(INPUT_NAME)
67
+ end
68
+ my_input = test1_inputs.create(
69
+ INPUT_NAME,
70
+ :required_on_create => "boris",
71
+ :number_field => 33
72
+ )
73
+
74
+ # Print the values of the fields. In the output, number_field and
75
+ # required_on_create will be set, but all others will have no value.
76
+ puts "Initial state:"
77
+ args.keys().each do |arg|
78
+ puts " #{arg}: #{my_input[arg] || "(value not set)"}"
79
+ end
80
+
81
+ # Now we update the input kind. The argument 'arg_required_on_edit' has
82
+ # required_on_edit=1, so we have to send a value for it when we update
83
+ # the input.
84
+ my_input.update(:boolean_field => true, :arg_required_on_edit => "meep")
85
+ my_input.refresh() # We have to refresh to see the changes we made.
86
+
87
+ puts
88
+ puts "After update:"
89
+ args.keys().each do |arg|
90
+ puts " #{arg}: #{my_input[arg] || "(value not set)"}"
91
+ end
92
+
93
+ # Delete the input we created.
94
+ my_input.delete()
95
+
96
+
@@ -181,7 +181,7 @@ module Splunk
181
181
  element.elements.each do |element|
182
182
  if element.name == "msg"
183
183
  metadata["messages"] << {
184
- "type" => element.attributes["type"].text.intern,
184
+ "type" => element.attributes["type"].text,
185
185
  "message" => children_to_s(element)
186
186
  }
187
187
  end
@@ -25,17 +25,7 @@ require_relative 'splunk_http_error'
25
25
  require_relative 'synonyms'
26
26
 
27
27
  module Splunk
28
- # Class representing a collection in Splunk.
29
- #
30
- # A +Collection+ is a group of items, usually of class +Entity+ or one of its
31
- # subclasses, but occasionally another +Collection+. Usually you obtain a
32
- # +Collection+ by calling one of the convenience methods on +Service+.
33
- #
34
- # A +Collection+ is enumerable, and implements many of the methods found on
35
- # +Hash+, so methods like +each+, +select+, and +delete_if+ all work, as does
36
- # fetching a member of the +Collection+ with [].
37
- #
38
- class Collection
28
+ class ReadOnlyCollection
39
29
  include Enumerable
40
30
  extend Synonyms
41
31
 
@@ -49,8 +39,8 @@ module Splunk
49
39
  @infinite_count = -1
50
40
 
51
41
  # @always_fetch tells whether, when creating an entity in this collection,
52
- # to bother trying to parse the response and always fetch the new state
53
- # after the fact. This is necessary for some collections, such as users,
42
+ # to bother trying to parse the response and always fetch the new state
43
+ # after the fact. This is necessary for some collections, such as users,
54
44
  # which don't return the newly created object.
55
45
  @always_fetch = false
56
46
  end
@@ -120,100 +110,6 @@ module Splunk
120
110
  state=entry)
121
111
  end
122
112
 
123
- ##
124
- # Creates an item in this collection.
125
- #
126
- # The _name_ argument is required. All other arguments are passed as a hash,
127
- # though they vary from collection to collection.
128
- #
129
- # Returns: the created entity.
130
- #
131
- # *Example:*
132
- # service = Splunk::connect(:username => 'admin', :password => 'foo')
133
- # service.users.create('jack',
134
- # :password => 'mypassword',
135
- # :realname => 'Jack_be_nimble',
136
- # :roles => ['user'])
137
- #
138
- def create(name, args={})
139
- body_args = args.clone()
140
- body_args["name"] = name
141
-
142
- request_args = {
143
- :method => :POST,
144
- :resource => @resource,
145
- :body => body_args
146
- }
147
- if args.has_key?(:namespace)
148
- request_args[:namespace] = body_args.delete(:namespace)
149
- end
150
-
151
- response = @service.request(request_args)
152
-
153
- if @always_fetch
154
- fetch_args = {:method => :GET,
155
- :resource => @resource + [name]}
156
- if args.has_key?(:namespace)
157
- fetch_args[:namespace] = args[:namespace]
158
- end
159
- response = @service.request(fetch_args)
160
- end
161
- feed = AtomFeed.new(response.body)
162
- raise StandardError.new("No entities returned") if feed.entries.empty?
163
- entity = atom_entry_to_entity(feed.entries[0])
164
- raise StandardError.new("Found nil entity.") if entity.nil?
165
- return entity
166
- end
167
-
168
- ##
169
- # Deletes an item from the collection.
170
- #
171
- # Entities from different namespaces may have the same name, so if you are
172
- # connected to Splunk using a namespace with wildcards in it, there may
173
- # be multiple entities in the collection with the same name. In this case
174
- # you must specify a namespace as well, or the +delete+ method will raise an
175
- # AmbiguousEntityReference error.
176
- #
177
- # *Example:*
178
- # service = Splunk::connect(:username => 'admin', :password => 'foo')
179
- # props = service.confs['props']
180
- # props.delete('sdk-tests')
181
- #
182
- def delete(name, namespace=nil)
183
- if namespace.nil?
184
- namespace = @service.namespace
185
- end
186
-
187
- # We don't want to handle any cases about deleting ambiguously named
188
- # entities.
189
- if !namespace.is_exact?
190
- raise StandardError.new("Must provide an exact namespace to delete an entity.")
191
- end
192
-
193
- @service.request(:method => :DELETE,
194
- :namespace => namespace,
195
- :resource => @resource + [name])
196
- return self
197
- end
198
-
199
- ##
200
- # Deletes all entities on this collection for which the block returns true.
201
- #
202
- # If block is omitted, returns an enumerator over all members of the
203
- # collection.
204
- #
205
- def delete_if(&block)
206
- # Without a block, just return an enumerator
207
- return each() if !block_given?
208
-
209
- values.each() do |entity|
210
- if block.call(entity)
211
- delete(entity.name, entity.namespace)
212
- end
213
- end
214
-
215
- end
216
-
217
113
  ##
218
114
  # Calls block once for each item in the collection.
219
115
  #
@@ -294,7 +190,7 @@ module Splunk
294
190
  end
295
191
 
296
192
  ##
297
- # Identical to the +each+ method, but the block is passed both the entity's
193
+ # Identical to the +each+ method, but the block is passed both the entity's
298
194
  # name, and the entity.
299
195
  #
300
196
  def each_pair(args={}, &block)
@@ -387,7 +283,10 @@ module Splunk
387
283
  # Synonyms: +size+.
388
284
  #
389
285
  def length()
390
- return values().length()
286
+ response = @service.request(:resource => @resource,
287
+ :query => {"count" => 0})
288
+ feed = AtomFeed.new(response.body)
289
+ return Integer(feed.metadata["totalResults"])
391
290
  end
392
291
 
393
292
  synonym "size", "length"
@@ -414,4 +313,108 @@ module Splunk
414
313
  synonym "to_a", "values"
415
314
  end
416
315
 
316
+ # Class representing a collection in Splunk.
317
+ #
318
+ # A +Collection+ is a group of items, usually of class +Entity+ or one of its
319
+ # subclasses, but occasionally another +Collection+. Usually you obtain a
320
+ # +Collection+ by calling one of the convenience methods on +Service+.
321
+ #
322
+ # A +Collection+ is enumerable, and implements many of the methods found on
323
+ # +Hash+, so methods like +each+, +select+, and +delete_if+ all work, as does
324
+ # fetching a member of the +Collection+ with [].
325
+ #
326
+ class Collection < ReadOnlyCollection
327
+ ##
328
+ # Creates an item in this collection.
329
+ #
330
+ # The _name_ argument is required. All other arguments are passed as a hash,
331
+ # though they vary from collection to collection.
332
+ #
333
+ # Returns: the created entity.
334
+ #
335
+ # *Example:*
336
+ # service = Splunk::connect(:username => 'admin', :password => 'foo')
337
+ # service.users.create('jack',
338
+ # :password => 'mypassword',
339
+ # :realname => 'Jack_be_nimble',
340
+ # :roles => ['user'])
341
+ #
342
+ def create(name, args={})
343
+ body_args = args.clone()
344
+ body_args["name"] = name
345
+
346
+ request_args = {
347
+ :method => :POST,
348
+ :resource => @resource,
349
+ :body => body_args
350
+ }
351
+ if args.has_key?(:namespace)
352
+ request_args[:namespace] = body_args.delete(:namespace)
353
+ end
354
+
355
+ response = @service.request(request_args)
356
+
357
+ if @always_fetch
358
+ fetch_args = {:method => :GET,
359
+ :resource => @resource + [name]}
360
+ if args.has_key?(:namespace)
361
+ fetch_args[:namespace] = args[:namespace]
362
+ end
363
+ response = @service.request(fetch_args)
364
+ end
365
+ feed = AtomFeed.new(response.body)
366
+ raise StandardError.new("No entities returned") if feed.entries.empty?
367
+ entity = atom_entry_to_entity(feed.entries[0])
368
+ raise StandardError.new("Found nil entity.") if entity.nil?
369
+ return entity
370
+ end
371
+
372
+ ##
373
+ # Deletes an item from the collection.
374
+ #
375
+ # Entities from different namespaces may have the same name, so if you are
376
+ # connected to Splunk using a namespace with wildcards in it, there may
377
+ # be multiple entities in the collection with the same name. In this case
378
+ # you must specify a namespace as well, or the +delete+ method will raise an
379
+ # AmbiguousEntityReference error.
380
+ #
381
+ # *Example:*
382
+ # service = Splunk::connect(:username => 'admin', :password => 'foo')
383
+ # props = service.confs['props']
384
+ # props.delete('sdk-tests')
385
+ #
386
+ def delete(name, namespace=nil)
387
+ if namespace.nil?
388
+ namespace = @service.namespace
389
+ end
390
+
391
+ # We don't want to handle any cases about deleting ambiguously named
392
+ # entities.
393
+ if !namespace.is_exact?
394
+ raise StandardError.new("Must provide an exact namespace to delete an entity.")
395
+ end
396
+
397
+ @service.request(:method => :DELETE,
398
+ :namespace => namespace,
399
+ :resource => @resource + [name])
400
+ return self
401
+ end
402
+
403
+ ##
404
+ # Deletes all entities on this collection for which the block returns true.
405
+ #
406
+ # If block is omitted, returns an enumerator over all members of the
407
+ # collection.
408
+ #
409
+ def delete_if(&block)
410
+ # Without a block, just return an enumerator
411
+ return each() if !block_given?
412
+
413
+ values.each() do |entity|
414
+ if block.call(entity)
415
+ delete(entity.name, entity.namespace)
416
+ end
417
+ end
418
+ end
419
+ end
417
420
  end