I have log file which gives CPU utilization and timestamp value at regular interval.

Logs snippet below:
==========LOGS==============

a:CPU [ 85%]: asdf asd 123 xyz
A: Ts 23086, Netvalue 3286, someothervalues 3456
abc abc
xyz xyz
a:CPU [ 75%]: asdf asd 123 xyz
A: Ts 24088, Netvalue 3266, someothervalues 6576
======End of Logs ===========

What I want to do:
I want to make a graph of CPU load vs timestamp(Ts) value
So I thought if I can create a Dictionary like Dict = {"Timestamp":"CPU Load"} (eg: Dict = {24088:85} for above logs)
then plot a graph somehow using entries in dictionary.

My Queries:
1. How to get a dictionary in above format? I'm weak at data mining...
2. For plotting graph - suggest if this can be a correct approach using dictionary? ..or some other approach would work ...(something like exporting values to MS Excel and creating plot cpu/time.)


Any help appreciated....

Logs snippet below:
==========LOGS==============

a:CPU [ 85%]: asdf asd 123 xyz
A: Ts 23086, Netvalue 3286, someothervalues 3456
abc abc
xyz xyz
a:CPU [ 75%]: asdf asd 123 xyz
A: Ts 24088, Netvalue 3266, someothervalues 6576
======End of Logs ===========

Will your logs always have a: A: on the lines that your relevant information is on?

If so you could do something like this:

fh = open('my_log.inf')
flines = fh.readlines()
fh.close()

for line in flines:
    if line[:2\] == 'a:'
        #get CPU
    # etc...

Do you understand what I'm saying? But the important question is, is this the consisent pattern in your source data? Once you hammer that out this will be a simple task.

Thanks for the input... I'm able to extract keywords ...but Im stuck at next level:
Now I want to check a condition that
if
"CPU" keyword occurs in line 1 (#I'm able to do this)
then
check for next line for keyword "Ts" (How do I do this ???)
and then only
create an entry in dictionary like D{CPU : timestamp} (#I'm able to do this)


Code Snippet below:
===========
a:CPU [ 75%]: asdf asd 123 xyz
A: Ts 24088,
=========

If you read your lines into a list via readlines() you can simply use indexing... something like this:

my_index = 0
while my_index < len(my_lines):
    current_line = my_lines[index]
    if 'CPU' in current_line:
        #get CPU data
        index += 1
        next_line = my_lines[index]
        # Get Ts data
        # Save data
    index += 1

That's an extremely simple solution that you could use. There's many ways to do it, but I hope this at least gets you on a path to answering your query

Thanks a lot for useful tip... it worked for me...
Python really rocks...

This article has been dead for over six months. Start a new discussion instead.