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?

SPL完全分解【1行ずつ超詳しく】 うさぎさんでもわかる!検索の仕組み

Posted at

📚 SPL完全分解【1行ずつ超詳しく】

うさぎさんでもわかる!検索の仕組み


🔍 全体像

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

これを料理で例えると:

┌─────────────────────────────────────┐
│  うさうさラーメン店の売上分析        │
└─────────────────────────────────────┘

index="server_logs"
↓
「渋谷店の伝票」から探す
(新宿店や池袋店の伝票は見ない)

sourcetype="linux:syslog"
↓
「ラーメンの注文」だけを見る
(餃子や唐揚げは見ない)

timechart span=30m
↓
「30分ごとに」集計する
(10:00-10:30、10:30-11:00...)

avg(cpu_usage)
↓
「売上金額の平均」を計算
(30分間の平均値)

as CPU使用率
↓
計算結果に「平均売上」という名前を付ける

📖 パート1: index="server_logs"

🎯 indexとは?

Splunkのデータ保管庫
┌─────────────────────────────────────┐
│  Splunk全体                         │
│  ┌───────────┐ ┌───────────┐       │
│  │index=     │ │index=     │       │
│  │web_logs   │ │server_logs│       │
│  │           │ │           │       │
│  │・アクセス  │ │・CPU      │       │
│  │・エラー    │ │・メモリ    │       │
│  │・セッション│ │・ディスク  │       │
│  └───────────┘ └───────────┘       │
│  ┌───────────┐ ┌───────────┐       │
│  │index=     │ │index=     │       │
│  │app_logs   │ │security   │       │
│  └───────────┘ └───────────┘       │
└─────────────────────────────────────┘

indexは「本棚」のようなもの:

図書館の例
┌─────────────────────────────────────┐
│  文学の棚(index="literature")      │
│  ├─ 小説                            │
│  ├─ 詩集                            │
│  └─ エッセイ                        │
│                                     │
│  技術書の棚(index="tech")         │
│  ├─ プログラミング                  │
│  ├─ ネットワーク                    │
│  └─ データベース                    │
└─────────────────────────────────────┘

「技術書の棚から本を探す」
= index="tech"

📝 index指定の例

# 1つのindexから検索
index="server_logs"

# 複数のindexから検索
index="server_logs" OR index="web_logs"

# ワイルドカード(*)を使う
index="server_*"
# server_logs, server_metrics, server_errors など
# server_で始まる全てのindex

# 除外
index="logs" NOT index="test_logs"

🎯 なぜindex指定が重要?

index指定なし(遅い)
┌─────────────────────────────────────┐
│  全てのindexを探す                   │
│  📚📚📚📚📚📚📚📚📚📚               │
│  100万件のデータから検索             │
│  ⏱️ 検索時間: 30秒                  │
└─────────────────────────────────────┘

index指定あり(速い)
┌─────────────────────────────────────┐
│  特定のindexだけ探す                 │
│  📚                                 │
│  1万件のデータから検索               │
│  ⏱️ 検索時間: 3秒                   │
└─────────────────────────────────────┘

検索が100倍速くなる!

📖 パート2: sourcetype="linux:syslog"

🎯 sourcetypeとは?

indexの中のデータの「種類」
┌─────────────────────────────────────┐
│  index="server_logs"                │
│  ┌─────────────────────────────┐   │
│  │ sourcetype="linux:syslog"   │   │
│  │ ・システムログ              │   │
│  │ ・起動/停止                 │   │
│  │ ・エラーメッセージ          │   │
│  └─────────────────────────────┘   │
│  ┌─────────────────────────────┐   │
│  │ sourcetype="apache:access"  │   │
│  │ ・アクセスログ              │   │
│  │ ・HTTPリクエスト            │   │
│  └─────────────────────────────┘   │
│  ┌─────────────────────────────┐   │
│  │ sourcetype="mysql:query"    │   │
│  │ ・SQLログ                   │   │
│  │ ・クエリ実行時間            │   │
│  └─────────────────────────────┘   │
└─────────────────────────────────────┘

レストランで例えると:

レストランの注文伝票(index="orders")
┌─────────────────────────────────────┐
│  sourcetype="ramen"(ラーメン伝票)  │
│  ・味噌ラーメン 800円               │
│  ・醤油ラーメン 750円               │
│                                     │
│  sourcetype="gyoza"(餃子伝票)     │
│  ・焼き餃子 400円                   │
│  ・水餃子 450円                     │
│                                     │
│  sourcetype="drink"(ドリンク伝票)  │
│  ・ビール 500円                     │
│  ・ウーロン茶 200円                 │
└─────────────────────────────────────┘

「ラーメンの注文だけ見たい」
= sourcetype="ramen"

📝 sourcetype指定の例

# 特定のsourcetype
sourcetype="linux:syslog"

# 複数のsourcetype
sourcetype="linux:syslog" OR sourcetype="windows:event"

# ワイルドカード
sourcetype="apache:*"
# apache:access, apache:error など

# 除外
sourcetype="*" NOT sourcetype="test:*"

🎯 よく使うsourcetype一覧

sourcetype 内容
linux:syslog Linuxシステムログ 起動/停止、エラー
apache:access Apacheアクセスログ HTTPリクエスト
apache:error Apacheエラーログ サーバーエラー
mysql:query MySQLクエリログ SQL実行履歴
windows:event Windowsイベントログ システムイベント
syslog 一般的なsyslog 汎用ログ

📖 パート3: timechart span=30m

🎯 timechartとは?

時間軸でデータを集計するコマンド

元のデータ(バラバラ)
┌─────────────────────────────────────┐
│ 時刻      CPU                       │
│ 10:05     65%                       │
│ 10:12     70%                       │
│ 10:23     68%                       │
│ 10:35     75%                       │
│ 10:48     72%                       │
│ 10:55     78%                       │
└─────────────────────────────────────┘

↓ timechart span=30m で集計

┌─────────────────────────────────────┐
│ 時刻      CPU平均                   │
│ 10:00     67.7%  ← 10:00-10:30の平均│
│ 10:30     75.0%  ← 10:30-11:00の平均│
└─────────────────────────────────────┘

📝 spanの指定方法

# 秒単位
span=30s    # 30秒
span=1s     # 1秒

# 分単位
span=1m     # 1分
span=5m     # 5分
span=15m    # 15分
span=30m    # 30分

# 時間単位
span=1h     # 1時間
span=2h     # 2時間
span=6h     # 6時間

# 日単位
span=1d     # 1日
span=7d     # 7日

# 週・月単位
span=1w     # 1週間
span=1mon   # 1ヶ月

🎯 span選択の目安

監視する期間に応じて選ぶ
┌─────────────────────────────────────┐
│  直近1時間を見る                     │
│  → span=1m または span=5m           │
│                                     │
│  直近24時間を見る                    │
│  → span=30m または span=1h          │
│                                     │
│  直近7日間を見る                     │
│  → span=1h または span=6h           │
│                                     │
│  直近1ヶ月を見る                     │
│  → span=1d                          │
│                                     │
│  直近1年を見る                       │
│  → span=1w または span=1mon         │
└─────────────────────────────────────┘

🎨 グラフの見え方

span=5m(細かい)
┌─────────────────────────────────────┐
│    ╱╲╱╲╱╲╱╲╱╲                       │
│  ╱          ╲                       │
│╱              ╲                     │
└─────────────────────────────────────┘
詳細だが、長期間を見ると重い

span=1h(標準)
┌─────────────────────────────────────┐
│    ╱╲    ╱╲                         │
│  ╱  ╲  ╱  ╲                        │
│╱      ╲╱    ╲                      │
└─────────────────────────────────────┘
バランスが良い

span=1d(粗い)
┌─────────────────────────────────────┐
│    ╱‾‾╲                             │
│  ╱    ╲                            │
│╱        ╲                          │
└─────────────────────────────────────┘
傾向把握に適している

📖 パート4: avg(cpu_usage)

🎯 avgとは?

平均値を計算する関数

元データ
┌─────────────────────────────────────┐
│ 10:05  cpu_usage=60                 │
│ 10:12  cpu_usage=70                 │
│ 10:23  cpu_usage=80                 │
└─────────────────────────────────────┘

↓ avg(cpu_usage)

平均値 = (60 + 70 + 80) ÷ 3 = 70

📝 よく使う集計関数

# 平均値
avg(field)
例: avg(cpu_usage) → 65.5

# 合計
sum(field)
例: sum(sales) → 15000

# 件数
count
例: count → 120件

# 最大値
max(field)
例: max(temperature) → 35.8

# 最小値
min(field)
例: min(temperature) → 18.2

# 最新値
latest(field)
例: latest(stock) → 50

# 最古値
earliest(field)
例: earliest(stock) → 100

🎯 集計関数の使い分け

┌─────────────────────────────────────┐
│  CPU使用率を監視                     │
│  → avg(cpu_usage)                   │
│     平均的な負荷を知りたい           │
│                                     │
│  ピーク負荷を知りたい                │
│  → max(cpu_usage)                   │
│     最大でどこまで上がったか         │
│                                     │
│  アクセス数を集計                    │
│  → count                            │
│     何回アクセスされたか             │
│                                     │
│  売上金額を集計                      │
│  → sum(amount)                      │
│     合計いくら売れたか               │
└─────────────────────────────────────┘

🎨 実例

# CPU使用率の平均(推奨)
| timechart span=30m avg(cpu_usage)

# CPU使用率の最大値(ピーク監視)
| timechart span=30m max(cpu_usage)

# アクセス数(回数)
| timechart span=1h count

# エラー数(条件付きカウント)
| timechart span=10m count(eval(status="error"))

# 売上合計
| timechart span=1d sum(sales_amount)

📖 パート5: as CPU使用率

🎯 asとは?

計算結果に「名前」を付ける

名前を付けない場合
┌─────────────────────────────────────┐
│ _time        avg(cpu_usage)         │
│ 10:00        67.5                   │
│ 10:30        72.3                   │
└─────────────────────────────────────┘
     ↑
  わかりにくい名前

名前を付けた場合
┌─────────────────────────────────────┐
│ _time        CPU使用率               │
│ 10:00        67.5                   │
│ 10:30        72.3                   │
└─────────────────────────────────────┘
     ↑
  わかりやすい名前!

📝 as の使い方

# 基本形
| timechart span=30m avg(cpu_usage) as CPU使用率

# 複数のフィールドに名前を付ける
| timechart span=30m 
    avg(cpu_usage) as CPU使用率
    avg(memory_usage) as メモリ使用率
    avg(disk_usage) as ディスク使用率

# 日本語も使える
| timechart span=1h count as アクセス数

# 英語でもOK
| timechart span=1h count as access_count

🎯 なぜasが必要?

理由1: グラフの凡例がわかりやすくなる
┌─────────────────────────────────────┐
│  グラフのタイトル                    │
│  ━ avg(cpu_usage)  ← わかりにくい   │
│  ━ CPU使用率       ← わかりやすい!  │
└─────────────────────────────────────┘

理由2: 後続の処理で使いやすい
| timechart span=30m avg(cpu_usage) as CPU使用率
| eval 警告=if(CPU使用率>80, "警告", "正常")
              ↑
         この名前を使える

理由3: ダッシュボードで表示される

🎯 全体を通して理解

ステップ1: データを絞り込む

index="server_logs" sourcetype="linux:syslog"

イメージ:

全データ(100万件)
┌─────────────────────────────────────┐
│  🗄️ 全てのindex                     │
│  └─ 📁 server_logs(選択)          │
│      └─ 📄 linux:syslog(選択)     │
│      └─ 📄 apache:access            │
│      └─ 📄 mysql:query              │
└─────────────────────────────────────┘
         ↓
絞り込み後(1万件)
┌─────────────────────────────────────┐
│  📄 linux:syslogだけ                │
│  10:05  cpu=65                      │
│  10:12  cpu=70                      │
│  10:23  cpu=68                      │
│  ...                                │
└─────────────────────────────────────┘

ステップ2: 時間で集計

| timechart span=30m

イメージ:

バラバラのデータ
┌─────────────────────────────────────┐
│  10:05  cpu=65                      │
│  10:12  cpu=70                      │
│  10:23  cpu=68                      │
│  10:35  cpu=75                      │
│  10:48  cpu=72                      │
└─────────────────────────────────────┘
         ↓ 30分ごとにまとめる
┌─────────────────────────────────────┐
│  10:00-10:30 グループ               │
│    65, 70, 68                       │
│                                     │
│  10:30-11:00 グループ               │
│    75, 72                           │
└─────────────────────────────────────┘

ステップ3: 平均を計算

avg(cpu_usage)

イメージ:

10:00-10:30 グループ
┌─────────────────────────────────────┐
│  65, 70, 68                         │
│  平均 = (65+70+68)÷3 = 67.7        │
└─────────────────────────────────────┘

10:30-11:00 グループ
┌─────────────────────────────────────┐
│  75, 72                             │
│  平均 = (75+72)÷2 = 73.5           │
└─────────────────────────────────────┘

ステップ4: 名前を付ける

as CPU使用率

イメージ:

最終結果
┌─────────────────────────────────────┐
│ 時刻      CPU使用率                  │
│ 10:00     67.7                      │
│ 10:30     73.5                      │
└─────────────────────────────────────┘
      ↑
わかりやすい名前で表示

🎓 実践練習

練習1: 基本の検索

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

何が起きる?

  1. server_logsインデックスから
  2. linux:syslogタイプのデータを取得
  3. 1時間ごとに
  4. cpu_usageの平均値を計算
  5. 「CPU使用率」という名前で表示

練習2: 複数フィールド

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

結果:

┌───────────────────────────────────┐
│ 時刻      CPU使用率  メモリ使用率 │
│ 10:00     67.7      72.3         │
│ 10:30     73.5      78.1         │
└───────────────────────────────────┘

練習3: 異なる集計

index="web_logs" sourcetype="access_combined"
| timechart span=10m 
    count as アクセス数
    avg(response_time) as 平均応答時間

結果:

┌─────────────────────────────────────┐
│ 時刻    アクセス数  平均応答時間    │
│ 10:00   1250       185ms           │
│ 10:10   1380       192ms           │
└─────────────────────────────────────┘

📋 まとめ【5つの要素】

┌─────────────────────────────────────┐
│  1️⃣ index="server_logs"            │
│     どの本棚から探すか              │
│                                     │
│  2️⃣ sourcetype="linux:syslog"      │
│     どの種類のデータか              │
│                                     │
│  3️⃣ timechart span=30m             │
│     何分ごとに集計するか            │
│                                     │
│  4️⃣ avg(cpu_usage)                 │
│     どう計算するか(平均)          │
│                                     │
│  5️⃣ as CPU使用率                   │
│     結果に何という名前を付けるか    │
└─────────────────────────────────────┘

🎯 即使えるテンプレート

index="あなたのindex" sourcetype="あなたのsourcetype"
| timechart span=30m avg(あなたのフィールド) as わかりやすい名前

実例:

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

# アクセス数監視
index="web_logs" sourcetype="access_combined"
| timechart span=1h count as アクセス数

# エラー監視
index="app_logs" sourcetype="application"
| timechart span=10m count(eval(level="ERROR")) as エラー数

# メモリ監視
index="server_logs" sourcetype="vmstat"
| timechart span=1h avg(memory_used) as メモリ使用量

これで5つの要素が完璧に理解できましたか?😊

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?