Splunk Search

フィールド内文字列の日付12ケタを抜き出して現時刻と比較し、一週間より前のものだけをレコード出力する

yuusuke611
New Member

AWSの構成情報をSplunkに取り込んでいますが、AMIの取得日付が取り込みRowデータ自体に無い為、代替案として、AMIのnameに記載されている日付を取得して、本日日付と比較し、一週間以上前のものを取り出したいと思っています。どういうサーチ文を実行すればよいでしょうか。(以下、マスク部分はXXXXで書いてます。)

例)

source="*:ec2_images" AND aws_account_id="XXXXXXXXXXXX"
| dedup id

出力例) ※AMIの取得日付がなぜかない。。
{"owner_id": "XXXXXXXXXXXX", "instance_lifecycle": null, "ramdisk_id": null, "is_public": false, "description": "XXXXXXXXXXXX-201807301152-manual", "root_device_type": "ebs", "id": "ami-XXXXXXXXXXXX", "virtualization_type": "hvm", "account_id": "XXXXXXXXXXXX", "owner_alias": null, "type": "machine", "kernel_id": null, "state": "available", "sriov_net_support": "simple", "product_codes": [], "architecture": "x86_64", "location": "XXXXXXXXXXXX/XXXXXXXXXXXX-201807301152-manual", "billing_products": [], "region": "ap-northeast-1", "name": "XXXXXXXXXXXX-201807301152-manual", "hypervisor": "xen", "platform": null, "root_device_name": "/dev/xvda"}

代替案としてやりたいこと)
descriptionやnameフィールドの日付12ケタを抜き出して現時刻と比較し、一週間より前のものだけをレコード出力する

0 Karma
1 Solution

melonman
Motivator

@kamlesh_vaghela さんの回答に補足する形になりますが、
1. nameフィールドからrexで日付部分を取得したあと、
2. 現在時刻から1週間前の時刻(UNIX時間)と比較して、
3. where句でフィルタを行う
という流れでいけるとおもいますよ。

 ... your search to get events
| rex field=name "-(?<ami_cdatehour>\d{12})-"
| eval ami_ctime = strptime(ami_cdatehour,"%Y%m%d%H%M")
| eval a_week_ago = relative_time( now(), "-7d@d")
| where ami_ctime < a_week_ago

View solution in original post

0 Karma

melonman
Motivator

@kamlesh_vaghela さんの回答に補足する形になりますが、
1. nameフィールドからrexで日付部分を取得したあと、
2. 現在時刻から1週間前の時刻(UNIX時間)と比較して、
3. where句でフィルタを行う
という流れでいけるとおもいますよ。

 ... your search to get events
| rex field=name "-(?<ami_cdatehour>\d{12})-"
| eval ami_ctime = strptime(ami_cdatehour,"%Y%m%d%H%M")
| eval a_week_ago = relative_time( now(), "-7d@d")
| where ami_ctime < a_week_ago
0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

@yuusuke611 @melonman

すばらしいです。いずれにせよ私の答えが参考になるならば挨拶として支持してください。 🙂

0 Karma

yuusuke611
New Member

@kamlesh_vaghela さん、@melonman さん、回答ありがとうございました!

0 Karma

melonman
Motivator

回答にAcceptしていただけるとありがたいです!

0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

@yuusuke611
You can extract your required data using rex command.
https://docs.splunk.com/Documentation/Splunk/7.3.0/SearchReference/Rex

Can you please try this for your specific case?

====================================
必要なデータは rexコマンドを使って取り出すことができます。
https://docs.splunk.com/Documentation/Splunk/7.3.0/SearchReference/Rex

あなたの特定のケースでこれを試してください。

====================================

YOUR_SEARCH | rex field=name "-(?<date>\d{12})-" | eval epoch = strptime(date,"%Y%m%d%H%M"), readable=strftime(epoch,"%Y-%m-%d %H:%M")

My Sample Search:

| makeresults | eval _raw="{\"owner_id\": \"XXXXXXXXXXXX\", \"instance_lifecycle\": null, \"ramdisk_id\": null, \"is_public\": false, \"description\": \"XXXXXXXXXXXX-201807301152-manual\", \"root_device_type\": \"ebs\", \"id\" : \"ami-XXXXXXXXXXXX\", \"virtualization_type\": \"hvm\", \"account_id\": \"XXXXXXXXXXXX\", \"owner_alias\": null, \"type\": \"machine\", \"kernel_id\": null, \"state\": \"available\" , \"sriov_net_support\": \"simple\", \"product_codes\": [], \"architecture\": \"x86_64\", \"location\": \"XXXXXXXXXXXX / XXXXXXXXXXXX-201807301152-manual\", \"billing_products\": [], \"region\": \" ap-northeast-1 \",\" name \":\" XXXXXXXXXXXX-201807301152-manual \",\" hypervisor \":\" xen \",\" platform \": null,\" root_device_name \":\" / dev / xvda \"}" |kv | table description name | rex field=name "-(?<date>\d{12})-" | eval epoch = strptime(date,"%Y%m%d%H%M"), readable=strftime(epoch,"%Y-%m-%d %H:%M")

Thanks

0 Karma
Get Updates on the Splunk Community!

Routing logs with Splunk OTel Collector for Kubernetes

The Splunk Distribution of the OpenTelemetry (OTel) Collector is a product that provides a way to ingest ...

Welcome to the Splunk Community!

(view in My Videos) We're so glad you're here! The Splunk Community is place to connect, learn, give back, and ...

Tech Talk | Elevating Digital Service Excellence: The Synergy of Splunk RUM & APM

Elevating Digital Service Excellence: The Synergy of Real User Monitoring and Application Performance ...