Table of Contents
Overview¶
Splunk Solutions SDK is an open source packaged solution for getting data into Splunk using modular inputs. This SDK is used by Splunk Add-on builder, and Splunk UCC based add-ons and is intended for use by partner developers. This SDK/Library extends the Splunk SDK for Python.
Note: this project uses
poetry
1.5.1.
Removed requests and urllib3 from solnlib¶
The requests
and urllib3
libraries has been removed from solnlib, so solnlib now depends on the requests
and urllib3
libraries from the running environment.
By default, Splunk delivers the above libraries and their version depends on the Splunk version. More information here.
IMPORTANT: urllib3
is available in Splunk v8.1.0
and later
Please note that if requests
or urllib3
are installed in <Add-on>/lib
e.g. as a dependency of another library, that version will be taken first.
If requests
or urllib3
is missing in the add-on’s lib
directory, the version provided by Splunk will be used.
Custom Version of requests and urllib3¶
In case the Splunk’s requests
or urllib3
version is not sufficient for you,
you can deliver version you need by simply adding it to the requirements.txt
or pyproject.toml
file in your add-on.
Use solnlib outside the Splunk¶
Solnlib no longer provides requests
and urllib3
so if you want to use solnlib outside the Splunk, please note that you will need to
provide these libraries yourself in the environment where solnlib is used.
References ↵
modular_input ↵
checkpointer.py¶
This module provides two kinds of checkpointer: KVStoreCheckpointer, FileCheckpointer for modular input to save checkpoint.
__all__ = ['CheckpointerException', 'KVStoreCheckpointer', 'FileCheckpointer']
module-attribute
¶
Checkpointer
¶
Base class of checkpointer.
Source code in solnlib/modular_input/checkpointer.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
|
batch_update(states)
abstractmethod
¶
Updates multiple documents.
Source code in solnlib/modular_input/checkpointer.py
49 50 51 |
|
delete(key)
abstractmethod
¶
Deletes document with an id that equals to key
.
Source code in solnlib/modular_input/checkpointer.py
57 58 59 |
|
get(key)
abstractmethod
¶
Gets document with an id that equals to key
.
Source code in solnlib/modular_input/checkpointer.py
53 54 55 |
|
update(key, state)
abstractmethod
¶
Updates document with an id that equals to key
and state
as
document data.
Source code in solnlib/modular_input/checkpointer.py
44 45 46 47 |
|
CheckpointerException
¶
Bases: Exception
Source code in solnlib/modular_input/checkpointer.py
37 38 |
|
FileCheckpointer
¶
Bases: Checkpointer
File checkpointer.
Use file to save modular input checkpoint.
Examples:
>>> from solnlib.modular_input import checkpointer
>>> ck = checkpointer.FileCheckpointer('/opt/splunk/var/...')
>>> ck.update(...)
>>> ck.get(...)
Source code in solnlib/modular_input/checkpointer.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
|
__init__(checkpoint_dir)
¶
Initializes FileCheckpointer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
checkpoint_dir |
str
|
Checkpoint directory. |
required |
Source code in solnlib/modular_input/checkpointer.py
218 219 220 221 222 223 224 225 226 227 228 |
|
batch_update(states)
¶
Source code in solnlib/modular_input/checkpointer.py
246 247 248 |
|
delete(key)
¶
Source code in solnlib/modular_input/checkpointer.py
258 259 260 261 262 263 |
|
encode_key(key)
¶
Source code in solnlib/modular_input/checkpointer.py
230 231 |
|
get(key)
¶
Source code in solnlib/modular_input/checkpointer.py
250 251 252 253 254 255 256 |
|
update(key, state)
¶
Source code in solnlib/modular_input/checkpointer.py
233 234 235 236 237 238 239 240 241 242 243 244 |
|
KVStoreCheckpointer
¶
Bases: Checkpointer
KVStore checkpointer.
Use KVStore to save modular input checkpoint.
More information about KV Store in Splunk is here.
Examples:
>>> from solnlib.modular_input import checkpointer
>>> checkpoint = checkpointer.KVStoreCheckpointer(
"unique_addon_checkpoints",
"session_key",
"unique_addon"
)
>>> checkpoint.update("input_1", {"timestamp": 1638043093})
>>> checkpoint.get("input_1")
>>> # returns {"timestamp": 1638043093}
Source code in solnlib/modular_input/checkpointer.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
|
__init__(collection_name, session_key, app, owner='nobody', scheme=None, host=None, port=None, **context)
¶
Initializes KVStoreCheckpointer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
collection_name |
str
|
Collection name of kvstore checkpointer. |
required |
session_key |
str
|
Splunk access token. |
required |
app |
str
|
App name of namespace. |
required |
owner |
Optional[str]
|
(optional) Owner of namespace, default is |
'nobody'
|
scheme |
Optional[str]
|
(optional) The access scheme, default is None. |
None
|
host |
Optional[str]
|
(optional) The host name, default is None. |
None
|
port |
Optional[int]
|
(optional) The port number, default is None. |
None
|
context |
Any
|
Other configurations for Splunk rest client. |
{}
|
Raises:
Type | Description |
---|---|
binding.HTTPError
|
HTTP error different from 404, for example 503 when KV Store is initializing and not ready to serve requests. |
CheckpointerException
|
If init KV Store checkpointer failed. |
Source code in solnlib/modular_input/checkpointer.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
|
batch_update(states)
¶
Updates multiple documents.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
states |
Iterable[Dict[str, Any]]
|
Iterable that contains documents to update. Document should be a dict with at least “state” key. |
required |
Raises:
Type | Description |
---|---|
binding.HTTPError
|
when an error occurred in Splunk, for example, when Splunk is restarting and KV Store is not yet initialized. |
Source code in solnlib/modular_input/checkpointer.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
|
delete(key)
¶
Deletes document with an id that equals to key
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
str
|
|
required |
Raises:
Type | Description |
---|---|
binding.HTTPError
|
When an error occurred in Splunk (not 404 code), can be 503 code, when Splunk is restarting and KV Store is not yet initialized. |
Source code in solnlib/modular_input/checkpointer.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
|
get(key)
¶
Gets document with an id that equals to key
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
str
|
|
required |
Raises:
Type | Description |
---|---|
binding.HTTPError
|
When an error occurred in Splunk (not 404 code), can be 503 code, when Splunk is restarting and KV Store is not yet initialized. |
Returns:
Type | Description |
---|---|
Optional[Any]
|
Document data under |
Source code in solnlib/modular_input/checkpointer.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
|
update(key, state)
¶
Updates document with an id that equals to key
and state
as
document data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
str
|
|
required |
state |
Any
|
Document data to update. It can be integer, string,
or a dict, or anything that can be an argument to |
required |
Raises:
Type | Description |
---|---|
binding.HTTPError
|
when an error occurred in Splunk, for example, when Splunk is restarting and KV Store is not yet initialized. |
Source code in solnlib/modular_input/checkpointer.py
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
|
event.py¶
This module provides Splunk modular input event encapsulation.
__all__ = ['EventException', 'XMLEvent', 'HECEvent']
module-attribute
¶
Event
¶
Base class of modular input event.
Source code in solnlib/modular_input/event.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
|
__init__(data, time=None, index=None, host=None, source=None, sourcetype=None, fields=None, stanza=None, unbroken=False, done=False)
¶
Modular input event.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
dict
|
Event data. |
required |
time |
float
|
(optional) Event timestamp, default is None. |
None
|
index |
str
|
(optional) The index event will be written to, default is None. |
None
|
host |
str
|
(optional) Event host, default is None. |
None
|
source |
str
|
(optional) Event source, default is None. |
None
|
sourcetype |
str
|
(optional) Event sourcetype, default is None. |
None
|
fields |
dict
|
(optional) Event fields, default is None. |
None
|
stanza |
str
|
(optional) Event stanza name, default is None. |
None
|
unbroken |
bool
|
(optional) Event unbroken flag, default is False. |
False
|
done |
bool
|
(optional) The last unbroken event, default is False. |
False
|
Examples:
>>> event = Event(
>>> data='This is a test data.',
>>> time=1372274622.493,
>>> index='main',
>>> host='localhost',
>>> source='Splunk',
>>> sourcetype='misc',
>>> fields= {'Cloud':'AWS','region': 'us-west-1'},
>>> stanza='test_scheme://test',
>>> unbroken=True,
>>> done=True)
Source code in solnlib/modular_input/event.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
|
__str__()
¶
Source code in solnlib/modular_input/event.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
|
format_events(events)
classmethod
¶
Format events to list of string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
events |
List
|
List of events to format. |
required |
Returns:
Type | Description |
---|---|
List
|
List of formatted events string. |
Source code in solnlib/modular_input/event.py
108 109 110 111 112 113 114 115 116 117 118 119 |
|
EventException
¶
Bases: Exception
Source code in solnlib/modular_input/event.py
28 29 |
|
HECEvent
¶
Bases: Event
HEC event.
Source code in solnlib/modular_input/event.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
|
max_hec_event_length = 1000000
instance-attribute
class-attribute
¶
format_events(events, event_field='event')
classmethod
¶
Format events to list of string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
events |
List
|
List of events to format. |
required |
event_field |
str
|
Event field. |
'event'
|
Returns:
Type | Description |
---|---|
List
|
List of formatted events string, example:: [ ‘{“index”: “main”, … “event”: {“kk”: [1, 2, 3]}}\n {“index”: “main”, … “event”: {“kk”: [3, 2, 3]}}’, ‘…’ ] |
Source code in solnlib/modular_input/event.py
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
|
XMLEvent
¶
Bases: Event
XML event.
Source code in solnlib/modular_input/event.py
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
|
format_events(events)
classmethod
¶
Format events to list of string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
events |
List
|
List of events to format. |
required |
Returns:
Type | Description |
---|---|
List
|
List of formatted events string, example:: [
‘ |
Source code in solnlib/modular_input/event.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
|
event_writer.py¶
This module provides two kinds of event writers (ClassicEventWriter, HECEventWriter) to write Splunk modular input events.
__all__ = ['ClassicEventWriter', 'HECEventWriter']
module-attribute
¶
ClassicEventWriter
¶
Bases: EventWriter
Classic event writer.
Use sys.stdout as the output.
Examples:
>>> from solnlib.modular_input import event_writer
>>> ew = event_writer.ClassicEventWriter()
>>> ew.write_events([event1, event2])
Source code in solnlib/modular_input/event_writer.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
|
description = 'ClassicEventWriter'
instance-attribute
class-attribute
¶
__init__(lock=None)
¶
Initializes ClassicEventWriter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lock |
Union[threading.Lock, multiprocessing.Lock]
|
(optional) lock to exclusively access stdout. by default, it is None and it will use threading safe lock. if user would like to make the lock multiple-process safe, user should pass in multiprocessing.Lock() instead |
None
|
Source code in solnlib/modular_input/event_writer.py
123 124 125 126 127 128 129 130 131 132 133 134 135 |
|
create_event(data, time=None, index=None, host=None, source=None, sourcetype=None, fields=None, stanza=None, unbroken=False, done=False)
¶
Create a new XMLEvent object.
Source code in solnlib/modular_input/event_writer.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
|
write_events(events)
¶
Source code in solnlib/modular_input/event_writer.py
164 165 166 167 168 169 170 171 172 173 |
|
EventWriter
¶
Base class of event writer.
Source code in solnlib/modular_input/event_writer.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
|
description = 'EventWriter'
instance-attribute
class-attribute
¶
create_event(data, time=None, index=None, host=None, source=None, sourcetype=None, fields=None, stanza=None, unbroken=False, done=False)
abstractmethod
¶
Create a new event.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
dict
|
Event data. |
required |
time |
float
|
(optional) Event timestamp, default is None. |
None
|
index |
str
|
(optional) The index event will be written to, default is None. |
None
|
host |
str
|
(optional) Event host, default is None. |
None
|
source |
str
|
(optional) Event source, default is None. |
None
|
sourcetype |
str
|
(optional) Event sourcetype, default is None. |
None
|
fields |
dict
|
(optional) Event fields, default is None. |
None
|
stanza |
str
|
(optional) Event stanza name, default is None. |
None
|
unbroken |
bool
|
(optional) Event unbroken flag, default is False. It is only meaningful when for XMLEvent when using ClassicEventWriter. |
False
|
done |
bool
|
(optional) The last unbroken event, default is False. It is only meaningful when for XMLEvent when using ClassicEventWriter. |
False
|
Examples:
>>> ew = event_writer.HECEventWriter(...)
>>> event = ew.create_event(
>>> data='This is a test data.',
>>> time='%.3f' % 1372274622.493,
>>> index='main',
>>> host='localhost',
>>> source='Splunk',
>>> sourcetype='misc',
>>> fields={'accountid': '603514901691', 'Cloud': u'AWS'},
>>> stanza='test_scheme://test',
>>> unbroken=True,
>>> done=True)
Source code in solnlib/modular_input/event_writer.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
|
write_events(events)
abstractmethod
¶
Write events.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
events |
List
|
List of events to write. |
required |
Examples:
>>> from solnlib.modular_input import event_writer
>>> ew = event_writer.EventWriter(...)
>>> ew.write_events([event1, event2])
Source code in solnlib/modular_input/event_writer.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
|
HECEventWriter
¶
Bases: EventWriter
HEC event writer.
Use Splunk HEC as the output.
Examples:
>>> from solnlib.modular_input import event_writer
>>> ew = event_writer.HECEventWriter(hec_input_name, session_key)
>>> ew.write_events([event1, event2])
Source code in solnlib/modular_input/event_writer.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 |
|
HTTP_EVENT_COLLECTOR_ENDPOINT = '/services/collector'
instance-attribute
class-attribute
¶
HTTP_INPUT_CONFIG_ENDPOINT = '/servicesNS/nobody/splunk_httpinput/data/inputs/http'
instance-attribute
class-attribute
¶
SERVICE_UNAVAILABLE = 503
instance-attribute
class-attribute
¶
TOO_MANY_REQUESTS = 429
instance-attribute
class-attribute
¶
WRITE_EVENT_RETRIES = 5
instance-attribute
class-attribute
¶
description = 'HECEventWriter'
instance-attribute
class-attribute
¶
headers = [('Content-Type', 'application/json')]
instance-attribute
class-attribute
¶
logger = logger
instance-attribute
¶
__init__(hec_input_name, session_key, scheme=None, host=None, port=None, hec_uri=None, hec_token=None, global_settings_schema=True, logger=None, **context)
¶
Initializes HECEventWriter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hec_input_name |
str
|
Splunk HEC input name. |
required |
session_key |
str
|
Splunk access token. |
required |
scheme |
str
|
(optional) The access scheme, default is None. |
None
|
host |
str
|
(optional) The host name, default is None. |
None
|
port |
int
|
(optional) The port number, default is None. |
None
|
hec_uri |
str
|
(optional) If hec_uri and hec_token are provided, they will higher precedence than hec_input_name. |
None
|
hec_token |
str
|
(optional) HEC token. |
None
|
global_settings_schema |
bool
|
(optional) if True, scheme will be set based on HEC global settings, default False. |
True
|
logger |
logging.Logger
|
Logger object. |
None
|
context |
dict
|
Other configurations for Splunk rest client. |
{}
|
Source code in solnlib/modular_input/event_writer.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
|
create_event(data, time=None, index=None, host=None, source=None, sourcetype=None, fields=None, stanza=None, unbroken=False, done=False)
¶
Create a new HECEvent object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
dict
|
Event data. |
required |
time |
float
|
(optional) Event timestamp, default is None. |
None
|
index |
str
|
(optional) The index event will be written to, default is None. |
None
|
host |
str
|
(optional) Event host, default is None. |
None
|
source |
str
|
(optional) Event source, default is None. |
None
|
sourcetype |
str
|
(optional) Event sourcetype, default is None. |
None
|
fields |
dict
|
(optional) Event fields, default is None. |
None
|
stanza |
str
|
(optional) Event stanza name, default is None. |
None
|
unbroken |
bool
|
(optional) Event unbroken flag, default is False. It is only meaningful when for XMLEvent when using ClassicEventWriter. |
False
|
done |
bool
|
(optional) The last unbroken event, default is False. It is only meaningful when for XMLEvent when using ClassicEventWriter. |
False
|
Returns:
Type | Description |
---|---|
HECEvent
|
Created HECEvent. |
Source code in solnlib/modular_input/event_writer.py
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 |
|
create_from_input(hec_input_name, splunkd_uri, session_key, global_settings_schema=False, **context)
staticmethod
¶
Given HEC input stanza name, splunkd URI and splunkd session key, create HECEventWriter object. HEC URI and token etc will be discovered from HEC input stanza. When hitting HEC event limit, the underlying code will increase the HEC event limit automatically by calling corresponding REST API against splunkd_uri by using session_key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hec_input_name |
str
|
Splunk HEC input name. |
required |
splunkd_uri |
str
|
Splunkd URI, like https://localhost:8089 |
required |
session_key |
str
|
Splunkd access token. |
required |
global_settings_schema |
bool
|
(optional) if True, scheme will be set based on HEC global settings, default False. |
False
|
context |
dict
|
Other configurations. |
{}
|
Returns:
Type | Description |
---|---|
HECEventWriter
|
Created HECEventWriter. |
Source code in solnlib/modular_input/event_writer.py
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
|
create_from_token(hec_uri, hec_token, global_settings_schema=False, **context)
staticmethod
¶
Given HEC URI and HEC token, create HECEventWriter object. This function simplifies the standalone mode HECEventWriter usage (not in a modinput).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hec_uri |
str
|
HTTP Event Collector URI, like https://localhost:8088. |
required |
hec_token |
str
|
HTTP Event Collector token. |
required |
global_settings_schema |
bool
|
(optional) if True, scheme will be set based on HEC global settings, default False. |
False
|
context |
dict
|
Other configurations. |
{}
|
Returns:
Type | Description |
---|---|
HECEventWriter
|
Created HECEventWriter. |
Source code in solnlib/modular_input/event_writer.py
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
|
create_from_token_with_session_key(splunkd_uri, session_key, hec_uri, hec_token, global_settings_schema=False, **context)
staticmethod
¶
Given Splunkd URI, Splunkd session key, HEC URI and HEC token, create HECEventWriter object. When hitting HEC event limit, the event writer will increase the HEC event limit automatically by calling corresponding REST API against splunkd_uri by using session_key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
splunkd_uri |
str
|
Splunkd URI, like https://localhost:8089. |
required |
session_key |
str
|
Splunkd access token. |
required |
hec_uri |
str
|
Http Event Collector URI, like https://localhost:8088. |
required |
hec_token |
str
|
Http Event Collector token. |
required |
global_settings_schema |
bool
|
(optional) if True, scheme will be set based on HEC global settings, default False. |
False
|
context |
dict
|
Other configurations. |
{}
|
Returns:
Type | Description |
---|---|
HECEventWriter
|
Created HECEventWriter. |
Source code in solnlib/modular_input/event_writer.py
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
|
write_events(events, retries=WRITE_EVENT_RETRIES, event_field='event')
¶
Write events to index in bulk.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
events |
List
|
List of events. |
required |
retries |
int
|
Number of retries for writing events to index. |
WRITE_EVENT_RETRIES
|
event_field |
str
|
Event field. |
'event'
|
Source code in solnlib/modular_input/event_writer.py
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 |
|
modular_input.py¶
This module provides a base class of Splunk modular input.
__all__ = ['ModularInputException', 'ModularInput']
module-attribute
¶
ModularInput
¶
Base class of Splunk modular input.
It’s a base modular input, it should be inherited by sub modular input. For sub modular input, properties: ‘app’, ‘name’, ‘title’ and ‘description’ must be overriden, also there are some other optional properties can be overriden like: ‘use_external_validation’, ‘use_single_instance’, ‘use_kvstore_checkpointer’ and ‘use_hec_event_writer’.
Notes: If you set ‘KVStoreCheckpointer’ or ‘use_hec_event_writer’ to True, you must override the corresponding ‘kvstore_checkpointer_collection_name’ and ‘hec_input_name’.
Examples:
>>> class TestModularInput(ModularInput):
>>> app = 'TestApp'
>>> name = 'test_modular_input'
>>> title = 'Test modular input'
>>> description = 'This is a test modular input'
>>> use_external_validation = True
>>> use_single_instance = False
>>> use_kvstore_checkpointer = True
>>> kvstore_checkpointer_collection_name = 'TestCheckpoint'
>>> use_hec_event_writer = True
>>> hec_input_name = 'TestEventWriter'
>>>
>>> def extra_arguments(self):
>>> ... .. .
>>>
>>> def do_validation(self, parameters):
>>> ... .. .
>>>
>>> def do_run(self, inputs):
>>> ... .. .
>>>
>>> if __name__ == '__main__':
>>> md = TestModularInput()
>>> md.execute()
Source code in solnlib/modular_input/modular_input.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 |
|
app = None
instance-attribute
class-attribute
¶
checkpointer: checkpointer.Checkpointer
property
¶
Get checkpointer object.
The checkpointer returned depends on use_kvstore_checkpointer flag, if use_kvstore_checkpointer is true will return an KVStoreCheckpointer object else an FileCheckpointer object.
Returns:
Type | Description |
---|---|
checkpointer.Checkpointer
|
A checkpointer object. |
config_name = None
instance-attribute
¶
description = None
instance-attribute
class-attribute
¶
event_writer: event_writer.EventWriter
property
¶
Get event writer object.
The event writer returned depends on use_hec_event_writer flag, if use_hec_event_writer is true will return an HECEventWriter object else an ClassicEventWriter object.
Returns:
Type | Description |
---|---|
event_writer.EventWriter
|
Event writer object. |
hec_global_settings_schema = False
instance-attribute
class-attribute
¶
hec_input_name = None
instance-attribute
class-attribute
¶
kvstore_checkpointer_collection_name = None
instance-attribute
class-attribute
¶
name = None
instance-attribute
class-attribute
¶
server_host = None
instance-attribute
¶
server_host_name = None
instance-attribute
¶
server_port = None
instance-attribute
¶
server_scheme = None
instance-attribute
¶
server_uri = None
instance-attribute
¶
session_key = None
instance-attribute
¶
should_exit = False
instance-attribute
¶
title = None
instance-attribute
class-attribute
¶
use_external_validation = False
instance-attribute
class-attribute
¶
use_hec_event_writer = True
instance-attribute
class-attribute
¶
use_kvstore_checkpointer = True
instance-attribute
class-attribute
¶
use_single_instance = False
instance-attribute
class-attribute
¶
__init__()
¶
Source code in solnlib/modular_input/modular_input.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
|
do_run(inputs)
abstractmethod
¶
Runs this modular input.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
dict
|
Command line arguments passed to this modular input. For single instance mode, inputs like::
For multiple instance mode, inputs like::
|
required |
Source code in solnlib/modular_input/modular_input.py
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
|
do_validation(parameters)
¶
Handles external validation for modular input kinds.
When Splunk calls a modular input script in validation mode, it will pass in an XML document giving information about the Splunk instance (so you can call back into it if needed) and the name and parameters of the proposed input. If this function does not throw an exception, the validation is assumed to succeed. Otherwise any errors thrown will be turned into a string and logged back to Splunk.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parameters |
The parameters of input passed by splunkd. |
required |
Raises:
Type | Description |
---|---|
Exception
|
If validation is failed. |
Source code in solnlib/modular_input/modular_input.py
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
|
execute()
¶
Modular input entry.
Examples:
>>> class TestModularInput(ModularInput):
>>> ... .. .
>>>
>>> if __name__ == '__main__':
>>> md = TestModularInput()
>>> md.execute()
Source code in solnlib/modular_input/modular_input.py
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 |
|
extra_arguments()
¶
Extra arguments for modular input.
Default implementation is returning an empty list.
Returns:
Type | Description |
---|---|
List
|
List of arguments like:: [ { ‘name’: ‘arg1’, ‘title’: ‘arg1 title’, ‘description’: ‘arg1 description’, ‘validation’: ‘arg1 validation statement’, ‘data_type’: Argument.data_type_string, ‘required_on_edit’: False, ‘required_on_create’: False }, {…}, {…} ] |
Source code in solnlib/modular_input/modular_input.py
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
|
get_input_definition()
¶
Get input definition.
This method can be overwritten to get input definition from
other input instead stdin
.
Returns:
Type | Description |
---|---|
dict
|
A dict object must contain example: { ‘metadata’: { ‘session_key’: ‘iCKPS0cvmpyeJk…sdaf’, ‘server_host’: ‘test-test.com’, ‘server_uri’: ‘https://127.0.0.1:8089’, ‘checkpoint_dir’: ‘/tmp’ }, inputs: { ‘stanza1’: {‘arg1’: value1, ‘arg2’: value2}, ‘stanza2’: {‘arg1’: value1, ‘arg2’: value2} } } |
Source code in solnlib/modular_input/modular_input.py
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 |
|
get_validation_definition()
¶
Get validation definition.
This method can be overwritten to get validation definition from
other input instead stdin
.
Returns:
Type | Description |
---|---|
dict
|
A dict object must contains example: { ‘metadata’: { ‘session_key’: ‘iCKPS0cvmpyeJk…sdaf’, ‘server_host’: ‘test-test.com’, ‘server_uri’: ‘https://127.0.0.1:8089’, ‘checkpoint_dir’: ‘/tmp’ }, parameters: {‘args1’: value1, ‘args2’: value2} } |
Source code in solnlib/modular_input/modular_input.py
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
|
register_orphan_handler(handler, *args)
¶
Register orphan process handler.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
handler |
Callable
|
Teardown signal handler. |
required |
args |
Arguments to the handler. |
()
|
Examples:
>>> mi = ModularInput(...)
>>> def orphan_handler(arg1, arg2, ...):
>>> ...
>>> mi.register_orphan_handler(orphan_handler, arg1, arg2, ...)
Source code in solnlib/modular_input/modular_input.py
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
|
register_teardown_handler(handler, *args)
¶
Register teardown signal handler.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
handler |
Callable
|
Teardown signal handler. |
required |
args |
Arguments to the handler. |
()
|
Examples:
>>> mi = ModularInput(...)
>>> def teardown_handler(arg1, arg2, ...):
>>> ...
>>> mi.register_teardown_handler(teardown_handler, arg1, arg2, ...)
Source code in solnlib/modular_input/modular_input.py
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
|
ModularInputException
¶
Bases: Exception
Exception for ModularInput class.
Source code in solnlib/modular_input/modular_input.py
41 42 43 44 |
|
Ended: modular_input
acl.py¶
This module contains interfaces that support CRUD operations on ACL.
__all__ = ['ACLException', 'ACLManager']
module-attribute
¶
ACLException
¶
Bases: Exception
Exception raised by ACLManager.
Source code in solnlib/acl.py
30 31 32 33 |
|
ACLManager
¶
ACL manager.
Examples:
>>> import solnlib.acl as sacl
>>> saclm = sacl.ACLManager(session_key, 'Splunk_TA_test')
>>> saclm.get('data/transforms/extractions')
>>> saclm.update('data/transforms/extractions/_acl',
perms_read=['*'], perms_write=['*'])
Source code in solnlib/acl.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
|
__init__(session_key, app, owner='nobody', scheme=None, host=None, port=None, **context)
¶
Initializes ACLManager.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
session_key |
str
|
Splunk access token. |
required |
app |
str
|
App name of namespace. |
required |
owner |
str
|
(optional) Owner of namespace, default is |
'nobody'
|
scheme |
str
|
(optional) The access scheme, default is None. |
None
|
host |
str
|
(optional) The host name, default is None. |
None
|
port |
int
|
(optional) The port number, default is None. |
None
|
context |
dict
|
Other configurations for Splunk rest client. |
{}
|
Source code in solnlib/acl.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
|
get(path)
¶
Get ACL of /servicesNS/{owner
}/{app
}/{path
}.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
Path of ACL relative to /servicesNS/{ |
required |
Returns:
Type | Description |
---|---|
dict
|
A dict contains ACL. |
Raises:
Type | Description |
---|---|
ACLException
|
If |
Examples:
>>> aclm = acl.ACLManager(session_key, 'Splunk_TA_test')
>>> perms = aclm.get('data/transforms/extractions/_acl')
Source code in solnlib/acl.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
|
update(path, owner=None, perms_read=None, perms_write=None)
¶
Update ACL of /servicesNS/{owner
}/{app
}/{path
}.
If the ACL is per-entity (ends in /acl), owner can be reassigned. If the acl is endpoint-level (ends in _acl), owner will be ignored. The ‘sharing’ setting is always retrieved from the current.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
Path of ACL relative to /servicesNS/{owner}/{app}. MUST end with /acl or /_acl indicating whether the permission is applied at the per-entity level or endpoint level respectively. |
required |
owner |
str
|
(optional) New owner of ACL, default is |
None
|
perms_read |
List
|
(optional) List of roles ([‘*’] for all roles). If unspecified we will POST with current (if available) perms.read, default is None. |
None
|
perms_write |
List
|
(optional) List of roles ([‘*’] for all roles). If unspecified we will POST with current (if available) perms.write, default is None. |
None
|
Returns:
Type | Description |
---|---|
dict
|
A dict contains ACL after update. |
Raises:
Type | Description |
---|---|
ACLException
|
If |
Examples:
>>> aclm = acl.ACLManager(session_key, 'Splunk_TA_test')
>>> perms = aclm.update('data/transforms/extractions/_acl',
perms_read=['admin'], perms_write=['admin'])
Source code in solnlib/acl.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
|
credentials.py¶
This module contains Splunk credential related interfaces.
__all__ = ['CredentialException', 'CredentialNotExistException', 'CredentialManager', 'get_session_key']
module-attribute
¶
CredentialException
¶
Bases: Exception
General exception regarding credentials.
Source code in solnlib/credentials.py
39 40 41 42 |
|
CredentialManager
¶
Credential manager.
Examples:
>>> from solnlib import credentials
>>> cm = credentials.CredentialManager(session_key,
'Splunk_TA_test',
realm='realm_test')
Source code in solnlib/credentials.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
|
END_MARK = '``splunk_cred_sep``S``splunk_cred_sep``P``splunk_cred_sep``L``splunk_cred_sep``U``splunk_cred_sep``N``splunk_cred_sep``K``splunk_cred_sep``'
instance-attribute
class-attribute
¶
SEP = '``splunk_cred_sep``'
instance-attribute
class-attribute
¶
SPLUNK_CRED_LEN_LIMIT = 255
instance-attribute
class-attribute
¶
service = rest_client.SplunkRestClient(session_key, app, owner=owner, scheme=scheme, host=host, port=port, None=context)
instance-attribute
¶
__init__(session_key, app, owner='nobody', realm=None, scheme=None, host=None, port=None, **context)
¶
Initializes CredentialManager.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
session_key |
str
|
Splunk access token. |
required |
app |
str
|
App name of namespace. |
required |
owner |
str
|
(optional) Owner of namespace, default is |
'nobody'
|
realm |
str
|
(optional) Realm of credential, default is None. |
None
|
scheme |
str
|
(optional) The access scheme, default is None. |
None
|
host |
str
|
(optional) The host name, default is None. |
None
|
port |
int
|
(optional) The port number, default is None. |
None
|
context |
dict
|
Other configurations for Splunk rest client. |
{}
|
Source code in solnlib/credentials.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
|
delete_password(user)
¶
Delete password.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
user |
str
|
User name. |
required |
Raises:
Type | Description |
---|---|
CredentialNotExistException
|
If password of realm:user doesn’t exist. |
Examples:
>>> from solnlib import credentials
>>> cm = credentials.CredentialManager(session_key,
'Splunk_TA_test',
realm='realm_test')
>>> cm.delete_password('testuser1')
Source code in solnlib/credentials.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
|
get_clear_passwords()
¶
Returns all passwords in the “clear” format.
Source code in solnlib/credentials.py
253 254 255 256 257 258 259 260 |
|
get_clear_passwords_in_realm()
¶
Returns all passwords within the realm in the “clear” format.
Source code in solnlib/credentials.py
262 263 264 265 266 267 |
|
get_password(user)
¶
Get password.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
user |
str
|
User name. |
required |
Returns:
Type | Description |
---|---|
str
|
Clear user password. |
Raises:
Type | Description |
---|---|
CredentialNotExistException
|
If password for ‘realm:user’ doesn’t exist. |
Examples:
>>> from solnlib import credentials
>>> cm = credentials.CredentialManager(session_key,
'Splunk_TA_test',
realm='realm_test')
>>> cm.get_password('testuser2')
Source code in solnlib/credentials.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
|
get_raw_passwords()
¶
Returns all passwords in the “raw” format.
Source code in solnlib/credentials.py
239 240 241 242 243 244 245 |
|
get_raw_passwords_in_realm()
¶
Returns all passwords within the realm in the “raw” format.
Source code in solnlib/credentials.py
247 248 249 250 251 |
|
set_password(user, password)
¶
Set password.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
user |
str
|
User name. |
required |
password |
str
|
User password. |
required |
Examples:
>>> from solnlib import credentials
>>> cm = credentials.CredentialManager(session_key,
'Splunk_TA_test',
realm='realm_test')
>>> cm.set_password('testuser1', 'password1')
Source code in solnlib/credentials.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
|
CredentialNotExistException
¶
Bases: Exception
Exception is raised when credentials do not exist.
Source code in solnlib/credentials.py
45 46 47 48 |
|
get_session_key(username, password, scheme=None, host=None, port=None, **context)
¶
Get splunkd access token.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
username |
str
|
The Splunk account username, which is used to authenticate the Splunk instance. |
required |
password |
str
|
The Splunk account password. |
required |
scheme |
str
|
(optional) The access scheme, default is None. |
None
|
host |
str
|
(optional) The host name, default is None. |
None
|
port |
int
|
(optional) The port number, default is None. |
None
|
context |
dict
|
Other configurations for Splunk rest client. |
{}
|
Returns:
Type | Description |
---|---|
str
|
Splunk session key. |
Raises:
Type | Description |
---|---|
CredentialException
|
If username/password are invalid. |
ValueError
|
if scheme, host or port are invalid. |
Examples:
>>> get_session_key('user', 'password')
Source code in solnlib/credentials.py
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
|
conf_manager.py¶
This module contains simple interfaces for Splunk config file management, you can update/get/delete stanzas and encrypt/decrypt some fields of stanza automatically.
__all__ = ['ConfFile', 'ConfManager']
module-attribute
¶
ConfFile
¶
Configuration file.
Source code in solnlib/conf_manager.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
|
ENCRYPTED_TOKEN = '******'
instance-attribute
class-attribute
¶
reserved_keys = ('userName', 'appName')
instance-attribute
class-attribute
¶
__init__(name, conf, session_key, app, owner='nobody', scheme=None, host=None, port=None, realm=None, **context)
¶
Initializes ConfFile.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
Configuration file name. |
required |
conf |
client.ConfigurationFile
|
Configuration file object. |
required |
session_key |
str
|
Splunk access token. |
required |
app |
str
|
App name of namespace. |
required |
owner |
str
|
(optional) Owner of namespace, default is |
'nobody'
|
scheme |
str
|
(optional) The access scheme, default is None. |
None
|
host |
str
|
(optional) The host name, default is None. |
None
|
port |
int
|
(optional) The port number, default is None. |
None
|
realm |
str
|
(optional) Realm of credential, default is None. |
None
|
context |
dict
|
Other configurations for Splunk rest client. |
{}
|
Source code in solnlib/conf_manager.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
|
delete(stanza_name)
¶
Delete stanza.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stanza_name |
str
|
Stanza name to delete. |
required |
Raises:
Type | Description |
---|---|
ConfStanzaNotExistException
|
If stanza does not exist. |
Examples:
>>> from solnlib import conf_manager
>>> cfm = conf_manager.ConfManager(session_key,
'Splunk_TA_test')
>>> conf = cfm.get_conf('test')
>>> conf.delete('test_stanza')
Source code in solnlib/conf_manager.py
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
|
get(stanza_name, only_current_app=False)
¶
Get stanza from configuration file.
Result is like
{ ‘disabled’: ‘0’, ‘eai:appName’: ‘solnlib_demo’, ‘eai:userName’: ‘nobody’, ‘k1’: ‘1’, ‘k2’: ‘2’ }
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stanza_name |
str
|
Stanza name. |
required |
only_current_app |
bool
|
Only include current app. |
False
|
Returns:
Type | Description |
---|---|
dict
|
Stanza. |
Raises:
Type | Description |
---|---|
ConfStanzaNotExistException
|
If stanza does not exist. |
Examples:
>>> from solnlib import conf_manager
>>> cfm = conf_manager.ConfManager(session_key,
'Splunk_TA_test')
>>> conf = cfm.get_conf('test')
>>> conf.get('test_stanza')
Source code in solnlib/conf_manager.py
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
|
get_all(only_current_app=False)
¶
Get all stanzas from configuration file.
Result is like
{ ‘test’: { ‘disabled’: ‘0’, ‘eai:appName’: ‘solnlib_demo’, ‘eai:userName’: ‘nobody’, ‘k1’: ‘1’, ‘k2’: ‘2’ } }
Parameters:
Name | Type | Description | Default |
---|---|---|---|
only_current_app |
bool
|
Only include current app. |
False
|
Returns:
Type | Description |
---|---|
dict
|
Dict of stanzas. |
Examples:
>>> from solnlib import conf_manager
>>> cfm = conf_manager.ConfManager(session_key,
'Splunk_TA_test')
>>> conf = cfm.get_conf('test')
>>> conf.get_all()
Source code in solnlib/conf_manager.py
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
|
reload()
¶
Reload configuration file.
Examples:
>>> from solnlib import conf_manager
>>> cfm = conf_manager.ConfManager(session_key,
'Splunk_TA_test')
>>> conf = cfm.get_conf('test')
>>> conf.reload()
Source code in solnlib/conf_manager.py
348 349 350 351 352 353 354 355 356 357 358 359 360 |
|
stanza_exist(stanza_name)
¶
Check whether stanza exists.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stanza_name |
str
|
Stanza name. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if stanza exists else False. |
Examples:
>>> from solnlib import conf_manager
>>> cfm = conf_manager.ConfManager(session_key,
'Splunk_TA_test')
>>> conf = cfm.get_conf('test')
>>> conf.stanza_exist('test_stanza')
Source code in solnlib/conf_manager.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
|
update(stanza_name, stanza, encrypt_keys=None)
¶
Update stanza.
It will try to encrypt the credential automatically fist if encrypt_keys are not None else keep stanza untouched.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stanza_name |
str
|
Stanza name. |
required |
stanza |
dict
|
Stanza to update. |
required |
encrypt_keys |
List[str]
|
Field names to encrypt. |
None
|
Examples:
>>> from solnlib import conf_manager
>>> cfm = conf_manager.ConfManager(session_key,
'Splunk_TA_test')
>>> conf = cfm.get_conf('test')
>>> conf.update('test_stanza', {'k1': 1, 'k2': 2}, ['k1'])
Source code in solnlib/conf_manager.py
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
|
ConfManager
¶
Configuration file manager.
Examples:
>>> from solnlib import conf_manager
>>> cfm = conf_manager.ConfManager(session_key,
'Splunk_TA_test')
Examples:
If stanza in passwords.conf is formatted as below:
credential:__REST_CREDENTIAL__#Splunk_TA_test#configs/conf-CONF_FILENAME:STANZA_NAME``splunk_cred_sep``1:
>>> from solnlib import conf_manager
>>> cfm = conf_manager.ConfManager(
session_key,
'Splunk_TA_test',
realm='__REST_CREDENTIAL__#Splunk_TA_test#configs/conf-CONF_FILENAME'
)
Source code in solnlib/conf_manager.py
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 |
|
__init__(session_key, app, owner='nobody', scheme=None, host=None, port=None, realm=None, **context)
¶
Initializes ConfManager.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
session_key |
str
|
Splunk access token. |
required |
app |
str
|
App name of namespace. |
required |
owner |
str
|
(optional) Owner of namespace, default is |
'nobody'
|
scheme |
str
|
(optional) The access scheme, default is None. |
None
|
host |
str
|
(optional) The host name, default is None. |
None
|
port |
int
|
(optional) The port number, default is None. |
None
|
realm |
str
|
(optional) Realm of credential, default is None. |
None
|
context |
dict
|
Other configurations for Splunk rest client. |
{}
|
Source code in solnlib/conf_manager.py
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 |
|
create_conf(name)
¶
Create conf file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
Conf file name. |
required |
Returns:
Type | Description |
---|---|
ConfFile
|
Conf file object. |
Source code in solnlib/conf_manager.py
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 |
|
get_conf(name, refresh=False)
¶
Get conf file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
Conf file name. |
required |
refresh |
bool
|
(optional) Flag to refresh conf file list, default is False. |
False
|
Returns:
Type | Description |
---|---|
ConfFile
|
Conf file object. |
Raises:
Type | Description |
---|---|
ConfManagerException
|
If |
Source code in solnlib/conf_manager.py
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 |
|
get_log_level(*, logger, session_key, app_name, conf_name, log_stanza='logging', log_level_field='loglevel', default_log_level='INFO')
¶
This function returns the log level for the addon from configuration file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
logger |
logging.Logger
|
Logger. |
required |
session_key |
str
|
Splunk access token. |
required |
app_name |
str
|
Add-on name. |
required |
conf_name |
str
|
Configuration file name where logging stanza is. |
required |
log_stanza |
str
|
Logging stanza to define |
'logging'
|
log_level_field |
str
|
Logging level field name under logging stanza. |
'loglevel'
|
default_log_level |
str
|
Default log level to return in case of errors. |
'INFO'
|
Returns:
Type | Description |
---|---|
str
|
Log level defined under |
str
|
file. In case of any error, |
Examples:
>>> from solnlib import conf_manager
>>> log_level = conf_manager.get_log_level(
>>> logger,
>>> "session_key",
>>> "ADDON_NAME",
>>> "splunk_ta_addon_settings",
>>> )
Source code in solnlib/conf_manager.py
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 |
|
get_proxy_dict(logger, session_key, app_name, conf_name, proxy_stanza='proxy', **kwargs)
¶
This function returns the proxy settings for the addon from configuration file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
logger |
logging.Logger
|
Logger. |
required |
session_key |
str
|
Splunk access token. |
required |
app_name |
str
|
Add-on name. |
required |
conf_name |
str
|
Configuration file name where logging stanza is. |
required |
proxy_stanza |
str
|
Proxy stanza that would contain the Proxy details |
'proxy'
|
Returns:
Type | Description |
---|---|
Union[Dict[str, str], NoReturn]
|
A dictionary is returned with stanza details present in the file. |
Union[Dict[str, str], NoReturn]
|
The keys related to |
Examples:
>>> from solnlib import conf_manager
>>> proxy_details = conf_manager.get_proxy_dict(
>>> logger,
>>> "session_key",
>>> "ADDON_NAME",
>>> "splunk_ta_addon_settings",
>>> )
Source code in solnlib/conf_manager.py
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 |
|
file_monitor.py¶
This module contains file monitoring class that can be used to check files change periodically and call callback function to handle properly when detecting files change.
__all__ = ['FileChangesChecker', 'FileMonitor']
module-attribute
¶
FileChangesChecker
¶
Files change checker.
Source code in solnlib/file_monitor.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
|
file_mtimes = {file_name: None for file_name in self._files}
instance-attribute
¶
__init__(callback, files)
¶
Initializes FileChangesChecker.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
callback |
Callable[[List[str]], Any]
|
Callback function for files change. |
required |
files |
List
|
Files to be monitored with full path. |
required |
Source code in solnlib/file_monitor.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
|
check_changes()
¶
Check files change.
If some files are changed and callback function is not None, call callback function to handle files change.
Returns:
Type | Description |
---|---|
bool
|
True if files changed else False |
Source code in solnlib/file_monitor.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
|
FileMonitor
¶
Files change monitor.
Monitor files change in a separated thread and call callback when there is files change.
Examples:
>>> import solnlib.file_monitor as fm
>>> fm = fm.FileMonitor(fm_callback, files_list, 5)
>>> fm.start()
Source code in solnlib/file_monitor.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
|
__init__(callback, files, interval=1)
¶
Initializes FileMonitor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
callback |
Callable[[List[str]], Any]
|
Callback for handling files change. |
required |
files |
List
|
Files to monitor. |
required |
interval |
int
|
Interval to check files change. |
1
|
Source code in solnlib/file_monitor.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
|
start()
¶
Start file monitor.
Start a background thread to monitor files change.
Source code in solnlib/file_monitor.py
106 107 108 109 110 111 112 113 114 115 116 |
|
stop()
¶
Stop file monitor.
Stop the background thread to monitor files change.
Source code in solnlib/file_monitor.py
118 119 120 121 122 123 124 |
|
hec_config.py¶
__all__ = ['HECConfig']
module-attribute
¶
HECConfig
¶
HTTP Event Collector configuration.
Source code in solnlib/hec_config.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
|
input_type = 'http'
instance-attribute
class-attribute
¶
__init__(session_key, scheme=None, host=None, port=None, **context)
¶
Initializes HECConfig.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
session_key |
str
|
Splunk access token. |
required |
scheme |
str
|
(optional) The access scheme, default is None. |
None
|
host |
str
|
(optional) The host name, default is None. |
None
|
port |
int
|
(optional) The port number, default is None. |
None
|
context |
dict
|
Other configurations for Splunk rest client. |
{}
|
Source code in solnlib/hec_config.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
|
create_input(name, stanza)
¶
Create http data input.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
HTTP data input name. |
required |
stanza |
dict
|
Data input stanza content. |
required |
Returns:
Type | Description |
---|---|
dict
|
Created input. |
Examples:
>>> from solnlib.hec_config import HECConfig
>>> hec = HECConfig(session_key)
>>> hec.create_input('my_hec_data_input',
{'index': 'main', 'sourcetype': 'hec'})
Source code in solnlib/hec_config.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
|
delete_input(name)
¶
Delete http data input.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
HTTP data input name. |
required |
Source code in solnlib/hec_config.py
127 128 129 130 131 132 133 134 135 136 137 138 |
|
get_input(name)
¶
Get http data input.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
HTTP event collector data input name. |
required |
Returns:
Type | Description |
---|---|
dict
|
HTTP event collector data input config dict. |
Source code in solnlib/hec_config.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
|
get_limits()
¶
Get HTTP input limits.
Returns:
Type | Description |
---|---|
dict
|
HTTP input limits. |
Source code in solnlib/hec_config.py
163 164 165 166 167 168 169 170 171 |
|
get_settings()
¶
Get http data input global settings.
Returns:
Type | Description |
---|---|
dict
|
HTTP global settings, for example: { ‘enableSSL’: 1, ‘disabled’: 0, ‘useDeploymentServer’: 0, ‘port’: 8088 } |
Source code in solnlib/hec_config.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
|
set_limits(limits)
¶
Set HTTP input limits.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
limits |
dict
|
HTTP input limits. |
required |
Source code in solnlib/hec_config.py
173 174 175 176 177 178 179 180 181 182 |
|
update_input(name, stanza)
¶
Update http data input.
It will create if the data input doesn’t exist.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
HTTP data input name. |
required |
stanza |
dict
|
Data input stanza. |
required |
Examples:
>>> from solnlib import HEConfig
>>> hec = HECConfig(session_key)
>>> hec.update_input('my_hec_data_input',
{'index': 'main', 'sourcetype': 'hec2'})
Source code in solnlib/hec_config.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
|
update_settings(settings)
¶
Update http data input global settings.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
settings |
dict
|
HTTP global settings. |
required |
Source code in solnlib/hec_config.py
73 74 75 76 77 78 79 80 81 82 |
|
log.py¶
This module provides log functionalities.
__all__ = ['log_enter_exit', 'LogException', 'Logs']
module-attribute
¶
log_authentication_error = partial(_base_error_log, exe_label='Authentication Error')
module-attribute
¶
log_configuration_error = partial(_base_error_log, exe_label='Configuration Error')
module-attribute
¶
log_connection_error = partial(_base_error_log, exe_label='Connection Error')
module-attribute
¶
log_permission_error = partial(_base_error_log, exe_label='Permission Error')
module-attribute
¶
log_server_error = partial(_base_error_log, exe_label='Server Error')
module-attribute
¶
LogException
¶
Bases: Exception
Exception raised by Logs class.
Source code in solnlib/log.py
60 61 62 63 |
|
Logs
¶
A singleton class that manage all kinds of logger.
Examples:
>>> from solnlib import log
>>> log.Logs.set_context(directory='/var/log/test',
namespace='test')
>>> logger = log.Logs().get_logger('mymodule')
>>> logger.set_level(logging.DEBUG)
>>> logger.debug('a debug log')
Source code in solnlib/log.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
|
__init__()
¶
Source code in solnlib/log.py
160 161 162 |
|
get_logger(name)
¶
Get logger with the name of name
.
If logger with the name of name
exists just return else create a new
logger with the name of name
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
Logger name, it will be used as log file name too. |
required |
Returns:
Type | Description |
---|---|
logging.Logger
|
A named logger. |
Source code in solnlib/log.py
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
|
set_context(**context)
classmethod
¶
Set log context.
List of keyword arguments
directory: Log directory, default is splunk log root directory.
namespace: Logger namespace, default is None.
log_format: Log format, default is _default_log_format
.
log_level: Log level, default is logging.INFO.
max_bytes: The maximum log file size before rollover, default is 25000000.
backup_count: The number of log files to retain,default is 5.
root_logger_log_file: Root logger log file name, default is ‘solnlib’ .
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context |
dict
|
Keyword arguments. See list of arguments above. |
{}
|
Source code in solnlib/log.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
|
set_level(level, name=None)
¶
Set log level of logger.
Set log level of all logger if name
is None else of
logger with the name of name
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
level |
int
|
Log level to set. |
required |
name |
str
|
The name of logger, default is None. |
None
|
Source code in solnlib/log.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
|
events_ingested(logger, modular_input_name, sourcetype, n_events, index, account=None, host=None, license_usage_source=None)
¶
Specific function to log the basic information of events ingested for the monitoring dashboard.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
logger |
logging.Logger
|
Add-on logger. |
required |
modular_input_name |
str
|
Full name of the modular input. It needs to be in a format |
required |
sourcetype |
str
|
Source type used to write event. |
required |
n_events |
int
|
Number of ingested events. |
required |
index |
str
|
Index used to write event. |
required |
license_usage_source |
str
|
source used to match data with license_usage.log. |
None
|
account |
str
|
Account used to write event. (optional) |
None
|
host |
str
|
Host used to write event. (optional) |
None
|
Source code in solnlib/log.py
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
|
log_enter_exit(logger)
¶
Decorator for logger to log function enter and exit.
This decorator will generate a lot of debug log, please add this only when it is required.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
logger |
logging.Logger
|
Logger to decorate. |
required |
Examples:
>>> @log_enter_exit
>>> def myfunc():
>>> doSomething()
Source code in solnlib/log.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
|
log_event(logger, key_values, log_level=logging.INFO)
¶
General function to log any event in key-value format.
Source code in solnlib/log.py
225 226 227 228 229 230 |
|
log_exception(logger, e, exc_label, full_msg=True, msg_before=None, msg_after=None, log_level=logging.ERROR)
¶
General function to log exceptions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
logger |
logging.Logger
|
Add-on logger. |
required |
e |
Exception
|
Exception to log. |
required |
exc_label |
str
|
label for the error to categorize it. |
required |
full_msg |
bool
|
if set to True, full traceback will be logged. Default: True |
True
|
msg_before |
str
|
custom message before exception traceback. Default: None |
None
|
msg_after |
str
|
custom message after exception traceback. Default: None |
None
|
log_level |
int
|
Log level to log exception. Default: ERROR. |
logging.ERROR
|
Source code in solnlib/log.py
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
|
modular_input_end(logger, modular_input_name)
¶
Specific function to log the end of the modular input.
Source code in solnlib/log.py
244 245 246 247 248 249 250 251 252 |
|
modular_input_start(logger, modular_input_name)
¶
Specific function to log the start of the modular input.
Source code in solnlib/log.py
233 234 235 236 237 238 239 240 241 |
|
net_utils.py¶
Net utilities.
__all__ = ['resolve_hostname', 'validate_scheme_host_port']
module-attribute
¶
is_valid_hostname(hostname)
¶
Validate a host name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hostname |
str
|
host name to validate. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if is valid else False. |
Source code in solnlib/net_utils.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
|
is_valid_ip(addr)
¶
Validate an IPV4 address.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
addr |
str
|
IP address to validate. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if is valid else False. |
Source code in solnlib/net_utils.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
|
is_valid_port(port)
¶
Validate a port.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
port |
Union[str, int]
|
port to validate. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if is valid else False. |
Source code in solnlib/net_utils.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
|
is_valid_scheme(scheme)
¶
Validate a scheme.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scheme |
str
|
scheme to validate. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if is valid else False. |
Source code in solnlib/net_utils.py
126 127 128 129 130 131 132 133 134 135 136 |
|
resolve_hostname(addr)
¶
Try to resolve an IP to a host name and returns None on common failures.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
addr |
str
|
IP address to resolve. |
required |
Returns:
Type | Description |
---|---|
Optional[str]
|
Host name if success else None. |
Raises:
Type | Description |
---|---|
ValueError
|
If |
Source code in solnlib/net_utils.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
|
validate_scheme_host_port(scheme, host, port)
¶
Validates scheme, host and port.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scheme |
str
|
scheme to validate. |
required |
host |
str
|
hostname to validate. |
required |
port |
Union[str, int]
|
port to validate. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
if scheme, host or port are invalid. |
Source code in solnlib/net_utils.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
|
orphan_process_monitor.py¶
Orphan process monitor.
__all__ = ['OrphanProcessChecker', 'OrphanProcessMonitor']
module-attribute
¶
OrphanProcessChecker
¶
Orphan process checker.
Only work for Linux platform. On Windows platform, is_orphan is always False and there is no need to do this monitoring on Windows.
Source code in solnlib/orphan_process_monitor.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
|
__init__(callback=None)
¶
Initializes OrphanProcessChecker.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
callback |
Callable
|
(optional) Callback for orphan process. |
None
|
Source code in solnlib/orphan_process_monitor.py
34 35 36 37 38 39 40 41 42 43 44 |
|
check_orphan()
¶
Check if the process becomes orphan.
If the process becomes orphan then call callback function to handle properly.
Returns:
Type | Description |
---|---|
bool
|
True for orphan process else False. |
Source code in solnlib/orphan_process_monitor.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
|
is_orphan()
¶
Check process is orphan.
For windows platform just return False.
Returns:
Type | Description |
---|---|
bool
|
True for orphan process else False. |
Source code in solnlib/orphan_process_monitor.py
46 47 48 49 50 51 52 53 54 55 56 57 |
|
OrphanProcessMonitor
¶
Orphan process monitor.
Check if process become orphan in background thread per interval and call callback if process become orphan.
Source code in solnlib/orphan_process_monitor.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
|
__init__(callback, interval=1)
¶
Initializes OrphanProcessMonitor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
callback |
Callable
|
Callback for orphan process monitor. |
required |
interval |
int
|
(optional) Interval to monitor. |
1
|
Source code in solnlib/orphan_process_monitor.py
82 83 84 85 86 87 88 89 90 91 92 93 |
|
start()
¶
Start orphan process monitor.
Source code in solnlib/orphan_process_monitor.py
95 96 97 98 99 100 101 102 |
|
stop()
¶
Stop orphan process monitor.
Source code in solnlib/orphan_process_monitor.py
104 105 106 107 108 109 110 |
|
pattern.py¶
This module provides some common used patterns.
__all__ = ['Singleton']
module-attribute
¶
Singleton
¶
Bases: type
Singleton meta class.
Examples:
>>> class Test(object):
>>> __metaclass__ = Singleton
>>>
>>> def __init__(self):
>>> pass
Source code in solnlib/pattern.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
__call__(*args, **kwargs)
¶
Source code in solnlib/pattern.py
37 38 39 40 |
|
__init__(name, bases, attrs)
¶
Source code in solnlib/pattern.py
33 34 35 |
|
server_info.py¶
This module contains Splunk server info related functionalities.
__all__ = ['ServerInfo', 'ServerInfoException']
module-attribute
¶
ServerInfo
¶
This class is a wrapper of Splunk server info.
Source code in solnlib/server_info.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
|
SHC_CAPTAIN_INFO_ENDPOINT = '/services/shcluster/captain/info'
instance-attribute
class-attribute
¶
SHC_MEMBER_ENDPOINT = '/services/shcluster/member/members'
instance-attribute
class-attribute
¶
guid: str
property
¶
Get guid for the server.
Returns:
Type | Description |
---|---|
str
|
Server GUID. |
server_name: str
property
¶
Get server name.
Returns:
Type | Description |
---|---|
str
|
Server name. |
version: str
property
¶
Get Splunk server version.
Returns:
Type | Description |
---|---|
str
|
Splunk version. |
__init__(session_key, scheme=None, host=None, port=None, **context)
¶
Initializes ServerInfo.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
session_key |
str
|
Splunk access token. |
required |
scheme |
Optional[str]
|
The access scheme, default is None. |
None
|
host |
Optional[str]
|
The host name, default is None. |
None
|
port |
Optional[int]
|
The port number, default is None. |
None
|
context |
Any
|
Other configurations for Splunk rest client. |
{}
|
Source code in solnlib/server_info.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
|
captain_info()
¶
Get captain information.
Raises:
Type | Description |
---|---|
ServerInfoException
|
If there is SHC is not enabled. |
Returns:
Type | Description |
---|---|
dict
|
Captain information. |
Source code in solnlib/server_info.py
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
|
from_server_uri(server_uri, session_key, **context)
classmethod
¶
Creates ServerInfo class using server_uri and session_key.
Note: splunktalib uses these parameters to create it’s ServerInfo class, so this method should ease the transition from splunktalib to solnlib.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
server_uri |
str
|
splunkd URI. |
required |
session_key |
str
|
Splunk access token. |
required |
context |
Any
|
Other configurations for Splunk rest client. |
{}
|
Returns:
Type | Description |
---|---|
ServerInfo
|
An instance of |
Raises:
Type | Description |
---|---|
ValueError
|
server_uri is in the wrong format. |
Source code in solnlib/server_info.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
|
get_shc_members()
¶
Get SHC members.
Raises:
Type | Description |
---|---|
ServerInfoException
|
If this server has no SHC members. |
Returns:
Type | Description |
---|---|
list
|
List of SHC members [(label, peer_scheme_host_port) …]. |
Source code in solnlib/server_info.py
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
|
is_captain()
¶
Check if this server is SHC captain.
Note during a rolling start of SH members, the captain may be changed
from machine to machine. To avoid the race condition, client may need
do necessary sleep and then poll is_captain_ready() == True
and then
check is_captain()
. See is_captain_ready()
for more details.
Returns:
Type | Description |
---|---|
bool
|
True if this server is SHC captain else False. |
Source code in solnlib/server_info.py
168 169 170 171 172 173 174 175 176 177 178 179 180 |
|
is_captain_ready()
¶
Check if captain is ready.
Client usually first polls this function until captain is ready and then call is_captain to detect current captain machine
Returns:
Type | Description |
---|---|
bool
|
True if captain is ready else False. |
Examples:
>>> from solnlib import server_info
>>> serverinfo = server_info.ServerInfo(session_key)
>>> while 1:
>>> if serverinfo.is_captain_ready():
>>> break
>>> time.sleep(2)
>>>
>>> # If do_stuff can only be executed in SH captain
>>> if serverinfo.is_captain():
>>> do_stuff()
Source code in solnlib/server_info.py
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
|
is_cloud_instance()
¶
Check if this server is a cloud instance.
Returns:
Type | Description |
---|---|
bool
|
True if this server is a cloud instance else False. |
Source code in solnlib/server_info.py
182 183 184 185 186 187 188 189 190 191 192 |
|
is_search_head()
¶
Check if this server is a search head.
Returns:
Type | Description |
---|---|
bool
|
True if this server is a search head else False. |
Source code in solnlib/server_info.py
194 195 196 197 198 199 200 201 202 203 204 205 206 |
|
is_shc_member()
¶
Check if this server is a SHC member.
Returns:
Type | Description |
---|---|
bool
|
True if this server is a SHC member else False. |
Source code in solnlib/server_info.py
208 209 210 211 212 213 214 215 216 217 218 219 220 |
|
to_dict()
¶
Returns server information in a form dictionary.
Note: This method is implemented here to have compatibility with splunktalib’s analogue.
Returns:
Type | Description |
---|---|
Dict
|
Server information in a dictionary format. |
Source code in solnlib/server_info.py
126 127 128 129 130 131 132 133 134 135 |
|
ServerInfoException
¶
Bases: Exception
Exception raised by ServerInfo class.
Source code in solnlib/server_info.py
42 43 44 45 |
|
getWebCertFile()
¶
Source code in solnlib/server_info.py
27 28 |
|
getWebKeyFile()
¶
Source code in solnlib/server_info.py
30 31 |
|
splunk_rest_client.py¶
This module proxy all REST call to splunklib SDK, it handles proxy, certs etc in this centralized location.
All clients should use SplunkRestProxy to do REST call instead of calling splunklib SDK directly in business logic code.
MAX_REQUEST_RETRIES = 5
module-attribute
¶
__all__ = ['SplunkRestClient']
module-attribute
¶
SplunkRestClient
¶
Bases: client.Service
Splunk REST client.
Source code in solnlib/splunk_rest_client.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
|
__init__(session_key, app, owner='nobody', scheme=None, host=None, port=None, **context)
¶
Initializes SplunkRestClient.
Arguments scheme
, host
and port
are optional in the Splunk
environment (when environment variable SPLUNK_HOME is set). In this
situation get_splunkd_access_info
will be used to set scheme
,
host
and port
. In case of using SplunkRestClient
outside of
Splunk environment - scheme
, host
and port
should be provided.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
session_key |
str
|
Splunk access token. |
required |
app |
str
|
App name of namespace. |
required |
owner |
str
|
Owner of namespace, default is |
'nobody'
|
scheme |
str
|
The access scheme, default is None. |
None
|
host |
str
|
The host name, default is None. |
None
|
port |
int
|
The port number, default is None. |
None
|
context |
dict
|
Other configurations, it can contain |
{}
|
Raises:
Type | Description |
---|---|
ValueError
|
if scheme, host or port are invalid. |
Source code in solnlib/splunk_rest_client.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
|
bulletin_rest_client.py¶
__all__ = ['BulletinRestClient']
module-attribute
¶
BulletinRestClient
¶
REST client for handling Bulletin messages.
Source code in solnlib/bulletin_rest_client.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
|
MESSAGES_ENDPOINT = '/services/messages'
instance-attribute
class-attribute
¶
app = app
instance-attribute
¶
headers = [('Content-Type', 'application/json')]
instance-attribute
class-attribute
¶
message_name = message_name
instance-attribute
¶
session_key = session_key
instance-attribute
¶
Severity
¶
__init__(message_name, session_key, app, **context)
¶
Initializes BulletinRestClient.
When creating a new bulletin message, you must provide a name, which is a kind of ID.
If you try to create another message with the same name (ID), the API will not add another message
to the bulletin, but it will overwrite the existing one. Similar behaviour applies to deletion.
To delete a message, you must indicate the name (ID) of the message.
To provide better and easier control over bulletin messages, this client works in such a way
that there is one instance responsible for handling one specific message.
If you need to add another message to bulletin create another instance
with a different ‘message_name’
e.g.
msg_1 = BulletinRestClient(“message_1”, “
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message_name |
str
|
Name of the message in the Splunk’s bulletin. |
required |
session_key |
str
|
Splunk access token. |
required |
app |
str
|
App name of namespace. |
required |
context |
dict
|
Other configurations for Splunk rest client. |
{}
|
Source code in solnlib/bulletin_rest_client.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
|
create_message(msg, severity=Severity.WARNING, capabilities=None, roles=None)
¶
Creates a message in the Splunk’s bulletin. Calling this method multiple times for the same instance will overwrite existing message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
msg |
str
|
The message which will be displayed in the Splunk’s bulletin |
required |
severity |
Severity
|
Severity level of the message. It has to be one of: ‘info’, ‘warn’, ‘error’. If wrong severity is given, ValueError will be raised. |
Severity.WARNING
|
capabilities |
Optional[List[str]]
|
One or more capabilities that users must have to view the message. Capability names are validated. This argument should be provided as a list of string/s e.g. capabilities=[‘one’, ‘two’]. If a non-existent capability is used, HTTP 400 BAD REQUEST exception will be raised. If argument is not a List[str] ValueError will be raised. |
None
|
roles |
Optional[List]
|
One or more roles that users must have to view the message. Role names are validated. This argument should be provided as a list of string/s e.g. roles=[‘user’, ‘admin’]. If a non-existent role is used, HTTP 400 BAD REQUEST exception will be raised. If argument is not a List[str] ValueError will be raised. |
None
|
Source code in solnlib/bulletin_rest_client.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
|
delete_message()
¶
Delete specific message created by this instance.
Source code in solnlib/bulletin_rest_client.py
141 142 143 144 |
|
get_all_messages()
¶
Get all messages in the bulletin.
Source code in solnlib/bulletin_rest_client.py
134 135 136 137 138 139 |
|
get_message()
¶
Get specific message created by this instance.
Source code in solnlib/bulletin_rest_client.py
128 129 130 131 132 |
|
splunkenv.py¶
Splunk platform related utilities.
ETC_LEAF = 'etc'
module-attribute
¶
__all__ = ['make_splunkhome_path', 'get_splunk_host_info', 'get_splunk_bin', 'get_splunkd_access_info', 'get_scheme_from_hec_settings', 'get_splunkd_uri', 'get_conf_key_value', 'get_conf_stanza', 'get_conf_stanzas']
module-attribute
¶
on_shared_storage = [os.path.join(ETC_LEAF, 'apps'), os.path.join(ETC_LEAF, 'users'), os.path.join('var', 'run', 'splunk', 'dispatch'), os.path.join('var', 'run', 'splunk', 'srtemp'), os.path.join('var', 'run', 'splunk', 'rss'), os.path.join('var', 'run', 'splunk', 'scheduler'), os.path.join('var', 'run', 'splunk', 'lookup_tmp')]
module-attribute
¶
get_conf_key_value(conf_name, stanza, key)
¶
Get value of key
of stanza
in conf_name
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
conf_name |
str
|
Config file. |
required |
stanza |
str
|
Stanza name. |
required |
key |
str
|
Key name. |
required |
Returns:
Type | Description |
---|---|
Union[str, List, dict]
|
Config value. |
Raises:
Type | Description |
---|---|
KeyError
|
If |
Source code in solnlib/splunkenv.py
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
|
get_conf_stanza(conf_name, stanza)
¶
Get stanza
in conf_name
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
conf_name |
str
|
Config file. |
required |
stanza |
str
|
Stanza name. |
required |
Returns:
Type | Description |
---|---|
dict
|
Config stanza. |
Raises:
Type | Description |
---|---|
KeyError
|
If stanza doesn’t exist. |
Source code in solnlib/splunkenv.py
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
|
get_conf_stanzas(conf_name)
¶
Get stanzas of conf_name
Parameters:
Name | Type | Description | Default |
---|---|---|---|
conf_name |
str
|
Config file. |
required |
Returns:
Type | Description |
---|---|
dict
|
Config stanzas. |
Examples:
>>> stanzas = get_conf_stanzas('server')
>>> return: {'serverName': 'testServer', 'sessionTimeout': '1h', ...}
Source code in solnlib/splunkenv.py
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
|
get_scheme_from_hec_settings()
¶
Get scheme from HEC global settings.
Returns:
Type | Description |
---|---|
str
|
scheme (str) |
Source code in solnlib/splunkenv.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
|
get_splunk_bin()
¶
Get absolute path of splunk CLI.
Returns:
Type | Description |
---|---|
str
|
Absolute path of splunk CLI. |
Source code in solnlib/splunkenv.py
162 163 164 165 166 167 168 169 170 171 172 173 |
|
get_splunk_host_info()
¶
Get splunk host info.
Returns:
Type | Description |
---|---|
Tuple
|
Tuple of (server_name, host_name). |
Source code in solnlib/splunkenv.py
150 151 152 153 154 155 156 157 158 159 |
|
get_splunkd_access_info()
¶
Get splunkd server access info.
Returns:
Type | Description |
---|---|
Tuple[str, str, int]
|
Tuple of (scheme, host, port). |
Source code in solnlib/splunkenv.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
|
get_splunkd_uri()
¶
Get splunkd uri.
Returns:
Type | Description |
---|---|
str
|
Splunkd uri. |
Source code in solnlib/splunkenv.py
226 227 228 229 230 231 232 233 234 235 236 237 |
|
make_splunkhome_path(parts)
¶
Construct absolute path by $SPLUNK_HOME and parts
.
Concatenate $SPLUNK_HOME and parts
to an absolute path.
For example, parts
is [‘etc’, ‘apps’, ‘Splunk_TA_test’],
the return path will be $SPLUNK_HOME/etc/apps/Splunk_TA_test.
Note: this function assumed SPLUNK_HOME is in environment varialbes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parts |
Union[List, Tuple]
|
Path parts. |
required |
Returns:
Type | Description |
---|---|
str
|
Absolute path. |
Raises:
Type | Description |
---|---|
ValueError
|
Escape from intended parent directories. |
Source code in solnlib/splunkenv.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
|
time_parser.py¶
This module provides interfaces to parse and convert timestamp.
__all__ = ['TimeParser']
module-attribute
¶
InvalidTimeFormatException
¶
Bases: Exception
Exception for invalid time format.
Source code in solnlib/time_parser.py
31 32 33 34 |
|
TimeParser
¶
Datetime parser.
Use splunkd rest to parse datetime.
Examples:
>>> from solnlib import time_parser
>>> tp = time_parser.TimeParser(session_key)
>>> tp.to_seconds('2011-07-06T21:54:23.000-07:00')
>>> tp.to_utc('2011-07-06T21:54:23.000-07:00')
>>> tp.to_local('2011-07-06T21:54:23.000-07:00')
Source code in solnlib/time_parser.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
|
URL = '/services/search/timeparser'
instance-attribute
class-attribute
¶
__init__(session_key, scheme=None, host=None, port=None, **context)
¶
Initializes TimeParser.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
session_key |
str
|
Splunk access token. |
required |
scheme |
str
|
(optional) The access scheme, default is None. |
None
|
host |
str
|
(optional) The host name, default is None. |
None
|
port |
int
|
(optional) The port number, default is None. |
None
|
context |
Any
|
Other configurations for Splunk rest client. |
{}
|
Raises:
Type | Description |
---|---|
ValueError
|
if scheme, host or port are invalid. |
Source code in solnlib/time_parser.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
|
to_local(time_str)
¶
Parse time_str
and convert to local timestamp.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
time_str |
str
|
ISO8601 format timestamp, example: 2011-07-06T21:54:23.000-07:00. |
required |
Raises:
Type | Description |
---|---|
binding.HTTPError
|
rest client returns an exception (everything else than 400 code). |
InvalidTimeFormatException
|
when time format is invalid (rest client returns 400 code). |
Returns:
Type | Description |
---|---|
str
|
Local timestamp in ISO8601 format. |
Source code in solnlib/time_parser.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
|
to_seconds(time_str)
¶
Parse time_str
and convert to seconds since epoch.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
time_str |
str
|
ISO8601 format timestamp, example: 2011-07-06T21:54:23.000-07:00. |
required |
Raises:
Type | Description |
---|---|
binding.HTTPError
|
rest client returns an exception (everything else than 400 code). |
InvalidTimeFormatException
|
when time format is invalid (rest client returns 400 code). |
Returns:
Type | Description |
---|---|
float
|
Seconds since epoch. |
Source code in solnlib/time_parser.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
|
to_utc(time_str)
¶
Parse time_str
and convert to UTC timestamp.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
time_str |
str
|
ISO8601 format timestamp, example: 2011-07-06T21:54:23.000-07:00. |
required |
Raises:
Type | Description |
---|---|
binding.HTTPError
|
rest client returns an exception (everything else than 400 code). |
InvalidTimeFormatException
|
when time format is invalid (rest client returns 400 code). |
Returns:
Type | Description |
---|---|
datetime.datetime
|
UTC timestamp. |
Source code in solnlib/time_parser.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
|
timer_queue.py¶
A simple thread safe timer queue implementation which has O(logn) time complexity.
TEARDOWN_SENTINEL = None
module-attribute
¶
__all__ = ['Timer', 'TimerQueueStruct', 'TimerQueue']
module-attribute
¶
Timer
¶
Timer wraps the callback and timestamp related attributes.
Source code in solnlib/timer_queue.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
|
ident = Timer._ident + 1
instance-attribute
¶
interval = interval
instance-attribute
¶
when = when
instance-attribute
¶
__call__()
¶
Source code in solnlib/timer_queue.py
79 80 |
|
__eq__(other)
¶
Source code in solnlib/timer_queue.py
64 65 |
|
__ge__(other)
¶
Source code in solnlib/timer_queue.py
76 77 |
|
__gt__(other)
¶
Source code in solnlib/timer_queue.py
73 74 |
|
__hash__()
¶
Source code in solnlib/timer_queue.py
61 62 |
|
__init__(callback, when, interval, ident=None)
¶
Initializes Timer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
callback |
Callable
|
Arbitrary callable object. |
required |
when |
int
|
The first expiration time, seconds since epoch. |
required |
interval |
int
|
Timer interval, if equals 0, one time timer, otherwise the timer will be periodically executed. |
required |
ident |
int
|
(optional) Timer identity. |
None
|
Source code in solnlib/timer_queue.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
__le__(other)
¶
Source code in solnlib/timer_queue.py
70 71 |
|
__lt__(other)
¶
Source code in solnlib/timer_queue.py
67 68 |
|
update_expiration()
¶
Source code in solnlib/timer_queue.py
58 59 |
|
TimerQueue
¶
A simple timer queue implementation.
It runs a separate thread to handle timers Note: to effectively use this timer queue, the timer callback should be short, otherwise it will cause other timers’s delay execution. A typical use scenario in production is that the timers are just a simple functions which inject themselvies to a task queue and then they are picked up by a threading/process pool to execute, as shows below:
Timers --enqueue---> TimerQueue --------expiration-----------
|
|
\|/
Threading/Process Pool <---- TaskQueue <--enqueue-- Timers' callback (nonblocking)
Examples:
>>> from solnlib import timer_queue
>>> tq = timer_queue.TimerQueue()
>>> tq.start()
>>> t = tq.add_timer(my_func, time.time(), 10)
>>> # do other stuff
>>> tq.stop()
Source code in solnlib/timer_queue.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
|
__init__()
¶
Source code in solnlib/timer_queue.py
218 219 220 221 222 223 224 |
|
add_timer(callback, when, interval, ident=None)
¶
Add timer to the queue.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
callback |
Callable
|
Arbitrary callable object. |
required |
when |
int
|
The first expiration time, seconds since epoch. |
required |
interval |
int
|
Timer interval, if equals 0, one time timer, otherwise the timer will be periodically executed |
required |
ident |
int
|
(optional) Timer identity. |
None
|
Returns:
Type | Description |
---|---|
Timer
|
A timer object which should not be manipulated directly by clients. Used to delete/update the timer. |
Source code in solnlib/timer_queue.py
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
|
remove_timer(timer)
¶
Remove timer from the queue.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timer |
Timer
|
Timer object to remove. |
required |
Source code in solnlib/timer_queue.py
268 269 270 271 272 273 274 275 276 |
|
start()
¶
Start the timer queue.
Source code in solnlib/timer_queue.py
226 227 228 229 230 231 232 233 234 |
|
stop()
¶
Stop the timer queue.
Source code in solnlib/timer_queue.py
236 237 238 239 240 241 242 243 244 |
|
TimerQueueStruct
¶
The underlying data structure for TimerQueue.
Source code in solnlib/timer_queue.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
|
__init__()
¶
Source code in solnlib/timer_queue.py
89 90 91 |
|
add_timer(callback, when, interval, ident)
¶
Add timer to the data structure.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
callback |
Callable
|
Arbitrary callable object. |
required |
when |
int
|
The first expiration time, seconds since epoch. |
required |
interval |
int
|
Timer interval, if equals 0, one time timer, otherwise the timer will be periodically executed |
required |
ident |
int
|
(optional) Timer identity. |
required |
Returns:
Type | Description |
---|---|
Timer
|
A timer object which should not be manipulated directly by clients. Used to delete/update the timer. |
Source code in solnlib/timer_queue.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
|
check_and_execute()
¶
Get expired timers and execute callbacks for the timers.
Returns:
Type | Description |
---|---|
float
|
Duration of next expired timer. |
Source code in solnlib/timer_queue.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
|
get_expired_timers()
¶
Get a list of expired timers.
Returns:
Type | Description |
---|---|
Tuple
|
A tuple of |
Source code in solnlib/timer_queue.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
|
remove_timer(timer)
¶
Remove timer from data structure.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timer |
Timer
|
Timer object which is returned by |
required |
Source code in solnlib/timer_queue.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
|
reset_timers(expired_timers)
¶
Re-add the expired periodical timers to data structure for next round scheduling.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
expired_timers |
List[Timer]
|
List of expired timers. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if there are timers added, False otherwise. |
Source code in solnlib/timer_queue.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
|
user_access.py¶
Splunk user access control related utilities.
__all__ = ['ObjectACLException', 'ObjectACL', 'ObjectACLManagerException', 'ObjectACLManager', 'AppCapabilityManagerException', 'AppCapabilityManager', 'UserAccessException', 'check_user_access', 'InvalidSessionKeyException', 'get_current_username', 'UserNotExistException', 'get_user_capabilities', 'user_is_capable', 'get_user_roles']
module-attribute
¶
AppCapabilityManager
¶
App capability manager.
Examples:
>>> from solnlib import user_access
>>> acm = user_access.AppCapabilityManager('test_collection',
session_key,
'Splunk_TA_test')
>>> acm.register_capabilities(...)
>>> acm.unregister_capabilities(...)
Source code in solnlib/user_access.py
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 |
|
__init__(collection_name, session_key, app, owner='nobody', scheme=None, host=None, port=None, **context)
¶
Initializes AppCapabilityManager.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
collection_name |
str
|
Collection name to store capabilities. |
required |
session_key |
str
|
Splunk access token. |
required |
app |
str
|
App name of namespace. |
required |
owner |
str
|
(optional) Owner of namespace, default is |
'nobody'
|
scheme |
str
|
(optional) The access scheme, default is None. |
None
|
host |
str
|
(optional) The host name, default is None. |
None
|
port |
int
|
(optional) The port number, default is None. |
None
|
context |
dict
|
Other configurations for Splunk rest client. |
{}
|
Raises:
Type | Description |
---|---|
AppCapabilityManagerException
|
If init AppCapabilityManager failed. |
Source code in solnlib/user_access.py
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 |
|
capabilities_are_registered()
¶
Check if app capabilities are registered.
Returns:
Type | Description |
---|---|
bool
|
True if app capabilities are registered else False. |
Source code in solnlib/user_access.py
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 |
|
get_capabilities()
¶
Get app capabilities.
Returns:
Type | Description |
---|---|
dict
|
App capabilities. |
Raises:
Type | Description |
---|---|
AppCapabilityNotExistException
|
If app capabilities are not registered. |
Source code in solnlib/user_access.py
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 |
|
register_capabilities(capabilities)
¶
Register app capabilities.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
capabilities |
dict
|
App capabilities, example: { ‘object_type1’: { ‘read’: ‘read_app_object_type1’, ‘write’: ‘write_app_object_type1’, ‘delete’: ‘delete_app_object_type1’}, ‘object_type2’: { ‘read’: ‘read_app_object_type2’, ‘write’: ‘write_app_object_type2’, ‘delete’: ‘delete_app_object_type2’ }, … } |
required |
Source code in solnlib/user_access.py
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 |
|
unregister_capabilities()
¶
Unregister app capabilities.
Raises:
Type | Description |
---|---|
AppCapabilityNotExistException
|
If app capabilities are not registered. |
Source code in solnlib/user_access.py
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 |
|
AppCapabilityManagerException
¶
Bases: Exception
Exception for AppCapabilityManager.
Source code in solnlib/user_access.py
542 543 544 545 |
|
AppCapabilityNotExistException
¶
Bases: Exception
Exception for the situation when AppCapability does not exist for a specific app.
Source code in solnlib/user_access.py
548 549 550 551 552 |
|
InvalidSessionKeyException
¶
Bases: Exception
Exception when Splunk session key is invalid.
Source code in solnlib/user_access.py
773 774 775 776 |
|
ObjectACL
¶
Object ACL record.
Examples:
>>> from solnlib import user_access
>>> obj_acl = user_access.ObjectACL(
>>> 'test_collection',
>>> '9defa6f510d711e6be16a45e60e34295',
>>> 'test_object',
>>> 'Splunk_TA_test',
>>> 'admin',
>>> {'read': ['*'], 'write': ['admin'], 'delete': ['admin']},
>>> False)
Source code in solnlib/user_access.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
|
OBJ_APP_KEY = 'obj_app'
instance-attribute
class-attribute
¶
OBJ_COLLECTION_KEY = 'obj_collection'
instance-attribute
class-attribute
¶
OBJ_ID_KEY = 'obj_id'
instance-attribute
class-attribute
¶
OBJ_OWNER_KEY = 'obj_owner'
instance-attribute
class-attribute
¶
OBJ_PERMS_ALLOW_ALL = '*'
instance-attribute
class-attribute
¶
OBJ_PERMS_DELETE_KEY = 'delete'
instance-attribute
class-attribute
¶
OBJ_PERMS_KEY = 'obj_perms'
instance-attribute
class-attribute
¶
OBJ_PERMS_READ_KEY = 'read'
instance-attribute
class-attribute
¶
OBJ_PERMS_WRITE_KEY = 'write'
instance-attribute
class-attribute
¶
OBJ_SHARED_BY_INCLUSION_KEY = 'obj_shared_by_inclusion'
instance-attribute
class-attribute
¶
OBJ_TYPE_KEY = 'obj_type'
instance-attribute
class-attribute
¶
obj_app = obj_app
instance-attribute
¶
obj_collection = obj_collection
instance-attribute
¶
obj_id = obj_id
instance-attribute
¶
obj_owner = obj_owner
instance-attribute
¶
obj_perms
property
writable
¶
obj_shared_by_inclusion = obj_shared_by_inclusion
instance-attribute
¶
obj_type = obj_type
instance-attribute
¶
record: dict
property
¶
Get object acl record.
Object acl record, like:
Type | Description |
---|---|
dict
|
{ ‘_key’: ‘test_collection-1234’, ‘obj_collection’: ‘test_collection’, ‘obj_id’: ‘1234’, ‘obj_type’: ‘test_object’, ‘obj_app’: ‘Splunk_TA_test’, ‘obj_owner’: ‘admin’, ‘obj_perms’: {‘read’: [‘*’], ‘write’: [‘admin’], ‘delete’: [‘admin’]}, ‘obj_shared_by_inclusion’: True |
dict
|
} |
__init__(obj_collection, obj_id, obj_type, obj_app, obj_owner, obj_perms, obj_shared_by_inclusion)
¶
Initializes ObjectACL.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj_collection |
str
|
Collection where object currently stored. |
required |
obj_id |
str
|
ID of this object. |
required |
obj_type |
str
|
Type of this object. |
required |
obj_app |
str
|
App of this object. |
required |
obj_owner |
str
|
Owner of this object. |
required |
obj_perms |
dict
|
Object perms, like: {‘read’: [‘*’], ‘write’: [‘admin’], ‘delete’: [‘admin’]}. |
required |
obj_shared_by_inclusion |
bool
|
Flag of object is shared by inclusion. |
required |
Source code in solnlib/user_access.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
|
__str__()
¶
Source code in solnlib/user_access.py
216 217 |
|
generate_key(obj_collection, obj_id)
staticmethod
¶
Generate object acl record key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj_collection |
str
|
Collection where object currently stored. |
required |
obj_id |
str
|
ID of this object. |
required |
Returns:
Type | Description |
---|---|
str
|
Object acl record key. |
Source code in solnlib/user_access.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
|
merge(obj_acl)
¶
Merge current object perms with perms of obj_acl
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj_acl |
ObjectACL
|
Object acl to merge. |
required |
Source code in solnlib/user_access.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
|
parse(obj_acl_record)
staticmethod
¶
Parse object acl record and construct a new ObjectACL
object from
it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj_acl_record |
dict
|
Object acl record. |
required |
Returns:
Type | Description |
---|---|
ObjectACL
|
New |
Source code in solnlib/user_access.py
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
|
ObjectACLException
¶
Bases: Exception
Source code in solnlib/user_access.py
46 47 |
|
ObjectACLManager
¶
Object ACL manager.
Examples:
>>> from solnlib import user_access
>>> oaclm = user_access.ObjectACLManager(session_key,
'Splunk_TA_test')
Source code in solnlib/user_access.py
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 |
|
__init__(collection_name, session_key, app, owner='nobody', scheme=None, host=None, port=None, **context)
¶
Initializes ObjectACLManager.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
collection_name |
str
|
Collection name to store object ACL info. |
required |
session_key |
str
|
Splunk access token. |
required |
app |
str
|
App name of namespace. |
required |
owner |
Optional[str]
|
(optional) Owner of namespace, default is |
'nobody'
|
scheme |
Optional[str]
|
(optional) The access scheme, default is None. |
None
|
host |
Optional[str]
|
(optional) The host name, default is None. |
None
|
port |
Optional[int]
|
(optional) The port number, default is None. |
None
|
context |
dict
|
Other configurations for Splunk rest client. |
{}
|
Raises:
Type | Description |
---|---|
ObjectACLManagerException
|
If init ObjectACLManager failed. |
Source code in solnlib/user_access.py
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
|
delete_acl(obj_collection, obj_id)
¶
Delete acl info.
Query object acl info with parameter of the combination of
obj_collection
and obj_ids
from KVStore and delete it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj_collection |
str
|
Collection where object currently stored. |
required |
obj_id |
str
|
ID of this object. |
required |
Raises:
Type | Description |
---|---|
ObjectACLNotExistException
|
If object ACL info does not exist. |
Source code in solnlib/user_access.py
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 |
|
delete_acls(obj_collection, obj_ids)
¶
Batch delete acl info.
Query objects acl info with parameter of the combination of
obj_collection
and obj_ids
from KVStore and delete them.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj_collection |
str
|
Collection where object currently stored. |
required |
obj_ids |
List[str]
|
IDs of objects. |
required |
Source code in solnlib/user_access.py
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 |
|
get_accessible_object_ids(user, operation, obj_collection, obj_ids)
¶
Get accessible IDs of objects from obj_acls
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
user |
str
|
User name of current |
required |
operation |
str
|
User operation, possible option: (read/write/delete). |
required |
obj_collection |
str
|
Collection where object currently stored. |
required |
obj_ids |
List[str]
|
IDs of objects. |
required |
Returns:
Type | Description |
---|---|
List[str]
|
List of IDs of accessible objects. |
Source code in solnlib/user_access.py
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 |
|
get_acl(obj_collection, obj_id)
¶
Get acl info.
Query object acl info with parameter of the combination of
obj_collection
and obj_id
from self.collection_name
and
return it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj_collection |
str
|
Collection where object currently stored. |
required |
obj_id |
str
|
ID of this object. |
required |
Returns:
Type | Description |
---|---|
ObjectACL
|
Object acl info if success else None. |
Raises:
Type | Description |
---|---|
ObjectACLNotExistException
|
If object ACL info does not exist. |
Source code in solnlib/user_access.py
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 |
|
get_acls(obj_collection, obj_ids)
¶
Batch get acl info.
Query objects acl info with parameter of the combination of
obj_collection
and obj_ids
from KVStore and return them.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj_collection |
str
|
Collection where object currently stored. |
required |
obj_ids |
List[str]
|
IDs of objects. |
required |
Returns:
Type | Description |
---|---|
List[ObjectACL]
|
List of |
Source code in solnlib/user_access.py
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 |
|
update_acl(obj_collection, obj_id, obj_type, obj_app, obj_owner, obj_perms, obj_shared_by_inclusion=True, replace_existing=True)
¶
Update acl info of object.
Construct a new object acl info first, if replace_existing
is True
then replace existing acl info else merge new object acl info with the
old one and replace the old acl info with merged acl info.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj_collection |
str
|
Collection where object currently stored. |
required |
obj_id |
str
|
ID of this object. |
required |
obj_type |
str
|
Type of this object. |
required |
obj_app |
str
|
App of this object. |
required |
obj_owner |
str
|
Owner of this object. |
required |
obj_perms |
dict
|
Object perms, like: { ‘read’: [‘*’], ‘write’: [‘admin’], ‘delete’: [‘admin’] }. |
required |
obj_shared_by_inclusion |
bool
|
(optional) Flag of object is shared by inclusion, default is True. |
True
|
replace_existing |
bool
|
(optional) Replace existing acl info flag, True indicates replace old acl info with new one else merge with old acl info, default is True. |
True
|
Source code in solnlib/user_access.py
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
|
update_acls(obj_collection, obj_ids, obj_type, obj_app, obj_owner, obj_perms, obj_shared_by_inclusion=True, replace_existing=True)
¶
Batch update object acl info to all provided obj_ids
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj_collection |
str
|
Collection where objects currently stored. |
required |
obj_ids |
List[str]
|
IDs list of objects. |
required |
obj_type |
str
|
Type of this object. |
required |
obj_app |
str
|
App of this object. |
required |
obj_owner |
str
|
Owner of this object. |
required |
obj_perms |
dict
|
Object perms, like: { ‘read’: [‘*’], ‘write’: [‘admin’], ‘delete’: [‘admin’] }. |
required |
obj_shared_by_inclusion |
bool
|
(optional) Flag of object is shared by inclusion, default is True. |
True
|
replace_existing |
bool
|
(optional) Replace existing acl info flag, True indicates replace old acl info with new one else merge with old acl info, default is True. |
True
|
Source code in solnlib/user_access.py
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
|
ObjectACLManagerException
¶
Bases: Exception
Exception for ObjectACLManager.
Source code in solnlib/user_access.py
220 221 222 223 |
|
ObjectACLNotExistException
¶
Bases: Exception
Exception for the situation when ACL does not exist.
Source code in solnlib/user_access.py
226 227 228 229 |
|
UserAccessException
¶
Bases: Exception
Exception for the situation when there is user access exception.
Source code in solnlib/user_access.py
697 698 699 700 |
|
UserNotExistException
¶
Bases: Exception
Exception when user does not exist.
Source code in solnlib/user_access.py
823 824 825 826 |
|
check_user_access(session_key, capabilities, obj_type, operation, scheme=None, host=None, port=None, **context)
¶
User access checker.
It will fetch user capabilities from given session_key
and check if
the capability extracted from capabilities
, obj_type
and operation
is contained, if user capabilities include the extracted capability user
access is ok else fail.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
session_key |
str
|
Splunk access token. |
required |
capabilities |
dict
|
App capabilities, example: { ‘object_type1’: { ‘read’: ‘read_app_object_type1’, ‘write’: ‘write_app_object_type1’, ‘delete’: ‘delete_app_object_type1’}, ‘object_type2’: { ‘read’: ‘read_app_object_type2’, ‘write’: ‘write_app_object_type2’, ‘delete’: ‘delete_app_object_type2’ }, … } |
required |
obj_type |
str
|
Object type. |
required |
operation |
str
|
User operation, possible option: (read/write/delete). |
required |
scheme |
str
|
(optional) The access scheme, default is None. |
None
|
host |
str
|
(optional) The host name, default is None. |
None
|
port |
int
|
(optional) The port number, default is None. |
None
|
context |
dict
|
Other configurations for Splunk rest client. |
{}
|
Raises:
Type | Description |
---|---|
UserAccessException
|
If user access permission is denied. |
Examples:
>>> from solnlib.user_access import check_user_access
>>> def fun():
>>> check_user_access(
>>> session_key, capabilities, 'test_object', 'read')
>>> ...
Source code in solnlib/user_access.py
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 |
|
get_current_username(session_key, scheme=None, host=None, port=None, **context)
¶
Get current user name from session_key
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
session_key |
str
|
Splunk access token. |
required |
scheme |
str
|
(optional) The access scheme, default is None. |
None
|
host |
str
|
(optional) The host name, default is None. |
None
|
port |
int
|
(optional) The port number, default is None. |
None
|
context |
dict
|
Other configurations for Splunk rest client. |
{}
|
Returns:
Type | Description |
---|---|
str
|
Current user name. |
Raises:
Type | Description |
---|---|
InvalidSessionKeyException
|
If |
Examples:
>>> from solnlib import user_access
>>> user_name = user_access.get_current_username(session_key)
Source code in solnlib/user_access.py
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 |
|
get_user_capabilities(session_key, username, scheme=None, host=None, port=None, **context)
¶
Get user capabilities.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
session_key |
str
|
Splunk access token. |
required |
scheme |
str
|
(optional) The access scheme, default is None. |
None
|
host |
str
|
(optional) The host name, default is None. |
None
|
port |
int
|
(optional) The port number, default is None. |
None
|
context |
dict
|
Other configurations for Splunk rest client. |
{}
|
Returns:
Type | Description |
---|---|
List[dict]
|
User capabilities. |
Raises:
Type | Description |
---|---|
UserNotExistException
|
If |
Examples:
>>> from solnlib import user_access
>>> user_capabilities = user_access.get_user_capabilities(
>>> session_key, 'test_user')
Source code in solnlib/user_access.py
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 |
|
get_user_roles(session_key, username, scheme=None, host=None, port=None, **context)
¶
Get user roles.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
session_key |
str
|
Splunk access token. |
required |
username |
str
|
(optional) User name of roles to get. |
required |
scheme |
(optional) The access scheme, default is None. |
None
|
|
host |
(optional) The host name, default is None. |
None
|
|
port |
(optional) The port number, default is None. |
None
|
|
context |
Other configurations for Splunk rest client. |
{}
|
Returns:
Type | Description |
---|---|
List
|
User roles. |
Raises:
Type | Description |
---|---|
UserNotExistException
|
If |
Examples:
>>> from solnlib import user_access
>>> user_roles = user_access.get_user_roles(session_key, 'test_user')
Source code in solnlib/user_access.py
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 |
|
user_is_capable(session_key, username, capability, scheme=None, host=None, port=None, **context)
¶
Check if user is capable for given capability
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
session_key |
str
|
Splunk access token. |
required |
username |
str
|
(optional) User name of roles to get. |
required |
capability |
str
|
The capability we wish to check for. |
required |
scheme |
str
|
(optional) The access scheme, default is None. |
None
|
host |
str
|
(optional) The host name, default is None. |
None
|
port |
int
|
(optional) The port number, default is None. |
None
|
context |
dict
|
Other configurations for Splunk rest client. |
{}
|
Returns:
Type | Description |
---|---|
bool
|
True if user is capable else False. |
Raises:
Type | Description |
---|---|
UserNotExistException
|
If |
Examples:
>>> from solnlib import user_access
>>> is_capable = user_access.user_is_capable(
>>> session_key, 'test_user', 'object_read_capability')
Source code in solnlib/user_access.py
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 |
|
utils.py¶
Common utilities.
__all__ = ['handle_teardown_signals', 'datetime_to_seconds', 'is_true', 'is_false', 'retry', 'extract_http_scheme_host_port', 'remove_http_proxy_env_vars']
module-attribute
¶
datetime_to_seconds(dt)
¶
Convert UTC datetime to seconds since epoch.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dt |
datetime.datetime
|
Date time. |
required |
Returns:
Type | Description |
---|---|
float
|
Seconds since epoch. |
Source code in solnlib/utils.py
82 83 84 85 86 87 88 89 90 91 92 93 |
|
extract_http_scheme_host_port(http_url)
¶
Extract scheme, host and port from a HTTP URL.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
http_url |
str
|
HTTP URL to extract. |
required |
Returns:
Type | Description |
---|---|
Tuple
|
A tuple of scheme, host and port |
Raises:
Type | Description |
---|---|
ValueError
|
If |
Source code in solnlib/utils.py
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
|
get_appname_from_path(absolute_path)
¶
Gets name of the app from its path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
absolute_path |
path of app |
required |
Source code in solnlib/utils.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
|
handle_teardown_signals(callback)
¶
Register handler for SIGTERM/SIGINT/SIGBREAK signal.
Catch SIGTERM/SIGINT/SIGBREAK signals, and invoke callback Note: this should be called in main thread since Python only catches signals in main thread.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
callback |
Callable
|
Callback for tear down signals. |
required |
Source code in solnlib/utils.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
|
is_false(val)
¶
Decide if val
is false.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
val |
Union[str, int]
|
Value to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
True or False. |
Source code in solnlib/utils.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
|
is_true(val)
¶
Decide if val
is true.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
val |
Union[str, int]
|
Value to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
True or False. |
Source code in solnlib/utils.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
|
remove_http_proxy_env_vars()
¶
Removes HTTP(s) proxies from environment variables.
Removes the following environment variables
- http_proxy
- https_proxy
- HTTP_PROXY
- HTTPS_PROXY
This function can be used in Splunk modular inputs code before starting the ingestion to ensure that no proxy is going to be used when doing requests. In case of proxy is needed, it can be defined in the modular inputs code.
Source code in solnlib/utils.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
|
retry(retries=3, reraise=True, default_return=None, exceptions=None)
¶
A decorator to run function with max retries
times if there is
exception.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
retries |
int
|
(optional) Max retries times, default is 3. |
3
|
reraise |
bool
|
Whether exception should be reraised, default is True. |
True
|
default_return |
Any
|
(optional) Default return value for function run after max retries and reraise is False. |
None
|
exceptions |
List
|
(optional) List of exceptions that should retry. |
None
|
Source code in solnlib/utils.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
|