Solved

Using grep with Python

Posted on 2010-09-24
8
1,381 Views
Last Modified: 2013-11-05
Hello and Thank you in advance.

I am trying to grep "dropped" from the command i run within the python. The command is a simple /sbin/ifconfig.

I have it being written to a file but would like to have it being kept in mem. Currently when i run the script i get the following.

          RX packets:3702045 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3587203 errors:0 dropped:0 overruns:0 carrier:0
          RX packets:310 errors:0 dropped:0 overruns:0 frame:0
          TX packets:310 errors:0 dropped:0 overruns:0 carrier:0

I am trying to make a script to alert me if we drop packets. So if the number equals 0 we are fine if it is anything else besides 0 then it will alert me.

I know the script can be made with perl as well but i want to learn python.

Thanks!
#!/usr/bin/env python

import os
import subprocess

ifconfig = os.popen('/sbin/ifconfig | grep dropped').read()

fname = open("/tmp/ifconfig.tmp",'w')
fname.write(ifconfig)
fname.close()

Open in new window

0
Comment
Question by:Realtime20
  • 4
  • 2
  • 2
8 Comments
 
LVL 9

Accepted Solution

by:
zaghaghi earned 125 total points
ID: 33755437
Hi,

I used some string manipulations to get 'dropped' values, and checked them against 0. See the attached code.
import time
import os
import subprocess
import sys

def checkDrops():
    ifconfig = os.popen('/sbin/ifconfig | grep dropped').read()
    lines= ifconfig.split('\n')
    for l in lines:
        keys =l.strip().split()
        if len(keys)>3:
            drops = keys[3]
            ndrops = drops.split(':')[1]
            if int(ndrops) >0:
                return False
    return True

try:
    if sys.argv[1]=='s':
        sleeptime = int(sys.argv[2])
except:
    sleeptime = 20
    
while true:
    if checkDrops():
        print "Ooops,  we have drops" # or you can do something else
    time.sleep(sleeptime)    # wait sleeptime seconds

Open in new window

0
 
LVL 9

Expert Comment

by:zaghaghi
ID: 33755453
Ooops, change
while true:
to
while True:
0
 

Author Comment

by:Realtime20
ID: 33755478
This looks like what i need. I can modify a few things but gives me the best examples of how to use.

Thanks again for the help.
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Closing Comment

by:Realtime20
ID: 33755481
Gave a perfect example of what i need to learn and what i can use.
0
 
LVL 9

Expert Comment

by:zaghaghi
ID: 33755485
Oops again, sorry for bad code. Use this one
import time
import os
import subprocess
import sys

def checkDrops():
    ifconfig = os.popen('/sbin/ifconfig | grep dropped').read()
    lines= ifconfig.split('\n')
    for l in lines:
        keys =l.strip().split()
        if len(keys)>3:
            drops = keys[3]
            ndrops = drops.split(':')[1]
            if int(ndrops) >0:
                return False
    return True

sleeptime = 10
try:
    if sys.argv[1]=='s':
        sleeptime = int(sys.argv[2])
except:
    pass
    
print sleeptime
    
while True:
    if not checkDrops():
        print "Ooops,  we have drops" # or you can do something else
    else:
        print "."
    time.sleep(sleeptime)    # wait 60 seconds

Open in new window

0
 
LVL 9

Expert Comment

by:zaghaghi
ID: 33755522
Also you can use raw_input.
if we save the below script as drops.py then we can use this command:

ifconfig | grep dropped | python drops.py
import os
import subprocess

def checkDrops():
    ifconfig = raw_input()
    lines= ifconfig.split('\n')
    for l in lines:
        keys =l.strip().split()
        if len(keys)>3:
            drops = keys[3]
            ndrops = drops.split(':')[1]
            if int(ndrops) >0:
                return False
    return True

if not checkDrops():
    print "Ooops, we have packet drops."
else:
    print "We haven't packet drops."

Open in new window

0
 
LVL 29

Expert Comment

by:pepr
ID: 33767707
There is a chance that you can avoid calling the grep completely.  As the grep does "general regular expression parsing", you can use the re Python module directly to do the same with the lines.  As the code should check only against the value different than zero, there is even no need to convert the value to int.  So, the core could be rewritten this way (the open of data.txt simulates the result of ifconfig).  I suggest also to use the subprocess module for getting the output from the ifconfig.
import re


def checkDrops(f):
    rexDropped = re.compile('dropped:(?P<num>\d+)\s')
    for line in f:
        m = rexDropped.search(line)
        if m:                          # If found
            if m.group('num') != '0':  # ... and the number is not zero
                return True            # ... do terminate immediately.
    return False                       # No drops found.
         
         
# Just for testing.
f  = open('data.txt')
areSomeDrops = checkDrops(f)
f.close()

if areSomeDrops:
    print 'Packet drops happened.'

Open in new window

0
 
LVL 29

Expert Comment

by:pepr
ID: 33767798
Here is the core of the calling the ifconfig and processing it directly in Python.  Warning, I did not checked under Unix, you may observe some problems. Check what happens with the launched ifconfig processes after termination of the Python script, for example.
import re
import subprocess

def checkDrops():
    rexDropped = re.compile('dropped:(?P<num>\d+)\s')
    proc = subprocess.Popen('/sbin/ifconfig', stdout=subprocess.PIPE)
    for line in proc.stdout:
        m = rexDropped.search(line)
        if m:
            print line                # just for debugging to see that line
            if m.group('num') != '0': # non-zero drops
                return True
    return False            
         
         
if checkDrops():
    print 'Packet drops happened.'
    # Not waiting for finishing the ifconfig process...

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question