#
## はじめに
Splunkでデータ分析を始める際、SPL(Splunk Processing Language)の文法理解が重要です。本記事では、実務で頻繁に使用するindex検索とデータ処理の文法を、実例を交えて解説します。
## 対象読者
- Splunkを使い始めたエンジニア
- ダッシュボード構築を担当する方
- ログ分析・可視化業務に携わる方
---
## 1. 基本構文:indexからのデータ取得
### 基本形
```spl
index=<インデックス名>
実務例
index=web_logs sourcetype=access_combined
| head 100
ポイント:
-
index=で対象データソースを指定 -
sourcetype=でログの種類を絞り込み - パイプライン
|で処理を連結
2. 時間範囲の指定(Time Range)
相対時間
index=main earliest=-24h latest=now
絶対時間
index=main earliest="01/15/2026:00:00:00" latest="01/15/2026:23:59:59"
よく使う時間指定
earliest=-1d@d latest=@d # 昨日1日分
earliest=-7d@d latest=@d # 過去7日間
earliest=-1h@h latest=now # 直近1時間
@記号の意味: 時間の切り捨て(例:@d = その日の00:00:00)
3. フィールド検索とフィルタリング
完全一致検索
index=app_logs status=200
index=app_logs user="yukiko.ishiguro@example.com"
部分一致検索
index=app_logs error=*timeout*
index=app_logs url="*/api/*"
複数条件(AND/OR/NOT)
# AND条件(スペース区切りまたはAND)
index=main status=500 method=POST
# OR条件
index=main (status=500 OR status=502 OR status=503)
# NOT条件
index=main status=200 NOT user=admin
4. 統計コマンド(Stats Commands)
stats - 基本集計
index=web_logs
| stats count by status
出力例:
status | count
200 | 15234
404 | 823
500 | 45
よく使う統計関数
index=web_logs
| stats
count as total_requests,
avg(response_time) as avg_time,
max(response_time) as max_time,
min(response_time) as min_time,
sum(bytes) as total_bytes
by host
timechart - 時系列グラフ
index=web_logs
| timechart span=1h count by status
5. データ変換と加工
eval - フィールド計算
index=app_logs
| eval response_time_ms=response_time*1000
| eval status_category=case(
status>=200 AND status<300, "Success",
status>=400 AND status<500, "Client Error",
status>=500, "Server Error",
1=1, "Other"
)
rex - 正規表現で抽出
index=web_logs
| rex field=url "\/api\/(?<api_version>v\d+)\/(?<endpoint>\w+)"
where - 条件フィルタ
index=app_logs
| stats avg(response_time) as avg_time by endpoint
| where avg_time > 1.0
6. テーブル整形とソート
table - 表示フィールド選択
index=web_logs
| table _time, host, status, url, response_time
sort - ソート
index=app_logs
| stats count by user
| sort -count # 降順(-をつける)
| head 10 # 上位10件
rename - フィールド名変更
index=web_logs
| stats count as リクエスト数, avg(response_time) as 平均応答時間
7. 実務でよく使うパターン集
パターン1: エラー率の監視
index=app_logs
| stats
count as total,
count(eval(status>=500)) as errors
| eval error_rate=round(errors/total*100, 2)
パターン2: トップN抽出
index=web_logs
| stats count by url
| sort -count
| head 20
| rename url as "アクセスURL", count as "アクセス数"
パターン3: 時間帯別の傾向分析
index=app_logs
| eval hour=strftime(_time, "%H")
| stats
avg(response_time) as avg_response,
count as requests
by hour
| sort hour
パターン4: 異常検知(閾値超え)
index=system_logs
| stats avg(cpu_usage) as avg_cpu by host
| where avg_cpu > 80
| eval alert_level=case(
avg_cpu>90, "Critical",
avg_cpu>80, "Warning"
)
8. パフォーマンス最適化のコツ
✅ Good Practice
# 1. 早い段階でデータを絞り込む
index=main sourcetype=access earliest=-1h
| search status=500
| stats count by host
# 2. フィールドを明示的に指定
index=main
| fields _time, host, status, response_time
| stats avg(response_time) by host
❌ Bad Practice
# 全データを取得してから絞り込み(遅い)
index=main
| stats count by sourcetype, status, host
| search status=500
最適化ポイント:
-
index=と時間範囲を必ず指定 - 早い段階で不要なフィールドを除外
-
fieldsコマンドで必要なフィールドのみ選択 - 統計処理の前にフィルタリング
9. デバッグとトラブルシューティング
データの存在確認
| tstats count where index=main by index, sourcetype
フィールド一覧の確認
index=main
| head 1
| fields *
サンプルデータの確認
index=main earliest=-5m
| head 10
| table _time, _raw
10. よく使うショートカット・Tips
時間指定の短縮形
-
-5m= 5分前 -
-1h= 1時間前 -
-1d= 1日前 -
-1w= 1週間前 -
-1mon= 1ヶ月前
ワイルドカード活用
index=prod_* # prod_で始まる全インデックス
sourcetype=*:access # :accessで終わる全ソースタイプ
サブサーチ(Subsearch)
index=main
[search index=error_logs | return user]
| stats count by user
まとめ
Splunk SPLの実務活用ポイント:
-
基本構文の徹底:
index=→ フィルタ → 統計 → 整形の流れ - パフォーマンス意識: 早期絞り込みとフィールド選択
- 再利用可能な検索: よく使うパターンをテンプレート化
- ダッシュボード連携: 検索結果をパネルに活用
参考資料
執筆者: Python Engineer
Splunkダッシュボード構築、データ可視化、エンジニア教育コンテンツ作成を専門としています。
#Splunk #SPL #ログ分析 #データ可視化 #ダッシュボード