0

I have executed few Linux commands using Python code. Please refer the code below with the output.

import os

uptime_from_top_command = "top -c -b -n1 | head -1 | cut -d ' ' -f 4-6 | sed -e 's/,//'"
uptime = os.system(uptime_from_top_command)

load_average_from_top_command = "top -c -b -n1 | head -1 | cut -d ',' -f 4-6 | sed -e 's/^[ \t]*//'"
load_average = os.system(load_average_from_top_command)

I got the below output.

up 100 days
load average: 0.06, 0.04, 0.00

I need to convert this output into the below JSON format and store it inside a JSON file.

{
    "uptime": "100 days",
    "load average": {
        "1_min": "0.06",
        "5_mins": "0.04",
        "15_mins": "0.00"
    }
}

Note - If the uptime of a server is less than 1 day, then it'll be displayed as up 12:45(for example). In that case, it should be converted to a JSON file in below format.

{
    "uptime": "12:45",
    "load average": {
        "1_min": "0.06",
        "5_mins": "0.04",
        "15_mins": "0.00"
    }
}

I mean, it should accept both the formats.

Please could someone help me how to achieve this?

3
  • 2
    Note that the os.system command does not capture the output of the subprocess. You're going to need to investigate the subprocess module. Commented Sep 9, 2022 at 19:47
  • @larsks thank you! I already got the required output in Python for those OS commands. Below is the same. up 100 days load average: 0.06, 0.04, 0.00 I just need to convert this output into the below JSON format and store it inside a JSON file. { "uptime": "100 days", "load average": { "1_min": "0.06", "5_mins": "0.04", "15_mins": "0.00" } } Commented Sep 9, 2022 at 20:01
  • "I already got the required output in Python for those OS commands." Not with that code you don't. Per the docs, the os.system call only returns an integer. Commented Sep 9, 2022 at 20:10

1 Answer 1

1

You can try re module to parse the string output. For example:

import re

s = """\
up 100 days
load average: 0.06, 0.04, 0.00"""

out = {
    "uptime": re.search(r"up ([^\s]+(?: days?)?)", s).group(1),
    "load average": dict(
        zip(
            ("1_min", "5_mins", "15_mins"),
            re.search(r"load average: ([^\s]+) ([^\s]+) ([^\s]+)", s).groups(),
        )
    ),
}

print(out)

Prints:

{
    "uptime": "100 days",
    "load average": {"1_min": "0.06,", "5_mins": "0.04,", "15_mins": "0.00"},
}

EDIT: To save data in Json file:

import re
import json

s = """\
up 100 days
load average: 0.06, 0.04, 0.00"""

out = {
    "uptime": re.search(r"up ([^\s]+(?: days?)?)", s).group(1),
    "load average": dict(
        zip(
            ("1_min", "5_mins", "15_mins"),
            re.search(r"load average: ([^\s]+) ([^\s]+) ([^\s]+)", s).groups(),
        )
    ),
}

with open("data.json", "w") as f_out:
    json.dump(out, f_out, indent=4)

Saves the data to data.json:

{
    "uptime": "100 days",
    "load average": {
        "1_min": "0.06,",
        "5_mins": "0.04,",
        "15_mins": "0.00"
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Andrej Kesely for the suggestions. Basically this conversion should be done and saved in a JSON file. It shouldn't be in STDOUT. Could you please advise me on achieving this?
@RaviTejaKothuru See my edit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.