Home > Blockchain >  Python3 to resolve TypeError for a log parser
Python3 to resolve TypeError for a log parser

Time:04-11

I am using a python3 log parser to filter the log and send an email to the outlook as an HTML attachment, while this is working fine with python2 but python3 I'm getting an alert.

any help on this will be much appreciated.

Python logfile parsing(pyParser.py)

#!/usr/bin/python3
import sys
import re
from subprocess import Popen, PIPE
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
msg_body = ''
node_name = ''

mailstr = """<html><head></head><body>
<table border=1>
     <tr>
       <th bgcolor=fe9a2e>Hostname</th>
       <th bgcolor=fe9a2e>Service</th>
     </tr>
"""

fh=open(sys.argv[1],"r")
for line in fh:

        pat_match_next=re.search("^\s*\"stdout.*", line)
        if pat_match_next:
                continue
        pat_match=re.search("^.*\"?Service Status:\s (.*)(\s \S )\"?.*$", line)
        if pat_match:
                msg_body = pat_match.group(1)
                node_name = pat_match.group(2)
                mailstr  = """<TR><TD bgcolor=fe9a2e>"""   node_name   """</TD>\n"""
                mailstr  = """<TR><TD><TD>"""   msg_body   node_name   """</TD></TD></TR>\n"""

mailstr  = """</body></html>"""
mailp = Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=PIPE)
msg = MIMEMultipart('alternative')
msg['To'] = ""
msg['Cc'] = ""
msg['Subject'] = "HealthCheck Report"
msg['From'] = ""
msg1 = MIMEText(mailstr, 'html')
msg.attach(msg1)
mailp.communicate(msg.as_string())

logfile(healthCheck_log):

HostName: fsxdb01.example.com
Service Status:  NTP is not Running on the host fsxdb01.example.com
Service Status:  NSCD is not Running on the host fsxdb01.example.com
Service Status:  Sendmail is Running on the host fsxdb01.example.com
Service Status:  Automount is Running on the host fsxdb01.example.com
Service Status:  Filesystem For root (/) is not normal and 47% used on the host fsxdb01.example.com
Service Status:  Filesystem For Var (/var) is not normal and 68% used on the host fsxdb01.example.com
Service Status:  Filesystem For tmp (/tmp) is normal on the host fsxdb01.example.com

When i am running the above script against the logfile, i am getting an alert like below.

$  ./pyParser.py healthCheck_log
Traceback (most recent call last):
  File "./pyParser.py", line 40, in <module>
    mailp.communicate(msg.as_string())
  File "/usr/lib64/python3.6/subprocess.py", line 848, in communicate
    self._stdin_write(input)
  File "/usr/lib64/python3.6/subprocess.py", line 801, in _stdin_write
    self.stdin.write(input)
TypeError: a bytes-like object is required, not 'str'
No recipient addresses found in header

another issue is not about the run but about the mail format like the hostname and message into aligning into same line and its creating a blank cell.

enter image description here

Should be :

enter image description here

CodePudding user response:

From the docs for subprocess (mentioned in several places):

If encoding or errors are specified, or text (also known as universal_newlines) is true, the file objects stdin, stdout and stderr will be opened in text mode using the encoding and errors specified in the call or the defaults for io.TextIOWrapper.

...

If text mode is not used, stdin, stdout and stderr will be opened as binary streams. No encoding or line ending conversion is performed.

so in python3 try

mailp.communicate(msg.as_bytes())

As to the html:

mailstr  = """<TD>"""   msg_body   node_name   """</TD></TR>\n"""

Of course, better use f-strings [or even template engine like jinja2], instead of concatenation

  • Related