10日で覚えるSplunkDay 8: アラートとレポート
books.chapter 810日で覚えるSplunk

Day 8: アラートとレポート

今日学ぶこと

  • スケジュールサーチ
  • レポートの作成と共有
  • アラートの設定
  • アラートアクション
  • マクロの定義

スケジュールサーチ

サーチを定期的に自動実行する機能です。

flowchart LR
    Schedule["スケジュール<br>毎時 / 毎日 / cron"]
    Search["SPLサーチ<br>実行"]
    Result["結果"]
    Action["アクション<br>メール / アラート"]
    Schedule --> Search --> Result --> Action
    style Schedule fill:#3b82f6,color:#fff
    style Search fill:#22c55e,color:#fff
    style Result fill:#f59e0b,color:#fff
    style Action fill:#8b5cf6,color:#fff

cron式

フィールド
0-59 */5(5分ごと)
0-23 9(9時)
1-31 1(1日)
1-12 *(毎月)
曜日 0-6(日-土) 1-5(平日)
# 毎時0分
0 * * * *

# 毎日9時
0 9 * * *

# 平日の毎時
0 * * * 1-5

# 5分ごと
*/5 * * * *

# 毎月1日の0時
0 0 1 * *

レポートの作成

Web UIから作成

  1. サーチを実行
  2. Save As > Report
  3. タイトルと説明を入力
  4. スケジュールを設定(オプション)

レポートの設定

設定 説明
Title レポート名
Description 説明
Time Range 検索対象期間
Schedule 実行スケジュール
Trigger Actions 実行後のアクション

レポートのスケジュール

Title: Daily Error Summary
Schedule: 0 9 * * * (毎日9時)
Time Range: Previous day (-1d@d to @d)

レポートの共有

権限レベル 説明
Private 作成者のみ
This app only 同じアプリ内のユーザー
All apps 全アプリのユーザー

アラートの設定

アラートの種類

flowchart TB
    subgraph AlertTypes["アラートの種類"]
        Scheduled["スケジュールアラート<br>定期的にチェック"]
        Realtime["リアルタイムアラート<br>即座に検知"]
    end
    style Scheduled fill:#3b82f6,color:#fff
    style Realtime fill:#ef4444,color:#fff
種類 説明 用途
スケジュール 定期的にサーチを実行 日次レポート、定期チェック
リアルタイム 継続的に監視 即時対応が必要な異常

アラートの作成

  1. サーチを実行
index=main sourcetype=access_combined status>=500
| stats count
| where count > 10
  1. Save As > Alert
  2. 設定を入力

アラートのトリガー条件

条件 説明
Number of Results 結果数で判定 > 0
Number of Hosts ホスト数で判定 > 3
Number of Sources ソース数で判定 > 5
Custom カスタム条件 SPLで定義

スロットリング

同じアラートが短時間に大量に発生するのを防ぐ設定です。

設定 説明
Suppress 一定時間アラートを抑制
Suppress fields 特定フィールドの値ごとに抑制
Suppress for: 1 hour
Suppress if field value matches: host
→ 同じホストからのアラートは1時間に1回のみ

アラートアクション

アラートがトリガーされた時に実行するアクションです。

flowchart TB
    Alert["アラート<br>トリガー"]
    Email["メール送信"]
    Webhook["Webhook"]
    Script["スクリプト実行"]
    Log["ログ記録"]
    Ticket["チケット作成<br>ServiceNow等"]
    Alert --> Email
    Alert --> Webhook
    Alert --> Script
    Alert --> Log
    Alert --> Ticket
    style Alert fill:#ef4444,color:#fff
    style Email fill:#3b82f6,color:#fff
    style Webhook fill:#22c55e,color:#fff
    style Script fill:#f59e0b,color:#fff
    style Log fill:#8b5cf6,color:#fff
    style Ticket fill:#3b82f6,color:#fff

メールアクション

設定
To ops-team@example.com
Subject [ALERT] Server Error Count Exceeded
Message 結果のサマリーとリンク
Include 結果のCSV添付、インライン表示等

Webhookアクション

