잠토의 잠망경

[Splunk] splunk with python (numpy, pandas, etc) 본문

공부/Splunk

[Splunk] splunk with python (numpy, pandas, etc)

잠수함토끼 2020. 4. 6. 22:09

0.Github

https://github.com/yiwonjae/Project_Splunk_App

 

1. Python for Scientific Computing - 설치


우선 아래를 다운 받고 app 설치를 진행한다.

Splunk_SA_Scientific_Python

OS 설치 파일 URL
linux https://splunkbase.splunk.com/app/2882/
window https://splunkbase.splunk.com/app/2883/

 

App 설치는 문안하니 쭉쭉진행한다.

 

※ 참고


다음 코드를 참고하자.

https://github.com/j8lp/Jackson-Lane-W205-Repo/blob/f48a7667b863a40cd68bb903a5b40d95501fb9fd/Final_Project/Jackson_Splunk_ML_Toolkit/bin/exec_anaconda.py

 

j8lp/Jackson-Lane-W205-Repo

Contribute to j8lp/Jackson-Lane-W205-Repo development by creating an account on GitHub.

github.com

 

※ 문제 발생


아래와 같이 오류가 발생한 경우 아래 sol을 참고하자.

 

sol1 - success


해당 file에 해당 내용 삽입함(없는 경우 추가, 있으면 수정 300 초)

 

리붓하고 나면 정상으로 된다. 리붓 필수

 

2. Python for Scientific Computing - 설치 완료


설치는 etc/apps 밑에 설치된다.

폴더에 뭐가 있는지 보자.

설치 완료 후 폴더

 

3. 환경 변수 Setup


해당 설치 app은 기본적으로 아래 환경 변수를 참고하고 있다.

그래서 해당 환경 변수가 없다면 정상 동작하지 않는다. 

꼭 등록해야 한다.

변수 이름 SPLUNK_HOME
C:\Program Files\Splunk

 

4. App 만들어 보기


Python for Scientific Computing 을 사용하는 Command를 만들어보자.

App을 만든다.

만들고 나면 폴더 구조는 다음과 같다.

사용자 command 폴더 구조

 

주요하게 봐야하는 폴더와 파일은 다음과 같다.

     
bin exec_anaconda.py Python for Scientific Computing에서 제공
mycommand4.py 내가 활용할 것
other_test.py 내가 활용하는 것

ssl.py

ssl 컴파일 오류 나는 경우 필요
default commands.conf  

 

mycommand4.py

import exec_anaconda
exec_anaconda.exec_anaconda()

from other_test import test

datas = test()

import sys
from splunklib.searchcommands import dispatch, StreamingCommand, Configuration




@Configuration()
class MyCommand4(StreamingCommand):
    def stream(self, records):


        for record in records:

            record['you'] = None  # big hint

            datas = record['col']

            x = []

            for item in datas.split(','):

                if item != '':
                    x.append(float(item))

			
			#test()
            # data = minmax_scale(x, feature_range=[-1,1])

            retValue = ','.join(map(str, x))

            record['you'] = retValue

            yield record


if __name__ == '__main__':
    dispatch(MyCommand4, sys.argv, sys.stdin, sys.stdout, __name__)

 

other_test.py 

import numpy as np

def test():
    return np.asarray([12,3,4,5])
	

 

commands.conf

[mycommand4]
chunked=true
filename=mycommand4.py

 

오류 발생안함

 

※ 참고

https://github.com/SoNicKKK/Splunk/tree/4267c11d394686b0d478a1488b9863eeac74cc56

 

SoNicKKK/Splunk

Contribute to SoNicKKK/Splunk development by creating an account on GitHub.

github.com

 


※ 별첨

1. Splunk Debugging 방법

해당 python을 실제 실행해보면 오류가 나온다.

$ splunk.exe cmd python mycommand4.py

 

실행 화면

 

띄어쓰기가 잘못되었다는 내용이다.

그래서 visual studio code로 조정하니 정상적으로 된다.

import exec_anaconda
exec_anaconda.exec_anaconda()


from other_test import test

import sys
from splunklib.searchcommands import dispatch, StreamingCommand, Configuration

	

@Configuration()
class MyCommand4(StreamingCommand):

    def stream(self, records):

        d = test()

        for record in records:

            d1 = test()

            record['you'] = None  # big hint

            datas = record['col']

            x = []	
			

            for item in datas.split(','):

                if item != '':
                    x.append(float(item))
                
            
            d2 = test()

            for item in d2:
                x.append(float(item))

			
            # data = minmax_scale(x, feature_range=[-1,1])

            retValue = ','.join(map(str, x))

            record['you'] = retValue
			
			
            yield record




if __name__ == '__main__':
	dispatch(MyCommand4, sys.argv, sys.stdin, sys.stdout, __name__)
Comments