Solved

Python: Notify if Error

Posted on 2011-02-11
10
441 Views
Last Modified: 2012-05-11
I have a script that stops and starts services on several servers (8). When it finishes, it sends me an email that the script has run successfully. I have the stderr going to a file also. what i want to do is read the file and do the following:

if error exists in output.log then send me an email
else
if no error exists continue with the script.


I know i  need to run this in the loop so that i get an email if there's an error. As it is now, i get an email when it completes looping through all of the servers and stops / restarts the services.

I'm only interested in either how to google examples of what i'm looking for, or resources that help me figure this out. I don't need it done for me. I'm not getting a clear picture of what that looks like in Python.

Thanks.

thanks.
0
Comment
Question by:huskeraider
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
10 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 200 total points
ID: 34891514
http://docs.python.org/tutorial/controlflow.html


Take a look at section 4.4 (break and continue) for how to exit the loop; section 4.5 show an example of a while loop (you could probably use a for loop as well--see section 4.2).
0
 
LVL 29

Expert Comment

by:pepr
ID: 34892242
Huskeraider, can you show how you call the script that start services and how you redirect its output?  My guess is that you may want to use the subprocess.Popen() with suitable arguments to capture the produced output in your Python script and simply check the produced content.
0
 

Author Comment

by:huskeraider
ID: 34900406
I call a batch file that launches a ruby script that stops the service and listens until the service stops. I have the stderr going to a text file. I was going to check the text file for errors. if errors then email me. if not, complete the process and send "completed successfully" message.

Is that what you wanted to know?
0
Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

 

Author Comment

by:huskeraider
ID: 34900443
Kaufmed,

thanks for the example. This is something i might be able to use in the layout i have.
0
 
LVL 29

Expert Comment

by:pepr
ID: 34900806
How complex the batch is? It is likely that it could be rewritten easily to Python (i.e. calling from Python the same things that are called from the batch).  Then it would be easy to capture the redirected stdout and stderr and to check their content. Can you show a representative fragment of the batch file (with how the redirection is done)?
0
 

Author Comment

by:huskeraider
ID: 34901168
I'm basically looking for an example of a If statement that reads and looks for certain text in a text file. if it finds "error" then it sends a error message. if there's nothing in the text file, it finishes and sends a completed message.

the batch has a ruby script that sends a stop request then it listens until the service is stopped. then it starts the service. when stopped it goes to the next server in the list.
0
 
LVL 29

Assisted Solution

by:pepr
pepr earned 300 total points
ID: 34902224
Basically, something like this:
    f = open('logfile.txt')
    errorFound = False
    for line in f:
        if line.startswith('error'):   # for the case you know how the error line starts
            errorFound = False         # set the flag
            break                      # terminate the loop early
    f.close()

    if errorFound:
        sendmail(...)

Open in new window


The good/bad result testing may vary.  You can use a regular expression to match more complex error-indication lines.

Does this make sense?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34903814
@pepr

Line 5 should be

    errorFound = True

correct?
0
 
LVL 29

Expert Comment

by:pepr
ID: 34904334
@kaufmed: Yes :)   Thanks for spotting the error.
0
 

Author Closing Comment

by:huskeraider
ID: 34906683
Thank you both for your assistance both of these examples gives me what i need.

thanks again.
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

688 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