0
0

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.

Splunkでマルチバリューを整列する(失敗しました)

Last updated at Posted at 2021-01-04

以前の記事でマルチバリューをsortみたく整列できなくて結構大変だったので、作れるんじゃないかと頑張りましたが、無理でした。

でも途中まで書いときます。

2020/1/19
https://github.com/gjanders/SplunkAdmins/blob/master/bin/streamfilter.py
が参考になると。

multivalue fields come through as a list, iterate through the list and run the regex against each entryin the multivalued field

あとは

Multivalue fields are sent as a ; delim string with each term incased in $, and the field name is set to __mv_fieldname.The original field name is sent as a \n delim string. Also need to set supports_multivalues in commands.conf

聞いてみるもんです。

#Code

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

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


@Configuration()
class mvsortCommand(StreamingCommand):
    """ sort multivalue
    """


    def stream(self, records):
        self.logger.debug('mvsortCommand: %s', self)  # logs command line

        for record in records:
            args=self.fieldnames[0]  # 引数のフィールド名
            if isinstance(record[args],(str)):  # フィールドがシングルバリューかの判定
                pass

            else:
                record[args]=sorted(record[args])
            yield record

dispatch(mvsortCommand, sys.argv, sys.stdin, sys.stdout, __name__)

うまくいかない・・・・:cry:

#pythonによる検証

Colaboratoryで作ってみた。

generator_test.py
# generator テスト
import random

num=[random.randrange(0,20) for i in range(20)]

def orig_gen():
  for i in num:
    yield i

g1=orig_gen()
j=[]

def gen_out():
  try:
    for i in g1:
      j.append(i)
      yield j

  except:
    pass

  finally:
       yield sorted(j)


g2=gen_out()
for i in g2:
  print(i)

##結果

[5]
[5, 16]
[5, 16, 6]
[5, 16, 6, 19]
[5, 16, 6, 19, 18]
[5, 16, 6, 19, 18, 3]
[5, 16, 6, 19, 18, 3, 16]
[5, 16, 6, 19, 18, 3, 16, 17]
[5, 16, 6, 19, 18, 3, 16, 17, 6]
[5, 16, 6, 19, 18, 3, 16, 17, 6, 8]
[5, 16, 6, 19, 18, 3, 16, 17, 6, 8, 8]
[5, 16, 6, 19, 18, 3, 16, 17, 6, 8, 8, 3]
[5, 16, 6, 19, 18, 3, 16, 17, 6, 8, 8, 3, 12]
[5, 16, 6, 19, 18, 3, 16, 17, 6, 8, 8, 3, 12, 13]
[5, 16, 6, 19, 18, 3, 16, 17, 6, 8, 8, 3, 12, 13, 8]
[5, 16, 6, 19, 18, 3, 16, 17, 6, 8, 8, 3, 12, 13, 8, 7]
[5, 16, 6, 19, 18, 3, 16, 17, 6, 8, 8, 3, 12, 13, 8, 7, 15]
[5, 16, 6, 19, 18, 3, 16, 17, 6, 8, 8, 3, 12, 13, 8, 7, 15, 10]
[5, 16, 6, 19, 18, 3, 16, 17, 6, 8, 8, 3, 12, 13, 8, 7, 15, 10, 1]
[5, 16, 6, 19, 18, 3, 16, 17, 6, 8, 8, 3, 12, 13, 8, 7, 15, 10, 1, 9]
[1, 3, 3, 5, 6, 6, 7, 8, 8, 8, 9, 10, 12, 13, 15, 16, 16, 17, 18, 19]

きちんと最後のsorted()が効いてくれている。

#考察

https://docs.splunk.com/DocumentationStatic/PythonSDK/1.6.14/searchcommands.html
の説明をみてもよくわからないので色々と試したところ、

  • もともとのイベントはdict
  • マルチバリューの時はlistで値がくる。
  • append()でくっつけるのは十分可能だけど、最後のsortedだけが効かない。
  • strのリストのはずなんだけどな〜
  • pstreeなんかは思いっきりマルチバリューを作っていてすごい。

#まとめ
手詰まりになったので、いったんSplunk>AnswersにあげてPythonの勉強をします。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?