我如何获得给定字段的所有记录的计数,包括该字段不存在的所有记录的计数。
例如:
给出通常看起来像这样的数据:
{"source_host":"host1", "msg":"some message", "user":"jack"}
{"source_host":"host2", "msg":"some other message", "user":"jill"}
我可以得到所有这样的记录的总数:
index="my_index" sourcetype=my_proj:my_logs | table _raw | stats count(_raw)
我可以像这样获得给定字段的记录数:
index="my_index" sourcetype=my_proj:my_logs | stats count(_raw) by source_host
提供这样的表格
host count
host_1 89
host_2 57
但是我希望查询也对字段存在但为空的记录进行计数,如下所示:
{"source_host":"", "msg":"some message", "user":"jack"}
并且还要计数这样的消息:
asdf asdf asdf asdf asd fasdfasdfafas
foo bar
Some other Junk someone wrote to my log
要获得这样的表
host count
host_1 89
host_2 57
null 1
no_def 3
请先使用fillnull
(docs.Splunk的fillnull页:]
| fillnull value="N/A" <field or field list or leave blank for all fields>
但是,除了琐碎的搜索之外,这将非常耗时
注意:如果愿意,可以使用其他value=""
case
功能可能有帮助。在此示例中,如果source_host字段不存在(值为null),则将其设置为“ no_def”;否则,将其设置为“ no_def”。如果值为空字符串,则设置为“ null”;否则,将其设置为自身。
index="my_index" sourcetype=my_proj:my_logs
| eval source_host = case(isnull(source_host), "no_def", source_host=="", "null", 1==1, source_host)
| stats count() by source_host