Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

What I'm trying to do: I have a python script that is meant to remotely monitor the CPU usage of another server while it is running a particular application. The only viable way I found to do this was with using the explicit CLI os.system("$GOBIN/rtop [user@ip]"). rtop is something I found online to remotely profile another server, however, there is no option to log the data. It simply prints the results out to your terminal every 5 seconds. What I'm trying to do is store specific values from this ostream into an array to use later.

What I've tried: I have my code set up using the Process object from the multiprocessing module to allow for the logging process to operate concurrently with the application process, otherwise I wouldn't be recording the CPU under load. My problem arises in recording said output into an array. I've tried using the logging module to recieve the terminal's ostream, but I don't think there is a method to store data outside of a file. I suppose I might be able to create a temporary csv file to hold these values and then call them later but there must be a better method to this. Another roadblock I've hit is trying to discern the values I need from the ostream. The rtop output looks like this:


Load:
    0.29 0.45 0.36

CPU:
    0.00% user, 0.00% sys, 0.00% nice, 0.00% idle, 0.00% iowait, 0.00% hardirq, 0.00% softirq, 0.00% guest

Processes:
    1 running of 977 total

Memory:
    free    =   1.75 GiB
    used    =   3.50 GiB
    buffers = 721.02 MiB
    cached  =   1.83 GiB
    swap    =  15.87 GiB free of  15.87 GiB

Filesystems:
           /: 340.48 GiB free of 420.20 GiB

Network Interfaces:
    enp8s0 - TEXT, fe80::TEXT
      rx = 311.29 MiB, tx =  48.52 MiB

    lo - TEXT, ::1/128
      rx = 670.56 KiB, tx = 670.56 KiB

The only value I need is the user CPU percentage.

Excerpt of code:

def prof():

    # Declare file to capture CPU readings
    logger = logging.getLogger('scope.name')
    log_file = logging.FileHandler('cpu_Record.log')
    logger.addHandler(log_file)

    # Execute CLI rtop
    os.system("$GOBIN/rtop [user@ip]")

    #Capture ostream


    #Append array

While this writes the ostream to a file, I would prefer to append the values to an array.

question from:https://stackoverflow.com/questions/65942738/parse-specific-terminal-ostream-values-and-store-into-array-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
210 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...