2020-01-30 20:29:13 +05:00
|
|
|
import os,sys
|
|
|
|
|
sys.path.append('/usr/local/CyberCP')
|
|
|
|
|
import django
|
|
|
|
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "CyberCP.settings")
|
|
|
|
|
try:
|
|
|
|
|
django.setup()
|
|
|
|
|
except:
|
|
|
|
|
pass
|
2017-10-24 19:16:36 +05:00
|
|
|
import subprocess
|
|
|
|
|
import time
|
2020-01-30 17:58:56 +05:00
|
|
|
import socket
|
2020-01-30 20:29:13 +05:00
|
|
|
try:
|
|
|
|
|
from loginSystem.models import Administrator
|
|
|
|
|
except:
|
|
|
|
|
pass
|
2020-01-30 17:58:56 +05:00
|
|
|
import os
|
2020-01-30 20:29:13 +05:00
|
|
|
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
|
|
|
|
2020-01-30 20:29:13 +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
|
|
|
|
|
def writeToFile(message):
|
|
|
|
|
try:
|
|
|
|
|
file = open(CyberCPLogFileWriter.fileName,'a')
|
|
|
|
|
file.writelines("[" + time.strftime(
|
2019-10-08 13:17:33 -04:00
|
|
|
"%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'
|
|
|
|
|
|
2020-01-30 20:29:13 +05:00
|
|
|
try:
|
|
|
|
|
if os.path.exists(emailPath):
|
|
|
|
|
SUBJECT = "CyberPanel log reporting"
|
|
|
|
|
admin = Administrator.objects.get(userName='admin')
|
|
|
|
|
sender = 'root@%s' % (socket.gethostname())
|
|
|
|
|
TO = [admin.email]
|
|
|
|
|
message = """\
|
2020-01-30 17:58:56 +05:00
|
|
|
From: %s
|
|
|
|
|
To: %s
|
|
|
|
|
Subject: %s
|
|
|
|
|
|
2020-01-30 20:29:13 +05:00
|
|
|
%s
|
|
|
|
|
""" % (
|
|
|
|
|
sender, ", ".join(TO), SUBJECT, '[%s] %s. \n' % (time.strftime("%m.%d.%Y_%H-%M-%S"), message))
|
|
|
|
|
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(
|
2019-10-08 13:17:33 -04:00
|
|
|
"%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]')
|
2019-07-24 22:37:37 +05:00
|
|
|
#print str(msg)
|
|
|
|
|
|
2018-08-22 00:37:43 +05:00
|
|
|
|