• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 862
  • Last Modified:

Python - Suggestion for a python script - match lines

I'm thinking about creating a PY script that would match the bold elements (this is a Linux command output):

# multipath -l
mpath1 (3600d0230003228bc000339414edb8101)
[size=10 GB][features="0"][hwhandler="0"]
\_ round-robin 0 [prio=1][active]
 \_ 2:0:0:6 sdb 8:16 [active][ready]
\_ round-robin 0 [prio=1][enabled]
 \_ 3:0:0:6 sdc 8:64 [active][ready]

Is there a way to match the row that :
begins with \_
continues with number:number:number
 sdc 8:64 are not important
and match [active][ready] ?

It's a monitoring script. Purpose is to see if active and ready statements change.

Thanks!
0
g0all
Asked:
g0all
  • 4
  • 4
1 Solution
 
gelonidaCommented:
If I understood coorectly:
You plan to write a Python script, that starts multipath -l and parses it's output and does something whenever it encounters an 'intersting' line.

I'll give you a solution assuming, that the process multipath is a process, that terminates quickly and that does not create gigabytes of output.
If the result of this command were long, then I would use generators instead of reading the output into a list.
def get_command_output():
    """ """
    cmd = ['multipath', '-l']
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
    result = proc.stdout.readlines()
    return result 

def find_intersting_lines():
    lines = get_command_output()
    # Te next command with the regular expression string 
    # could be written in one line.
    # Splitting it over multiple lines allows to comment it
    active_ready_re_str = ( 
        r'^\s*'                # potentially leading white space at 
                               # beginning of line.
                               # It existed at least in the example,that you
                               # pasted
        r'\\_\s'               # \_ folowed by one white space character
        r'(\d+:){3}\d+\s'      # num:num:num:num folowed by a space character
        r'.*'                  # whatever
        r'\[active\]\[ready\]$'# [active][ready] followed by end of line
    )
    active_ready_re = re.compile(active_ready_re_str)
    for line in lines:
        if re.match(active_ready_re, line):
            print '!!!', line,
    
find_intersting_lines()

Open in new window

0
 
gelonidaCommented:
Here as generator, which might be more useful if the 'multipath'
 is ronning for a long time and prints out the results over time.


def get_command_output():
    """ """
    cmd = ['multipath', '-l']
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
    for line in proc.stdout:
        yield line

Open in new window

0
 
g0allAuthor Commented:
Thank you,  gelonida.

Yeah. It's obvious that this is too much for me. I'd better take a python book and read some more about this language.

Meanwhile, I would have some newbie questions...

What is subprocess.Popen and stdout=subprocess.PIPE ?

Why this is considered as a suprocess?

Thanks again!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
gelonidaCommented:
OK.
subprocess is the library, that you should use if you want to start other processes.

In your case the command is "multipath -l'

as you want to read the output of this command you have to tell python to pass the stdout of this subprocess to a PIPE.
THis means, that you can then read the stdout of that process by reading proc.stdout.

whithout the 'stdout=sunprocess.PIPE'  option the output would just be sent to stdout (the our console)


For more info just google for python and subprocess and amongst other you should find.

http://docs.python.org/library/subprocess.html
explaining some more details.

The regular expressions, that I used to match your line are explained in the documentation of the module re. ( http://docs.python.org/library/re.html  )

Regular expressions are very powerful, but a little tricky to learn.

regular expressions are also used by sed / awk / grep / vi / perl and many other tools.
The exact features of regular expressions do vary however.

So after reading any good introduction / tutorial about regular expressions be sure to read what exactly is supported by python or any other tool you'd like to use.
0
 
g0allAuthor Commented:
Now it makes sense. Many thanks.
0
 
gelonidaCommented:
just for your Info.
I edited the previous message to add some info about regular expressions.

Not sure, whether you replied before or after I added the additional comments.
Wish you happy pythoning and a nice sunday.
0
 
g0allAuthor Commented:
I've replied after, I will read informations you've suggestion.

Have a great day too.
0
 
g0allAuthor Commented:
*suggested
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now