{
  "text": "Alert: $name$ triggered at $trigger_time$",
  "result": "$result$"
}

Slack、Microsoft Teams、PagerDutyなどと連携できます。

スクリプトアクション

#!/bin/bash
# $SPLUNK_HOME/bin/scripts/alert_handler.sh
echo "Alert: $1" >> /var/log/splunk_alerts.log
# 追加の処理(チケット作成、自動復旧など)

サーチマクロ

よく使うSPLの断片を再利用可能なマクロとして定義できます。

マクロの作成

Settings > Advanced search > Search macros > Add new

Name: get_errors
Definition: index=main sourcetype=access_combined status>=400

マクロの使用

`get_errors`
| stats count by status

引数付きマクロ

Name: get_errors(1)
Arguments: min_status
Definition: index=main sourcetype=access_combined status>=$min_status$
`get_errors(500)`
| stats count by host

`get_errors(400)`
| timechart span=1h count

便利なマクロの例

# エラー率の計算
Name: error_rate
Definition: stats count(eval(status>=400)) AS errors, count AS total | eval error_rate=round(errors/total*100, 2)

# 使用
index=main sourcetype=access_combined
| `error_rate`

# 時間帯のラベル
Name: time_label
Definition: eval time_label=case(date_hour>=6 AND date_hour<12, "Morning", date_hour>=12 AND date_hour<18, "Afternoon", date_hour>=18 AND date_hour<22, "Evening", 1=1, "Night")

ワークフローアクション

イベントを右クリックした時のカスタムアクションを定義できます。

ワークフローアクションの種類

種類 説明
GET link 外部URLを開く
POST link 外部URLにデータを送信
Search 新しいサーチを実行

例: IPアドレスの調査

Name: Investigate IP
Type: GET link
URI: https://www.virustotal.com/gui/ip-address/$clientip$
Apply to: field = clientip

実践: 運用監視の自動化

1. エラー急増アラート

index=main sourcetype=access_combined status>=500
| bin _time span=5m
| stats count by _time
| where count > 50

設定:

  • Schedule: */5 * * * *(5分ごと)
  • Trigger: Number of Results > 0
  • Suppress: 30分
  • Action: メール + Slack Webhook

2. 日次レポート

index=main sourcetype=access_combined earliest=-1d@d latest=@d
| stats
    count AS total_requests,
    dc(clientip) AS unique_visitors,
    avg(response_time) AS avg_response_time,
    count(eval(status>=400)) AS errors
| eval error_rate = round(errors/total_requests*100, 2) . "%"
| eval avg_response_time = round(avg_response_time, 3) . "s"

設定:

  • Schedule: 0 9 * * *(毎日9時)
  • Action: メール(CSVレポート添付)

3. ディスク使用量監視

index=_internal source=*metrics.log group=per_index_thruput
| stats sum(kb) AS total_kb by series
| eval total_gb = round(total_kb/1024/1024, 2)
| where total_gb > 10
| sort -total_gb

まとめ

概念 説明
スケジュールサーチ 定期的にサーチを自動実行
レポート 保存・共有可能なサーチ結果
アラート 条件に基づく通知
アラートアクション メール、Webhook、スクリプト等
スロットリング アラートの重複抑制
マクロ 再利用可能なSPLの断片

重要ポイント

  1. スケジュールサーチで定期的な監視を自動化
  2. アラートは条件とアクションを適切に設定
  3. スロットリングでアラート疲れを防ぐ
  4. マクロでSPLの再利用性を高める

練習問題

問題1: 基本

5分ごとにエラーイベント(status>=500)の数をチェックし、10件を超えた場合にログに記録するアラートを作成してください。

問題2: 応用

日次レポート(前日の統計)を毎朝9時にメール送信する設定を作成してください。

チャレンジ問題

引数付きサーチマクロ error_summary(2) を作成してください。引数はsourcetypethresholdで、指定されたソースタイプのエラー数が閾値を超えた場合に結果を返すようにしましょう。


参考リンク


次回予告: Day 9では「Splunkの管理」について学びます。インデックス管理、ロール設定、ナレッジオブジェクトの管理方法をマスターしましょう。