[Errno 9] Bad File Descriptor on Windows 2003 Server & Py 2.4.1

R

rbt

The below script produces a '[Errno 9] Bad File Descriptor' when
executed. If I remove the try: except: statements, the script stops when
the error occurs.

The purpose of the script is to monitor the size of the three main logs
on a Windows 2003 server and send and email should any of the logs get
shorter. It works fine... just don't know *why* it produces the '[Errno
9] Bad File Descriptor' error.

I'm running Python 2.4.1 on Windows 2003 SP1 Server with no funky win32
extensions ;)

logs = ['AppEvent.Evt', 'SecEvent.Evt', 'SysEvent.Evt']

app_size = 0
sec_size = 0
sys_size = 0

def send_email(LogName, old_size, new_size):
f = "Somebody<[email protected]>"
t = "(e-mail address removed)"

msg = MIMEText("""old_size = %d, new_size = %d""" %(old_size,
new_size))

msg["Subject"] = "%s Log Just Got Shorter" %LogName
msg["Message-id"] = email.Utils.make_msgid()
msg["From"] = f
msg["To"] = t

h = "smtp.vt.edu"
s = smtplib.SMTP(h)
s.sendmail(f, t, msg.as_string())
s.quit()

while 1:

for log in logs:

try:

a = os.stat('C:\WINDOWS\System32\config\%s' %log)
cur_size = a[6]
print log
print "cur_size", cur_size

if log == 'AppEvent.Evt':
print "old_size", app_size, "\n"
if cur_size >= app_size:
app_size = cur_size
time.sleep(6)
continue
else:
send_email(log, app_size, cur_size)
time.sleep(6)
continue

elif log == 'SecEvent.Evt':
print "old_size", sec_size, "\n"
if cur_size >= sec_size:
sec_size = cur_size
time.sleep(6)
continue
else:
send_email(log, sec_size, cur_size)
time.sleep(6)
continue

else:
print "old_size", sys_size, "\n"
if cur_size >= sys_size:
sys_size = cur_size
time.sleep(6)
continue
else:
send_email(log, sys_size, cur_size)
time.sleep(6)
continue

except Exception, e:
fp7 = file('log_mon_exception.txt', 'a')
print >> fp7, e
fp7.close()
 
V

vincent wehren

| The below script produces a '[Errno 9] Bad File Descriptor' when
| executed. If I remove the try: except: statements, the script stops when
| the error occurs.
|
| The purpose of the script is to monitor the size of the three main logs
| on a Windows 2003 server and send and email should any of the logs get
| shorter. It works fine... just don't know *why* it produces the '[Errno
| 9] Bad File Descriptor' error.
|
| I'm running Python 2.4.1 on Windows 2003 SP1 Server with no funky win32
| extensions ;)
|
| logs = ['AppEvent.Evt', 'SecEvent.Evt', 'SysEvent.Evt']
|
| app_size = 0
| sec_size = 0
| sys_size = 0
|
| def send_email(LogName, old_size, new_size):
| f = "Somebody<[email protected]>"
| t = "(e-mail address removed)"
|
| msg = MIMEText("""old_size = %d, new_size = %d""" %(old_size,
| new_size))
|
| msg["Subject"] = "%s Log Just Got Shorter" %LogName
| msg["Message-id"] = email.Utils.make_msgid()
| msg["From"] = f
| msg["To"] = t
|
| h = "smtp.vt.edu"
| s = smtplib.SMTP(h)
| s.sendmail(f, t, msg.as_string())
| s.quit()
|
| while 1:
|
| for log in logs:
|
| try:
|
| a = os.stat('C:\WINDOWS\System32\config\%s' %log)

Without looking at the rest of script:

Try using either escaped backslashes as in:

a = os.stat('C:\\WINDOWS\\System32\\config\\%s' %log)

