1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Splunk: Linuxのコマンドラインからサーチを実行する

Posted at
実施環境: Splunk Free 8.2.2
Linux
[root@testhost ~]# uname -a
Linux testhost 4.18.0-147.8.1.el8_1.x86_64 #1 SMP Thu Apr 9 13:49:54 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
[root@testhost ~]# echo $SHELL
/bin/bash

0. 概要

Splunk で SPL 文を入力してサーチを実行する場合、大抵はブラウザの GUI 画面から実行します。
ですが、時には OS のコマンドラインからサーチを実行したい場合もあります。
そのような場合に対応できるように、 Splunk には CUI のコマンドでサーチを実行する機能が存在します。
今回は、 Linux のコマンドラインから Splunk のサーチを実行してみます。

1. コマンドラインでの実行

まずは、以下のサーチをコマンドラインで実行することを考えます。

WS000430.JPG

コマンドラインでサーチを実行する際は、 splunk コマンドを使用します。
コマンドのパスは ${SPLUNK_HOME}/bin/splunk です。
このコマンドは splunk の起動/停止をはじめとしたさまざまな操作に使用するコマンドで、サーチの実行もその1つです。

基本的な文法は以下の通りです。

splunk search -earliest_time <検索期間の先頭> -latest_time <検索期間の末尾>

実際に実行してみました。

Linux
[root@testhost bin]# ./splunk search "index=\"_internal\" | stats count" -earliest_time "@h-1h" -latest_time "@h"
count
-----
15265

2. 期間指定

期間の指定は、 GUI で「詳細」を指定した場合と同様の形式で記載します。

WS000431.JPG

上記のように相対的な指定も可能ですし、決まった日時を指定することも可能です。
決まった日時を指定する場合は UNIX 時間で記載します。

Linux
[root@testhost bin]# ./splunk search "index=\"_internal\" | stats count" -earliest_time "1692403200" -latest_time "1692406800"
count
-----
15265

3. CSV 形式で出力

splunk コマンドからサーチを実行した場合のデフォルトの出力は stats コマンド等を使用しない場合元ログ、 stats コマンド等を使用する場合テーブル形式になりますが、これらの形式は少々使いにくい場合があります。

Linux
[root@testhost bin]# ./splunk search "index=\"_internal\" | head 5" -earliest_ti
me "@h-1h" -latest_time "@h"
08-19-2023 09:59:58.527 +0900 INFO  HealthChangeReporter - feature="IOWait" indicator="single_cpu__max_perc_last_3m" previous_color=yellow color=green measured_value=4
127.0.0.1 - splunk-system-user [19/Aug/2023:09:59:53.778 +0900] "GET //services/cluster/config?output_mode=json HTTP/1.0" 402 128 "-" "Python-httplib2/0.13.1 (gzip)" - 1ms
127.0.0.1 - splunk-system-user [19/Aug/2023:09:59:53.729 +0900] "GET //services/server/roles?output_mode=json HTTP/1.0" 200 780 "-" "Python-httplib2/0.13.1 (gzip)" - 1ms
08-19-2023 09:59:53.453 +0900 INFO  PeriodicHealthReporter - feature="System Check" color=green due_to_stanza="feature:wlm_system_check" node_type=feature node_path=splunkd.workload_management.system_check
08-19-2023 09:59:53.453 +0900 INFO  PeriodicHealthReporter - feature="Configuration Check" color=green due_to_stanza="feature:wlm_configuration_check" node_type=feature node_path=splunkd.workload_management.configuration_check
Linux
[root@testhost bin]# ./splunk search "index=\"_internal\" | stats count BY sourc
etype" -earliest_time "@h-1h" -latest_time "@h"
          sourcetype            count
------------------------------- -----
mongod                            252
scheduler                           8
secure_gateway_app_internal_log     4
splunk_search_messages             15
splunk_web_access                  93
splunk_web_service                682
splunkd                         13381
splunkd_access                    498
splunkd_conf                        2
splunkd_ui_access                 330

ここで便利なのが「 output 」パラメータです。
このパラメータに「 csv 」を指定することで、サーチの結果を CSV 形式で出力することができます。

Linux
[root@testhost bin]# ./splunk search "index=\"_internal\" | head 5" -earliest_time "@h-1h" -latest_time "@h" -output csv
"_serial","_time",source,sourcetype,host,index,"splunk_server","_raw"
0,"2023-08-19 09:59:58.527 JST","/opt/splunk/var/log/splunk/health.log",splunkd,testhost,"_internal",testhost,"08-19-2023 09:59:58.527 +0900 INFO  HealthChangeReporter - feature=""IOWait"" indicator=""single_cpu__max_perc_last_3m"" previous_color=yellow color=green measured_value=4"
1,"2023-08-19 09:59:53.778 JST","/opt/splunk/var/log/splunk/splunkd_access.log","splunkd_access",testhost,"_internal",testhost,"127.0.0.1 - splunk-system-user [19/Aug/2023:09:59:53.778 +0900] ""GET //services/cluster/config?output_mode=json HTTP/1.0"" 402 128 ""-"" ""Python-httplib2/0.13.1 (gzip)"" - 1ms"
2,"2023-08-19 09:59:53.729 JST","/opt/splunk/var/log/splunk/splunkd_access.log","splunkd_access",testhost,"_internal",testhost,"127.0.0.1 - splunk-system-user [19/Aug/2023:09:59:53.729 +0900] ""GET //services/server/roles?output_mode=json HTTP/1.0"" 200 780 ""-"" ""Python-httplib2/0.13.1 (gzip)"" - 1ms"
3,"2023-08-19 09:59:53.453 JST","/opt/splunk/var/log/splunk/health.log",splunkd,testhost,"_internal",testhost,"08-19-2023 09:59:53.453 +0900 INFO  PeriodicHealthReporter - feature=""System Check"" color=green due_to_stanza=""feature:wlm_system_check"" node_type=feature node_path=splunkd.workload_management.system_check"
4,"2023-08-19 09:59:53.453 JST","/opt/splunk/var/log/splunk/health.log",splunkd,testhost,"_internal",testhost,"08-19-2023 09:59:53.453 +0900 INFO  PeriodicHealthReporter - feature=""Configuration Check"" color=green due_to_stanza=""feature:wlm_configuration_check"" node_type=feature node_path=splunkd.workload_management.configuration_check"
Linux
[root@testhost bin]# ./splunk search "index=\"_internal\" | stats count BY sourcetype" -earliest_time "@h-1h" -latest_time "@h" -output csv
sourcetype,count
mongod,252
scheduler,8
"secure_gateway_app_internal_log",4
"splunk_search_messages",15
"splunk_web_access",93
"splunk_web_service",682
splunkd,13381
"splunkd_access",498
"splunkd_conf",2
"splunkd_ui_access",330

こちらの形式の方が、使いやすいことも多いと思います。

4. 備考

splunk コマンドでサーチを実行する際には他にもいくつかのパラメータが指定できます。
使用できるパラメータについては以下のサイトを参照してください。

Syntax for searches in the CLI

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?