0
1

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 3 years have passed since last update.

UbuntuのPythonでSplunkの実行結果を受け取る

Last updated at Posted at 2020-03-28

今回はUbuntu18.04のPython3.6.9から、Splunkのクエリを実行して結果をJSONで受け取るプログラムを作ります。なおFree版では動作しませんので、ご了承ください。

#原理

SplunkにはGUIだけでなく、外部からのクエリを受け取り実行する機能があるので、この機能を利用します。この機能の利用方法は簡単で、下のURLにユーザ名、パスワード、クエリ(SPL)、出力形式を入れてPOSTするだけです。

https://localhost:8089/services/search/jobs/export

#プログラムの作成

下のようなプログラムをPython3で作成しました。

splunkpost.py
import requests
import urllib

requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning)
postdata = urllib.parse.urlencode({"search":"search host=sample.csv earliest=01/01/2020:00:00:00","output_mode":"json"})
response = requests.post("https://localhost:8089/services/search/jobs/export",auth=("username","password"),data=postdata,verify=False)
print(response.text)

4行目は、POSTを実行した時に出力されるWarningを消すために書かれています。
5行目はPOSTするデータ(URLエンコード)を作成しています。searchにはクエリが入っており、outputには出力形式が書かれています。クエリはsearchを省略できませんので気をつけてください。また、検索期間を指定したい時はearliestlatestで範囲を記載する必要があります。
6行目のauthにSplunkのユーザ名(例ではusername)とパスワード(例ではpassword)を入れてPOSTを実行しています。

#プログラムの実行

今回のクエリで出力されるデータを下に記載します。

/dummy/sample.csv
"time","number","value"
"2020/01/01 12:00:00","1","a"
"2020/01/01 12:00:00","2","b"
"2020/01/01 12:00:00","3","c"


それでは、プログラムsplunkpost.pyを実行します。

# python3 splunkpost.py
{"preview":false,"offset":0,"result":{"_bkt":"main~0~38327377-7FDC-4E8B-B00E-61AA849FBF3E","_cd":"0:15","_indextime":"1585398968","_raw":"\"2020/01/01 12:00:00\",\"3\",\"c\"","_serial":"0","_si":["dummy","main"],"_sourcetype":"csv","_time":"2020-01-01 12:00:00.000 JST","host":"sample.csv","index":"main","linecount":"1","source":"/dummy/sample.csv","sourcetype":"csv","splunk_server":"dummy"}}
{"preview":false,"offset":1,"result":{"_bkt":"main~0~38327377-7FDC-4E8B-B00E-61AA849FBF3E","_cd":"0:12","_indextime":"1585398968","_raw":"\"2020/01/01 12:00:00\",\"2\",\"b\"","_serial":"1","_si":["dummy","main"],"_sourcetype":"csv","_time":"2020-01-01 12:00:00.000 JST","host":"sample.csv","index":"main","linecount":"1","source":"/dummy/sample.csv","sourcetype":"csv","splunk_server":"dummy"}}
{"preview":false,"offset":2,"lastrow":true,"result":{"_bkt":"main~0~38327377-7FDC-4E8B-B00E-61AA849FBF3E","_cd":"0:9","_indextime":"1585398968","_raw":"\"2020/01/01 12:00:00\",\"1\",\"a\"","_serial":"2","_si":["dummy","main"],"_sourcetype":"csv","_time":"2020-01-01 12:00:00.000 JST","host":"sample.csv","index":"main","linecount":"1","source":"/dummy/sample.csv","sourcetype":"csv","splunk_server":"dummy"}}

JSON形式で結果を受け取ったので、このような出力になりました。

#おわりに

結構簡単にSplunkからクエリの結果を受け取ることができました。Splunkが更に便利になりましたね。


動作環境
Ubuntu 18.04.4 LTS
Splunk 8.0.2.1
Python3.6.9

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?