所需帮助如何在 splunk 查询中的文件名中添加具有相同字符串日期的特定源文件

问题描述

我有一个要求,即我们每天​​都获取文件中提到的相应日期的文件

例如文件名是:

test_dev_08_07_2021.json
test_dev_09_07_2021.json
test_prod_08_07_2021.json
test_prod_09_07_2021.json

现在我们这里的要求是添加具有相同日期的文件内容。我们使用的 splunk 查询如下:

eventtype="metric:sample:example" source="test_dev_.json" OR source="test_prod_.json" | stats sum(number_of_car) as "# Total_Car  ",sum(Parked_cars) as "# Stopped_Cars",sum(Buses) as "# Total_Bus",sum(Parked_buses) as "# Stopped_Buses " by source | addcoltotals

但是它得到了所有四个文件的组合结果:

source  # Total Car # Stopped Cars  # Total Bus # Stopped  Buses
test_dev_08_07_2021.json    23  21  295 124
test_dev_09_07_2021.json    22  22  297 123
test_prod_08_07_2021.json   2   3   429 66
test_prod_09_07_2021.json   2   3   427 66
                                                                             
                           49   49  1448    379

应该添加我们试图实现的仅具有相同日期的文件内容。例如,如果在 test_dev 和 test_prod 中提到了日期 08_07_2021,那么应该只添加这两个文件内容,并且它应该显示日期为 09_07_2021 的文件的结果和相同的结果。加法后我们应该得到单独的结果。

请注意:我们每天都会收到这些文件。因此每个文件中的日期和月份范围会有所不同,我们现在无法更改文件

我们有什么办法可以完成这项任务,或者如果有人可以帮助我们进行相应的 splunk 查询,将会大有帮助。

请帮忙。

解决方法

诀窍是提取日期并将其用作分组字段。

const makeReportLink = (reportPath,reportParams = {},zoom = 100) => {
    const reportViewerUrl = 'http://report-server-host/ReportServer_SSRS/Pages/ReportViewer.aspx';
    const reportParamDefaults = {
        'rs:Command': 'Render','rc:Parameters' : 'false','rc:Zoom': zoom,};

    let reportSP = new URLSearchParams({...reportParamDefaults,...reportParams});

    return `${reportViewerUrl}?${reportPath}&${reportSP.toString()}`;
}