splunkenv.py¶
Splunk platform related utilities.
APP_HEC = 'splunk_httpinput'
module-attribute
¶
APP_SYSTEM = 'system'
module-attribute
¶
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
¶
SessionKeyNotFound
¶
Bases: Exception
Source code in solnlib/splunkenv.py
74 75 | |
get_conf_key_value(conf_name, stanza, key, app_name, session_key=None, user='nobody', raw_output=False)
¶
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 in the stanza. |
required |
app_name
|
str
|
Application name. To make a call to global context use ‘-’ as app_name and set raw_output=True. In that case manual parsing is needed as response may be the list with multiple entries. |
required |
session_key
|
Optional[str]
|
Needed to make a call to config endpoint. If ‘None’, solnlib will try to get it from splunk.getSessionKey() and/or main module and if it won’t get it, SessionKeyNotFound will be raised. |
None
|
user
|
str
|
used for set user context in API call. Optional. |
'nobody'
|
raw_output
|
Optional[bool]
|
if ‘true’ full, decoded response in json format will be returned. It should be set to True when app_name is a global context ‘/-/’. In that case splunk API may return multiple entries. |
False
|
Returns:
| Type | Description |
|---|---|
Union[str, list, dict]
|
Config value. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
Source code in solnlib/splunkenv.py
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 | |
get_conf_stanza(conf_name, stanza, app_name, session_key=None, user='nobody', raw_output=False)
¶
Get stanza in conf_name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conf_name
|
str
|
Config file. |
required |
stanza
|
str
|
Stanza name. |
required |
app_name
|
str
|
Application name. To make a call to global context use ‘-’ as app_name and set raw_output=True. In that case manual parsing is needed as response may be the list with multiple entries. |
required |
session_key
|
Optional[str]
|
Needed to make a call to config endpoint. If ‘None’, solnlib will try to get it from splunk.getSessionKey() and/or main module and if it won’t get it, SessionKeyNotFound will be raised. |
None
|
user
|
str
|
used for set user context in API call. Optional. |
'nobody'
|
raw_output
|
Optional[bool]
|
if ‘true’ full, decoded response in json format will be returned. It should be set to True when app_name is a global context ‘/-/’. In that case splunk API may return multiple entries. |
False
|
Returns:
| Type | Description |
|---|---|
dict
|
Config stanza. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If stanza doesn’t exist. |
Source code in solnlib/splunkenv.py
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 | |
get_conf_stanzas(conf_name, app_name=None)
¶
Get stanzas of conf_name
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conf_name
|
str
|
Config file. |
required |
app_name
|
Optional[str]
|
Application name. Optional. |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
Config stanzas. |
Examples:
>>> stanzas = get_conf_stanzas('server')
>>> return: {'serverName': 'testServer', 'sessionTimeout': '1h', ...}
Source code in solnlib/splunkenv.py
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 | |
get_scheme_from_hec_settings(session_key=None)
¶
Get scheme from HEC global settings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_key
|
Optional[str]
|
Needed to make a call to config endpoint. If ‘None’, solnlib will try to get it from splunk.getSessionKey() and/or main module and if it won’t get it, SessionKeyNotFound will be raised. |
None
|
Returns: scheme (str)
Source code in solnlib/splunkenv.py
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 | |
get_splunk_bin()
¶
Get absolute path of splunk CLI.
Returns:
| Type | Description |
|---|---|
str
|
Absolute path of splunk CLI. |
Source code in solnlib/splunkenv.py
179 180 181 182 183 184 185 186 187 188 189 190 | |
get_splunk_host_info(session_key=None)
¶
Get splunk host info.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_key
|
Optional[str]
|
Needed to make a call to config endpoint. If ‘None’, solnlib will try to get it from splunk.getSessionKey() and/or main module and if it won’t get it, SessionKeyNotFound will be raised. |
None
|
Returns: Tuple of (server_name, host_name).
Source code in solnlib/splunkenv.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | |
get_splunkd_access_info(session_key=None)
¶
Get splunkd server access info.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_key
|
Optional[str]
|
Needed to make a call to config endpoint. If ‘None’, solnlib will try to get it from splunk.getSessionKey() and/or main module and if it won’t get it, SessionKeyNotFound will be raised. |
None
|
Returns: Tuple of (scheme, host, port).
Source code in solnlib/splunkenv.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 | |
get_splunkd_uri(session_key=None)
¶
Get splunkd uri.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_key
|
Optional[str]
|
Needed to make a call to config endpoint. If ‘None’, solnlib will try to get it from splunk.getSessionKey() and/or main module and if it won’t get it, SessionKeyNotFound will be raised. |
None
|
Returns: Splunkd uri.
Source code in solnlib/splunkenv.py
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | |
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
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | |