I want to check some lines from a log file [check msyql replication slave status log] named "status.txt" (size is always about 3-4 KB):
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
If any of these two line's value is 'No' then email is sent to alert@example.com. These two lines are unique and both occur only once.
#!/usr/bin/python
import smtplib
import os
import email.Utils
from email.mime.text import MIMEText
import sys
i=0
for line in open("status.txt"):
if "Slave_IO_Running: Yes" in line:
print "match1",line
i=i+1
else:
pass
for line2 in open("status.txt"):
if "Slave_SQL_Running: Yes" in line2:
i=i+1
else:
pass
print i
if i<2:
msg=MIMEText("Mysql not running")
fromaddr="root@DBausit.com"
toaddr="alert@example.com"
server = smtplib.SMTP('localhost')
server.set_debuglevel(1)
server.sendmail(fromaddr, [toaddr], msg.as_string())
server.quit()
else:
print "mysql running OK"
Please review my code.
Contents of "status.txt" file:
Relay_Log_Pos: 195294327 Relay_Master_Log_File: web01-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: mysql,information_schema,cphulkd,eximstats,horde,leechprotect,logaholicDB_web01,performance_schema,roundcube,whmxfer Replicate_Do_Table: Replicate_Ignore_Table: