Files
CyberPanel/plogical/CyberCPLogFileWriter.py

92 lines
3.0 KiB
Python
Raw Normal View History

2017-10-24 19:16:36 +05:00
import subprocess
import time
2020-01-30 17:58:56 +05:00
import socket
import os
import smtplib
2018-08-28 23:11:33 +05:00
2017-10-24 19:16:36 +05:00
class CyberCPLogFileWriter:
2017-12-09 22:30:10 +05:00
fileName = "/home/cyberpanel/error-logs.txt"
2017-10-24 19:16:36 +05:00
@staticmethod
def SendEmail(sender, receivers, message):
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receivers, message)
print("Successfully sent email")
except BaseException as msg:
CyberCPLogFileWriter.writeToFile(str(msg))
2017-10-24 19:16:36 +05:00
@staticmethod
2020-01-30 21:38:39 +05:00
def writeToFile(message, email=None):
2017-10-24 19:16:36 +05:00
try:
file = open(CyberCPLogFileWriter.fileName,'a')
file.writelines("[" + time.strftime(
"%m.%d.%Y_%H-%M-%S") + "] "+ message + "\n")
2017-10-24 19:16:36 +05:00
file.close()
2018-08-21 13:10:40 +05:00
2020-01-30 17:58:56 +05:00
## Send Email
emailPath = '/usr/local/CyberCP/emailDebug'
try:
if os.path.exists(emailPath):
SUBJECT = "CyberPanel log reporting"
2020-01-30 21:38:39 +05:00
adminEmailPath = '/home/cyberpanel/adminEmail'
adminEmail = open(adminEmailPath, 'r').read().rstrip('\n')
sender = 'root@%s' % (socket.gethostname())
2020-01-30 21:38:39 +05:00
TO = [adminEmail]
message = """\
2020-01-30 17:58:56 +05:00
From: %s
To: %s
Subject: %s
%s
""" % (
sender, ", ".join(TO), SUBJECT, '[%s] %s. \n' % (time.strftime("%m.%d.%Y_%H-%M-%S"), message))
2020-01-30 21:38:39 +05:00
if email == None or email == 1:
CyberCPLogFileWriter.SendEmail(sender, TO, message)
except BaseException as msg:
file = open(CyberCPLogFileWriter.fileName, 'a')
file.writelines("[" + time.strftime(
"%m.%d.%Y_%H-%M-%S") + "] " + str(msg) + "\n")
file.close()
2020-01-30 17:58:56 +05:00
2019-12-16 11:53:58 +05:00
except BaseException as msg:
2017-10-24 19:16:36 +05:00
return "Can not write to error file."
2018-06-01 02:08:21 +05:00
@staticmethod
def writeforCLI(message, level, method):
try:
file = open(CyberCPLogFileWriter.fileName, 'a')
file.writelines("[" + time.strftime(
"%m.%d.%Y_%H-%M-%S") + "] [" + level + ":" + method + "] " + message + "\n")
2018-06-01 02:08:21 +05:00
file.close()
file.close()
2019-12-16 11:53:58 +05:00
except BaseException:
2018-06-01 02:08:21 +05:00
return "Can not write to error file!"
2017-10-24 19:16:36 +05:00
@staticmethod
def readLastNFiles(numberOfLines,fileName):
try:
2019-12-15 11:34:09 +05:00
lastFewLines = str(subprocess.check_output(["tail", "-n",str(numberOfLines),fileName]).decode("utf-8"))
2017-10-24 19:16:36 +05:00
return lastFewLines
2019-12-10 15:09:10 +05:00
except subprocess.CalledProcessError as msg:
2017-10-24 19:16:36 +05:00
return "File was empty"
2018-08-22 00:37:43 +05:00
@staticmethod
2018-11-10 16:05:40 +05:00
def statusWriter(tempStatusPath, mesg, append = None):
2018-08-22 00:37:43 +05:00
try:
2018-11-10 16:05:40 +05:00
if append == None:
statusFile = open(tempStatusPath, 'w')
else:
statusFile = open(tempStatusPath, 'a')
2019-07-03 13:15:26 +05:00
statusFile.writelines(mesg + '\n')
2018-08-22 00:37:43 +05:00
statusFile.close()
2019-12-10 15:09:10 +05:00
print((mesg + '\n'))
except BaseException as msg:
2019-10-06 18:41:09 +05:00
CyberCPLogFileWriter.writeToFile(str(msg) + ' [statusWriter]')
2020-01-30 21:38:39 +05:00
#print str(msg)