首页
/ Splunk Enterprise Python SDK 技术文档

Splunk Enterprise Python SDK 技术文档

2024-12-25 10:05:31作者:钟日瑜

本文档将详细介绍如何使用和安装Splunk Enterprise Python SDK,包括安装指南、项目使用说明和API使用文档。

1. 安装指南

环境要求

  • Python版本:Python 3.7, Python 3.9 或 Python 3.13
  • Splunk Enterprise:版本 9.2 或 8.2

如果尚未安装Splunk Enterprise,可以从这里下载。有关更多信息,请参阅Splunk Enterprise的安装手册

安装SDK

使用以下命令安装Splunk Enterprise Python SDK库。不过,运行SDK的单元测试不需要安装这些库。

使用pip

[sudo] pip install splunk-sdk

安装Python egg:

[sudo] pip install --egg splunk-sdk

从GitHub克隆源代码安装:

[sudo] python setup.py install

2. 项目使用说明

Splunk Enterprise Python SDK包含用于与Splunk平台交互的库代码。以下是如何连接到Splunk Enterprise的不同方式。

使用用户名/密码连接

import splunklib.client as client
service = client.connect(host=<host_url>, username=<username>, password=<password>, autologin=True)

使用Bearer令牌连接

import splunklib.client as client
service = client.connect(host=<host_url>, splunkToken=<bearer_token>, autologin=True)

使用会话密钥连接

import splunklib.client as client
service = client.connect(host=<host_url>, token=<session_key>, autologin=True)

使用.env文件

为了方便开发,可以将主机、端口和登录凭据的值存储在.env文件中。SDK示例和单元测试会使用.env文件中的值,除非用户明确指定。

注意:在.env文件中存储登录凭据仅用于开发方便。此文件不是Splunk平台的一部分,不应用于生产环境中存储用户凭据。如果您对凭据的安全性有顾虑,请在命令行中输入它们,而不是保存在此文件中。

以下是.env文件的示例:

# Splunk Enterprise主机(默认:localhost)
host=localhost
# Splunk Enterprise管理员端口(默认:8089)
port=8089
# Splunk Enterprise用户名
username=admin
# Splunk Enterprise密码
password=changed!
# 访问协议(默认:https)
scheme=https
# 您的Splunk Enterprise版本
version=9.2
# 用于身份验证的Bearer令牌
#splunkToken=<Bearer-token>
# 用于身份验证的会话密钥
#token=<Session-Key>

3. 项目API使用文档

Splunk Enterprise Python SDK的API文档可以在官方文档中找到。具体使用方法请参考官方提供的API指南。

4. 项目安装方式

请按照以下步骤安装Splunk Enterprise Python SDK:

  1. 确保满足环境要求。
  2. 使用pip安装SDK库。
[sudo] pip install splunk-sdk
  1. 如果需要从源代码安装,可以从GitHub克隆仓库并运行安装脚本。
[sudo] python setup.py install

通过以上步骤,您应该能够成功安装Splunk Enterprise Python SDK并开始使用它来构建应用程序。

登录后查看全文