|
3 | 3 |
|
4 | 4 | # The Splunk Enterprise Software Development Kit for Python
|
5 | 5 |
|
6 |
| -#### Version 1.6.17 |
| 6 | +#### Version 1.6.18 |
7 | 7 |
|
8 | 8 | The Splunk Enterprise Software Development Kit (SDK) for Python contains library code and examples designed to enable developers to build applications using the Splunk platform.
|
9 | 9 |
|
@@ -71,6 +71,26 @@ To run the examples and unit tests, you must put the root of the SDK on your PYT
|
71 | 71 |
|
72 | 72 | The SDK command-line examples require a common set of arguments that specify the host, port, and login credentials for Splunk Enterprise. For a full list of command-line arguments, include `--help` as an argument to any of the examples.
|
73 | 73 |
|
| 74 | +### Following are the different ways to connect to Splunk Enterprise |
| 75 | +#### Using username/password |
| 76 | +```python |
| 77 | +import splunklib.client as client |
| 78 | + service = client.connect(host=<host_url>, username=<username>, password=<password>, autoLogin=True) |
| 79 | +``` |
| 80 | + |
| 81 | +#### Using bearer token |
| 82 | +```python |
| 83 | +import splunklib.client as client |
| 84 | +service = client.connect(host=<host_url>, splunkToken=<bearer_token>, autologin=True) |
| 85 | +``` |
| 86 | + |
| 87 | +#### Using session key |
| 88 | +```python |
| 89 | +import splunklib.client as client |
| 90 | +service = client.connect(host=<host_url>, token=<session_key>, autologin=True) |
| 91 | +``` |
| 92 | + |
| 93 | +### |
74 | 94 | #### Create a .splunkrc convenience file
|
75 | 95 |
|
76 | 96 | To connect to Splunk Enterprise, many of the SDK examples and unit tests take command-line arguments that specify values for the host, port, and login credentials for Splunk Enterprise. For convenience during development, you can store these arguments as key-value pairs in a text file named **.splunkrc**. Then, the SDK examples and unit tests use the values from the **.splunkrc** file when you don't specify them.
|
@@ -112,8 +132,18 @@ Save the file as **.splunkrc** in the current user's home directory.
|
112 | 132 |
|
113 | 133 | Examples are located in the **/splunk-sdk-python/examples** directory. To run the examples at the command line, use the Python interpreter and include any arguments that are required by the example. In the commands below, replace "examplename" with the name of the specific example in the directory that you want to run:
|
114 | 134 |
|
| 135 | +Using username and Password |
| 136 | + |
115 | 137 | python examplename.py --username="admin" --password="changeme"
|
116 | 138 |
|
| 139 | +Using Bearer token |
| 140 | + |
| 141 | + python examplename.py --bearerToken=<value> |
| 142 | + |
| 143 | +Using Session key |
| 144 | + |
| 145 | + python examplename.py --sessionKey="<value>" |
| 146 | + |
117 | 147 | If you saved your login credentials in the **.splunkrc** file, you can omit those arguments:
|
118 | 148 |
|
119 | 149 | python examplename.py
|
@@ -150,6 +180,53 @@ The test suite uses Python's standard library, the built-in `unittest` library,
|
150 | 180 | |/tests | Source for unit tests |
|
151 | 181 | |/utils | Source for utilities shared by the examples and unit tests |
|
152 | 182 |
|
| 183 | +### Customization |
| 184 | +* When working with custom search commands such as Custom Streaming Commands or Custom Generating Commands, We may need to add new fields to the records based on certain conditions. |
| 185 | +* Structural changes like this may not be preserved. |
| 186 | +* Make sure to use ``add_field(record, fieldname, value)`` method from SearchCommand to add a new field and value to the record. |
| 187 | +* ___Note:__ Usage of ``add_field`` method is completely optional, if you are not facing any issues with field retention._ |
| 188 | + |
| 189 | +Do |
| 190 | +```python |
| 191 | +class CustomStreamingCommand(StreamingCommand): |
| 192 | + def stream(self, records): |
| 193 | + for index, record in enumerate(records): |
| 194 | + if index % 1 == 0: |
| 195 | + self.add_field(record, "odd_record", "true") |
| 196 | + yield record |
| 197 | +``` |
| 198 | + |
| 199 | +Don't |
| 200 | +```python |
| 201 | +class CustomStreamingCommand(StreamingCommand): |
| 202 | + def stream(self, records): |
| 203 | + for index, record in enumerate(records): |
| 204 | + if index % 1 == 0: |
| 205 | + record["odd_record"] = "true" |
| 206 | + yield record |
| 207 | +``` |
| 208 | +### Customization for Generating Custom Search Command |
| 209 | +* Generating Custom Search Command is used to generate events using SDK code. |
| 210 | +* Make sure to use ``gen_record()`` method from SearchCommand to add a new record and pass event data as a key=value pair separated by , (mentioned in below example). |
| 211 | + |
| 212 | +Do |
| 213 | +```python |
| 214 | +@Configuration() |
| 215 | + class GeneratorTest(GeneratingCommand): |
| 216 | + def generate(self): |
| 217 | + yield self.gen_record(_time=time.time(), one=1) |
| 218 | + yield self.gen_record(_time=time.time(), two=2) |
| 219 | +``` |
| 220 | + |
| 221 | +Don't |
| 222 | +```python |
| 223 | +@Configuration() |
| 224 | + class GeneratorTest(GeneratingCommand): |
| 225 | + def generate(self): |
| 226 | + yield {'_time': time.time(), 'one': 1} |
| 227 | + yield {'_time': time.time(), 'two': 2} |
| 228 | +``` |
| 229 | + |
153 | 230 | ### Changelog
|
154 | 231 |
|
155 | 232 | The [CHANGELOG](CHANGELOG.md) contains a description of changes for each version of the SDK. For the latest version, see the [CHANGELOG.md](https://github.com/splunk/splunk-sdk-python/blob/master/CHANGELOG.md) on GitHub.
|
|
0 commit comments