0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Splunk クラシックダッシュボード【覚えるべきSPL集】 実務で即使える!インデックス制御パターン

Posted at

📚 クラシックダッシュボード【覚えるべきSPL集】

実務で即使える!インデックス制御パターン


🎯 クラシック vs スタジオの違い

┌─────────────────────────────────────────┐
│  Classic Dashboard(旧型)              │
├─────────────────────────────────────────┤
│  ✅ シンプルなXML設定                    │
│  ✅ 動作が軽い                          │
│  ✅ 古いSplunkバージョンでも使える       │
│  ❌ 細かいデザイン制御が難しい           │
│  ❌ UI設定が限定的                      │
└─────────────────────────────────────────┘

┌─────────────────────────────────────────┐
│  Dashboard Studio(新型)               │
├─────────────────────────────────────────┤
│  ✅ UI設定が豊富                        │
│  ✅ 細かいデザイン制御                   │
│  ✅ ドラッグ&ドロップ                    │
│  ❌ 重い                                │
│  ❌ 新しいバージョンのみ                 │
└─────────────────────────────────────────┘

📋 覚えるべきSPL【5つの基本パターン】

パターン1: インデックスからデータ取得(最重要)

index="インデックス名" sourcetype="ソースタイプ"
| timechart span=30m avg(フィールド名) as データ値

具体例:

index="server_logs" sourcetype="linux:syslog"
| timechart span=30m avg(cpu_usage) as CPU使用率

解説:

  • index="server_logs" → server_logsインデックスから検索
  • sourcetype="linux:syslog" → linux:syslogタイプだけ取得
  • timechart span=30m → 30分間隔で集計
  • avg(cpu_usage) → cpu_usageフィールドの平均値
  • as CPU使用率 → 結果に「CPU使用率」という名前を付ける

パターン2: 絶対値の基準線(固定値)

index="server_logs" sourcetype="linux:syslog"
| timechart span=30m avg(cpu_usage) as CPU使用率
| eval 警告線=80
| eval 危険線=90

結果:

時刻     CPU使用率  警告線  危険線
10:00    65        80     90
10:30    75        80     90
11:00    85        80     90
11:30    95        80     90

グラフ:

100│               ━━━━━━━━━━━━ 危険線(90)
 90│━━━━━━━━━━━━━━━━━━━━━━━━━━ 警告線(80)
    │    ╱╲    ╱╲
 50│  ╱  ╲  ╱  ╲
    └─────────────────────→

パターン3: 動的に色が変わる基準線

index="server_logs" sourcetype="linux:syslog"
| timechart span=30m avg(cpu_usage) as CPU使用率
| eval 通常線=if(CPU使用率<80, 90, null())
| eval 警告線=if(CPU使用率>=80 AND CPU使用率<90, 90, null())
| eval 危険線=if(CPU使用率>=90, 90, null())

結果:

時刻     CPU使用率  通常線  警告線  危険線
10:00    65        90     null   null   ← 通常
10:30    75        90     null   null   ← 通常
11:00    85        null   90     null   ← 警告
11:30    95        null   null   90     ← 危険

パターン4: 複数フィールドの集計

index="server_logs" sourcetype="linux:syslog"
| timechart span=30m 
    avg(cpu_usage) as CPU使用率
    avg(memory_usage) as メモリ使用率
    avg(disk_usage) as ディスク使用率

結果:

時刻     CPU使用率  メモリ使用率  ディスク使用率
10:00    65        72           55
10:30    75        78           58
11:00    85        82           60

パターン5: 条件フィルタ+集計

index="server_logs" sourcetype="linux:syslog" host="server01"
| timechart span=30m avg(cpu_usage) as CPU使用率
| eval 基準線=80

解説:

  • host="server01" → server01のデータだけ取得
  • 特定のサーバーだけを監視したい時に使う

🎓 実務パターン【レベル別】

【初級】基本の基準線(1本)

index="main" sourcetype="access_combined"
| timechart span=1h count as アクセス数
| eval 目標線=1000

用途: アクセス数の目標値を表示


【中級】2色切替の基準線

index="main" sourcetype="access_combined"
| timechart span=1h count as アクセス数
| eval 通常線=if(アクセス数<1000, 1200, null())
| eval 達成線=if(アクセス数>=1000, 1200, null())

用途: 目標達成時に線の色が変わる


【上級】3段階の基準線+複数メトリクス

index="server_logs" sourcetype="linux:syslog"
| timechart span=30m 
    avg(cpu_usage) as CPU
    avg(memory_usage) as メモリ
| eval 緑基準=if(CPU<70, 100, null())
| eval 黄基準=if(CPU>=70 AND CPU<90, 100, null())
| eval 赤基準=if(CPU>=90, 100, null())

用途: CPUとメモリを同時監視、3段階で色分け


📊 実務でよく使うSPLコマンド【チートシート】

検索コマンド

# 基本検索
index="インデックス名"

# 複数インデックス
index="logs1" OR index="logs2"

# 除外
index="logs" NOT sourcetype="error"

# ワイルドカード
index="server_*" host="web*"

# 時間指定
earliest=-7d latest=now

集計コマンド(timechart)

# 平均値
| timechart span=30m avg(field) as 平均

# 合計
| timechart span=1h sum(field) as 合計

# カウント
| timechart span=1d count as 件数

# 最大値
| timechart span=1h max(field) as 最大

# 最小値
| timechart span=1h min(field) as 最小

# 複数集計
| timechart span=1h 
    avg(field1) as 平均1
    max(field2) as 最大2
    count as 件数

計算コマンド(eval)

# 固定値
| eval 基準線=1000

# 計算
| eval 合計=A+B
| eval 差=A-B
| eval 積=A*B
| eval 商=A/B

# 条件分岐
| eval 結果=if(条件, 真, 偽)

# 複数条件
| eval 結果=if(A>10 AND B<20, "OK", "NG")

# null(非表示)
| eval 線=if(条件, 1000, null())

フィルタコマンド(where)

# 数値フィルタ
| where cpu_usage > 80

# 文字列フィルタ
| where status="error"

# 複数条件
| where cpu_usage > 80 AND memory > 90

# 範囲指定
| where cpu_usage >= 70 AND cpu_usage < 90

🎯 実践例【コピペで使える】

例1: CPU監視(3段階)

index="server_logs" sourcetype="linux:syslog"
| timechart span=30m avg(cpu_usage) as CPU使用率
| eval 通常基準=if(CPU使用率<70, 100, null())
| eval 注意基準=if(CPU使用率>=70 AND CPU使用率<90, 100, null())
| eval 警告基準=if(CPU使用率>=90, 100, null())

クラシックダッシュボードXML:

<panel>
  <chart>
    <search>
      <query>
index="server_logs" sourcetype="linux:syslog"
| timechart span=30m avg(cpu_usage) as CPU使用率
| eval 通常基準=if(CPU使用率&lt;70, 100, null())
| eval 注意基準=if(CPU使用率&gt;=70 AND CPU使用率&lt;90, 100, null())
| eval 警告基準=if(CPU使用率&gt;=90, 100, null())
      </query>
    </search>
    <option name="charting.chart">line</option>
    <option name="charting.seriesColors">[0x000000,0x00FF00,0xFFFF00,0xFF0000]</option>
  </chart>
</panel>

例2: アクセス数監視(目標達成)

index="web_logs" sourcetype="access_combined"
| timechart span=1h count as アクセス数
| eval 目標線=1000
| eval 未達成=if(アクセス数<1000, 1200, null())
| eval 達成=if(アクセス数>=1000, 1200, null())

用途:

  • 未達成: 赤い基準線
  • 達成: 緑の基準線
  • 1000アクセスが目標

例3: エラー率監視

index="app_logs"
| timechart span=10m 
    count(eval(status="error")) as エラー数
    count as 総リクエスト数
| eval エラー率=(エラー数/総リクエスト数)*100
| eval 正常線=if(エラー率<1, 5, null())
| eval 警告線=if(エラー率>=1 AND エラー率<3, 5, null())
| eval 危険線=if(エラー率>=3, 5, null())

解説:

count(eval(status="error"))
  • eval(status="error") → statusが"error"の時だけカウント
  • エラー数を計算
| eval エラー率=(エラー数/総リクエスト数)*100
  • エラー率を%で計算

例4: ディスク使用率(複数サーバー)

index="server_logs" sourcetype="df"
| timechart span=1h avg(percent_used) as ディスク使用率 by host
| eval 警告線=80
| eval 危険線=90

結果:

時刻     server01  server02  server03  警告線  危険線
10:00    65       72        55        80     90
11:00    70       75        60        80     90
12:00    75       82        65        80     90

グラフ:

100│                    ━━━━━━━━ 危険線(90)
 90│━━━━━━━━━━━━━━━━━━━━━━━━━━ 警告線(80)
    │  ━ server01
    │  ━ server02
    │  ━ server03
 50│
    └─────────────────────→

🔧 よく使う組み合わせパターン

パターンA: 時間帯フィルタ

index="logs"
| eval 時刻=tonumber(strftime(_time, "%H"))
| where 時刻>=9 AND 時刻<18
| timechart span=1h count as 営業時間アクセス
| eval 目標=500

解説:

  • 9時~18時のデータだけを集計
  • 営業時間のみの監視

パターンB: 曜日フィルタ

index="logs"
| eval 曜日=tonumber(strftime(_time, "%w"))
| where 曜日>=1 AND 曜日<=5
| timechart span=1d count as 平日アクセス
| eval 目標=10000

解説:

  • %w = 曜日(0=日曜、6=土曜)
  • 1~5(月~金)のデータだけ

パターンC: 比率計算

index="logs"
| timechart span=1h 
    count(eval(status=200)) as 成功
    count as 合計
| eval 成功率=(成功/合計)*100
| eval 目標線=95