or a raw string (as long as it doesn't end with a single backslash) as in:

a = os.stat(r'C:\WINDOWS\System32\config\%s' %log)

or simply use forward slashes as in:

a = os.stat('C:/WINDOWS/System32/config/%s' %log)




--

Vincent Wehren







| cur_size = a[6]
| print log
| print "cur_size", cur_size
|
| if log == 'AppEvent.Evt':
| print "old_size", app_size, "\n"
| if cur_size >= app_size:
| app_size = cur_size
| time.sleep(6)
| continue
| else:
| send_email(log, app_size, cur_size)
| time.sleep(6)
| continue
|
| elif log == 'SecEvent.Evt':
| print "old_size", sec_size, "\n"
| if cur_size >= sec_size:
| sec_size = cur_size
| time.sleep(6)
| continue
| else:
| send_email(log, sec_size, cur_size)
| time.sleep(6)
| continue
|
| else:
| print "old_size", sys_size, "\n"
| if cur_size >= sys_size:
| sys_size = cur_size
| time.sleep(6)
| continue
| else:
| send_email(log, sys_size, cur_size)
| time.sleep(6)
| continue
|
| except Exception, e:
| fp7 = file('log_mon_exception.txt', 'a')
| print >> fp7, e
| fp7.close()
 
D

Dennis Lee Bieber

The below script produces a '[Errno 9] Bad File Descriptor' when
executed. If I remove the try: except: statements, the script stops when
the error occurs.
Might help to supply the traceback so one can examine the
particular line in question...
a = os.stat('C:\WINDOWS\System32\config\%s' %log)

Might I suggest using os.path.join() rather than the % notation?
if log == 'AppEvent.Evt':
print "old_size", app_size, "\n"
if cur_size >= app_size:
app_size = cur_size
time.sleep(6)
continue
else:
send_email(log, app_size, cur_size)
time.sleep(6)
continue
Drop the laddered if: structure, and use a dictionary...

logs = {"AppEvent.Evt" : 0, "SecEvent.Evt" : 0, "SysEvent.Evt" : 0}

def send_mail(...):
...

while True:
for log in logs.keys():
cur_size = os.stat(os.path.join("C:/windows/system32/config",
log))[6]
if cur_size >= logs[log]:
logs[log] = cur_size
else:
send_email(log, logs[log], cur_size)

time.sleep(6)


--
 
R

rbt

vincent said:
| The below script produces a '[Errno 9] Bad File Descriptor' when
| executed. If I remove the try: except: statements, the script stops when
| the error occurs.
|
| The purpose of the script is to monitor the size of the three main logs
| on a Windows 2003 server and send and email should any of the logs get
| shorter. It works fine... just don't know *why* it produces the '[Errno
| 9] Bad File Descriptor' error.
|
| I'm running Python 2.4.1 on Windows 2003 SP1 Server with no funky win32
| extensions ;)
|
| logs = ['AppEvent.Evt', 'SecEvent.Evt', 'SysEvent.Evt']
|
| app_size = 0
| sec_size = 0
| sys_size = 0
|
| def send_email(LogName, old_size, new_size):
| f = "Somebody<[email protected]>"
| t = "(e-mail address removed)"
|
| msg = MIMEText("""old_size = %d, new_size = %d""" %(old_size,
| new_size))
|
| msg["Subject"] = "%s Log Just Got Shorter" %LogName
| msg["Message-id"] = email.Utils.make_msgid()
| msg["From"] = f
| msg["To"] = t
|
| h = "smtp.vt.edu"
| s = smtplib.SMTP(h)
| s.sendmail(f, t, msg.as_string())
| s.quit()
|
| while 1:
|
| for log in logs:
|
| try:
|
| a = os.stat('C:\WINDOWS\System32\config\%s' %log)

Without looking at the rest of script:

Try using either escaped backslashes as in:

a = os.stat('C:\\WINDOWS\\System32\\config\\%s' %log)

or a raw string (as long as it doesn't end with a single backslash) as in:

a = os.stat(r'C:\WINDOWS\System32\config\%s' %log)

or simply use forward slashes as in:

a = os.stat('C:/WINDOWS/System32/config/%s' %log)

Thanks, that worked.
 
R

rbt

Dennis said:
The below script produces a '[Errno 9] Bad File Descriptor' when
executed. If I remove the try: except: statements, the script stops when
the error occurs.

Might help to supply the traceback so one can examine the
particular line in question...

a = os.stat('C:\WINDOWS\System32\config\%s' %log)


Might I suggest using os.path.join() rather than the % notation?

if log == 'AppEvent.Evt':
print "old_size", app_size, "\n"
if cur_size >= app_size:
app_size = cur_size
time.sleep(6)
continue
else:
send_email(log, app_size, cur_size)
time.sleep(6)
continue

Drop the laddered if: structure, and use a dictionary...

logs = {"AppEvent.Evt" : 0, "SecEvent.Evt" : 0, "SysEvent.Evt" : 0}

def send_mail(...):
...

while True:
for log in logs.keys():
cur_size = os.stat(os.path.join("C:/windows/system32/config",
log))[6]
if cur_size >= logs[log]:
logs[log] = cur_size
else:
send_email(log, logs[log], cur_size)

time.sleep(6)

This was my first go at it. I know it's not pretty, but it works ;)
Perhaps when I refactor or offer it to friends, I'll incorporate your
suggestions... as of now, I'm the only one who sees or uses it.

Thanks,
rbt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top