You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否创建Splunk仪表板批量执行指定时段内所有启用的关联搜索?

Splunk批量执行指定时段关联搜索的仪表板实现

核心实现思路

通过Splunk REST API抓取所有已启用的关联搜索,结合仪表板的时间输入控件指定目标时段,用自定义按钮触发批量调用dispatch接口,自动执行这些搜索并触发警报动作,避免手动逐个运行的麻烦。


1. 仪表板Simple XML代码

直接创建新仪表板,替换为以下代码:

<dashboard version="1.1" script="batch_correlation.js">
  <label>批量执行关联搜索</label>
  <description>一键触发指定时段内所有已启用的关联搜索</description>
  <row>
    <panel>
      <input type="time" token="time_range" searchWhenChanged="false">
        <label>目标执行时段</label>
        <default>
          <earliest>-24h@h</earliest>
          <latest>now</latest>
        </default>
      </input>
      <button id="batch_execute">
        <label>批量执行所有启用的关联搜索</label>
      </button>
    </panel>
  </row>
  <row>
    <panel>
      <table>
        <title>待执行的关联搜索列表</title>
        <search id="get_enabled_searches">
          <query>| rest /services/saved/searches splunk_server=local 
| where is_scheduled=1 AND disabled=0 AND action.correlationsearch.enabled=1 
| titlecase name 
| table name eai:acl.app</query>
          <earliest>-1m</earliest>
          <latest>now</latest>
        </search>
        <option name="drilldown">none</option>
      </table>
    </panel>
  </row>
</dashboard>

2. 配套JavaScript脚本

在你的Splunk应用目录(比如$SPLUNK_HOME/etc/apps/你的应用名/appserver/static/)下创建batch_correlation.js文件,内容如下:

require([
    'splunkjs/mvc',
    'splunkjs/mvc/utils',
    'jquery',
    'splunkjs/mvc/tokenutils'
], function(Mvc, Utils, $, TokenUtils) {
    const batchButton = $('#batch_execute');
    const timePicker = Mvc.Components.get('time_range');

    batchButton.on('click', function() {
        const timeRange = timePicker.val();
        const earliest = timeRange.earliest;
        const latest = timeRange.latest;

        // 获取已启用的关联搜索列表
        const searchManager = Mvc.Components.get('get_enabled_searches');
        const results = searchManager.data('results');

        results.on('data', function() {
            const rows = results.data().rows;
            rows.forEach(row => {
                const searchName = row[0];
                const appName = row[1];
                // 构造API请求触发搜索执行
                const apiUrl = Utils.make_url(`/servicesNS/nobody/${appName}/saved/searches/${encodeURIComponent(searchName)}/dispatch`);
                $.post(apiUrl, {
                    earliest_time: earliest,
                    latest_time: latest,
                    trigger_actions: '1' // 确保触发警报动作
                }).done(() => {
                    console.log(`已成功触发:${searchName}`);
                }).fail(err => {
                    console.error(`触发失败:${searchName},错误信息:${err.responseText}`);
                });
            });
            alert('批量执行已启动,可前往「活动」页面查看任务进度');
        });
        // 手动刷新搜索获取最新列表
        searchManager.startSearch();
    });
});

3. 权限配置

  • 运行仪表板的角色需要拥有rest_properties_get(读取搜索配置)和rest_properties_edit(触发搜索执行)权限
  • 确保关联搜索本身的权限允许该角色执行

4. 使用步骤

  1. 将Simple XML导入到Splunk仪表板
  2. 上传JavaScript脚本到对应应用的static目录
  3. 选择需要补跑的故障时段,点击「批量执行所有启用的关联搜索」按钮即可自动执行

内容的提问来源于stack exchange,提问作者Naunidh gulati

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 16:52:54