解説:

  • HTTP 200(成功)の比率を計算
  • 95%以上が目標

パターンD: 複数条件の組み合わせ

index="logs" (status="error" OR status="warning")
| timechart span=30m count as 異常数 by status
| eval 基準線=10

解説:

  • errorとwarningだけを抽出
  • statusごとに集計
  • 10件以上で警告

📝 クラシックダッシュボード専用テクニック

テクニック1: 色の指定(XML)

<option name="charting.seriesColors">
  [0x00FF00,0xFFFF00,0xFF0000]
</option>

色の順番:

  • 1番目のフィールド: 緑 0x00FF00
  • 2番目のフィールド: 黄 0xFFFF00
  • 3番目のフィールド: 赤 0xFF0000

テクニック2: 線のスタイル(XML)

<option name="charting.chart.showDataLabels">none</option>
<option name="charting.legend.placement">bottom</option>
<option name="charting.lineWidth">2</option>

テクニック3: 凡例の非表示(XML)

<option name="charting.legend.mode">seriesCompare</option>
<option name="charting.legend.labels">
  ["データ値","通常","警告","危険"]
</option>

🎯 現場で使える実践SPL【5選】

実践1: リアルタイムCPU監視

index="server_logs" host="prod-*"
| timechart span=5m avg(cpu_usage) as CPU by host
| eval 警告=80
| eval 危険=95

特徴:

  • 本番サーバー(prod-*)を監視
  • 5分間隔でリアルタイム
  • サーバーごとに表示

実践2: アプリケーションエラー監視

index="app_logs" log_level="ERROR"
| timechart span=10m count as エラー数
| eval 注意線=if(エラー数<10, 20, null())
| eval 警告線=if(エラー数>=10 AND エラー数<50, 20, null())
| eval 緊急線=if(エラー数>=50, 20, null())

特徴:

  • エラーログだけを監視
  • 3段階のアラート
  • 50件以上で緊急

実践3: メモリリーク検知

index="server_logs"
| timechart span=1h avg(memory_used) as メモリ使用量
| eval 通常線=if(メモリ使用量<8000, 10000, null())
| eval 要調査線=if(メモリ使用量>=8000, 10000, null())

特徴:

  • メモリ使用量の増加傾向を監視
  • 8GB以上で要調査

実践4: レスポンスタイム監視

index="web_logs" sourcetype="access_combined"
| timechart span=10m avg(response_time) as レスポンスタイム
| eval 快適線=if(レスポンスタイム<200, 500, null())
| eval 遅延線=if(レスポンスタイム>=200 AND レスポンスタイム<500, 500, null())
| eval 問題線=if(レスポンスタイム>=500, 500, null())

特徴:

  • レスポンスタイム(ms)を監視
  • 200ms未満: 快適(緑)
  • 200-500ms: 遅延(黄)
  • 500ms以上: 問題(赤)

実践5: ディスク容量アラート

index="server_logs" sourcetype="df" mount="/"
| timechart span=1h latest(percent_used) as ディスク使用率 by host
| eval 安全線=if(ディスク使用率<80, 100, null())
| eval 警告線=if(ディスク使用率>=80 AND ディスク使用率<90, 100, null())
| eval 危険線=if(ディスク使用率>=90, 100, null())

特徴:

  • ルートパーティション(/)を監視
  • 80%以上で警告
  • 90%以上で危険

📋 最重要SPL【これだけは覚える】

必須パターン1: 基本構文

index="xxx" sourcetype="yyy"
| timechart span=30m avg(field) as データ値
| eval 基準線=1000

必須パターン2: 条件分岐

| eval 線=if(データ値<閾値, 位置, null())

必須パターン3: 複数条件

| eval 線=if(A>=X AND A<Y, 位置, null())

必須パターン4: 複数フィールド

| timechart span=1h 
    avg(field1) as データ1
    avg(field2) as データ2

必須パターン5: フィルタ+集計

index="xxx" host="server01"
| where field > 100
| timechart span=30m avg(field) as データ値

🎓 段階的学習パス

STEP 1: 基本の検索(1日目)

index="main"
| timechart span=1h count

STEP 2: フィールド集計(2日目)

index="main"
| timechart span=1h avg(cpu) as CPU使用率

STEP 3: 固定基準線(3日目)

index="main"
| timechart span=1h avg(cpu) as CPU使用率
| eval 基準線=80

STEP 4: 条件付き基準線(4日目)

index="main"
| timechart span=1h avg(cpu) as CPU使用率
| eval 線=if(CPU使用率>=80, 100, null())

STEP 5: 複数条件(5日目)

index="main"
| timechart span=1h avg(cpu) as CPU使用率
| eval 緑線=if(CPU使用率<70, 100, null())
| eval 黄線=if(CPU使用率>=70 AND CPU使用率<90, 100, null())
| eval 赤線=if(CPU使用率>=90, 100, null())

これで実務で使えるSPLは完璧です!🎉

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?