如何通过';时间';使用splunk Python SDK查询splunk企业?

如何通过';时间';使用splunk Python SDK查询splunk企业?,python,python-requests,splunk,splunk-query,Python,Python Requests,Splunk,Splunk Query,我正在尝试从Python(EclipseIDE)传递查询以从中提取数据 SPLUNK Enterprise上的特定仪表板。我能得到数据 通过传递所需查询在我的控制台上打印,但我不是 能够提取特定时间间隔的数据(如我需要数据 1小时、1天、1周或1个月) 我在查询中尝试了“最早”、“最晚”等命令,但每次它抛出一个错误,声明“raise HTTPError(response)splunklib.binding.HTTPError:HTTP 400错误请求--搜索工厂:未知搜索命令“最早” 这是我的代

我正在尝试从Python(EclipseIDE)传递查询以从中提取数据 SPLUNK Enterprise上的特定仪表板。我能得到数据 通过传递所需查询在我的控制台上打印,但我不是 能够提取特定时间间隔的数据(如我需要数据 1小时、1天、1周或1个月)

我在查询中尝试了“最早”、“最晚”等命令,但每次它抛出一个错误,声明“raise HTTPError(response)splunklib.binding.HTTPError:HTTP 400错误请求--搜索工厂:未知搜索命令“最早”

这是我的代码

import splunklib.client as client
import splunklib.results as results


HOST = "my hostname"
PORT = 8089
USERNAME = "my username"
PASSWORD = "my password"
service = client.connect(
host=HOST,
port=PORT, 
username=USERNAME,
password=PASSWORD)
rr = results.ResultsReader(service.jobs.export("search index=ccmjimmie | stats count(eval(resCode!=00200)) AS errored | chart sum(errored)|earliest=-1d"))

for result in rr:
    if isinstance(result, results.Message):
    # Diagnostic messages might be returned in the results
        print(result.type, result.message)
    elif isinstance(result, dict):
    # Normal events are returned as dicts
        print (result)
assert rr.is_preview == False
我在不使用时间查询的情况下获得的输出

OrderedDict([('sum(errored)', '1566')])
OrderedDict([('sum(errored)', '4404')])
OrderedDict([('sum(errored)', '6655')])
OrderedDict([('sum(errored)', '8992')])
etc...
此输出与预期相同,但不受时间限制。我想要相同的输出,但在给定的时间间隔内。时间间隔应该从上述Python代码中的搜索查询“serch.jobs.export()”传递

请让我知道如何将“时间”查询与所需查询一起传递


非常感谢您的帮助!提前谢谢

您必须将最早的时间放在搜索的开始处。到目前为止的-1天示例:

“搜索索引=ccmjimmie最早=-1d |统计计数(评估(重新编码!=00200))为错误|图表总和(错误)”


详细信息请参见此处:

我可能使用了不同的进程通过Python运行Splunk查询,并以JSON获取搜索结果。然而,通过这种方式打发“时间”是非常方便的

在这里,我通过在post请求的请求体中传递最早和最晚的时间变量来实现

post_data = { 'id' : unique_id,
              'search' : search_query,
              'earliest_time' : '1',
              'latest_time' : 'now',
            }
您可以在此处找到完整的详细信息: