Getting Data In

I have python script with correct output in JSON format , but why 2 event with broken message?

alexeysharkov
Path Finder

Hello

I have python script just like this

 

 

 

 

#!/bin/python
import os
import json
import datetime

HOMEPATH = '/opt/monitor_dirs/SomeDir'

def path_to_dict(path, depth = 1, first = False):
    for base, dirs, files in os.walk(path):
        r = {'name': base, 'dirs': len(dirs), 'files': len(files)}
        if first:
            r['datetime'] = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S%z")
        if depth > 0:
            r['subdirs'] = {}
            for subdir in dirs:
                r['subdirs'][subdir] = path_to_dict(os.path.join(path, subdir), depth - 1);
        return r


#print path_to_dict(HOMEPATH, 1)
result = path_to_dict(HOMEPATH, 1, True)
if result:
    print (json.dumps(result, sort_keys=True, indent=4))

 

 

 

 

 

And i have output 

 

 

 

 

# ./file_count.py
{
    "datetime": "2023-02-22T21:10:49",
    "dirs": 9,
    "files": 0,
    "name": "/opt/monitor_dirs/SomeDir",
    "subdirs": {
        "XXXX": {
            "dirs": 0,
            "files": 63,
            "name": "/opt/monitor_dirs/XXXX"
        }
    }
}

 

 

 

 

 

 

There is some problem in Index

I have 2 event instead just only one

1. {

2.  ""datetime": "2023-02-22T21:10:49",
"dirs": 9,
"files": 0, and so on, but there is no '{'

 

How i can get only one event with my JSON

 

Labels (1)
Tags (3)
0 Karma
1 Solution

kamlesh_vaghela
SplunkTrust
SplunkTrust

@alexeysharkov 

Can you please try updating the print statement in python code?

From:

print (json.dumps(result, sort_keys=True, indent=4))

To

print (json.loads(json.dumps(result, sort_keys=True, indent=4)))

 

if the solution doesn't work for you then please share the extraction configuration from props.conf.

Thanks
KV
If any of my replies help you to solve the problem Or gain knowledge, an upvote would be appreciated.

 

View solution in original post

Tags (1)

alexeysharkov
Path Finder

Thanks Kamlesh!

props.conf was empty 😞

And now

[mymonitorinf]
CHARSET=UTF-8
INDEXED_EXTRACTIONS=json
KV_MODE=none
SHOULD_LINEMERGE=true
category=Structured
description=JavaScript Object Notation format. For more information, visit http://json.org/
disabled=false
pulldown_type=true

Everything OK now! Thanks

0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

@alexeysharkov 

Can you please try updating the print statement in python code?

From:

print (json.dumps(result, sort_keys=True, indent=4))

To

print (json.loads(json.dumps(result, sort_keys=True, indent=4)))

 

if the solution doesn't work for you then please share the extraction configuration from props.conf.

Thanks
KV
If any of my replies help you to solve the problem Or gain knowledge, an upvote would be appreciated.

 

Tags (1)
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...

Unlock What’s Next: The Splunk Cloud Platform at .conf25

In just a few days, Boston will be buzzing as the Splunk team and thousands of community members come together ...