将数据发送到Splunk的Python连接器
splunk-data-sender的Python项目详细描述
喷溅数据发送器
Splunk Data Sender是一个非常简单和最小的Python包,用于将记录的事件发送到Splunk Enterprise throw REST API的安装中
此记录器要求目标Splunk企业服务器已启用并配置了Splunk HTTP Event Collector。
安装
皮普:
pip install splunk-data-sender
手册:
^{pr2}$使用
from splunk_data_sender import SplunkSender
然后使用您的首选配置创建一个SplunkSender实例。现在可以使用两个方法send_data() 和check_acks()分别发送日志和检查消息acks(需要在HEC中启用useAck)。在
示例:
importloggingfromsplunk_data_senderimportSplunkSendersplunk_conf=SplunkSender(endpoint='splunk.example.com',port='8088',token='851A5E58-4EF1-7291-F947-F614A76ACB21',index='main',channel='16c70678-e516-44a9-854d-d139929e6869',# GUIDapi_version='0.1',#hostname='hostname', # manually set a hostname parameter, defaults to socket.gethostname()#source='source', # manually set a source, defaults to the log record.pathname#source_type='generic_single_line', # manually set a source_type, defaults to 'generic_single_line'allow_overrides=True,# Whether to look for one of the Splunk built-in parameters(index, host, ecc)verify=False,# turn SSL verification on or off, defaults to True#timeout=60, # timeout for waiting on a 200 OK from Splunk server, defaults to 60s#retry_count=5, # Number of retry attempts on a failed/erroring connection, defaults to 5#retry_backoff=2.0, # Backoff factor, default options will retry for 1 min, defaults to 2.0enable_debug=True,# turn on debug mode; prints module activity to stdout, defaults to False)splunk=SplunkSender(**splunk_conf)is_alive=splunk.get_health()logging.info(is_alive)ifnotis_alive:raiseException("HEC not alive")# The first payload is a quote to the italian theme song of Hello!Spanktxt_record="Hello! Splunk resta con me, Hello! Splunk non te ne andare, Caro Splunk! gioca con me, siamo amici io e te."json_record={# this record will be parsed as normal text due to default "sourcetype" conf param"source":"spacecraft Discovery 1","host":"HAL9000",#"sourcetype": "_json", # source type without underscore to allow the override of this built-in parameter"index":"main","event":{"message":"I am afraid I can't do that Dave.","severity":"ERROR"},"rack":"42","os":"Linux, obvious","arch":"x64"}payloads=[txt_record,json_record]splunk_res=splunk.send_data(payloads)logging.info(splunk_res)ack_id=splunk_res.get('ackId')splunk_ack_res=splunk.send_acks(ack_id)logging.info(splunk_ack_res)
配置参数注释
“震源类型”
如果此参数设置为“_json”(以及“allow_overrides”,则不会在下面描述的“fields”键中添加内置参数。 参考official Splunk documentation 有关源类型的详细信息。在
“允许覆盖”
如果此参数设置为“True”,则是否查找Splunk内置参数之一 (时间、源、主机、索引)它将覆盖自动完成的参数。 例如,“time”=1486683865.000的json记录将模拟过去Splunk的有效负载。在
JSON源类型事件的注释
参考official Splunk documentation 有关使用JSON源类型的更多信息。在
在“event”属性中嵌套JSON
在某些情况下,事件具有嵌套的json,其中包含要索引的自定义字段。在这种情况下,您必须设置“sourcetype”=“ujson”。在
“字段”
用于索引不在事件负载本身中出现的字段。 当不希望事件数据中包含特定字段时,可以使用此参数, 但是您需要额外的元数据来索引和搜索。 在上面的例子中,“机架”、“操作系统”和“架构”将包含在“字段”键中。在
使用此方法通常比嵌套JSON方法更快。 请注意,您必须将包含fields属性的HEC请求发送到/collector/event端点。否则,它们将不会被索引。在
重试逻辑
此库使用urllib3中的内置重试逻辑(重试计数器和退避因子)。 如果默认设置不可取,您可以找到有关如何最好地配置这些设置的更多信息 urllib3 documentation中的设置。在
贡献
随时提出问题或请求:
- 检查现有问题和PRs
- 派生回购,并在本地克隆它
- 为您的贡献创建一个新分支
- 推到你的叉子并提交一个拉请求
许可证
这个项目是根据MIT license的条款授权的。在
- 项目
标签: