0
1

More than 1 year has passed since last update.

Splunk: Splunkカスタムコマンド入門(1/4) - とりあえず作ってみる

Posted at
実施環境

CentOS 8-Stream

Splunk Free 8.2.2

Python 3.7.10

splunk-sdk-python 1.7.3

0. 概要

Splunk には、数多くのコマンドや機能が存在します。それらを使用すれば、大抵のことは実現することができます。
ただ、ときには、それらのコマンドや機能だけでは実現が難しいことを Splunk で行いたい場合があるかもしれません。
そのようなときのために、 Splunk にはコマンドを自作するための開発キットが存在します。
本記事ではそれを用いて、 Splunk コマンドを作成する流れを紹介していきます。

1. splunk-sdk-python の配置

まずは、 GitHub (ざっくりいうとプログラムの共有サービス)から splunk-sdk-python をダウンロードします。

GitHub

WS000600.JPG

ダウンロードした ZIP ファイルをサーバ上に転送し解凍します。

Linux
[root@testhost test]# ls -l
合計 504
-rw-rw-r--. 1 testuser testuser 514607  2月 27 22:56 splunk-sdk-python-master.zip
[root@testhost test]# unzip splunk-sdk-python-master.zip
Archive:  splunk-sdk-python-master.zip
(省略)
[root@testhost test]# ls -l
合計 508
drwxr-xr-x. 8 root     root       4096  2月 13 14:59 splunk-sdk-python-master
-rw-rw-r--. 1 testuser testuser 514607  2月 27 22:56 splunk-sdk-python-master.zip

解凍されたファイルが以下となります。

Linux
[root@testhost test]# ls -lR splunk-sdk-python-master/
splunk-sdk-python-master/:
合計 132
-rw-r--r--. 1 root root 34362  2月 13 14:59 CHANGELOG.md
-rw-r--r--. 1 root root  1578  2月 13 14:59 CONTRIBUTING.md
-rw-r--r--. 1 root root 22530  2月 13 14:59 Commands.conf.spec.xlsx
-rwxr-xr-x. 1 root root 11358  2月 13 14:59 LICENSE
-rw-r--r--. 1 root root    17  2月 13 14:59 MANIFEST.in
-rw-r--r--. 1 root root  2203  2月 13 14:59 Makefile
-rw-r--r--. 1 root root 14701  2月 13 14:59 README.md
-rw-r--r--. 1 root root  1380  2月 13 14:59 docker-compose.yml
drwxr-xr-x. 4 root root  4096  2月 13 14:59 docs
-rw-r--r--. 1 root root   116  2月 13 14:59 pytest.ini
drwxr-xr-x. 3 root root    67  2月 13 14:59 scripts
-rwxr-xr-x. 1 root root  4401  2月 13 14:59 setup.py
-rw-r--r--. 1 root root   728  2月 13 14:59 sitecustomize.py
drwxr-xr-x. 4 root root   149  2月 13 14:59 splunklib
drwxr-xr-x. 5 root root  4096  2月 13 14:59 tests
-rw-r--r--. 1 root root  1136  2月 13 14:59 tox.ini
drwxr-xr-x. 2 root root    43  2月 13 14:59 utils

splunk-sdk-python-master/docs:
合計 60
drwxr-xr-x. 2 root root   55  2月 13 14:59 CSS
-rw-r--r--. 1 root root 5717  2月 13 14:59 Makefile
drwxr-xr-x. 2 root root   25  2月 13 14:59 _templates
-rw-r--r--. 1 root root  494  2月 13 14:59 binding.rst
-rw-r--r--. 1 root root 3706  2月 13 14:59 client.rst
-rw-r--r--. 1 root root 8171  2月 13 14:59 conf.py
-rw-r--r--. 1 root root  151  2月 13 14:59 data.rst
-rw-r--r--. 1 root root 4016  2月 13 14:59 index.rst
-rw-r--r--. 1 root root 5114  2月 13 14:59 make.bat
-rw-r--r--. 1 root root  381  2月 13 14:59 modularinput.rst
-rw-r--r--. 1 root root  177  2月 13 14:59 munge_links.sh
-rw-r--r--. 1 root root  129  2月 13 14:59 results.rst
-rw-r--r--. 1 root root 3134  2月 13 14:59 searchcommands.rst
-rw-r--r--. 1 root root  253  2月 13 14:59 searchcommandsvalidators.rst

splunk-sdk-python-master/docs/CSS:
合計 8
-rw-r--r--. 1 root root 1650  2月 13 14:59 epub.css
-rw-r--r--. 1 root root   33  2月 13 14:59 splunk_customizations.css

splunk-sdk-python-master/docs/_templates:
合計 4
-rw-r--r--. 1 root root 128  2月 13 14:59 layout.html

splunk-sdk-python-master/scripts:
合計 8
-rw-r--r--. 1 root root 3354  2月 13 14:59 build-env.py
drwxr-xr-x. 2 root root   26  2月 13 14:59 templates
-rw-r--r--. 1 root root   87  2月 13 14:59 test_specific.sh

splunk-sdk-python-master/scripts/templates:
合計 4
-rw-r--r--. 1 root root 387  2月 13 14:59 env.template

splunk-sdk-python-master/splunklib:
合計 280
-rw-r--r--. 1 root root   1371  2月 13 14:59 __init__.py
-rw-r--r--. 1 root root  60209  2月 13 14:59 binding.py
-rw-r--r--. 1 root root 150652  2月 13 14:59 client.py
-rw-r--r--. 1 root root   8508  2月 13 14:59 data.py
drwxr-xr-x. 2 root root    192  2月 13 14:59 modularinput
-rw-r--r--. 1 root root  13892  2月 13 14:59 results.py
drwxr-xr-x. 2 root root   4096  2月 13 14:59 searchcommands
-rw-r--r--. 1 root root  34536  2月 13 14:59 six.py

splunk-sdk-python-master/splunklib/modularinput:
合計 48
-rw-r--r--. 1 root root  400  2月 13 14:59 __init__.py
-rw-r--r--. 1 root root 4219  2月 13 14:59 argument.py
-rw-r--r--. 1 root root 4456  2月 13 14:59 event.py
-rw-r--r--. 1 root root 2878  2月 13 14:59 event_writer.py
-rw-r--r--. 1 root root 1888  2月 13 14:59 input_definition.py
-rw-r--r--. 1 root root 3073  2月 13 14:59 scheme.py
-rw-r--r--. 1 root root 6597  2月 13 14:59 script.py
-rw-r--r--. 1 root root 2729  2月 13 14:59 utils.py
-rw-r--r--. 1 root root 2775  2月 13 14:59 validation_definition.py

splunk-sdk-python-master/splunklib/searchcommands:
合計 176
-rw-r--r--. 1 root root  6330  2月 13 14:59 __init__.py
-rw-r--r--. 1 root root 15657  2月 13 14:59 decorators.py
-rw-r--r--. 1 root root  4683  2月 13 14:59 environment.py
-rw-r--r--. 1 root root  5432  2月 13 14:59 eventing_command.py
-rw-r--r--. 1 root root  7872  2月 13 14:59 external_search_command.py
-rw-r--r--. 1 root root 18949  2月 13 14:59 generating_command.py
-rw-r--r--. 1 root root 28820  2月 13 14:59 internals.py
-rw-r--r--. 1 root root  9697  2月 13 14:59 reporting_command.py
-rw-r--r--. 1 root root 40702  2月 13 14:59 search_command.py
-rw-r--r--. 1 root root  6778  2月 13 14:59 streaming_command.py
-rw-r--r--. 1 root root 13226  2月 13 14:59 validators.py

splunk-sdk-python-master/tests:
合計 252
-rw-r--r--. 1 root root  1942  2月 13 14:59 README.md
-rw-r--r--. 1 root root     5  2月 13 14:59 __init__.py
drwxr-xr-x. 3 root root   127  2月 13 14:59 data
drwxr-xr-x. 3 root root  4096  2月 13 14:59 modularinput
drwxr-xr-x. 4 root root  4096  2月 13 14:59 searchcommands
-rwxr-xr-x. 1 root root  1023  2月 13 14:59 test_all.py
-rwxr-xr-x. 1 root root  3942  2月 13 14:59 test_app.py
-rwxr-xr-x. 1 root root 38891  2月 13 14:59 test_binding.py
-rwxr-xr-x. 1 root root 12306  2月 13 14:59 test_collection.py
-rwxr-xr-x. 1 root root  3799  2月 13 14:59 test_conf.py
-rwxr-xr-x. 1 root root  9178  2月 13 14:59 test_data.py
-rwxr-xr-x. 1 root root  3388  2月 13 14:59 test_event_type.py
-rwxr-xr-x. 1 root root  3911  2月 13 14:59 test_fired_alert.py
-rwxr-xr-x. 1 root root  7702  2月 13 14:59 test_index.py
-rwxr-xr-x. 1 root root 11825  2月 13 14:59 test_input.py
-rwxr-xr-x. 1 root root 17905  2月 13 14:59 test_job.py
-rwxr-xr-x. 1 root root  2813  2月 13 14:59 test_kvstore_batch.py
-rwxr-xr-x. 1 root root  4027  2月 13 14:59 test_kvstore_conf.py
-rwxr-xr-x. 1 root root  4269  2月 13 14:59 test_kvstore_data.py
-rwxr-xr-x. 1 root root  1682  2月 13 14:59 test_logger.py
-rwxr-xr-x. 1 root root  2139  2月 13 14:59 test_message.py
-rwxr-xr-x. 1 root root  1961  2月 13 14:59 test_modular_input.py
-rwxr-xr-x. 1 root root  2864  2月 13 14:59 test_modular_input_kinds.py
-rwxr-xr-x. 1 root root  5256  2月 13 14:59 test_results.py
-rwxr-xr-x. 1 root root  4155  2月 13 14:59 test_role.py
-rwxr-xr-x. 1 root root 10308  2月 13 14:59 test_saved_search.py
-rwxr-xr-x. 1 root root 14723  2月 13 14:59 test_service.py
-rw-r--r--. 1 root root  9712  2月 13 14:59 test_storage_passwords.py
-rwxr-xr-x. 1 root root  3308  2月 13 14:59 test_user.py
-rw-r--r--. 1 root root  3224  2月 13 14:59 test_utils.py
-rw-r--r--. 1 root root  9595  2月 13 14:59 testlib.py

splunk-sdk-python-master/tests/data:
合計 6360
drwxr-xr-x. 2 root root    4096  2月 13 14:59 custom_search
-rw-r--r--. 1 root root 6490116  2月 13 14:59 results.xml
-rw-r--r--. 1 root root    2609  2月 13 14:59 services.server.info.xml
-rw-r--r--. 1 root root    4330  2月 13 14:59 services.xml
-rw-r--r--. 1 root root    1018  2月 13 14:59 streaming_results.xml

splunk-sdk-python-master/tests/data/custom_search:
合計 152
-rw-r--r--. 1 root root  6842  2月 13 14:59 hashtags.baseline
-rw-r--r--. 1 root root  7175  2月 13 14:59 hashtags.in
-rw-r--r--. 1 root root 26288  2月 13 14:59 multibyte_input.gz
-rw-r--r--. 1 root root   144  2月 13 14:59 tophashtags.baseline
-rw-r--r--. 1 root root  7377  2月 13 14:59 tophashtags.in
-rw-r--r--. 1 root root   134  2月 13 14:59 usercount.baseline
-rw-r--r--. 1 root root 25583  2月 13 14:59 usercount.in
-rw-r--r--. 1 root root 63222  2月 13 14:59 v1_search_input.gz

splunk-sdk-python-master/tests/modularinput:
合計 40
-rw-r--r--. 1 root root    0  2月 13 14:59 __init__.py
drwxr-xr-x. 2 root root 4096  2月 13 14:59 data
-rw-r--r--. 1 root root 1081  2月 13 14:59 modularinput_testlib.py
-rw-r--r--. 1 root root 4809  2月 13 14:59 test_event.py
-rw-r--r--. 1 root root 2756  2月 13 14:59 test_input_definition.py
-rw-r--r--. 1 root root 4909  2月 13 14:59 test_scheme.py
-rw-r--r--. 1 root root 6598  2月 13 14:59 test_script.py
-rw-r--r--. 1 root root 1705  2月 13 14:59 test_validation_definition.py

splunk-sdk-python-master/tests/modularinput/data:
合計 56
-rw-r--r--. 1 root root  161  2月 13 14:59 argument_with_defaults.xml
-rw-r--r--. 1 root root  279  2月 13 14:59 argument_without_defaults.xml
-rw-r--r--. 1 root root  730  2月 13 14:59 conf_with_0_inputs.xml
-rw-r--r--. 1 root root 2384  2月 13 14:59 conf_with_2_inputs.xml
-rw-r--r--. 1 root root 1358  2月 13 14:59 conf_with_invalid_inputs.xml
-rw-r--r--. 1 root root  267  2月 13 14:59 event_maximal.xml
-rw-r--r--. 1 root root  155  2月 13 14:59 event_minimal.xml
-rw-r--r--. 1 root root  298  2月 13 14:59 scheme_with_defaults.xml
-rw-r--r--. 1 root root  942  2月 13 14:59 scheme_without_defaults.xml
-rw-r--r--. 1 root root 1002  2月 13 14:59 scheme_without_defaults_and_argument_title.xml
-rw-r--r--. 1 root root  286  2月 13 14:59 stream_with_one_event.xml
-rw-r--r--. 1 root root  626  2月 13 14:59 stream_with_two_events.xml
-rw-r--r--. 1 root root 1808  2月 13 14:59 validation.xml
-rw-r--r--. 1 root root   59  2月 13 14:59 validation_error.xml

splunk-sdk-python-master/tests/searchcommands:
合計 132
-rw-r--r--. 1 root root  1488  2月 13 14:59 __init__.py
drwxr-xr-x. 4 root root    85  2月 13 14:59 apps
-rw-r--r--. 1 root root  2724  2月 13 14:59 chunked_data_stream.py
drwxr-xr-x. 6 root root    90  2月 13 14:59 test_apps
-rw-r--r--. 1 root root  8673  2月 13 14:59 test_builtin_options.py
-rw-r--r--. 1 root root  6540  2月 13 14:59 test_configuration_settings.py
-rwxr-xr-x. 1 root root  8949  2月 13 14:59 test_csc_apps.py
-rwxr-xr-x. 1 root root 18558  2月 13 14:59 test_decorators.py
-rw-r--r--. 1 root root  2982  2月 13 14:59 test_generator_command.py
-rwxr-xr-x. 1 root root 12644  2月 13 14:59 test_internals_v1.py
-rwxr-xr-x. 1 root root 13779  2月 13 14:59 test_internals_v2.py
-rw-r--r--. 1 root root  1004  2月 13 14:59 test_multibyte_processing.py
-rw-r--r--. 1 root root  1143  2月 13 14:59 test_reporting_command.py
-rwxr-xr-x. 1 root root 11177  2月 13 14:59 test_search_command.py
-rw-r--r--. 1 root root  3263  2月 13 14:59 test_streaming_command.py
-rwxr-xr-x. 1 root root 11959  2月 13 14:59 test_validators.py

splunk-sdk-python-master/tests/searchcommands/apps:
合計 0
drwxr-xr-x. 4 root root 52  2月 13 14:59 app_with_logging_configuration
drwxr-xr-x. 4 root root 32  2月 13 14:59 app_without_logging_configuration

splunk-sdk-python-master/tests/searchcommands/apps/app_with_logging_configuration:
合計 4
drwxr-xr-x. 2 root root   29  2月 13 14:59 bin
drwxr-xr-x. 2 root root   58  2月 13 14:59 default
-rw-r--r--. 1 root root 1563  2月 13 14:59 logging.conf

splunk-sdk-python-master/tests/searchcommands/apps/app_with_logging_configuration/bin:
合計 0
-rw-r--r--. 1 root root 0  2月 13 14:59 empty-directory

splunk-sdk-python-master/tests/searchcommands/apps/app_with_logging_configuration/default:
合計 8
-rw-r--r--. 1 root root 1563  2月 13 14:59 alternative-logging.conf
-rw-r--r--. 1 root root 1563  2月 13 14:59 logging.conf

splunk-sdk-python-master/tests/searchcommands/apps/app_without_logging_configuration:
合計 0
drwxr-xr-x. 2 root root 29  2月 13 14:59 bin
drwxr-xr-x. 2 root root 29  2月 13 14:59 default

splunk-sdk-python-master/tests/searchcommands/apps/app_without_logging_configuration/bin:
合計 0
-rw-r--r--. 1 root root 0  2月 13 14:59 empty-directory

splunk-sdk-python-master/tests/searchcommands/apps/app_without_logging_configuration/default:
合計 0
-rw-r--r--. 1 root root 0  2月 13 14:59 empty-directory

splunk-sdk-python-master/tests/searchcommands/test_apps:
合計 0
drwxr-xr-x. 5 root root 48  2月 13 14:59 eventing_app
drwxr-xr-x. 5 root root 48  2月 13 14:59 generating_app
drwxr-xr-x. 5 root root 48  2月 13 14:59 reporting_app
drwxr-xr-x. 5 root root 48  2月 13 14:59 streaming_app

splunk-sdk-python-master/tests/searchcommands/test_apps/eventing_app:
合計 0
drwxr-xr-x. 2 root root 28  2月 13 14:59 bin
drwxr-xr-x. 2 root root 43  2月 13 14:59 default
drwxr-xr-x. 2 root root 26  2月 13 14:59 metadata

splunk-sdk-python-master/tests/searchcommands/test_apps/eventing_app/bin:
合計 4
-rw-r--r--. 1 root root 1549  2月 13 14:59 eventingcsc.py

splunk-sdk-python-master/tests/searchcommands/test_apps/eventing_app/default:
合計 8
-rw-r--r--. 1 root root 206  2月 13 14:59 app.conf
-rw-r--r--. 1 root root  80  2月 13 14:59 commands.conf

splunk-sdk-python-master/tests/searchcommands/test_apps/eventing_app/metadata:
合計 4
-rw-r--r--. 1 root root 495  2月 13 14:59 default.meta

splunk-sdk-python-master/tests/searchcommands/test_apps/generating_app:
合計 0
drwxr-xr-x. 2 root root 30  2月 13 14:59 bin
drwxr-xr-x. 2 root root 43  2月 13 14:59 default
drwxr-xr-x. 2 root root 26  2月 13 14:59 metadata

splunk-sdk-python-master/tests/searchcommands/test_apps/generating_app/bin:
合計 4
-rw-r--r--. 1 root root 1441  2月 13 14:59 generatingcsc.py

splunk-sdk-python-master/tests/searchcommands/test_apps/generating_app/default:
合計 8
-rw-r--r--. 1 root root 210  2月 13 14:59 app.conf
-rw-r--r--. 1 root root  84  2月 13 14:59 commands.conf

splunk-sdk-python-master/tests/searchcommands/test_apps/generating_app/metadata:
合計 4
-rw-r--r--. 1 root root 497  2月 13 14:59 default.meta

splunk-sdk-python-master/tests/searchcommands/test_apps/reporting_app:
合計 0
drwxr-xr-x. 2 root root 29  2月 13 14:59 bin
drwxr-xr-x. 2 root root 43  2月 13 14:59 default
drwxr-xr-x. 2 root root 26  2月 13 14:59 metadata

splunk-sdk-python-master/tests/searchcommands/test_apps/reporting_app/bin:
合計 4
-rw-r--r--. 1 root root 2154  2月 13 14:59 reportingcsc.py

splunk-sdk-python-master/tests/searchcommands/test_apps/reporting_app/default:
合計 8
-rw-r--r--. 1 root root 208  2月 13 14:59 app.conf
-rw-r--r--. 1 root root  82  2月 13 14:59 commands.conf

splunk-sdk-python-master/tests/searchcommands/test_apps/reporting_app/metadata:
合計 4
-rw-r--r--. 1 root root 496  2月 13 14:59 default.meta

splunk-sdk-python-master/tests/searchcommands/test_apps/streaming_app:
合計 0
drwxr-xr-x. 2 root root 29  2月 13 14:59 bin
drwxr-xr-x. 2 root root 43  2月 13 14:59 default
drwxr-xr-x. 2 root root 26  2月 13 14:59 metadata

splunk-sdk-python-master/tests/searchcommands/test_apps/streaming_app/bin:
合計 4
-rw-r--r--. 1 root root 1338  2月 13 14:59 streamingcsc.py

splunk-sdk-python-master/tests/searchcommands/test_apps/streaming_app/default:
合計 8
-rw-r--r--. 1 root root 208  2月 13 14:59 app.conf
-rw-r--r--. 1 root root  82  2月 13 14:59 commands.conf

splunk-sdk-python-master/tests/searchcommands/test_apps/streaming_app/metadata:
合計 4
-rw-r--r--. 1 root root 496  2月 13 14:59 default.meta

splunk-sdk-python-master/utils:
合計 12
-rw-r--r--. 1 root root 3711  2月 13 14:59 __init__.py
-rw-r--r--. 1 root root 4353  2月 13 14:59 cmdopts.py

ここでコマンド実行に最低限必要なのは、「 splunklib 」ディレクトリです。
これを Splunk のプロセスを起動しているユーザが触れる場所に置きます。
今回は、「 /opt/splunk/etc/apps/search/ 」ディレクトリの下に「 lib 」フォルダを作成してそこに配置しましょう。
ファイルやディレクトリの権限変更も忘れずに。

Splunk
[root@testhost test]# mkdir /opt/splunk/etc/apps/search/lib
[root@testhost test]# chmod 755 /opt/splunk/etc/apps/search/lib
[root@testhost test]# chown splunk.splunk /opt/splunk/etc/apps/search/lib
[root@testhost test]# ls -l /opt/splunk/etc/apps/search
合計 8
drwxr-xr-x. 3 splunk splunk   20  9月 11  2021 appserver
drwxr-xr-x. 2 splunk splunk 4096  3月 12 15:56 bin
drwxr-xr-x. 3 splunk splunk  161  9月 11  2021 default
drwxr-xr-x. 3 splunk splunk    6  3月 19 11:30 lib
drwx------. 2 splunk splunk  112  3月 12 15:56 local
drwxr-xr-x. 5 splunk splunk 4096  3月  4 22:42 lookups
drwxr-xr-x. 2 splunk splunk   44  3月 12 15:56 metadata
drwxr-xr-x. 2 splunk splunk   28  9月 11  2021 scripts
drwxr-xr-x. 2 splunk splunk   94  9月 11  2021 static
[root@testhost test]# chown -R splunk.splunk splunk-sdk-python-master/splunklib
[root@testhost test]# ls -ld splunk-sdk-python-master/splunklib
drwxr-xr-x. 4 splunk splunk 149  2月 13 14:59 splunk-sdk-python-master/splunklib
[root@testhost test]# mv splunk-sdk-python-master/splunklib /opt/splunk/etc/apps/search/lib/
[root@testhost test]# ls -l /opt/splunk/etc/apps/search/lib/
合計 0
drwxr-xr-x. 4 splunk splunk 149  2月 13 14:59 splunklib

2. カスタムコマンド本体の作成

次に、カスタムコマンド本体を作成します。
ひとまず今回は、単に「 Hello World ! 」と出力するだけのコマンドを作成します。
以下にカスタムコマンドのテンプレートが公開されているので、それを加工して作成してみます。

GitHub

実際に作成してみたものが以下です。
極力機能を削り、ごく最低限のものだけ入れた形になります。

test.py
#!/usr/bin/env python

import sys

sys.path.insert(0, "/opt/splunk/etc/apps/search/lib")
from splunklib.searchcommands import \
    dispatch, GeneratingCommand, Configuration

@Configuration()
class test_class(GeneratingCommand):
    def generate(self):
        return [{"test_field": "Hello World !"}]

dispatch(test_class)

return 」で返しているのがコマンドの結果として出力されるイベントとなります。
辞書({}で囲われた部分)を要素として持つリスト([]で囲われた部分)という形式をとり、辞書のキー(左側)がフィールド名、値(右側)がフィールドの値、辞書の個数(リストの要素数)がイベント数になります。

これを、「 /opt/splunk/etc/apps/search/bin/ 」に配置します。

Linux
[root@testhost test]# ls -l /opt/splunk/etc/apps/search/bin
合計 292
-r-xr-xr-x. 1 splunk splunk  6730  8月 13  2021 bucketdir.py
-r-xr-xr-x. 1 splunk splunk  5783  8月 13  2021 createrss.py
-r-xr-xr-x. 1 splunk splunk  3376  8月 13  2021 deseasonal.py
-r-xr-xr-x. 1 splunk splunk  7022  8月 13  2021 diff.py
-r-xr-xr-x. 1 splunk splunk  3294  8月 13  2021 erex.py
-r-xr-xr-x. 1 splunk splunk  1108  8月 13  2021 gauge.py
-r-xr-xr-x. 1 splunk splunk   901  8月 13  2021 iconify.py
-r-xr-xr-x. 1 splunk splunk   683  8月 13  2021 indextimerange.py
-r-xr-xr-x. 1 splunk splunk 41355  8月 13  2021 predict.py
-r-xr-xr-x. 1 splunk splunk  4186  8月 13  2021 rawstats.py
-r-xr-xr-x. 1 splunk splunk  1786  8月 13  2021 reltime.py
-r-xr-xr-x. 1 splunk splunk  4800  8月 13  2021 return.py
-r-xr-xr-x. 1 splunk splunk 10651  8月 13  2021 runshellscript.py
-r-xr-xr-x. 1 splunk splunk 13675  8月 13  2021 scrub.py
-r-xr-xr-x. 1 splunk splunk 11115  8月 13  2021 searchtxn.py
-r-xr-xr-x. 1 splunk splunk 70119  8月 13  2021 sendemail.py
-r-xr-xr-x. 1 splunk splunk  4709  8月 13  2021 sendemail_handler.py
-r-xr-xr-x. 1 splunk splunk  7363  8月 13  2021 streambag.py
-r-xr-xr-x. 1 splunk splunk  6535  8月 13  2021 sumindexoverlap.py
-r-xr-xr-x. 1 splunk splunk   388  3月 19 11:50 test.py
-r-xr-xr-x. 1 splunk splunk  3088  8月 13  2021 trendline.py
-r-xr-xr-x. 1 splunk splunk 18615  8月 13  2021 typelearner.py
-r-xr-xr-x. 1 splunk splunk  7413  8月 13  2021 windbag.py
-r-xr-xr-x. 1 splunk splunk   875  8月 13  2021 xmlkv.py
-r-xr-xr-x. 1 splunk splunk   691  8月 13  2021 xmlunescape.py
-r-xr-xr-x. 1 splunk splunk  2134  8月 13  2021 xpath.py

以下のコマンドで、きちんと動作するかの確認が可能です。

Linux
[root@testhost test]# /opt/splunk/bin/splunk cmd python /opt/splunk/etc/apps/search/bin/test.py __EXECUTE__ < /dev/null

test_field,__mv_test_field
Hello World !,

3. commands.conf の作成

上で作成した python プログラムですが、このままでは Splunk 側でコマンドとして利用できません。
以下のように「 commands.conf 」に設定を記載する必要があります。

commands.conf
[testcommand]
filename = test.py
chunked = true

スタンザ(上のコンフィグの[]で囲われている部分)がコマンド名、「 filename 」に指定しているファイルが実行ファイル名となります。

[root@testhost test]# ls -l /opt/splunk/etc/apps/search/local
合計 24
-rw-------. 1 splunk splunk   30  3月 19 12:04 commands.conf
-rw-------. 1 splunk splunk 1109  1月  3 10:30 indexes.conf
-rw-------. 1 splunk splunk  311  1月 14 14:28 inputs.conf
-rw-------. 1 splunk splunk  581  1月 14 14:25 props.conf
-rw-------. 1 splunk splunk  145  3月 12 15:56 savedsearches.conf
-rw-------. 1 splunk splunk   92  7月 24  2022 transforms.conf

4. Splunk への登録

commands.conf を Splunk に認識させるためには、 Splunk を再起動する必要があります。

[root@testhost test]# systemctl restart Splunkd.service

5. Splunkでのコマンド実行

これで、カスタムコマンドの登録ができました。
では、 Splunk のサーチ画面から実行してみます。
そのままだとフィールドの値がわかりにくいので、「 table 」コマンドでフィールドの値を見やすくしています。

Splunk
| testcommand
| table test_field

WS000034.JPG

実行できました。
とりあえずこれで、カスタムコマンドを作ることには成功しました。
次回は、カスタムコマンドの種類について紹介していきます。

戻る

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