You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Splunk Enterprise Software Development Kit (SDK) for Python contains library code designed to enable developers to build applications using the Splunk platform.
6
+
The [Splunk Enterprise](https://www.splunk.com/en_us/products/splunk-enterprise.html) Software Development Kit (SDK) for Python is intended to be the primary way for developers to communicate with the Splunk platform's REST API.
7
7
8
-
Splunk is a search engine and analytic environment that uses a distributed map-reduce architecture to efficiently index, search, and process large time-varying data sets.
8
+
You may be asking:
9
+
10
+
-[What are Splunk apps?](https://help.splunk.com/en/splunk-enterprise/administer/admin-manual/10.0/meet-splunk-apps/apps-and-add-ons)
11
+
-[What can Splunk apps do?](https://dev.splunk.com/enterprise/docs/developapps/extensionpoints)
12
+
-[How do I write Splunk apps?](https://dev.splunk.com/enterprise/docs/welcome)
13
+
-[Where does the SDK fit in all this?](https://dev.splunk.com/enterprise/docs/devtools/python/sdk-python/)
9
14
10
15
## Getting started
11
16
12
17
### Requirements
13
18
14
-
#### Python compatibility
19
+
#### Python
15
20
16
-
Splunk Enterprise SDK for Python is tested only with Python 3.7, 3.9 and 3.13. Latest version is always recommended.
21
+
Please use the latest Python version supported when developing. Splunk Enterprise SDK for Python is tested with Python 3.9 and 3.13.
17
22
18
23
#### Splunk Enterprise
19
24
20
-
This SDK is only tested with Splunk versions supported in the [Splunk Software Support Policy](https://www.splunk.com/en_us/legal/splunk-software-support-policy.html)
25
+
This SDK is only tested with Splunk Enterprise versions supported in the [Splunk Software Support Policy](https://www.splunk.com/en_us/legal/splunk-software-support-policy.html).
21
26
22
27
[Go here](http://www.splunk.com/download) to get Splunk Enterprise.
23
28
24
-
For more information, see the Splunk Enterprise [Installation Manual](https://docs.splunk.com/Documentation/Splunk/latest/Installation).
25
-
26
29
### Installing the SDK
27
30
28
-
Using `pip` is the easiest way to pull the SDK into your project. `poetry` and `uv` should work just as well.
31
+
Using `pip` is the easiest way to pull the SDK into your project. `poetry` and `uv` should work just as well. A project-specific virtualenv is recommended.
29
32
30
-
A project-specific virtualenv is recommended.
33
+
In your app's project folder:
31
34
32
35
```sh
33
-
python -m venv .venv
34
-
source .venv/bin/activate
35
-
python -m pip install splunk-sdk
36
+
$ python -m venv .venv
37
+
$ source .venv/bin/activate
38
+
# Bundle all your dependencies into `bin/` before deploying!
39
+
# Skip it if you're not building an app.
40
+
$ python -m pip install splunk-sdk --target bin/
36
41
```
37
42
38
-
[See docs](https://dev.splunk.com/enterprise/docs/developapps/createapps/appanatomy/) for how to package additional dependencies with your app.
39
-
40
-
#### Optional: Create an `.env` file
43
+
[See docs](https://dev.splunk.com/enterprise/docs/developapps/createapps/appanatomy/) on more details about packaging additional dependencies with your app.
41
44
42
-
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 authentication. For convenience during development, you can store these arguments as key-value pairs in a `.env` file.
43
-
44
-
A file called `.env.template` exists in the root of this repository. Duplicate it as `.env`, then adjust it to your match your environment.
45
-
46
-
> **WARNING:** The `.env` file isn't part of the Splunk platform. This is **not** the place for production credentials!
47
-
48
-
### SDK usage examples
45
+
### Using SDK in apps
49
46
50
47
The easiest and most effective way of learning how to use this library should be reading through the apps in our test suite, as well as the [splunk-app-examples](https://github.com/splunk/splunk-app-examples) repository. They show how to programmatically interact with the Splunk platform in a variety of scenarios - from basic metadata retrieval, one-shot searching and managing saved searches to building complete applications with modular inputs and custom search commands.
51
48
@@ -77,10 +74,30 @@ import splunklib.client as client
77
74
service = client.connect(host=<HOST_URL>, token=<SESSION_KEY>, autologin=True)
78
75
```
79
76
80
-
### Customization
77
+
#### Creating Custom Search Commands
78
+
79
+
TODO: Link docs about this
80
+
81
+
##### Accessing instance metadata in CSCs
82
+
83
+
- The `service` metadata object is created from the `splunkd` URI and session key passed to the command invocation the search results info file and is available in `MyCommand`.`generate`/`transform`/`stream`/`reduce` methods depending on the Custom Search Command.
84
+
85
+
```python
86
+
from splunklib.searchcommands import StreamingCommand
87
+
88
+
classMyCommand(StreamingCommand):
89
+
defget_metadata(self):
90
+
# Access instance metadata
91
+
service =self.service
92
+
# Access Splunk service info
93
+
info = service.info
94
+
# [...]
95
+
```
96
+
97
+
##### Field retention in CSC
81
98
82
99
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. Structural changes like this may not be preserved.
83
-
If you're having issues with field retention, make sure to use `add_field(record, fieldname, value)` method from SearchCommand to add a new field and value to the record.
100
+
If you're having issues with field retention, make sure to use `add_field(record, fieldname, value)` method from `SearchCommand` to add a new field and value to the record.
84
101
85
102
```diff
86
103
class CustomStreamingCommand(StreamingCommand):
@@ -92,10 +109,10 @@ class CustomStreamingCommand(StreamingCommand):
92
109
yield record
93
110
```
94
111
95
-
###Customization for Generating Custom Search Command
112
+
##### Using a helper method to generate events properly
96
113
97
114
- Generating Custom Search Command is used to generate events using SDK code.
98
-
- Make sure to use `gen_record()` method from SearchCommand to add a new record and pass event data as comma-separated key=value pairs (mentioned in below example).
115
+
- Make sure to use `gen_record()` method from `SearchCommand` to add a new record and pass event data as comma-separated key=value pairs (mentioned in below example).
99
116
100
117
```diff
101
118
@Configuration()
@@ -108,9 +125,29 @@ class GeneratorTest(GeneratingCommand):
108
125
109
126
```
110
127
111
-
### Access metadata of Modular Inputs app example
128
+
#### Modular Inputs Example App
129
+
130
+
[Go here](https://help.splunk.com/en/splunk-enterprise/developing-views-and-apps-for-splunk-web/10.0/modular-inputs/modular-inputs-basic-example) to find out more about setting up a Modular Input.
131
+
132
+
#### Accessing instance metadata in scripts
133
+
134
+
- The `service` metadata object is created from the `splunkd` URI and session key passed to the command invocation on the modular input stream respectively, and is available as soon as the `<script_name>.stream_events()` method is called.
135
+
136
+
```python
137
+
from splunklib.modularinput import Script
138
+
139
+
classMyScript(Script):
140
+
defstream_events(self, inputs, ew):
141
+
# Access instance metadata
142
+
service =self.service
143
+
# Access Splunk service info
144
+
info = service.info
145
+
# [...]
146
+
```
112
147
113
-
- In `stream_events()` one can access modular input app metadata from `InputDefinition` object
148
+
#### Accessing Modular Inputs' metadata
149
+
150
+
- In `stream_events()` you can access Modular Input app metadata from `InputDefinition` object
114
151
- See the [Modular Input App example](https://github.com/splunk/splunk-app-examples/blob/master/modularinputs/python/github_commits/bin/github_commits.py) for reference.
115
152
116
153
```python
@@ -123,43 +160,22 @@ class GeneratorTest(GeneratingCommand):
- The `service` metadata object is created from the `splunkd` URI and session key passed to the command invocation the search results info file and is available in `MyCommand`.`generate`/`transform`/`stream`/`reduce` methods depending on the Custom Search Command.
131
-
132
-
```python
133
-
from splunklib.searchcommands import StreamingCommand
163
+
### Contributions
134
164
135
-
classMyCommand(StreamingCommand):
136
-
defget_metadata(self):
137
-
# Access instance metadata
138
-
service =self.service
139
-
# Access Splunk service info
140
-
info = service.info
141
-
# [...]
142
-
```
165
+
We welcome all contributions!
166
+
If you would like to contribute to the SDK, see [Contributing to Splunk](https://www.splunk.com/en_us/form/contributions.html). For additional guidelines, see [CONTRIBUTING](CONTRIBUTING.md).
143
167
144
-
#### Modular Inputs app
168
+
#### Testing
145
169
146
-
- The `service` metadata object is created from the `splunkd` URI and session key passed to the command invocation on the modular input stream respectively, and is available as soon as the `MyScript.stream_events` method is called.
170
+
This repository contains both unit and integration tests. The latter need `docker`/`podman` to work.
147
171
148
-
```python
149
-
from splunklib.modularinput import Script
172
+
##### Create an `.env` file
150
173
151
-
classMyScript(Script):
152
-
defstream_events(self, inputs, ew):
153
-
# Access instance metadata
154
-
service =self.service
155
-
# Access Splunk service info
156
-
info = service.info
157
-
# [...]
158
-
```
174
+
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 authentication. For convenience during development, you can store these arguments as key-value pairs in a `.env` file.
159
175
160
-
### Testing
176
+
A file called `.env.template` exists in the root of this repository. Duplicate it as `.env`, then adjust it to your match your environment.
161
177
162
-
This repo contains both unit and integration tests. The latter need `docker`/`podman` to work.
178
+
> **WARNING:** The `.env` file isn't part of the Splunk platform. This is **not** the place for production credentials!
163
179
164
180
```sh
165
181
# Run entire test suite:
@@ -168,12 +184,13 @@ make test
168
184
make test-unit
169
185
```
170
186
171
-
#### Integration tests
187
+
#####Integration tests
172
188
173
189
> NOTE: Before running the integration tests, make sure the instance of Splunk you are testing against doesn't have new events being dumped continuously into it. Several of the tests rely on a stable event count. It's best to test against a clean install of Splunk but if you can't, you should at least disable the \*NIX and Windows apps.
174
190
175
191
```sh
176
-
# Make sure a local Splunk instance is running
192
+
# This command starts a Splunk Docker container
193
+
# and waits until it reaches an operational state.
177
194
SPLUNK_VERSION=latest make docker-start
178
195
179
196
# Run the integration tests:
@@ -182,7 +199,7 @@ make test-integration
182
199
183
200
> Do not run the test suite against a production instance of Splunk! It will run just fine with the free Splunk license.
184
201
185
-
### Optional: Set up logging for splunklib
202
+
### Setting up logging for splunklib
186
203
187
204
The default level is WARNING, which means that only events of this level and above will be visible
188
205
To change a logging level we can call setup_logging() method and pass the logging level as an argument.
@@ -197,14 +214,6 @@ from splunklib import setup_logging
197
214
setup_logging(logging.DEBUG)
198
215
```
199
216
200
-
### Changelog
201
-
202
-
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.
203
-
204
-
### Branches
205
-
206
-
To learn more about our branching model, see [Branching Model](https://github.com/splunk/splunk-sdk-python/wiki/Branching-Model) on GitHub.
207
-
208
217
## Documentation and resources
209
218
210
219
| Resource | Description |
@@ -226,11 +235,6 @@ Stay connected with other developers building on the Splunk platform.
If you would like to contribute to the SDK, see [Contributing to Splunk](https://www.splunk.com/en_us/form/contributions.html). For additional guidelines, see [CONTRIBUTING](CONTRIBUTING.md).
233
-
234
238
### Support
235
239
236
240
- You will be granted support if you or your company are already covered under an existing maintenance/support agreement. Submit a new case in the [Support Portal](https://www.splunk.com/en_us/support-and-services.html) and include `Splunk Enterprise SDK for Python` in the subject line.
0 commit comments