mirror of
https://github.com/usmannasir/cyberpanel.git
synced 2026-02-05 06:09:06 +01:00
- Add CyberCPLogFileWriter.get_current_timestamp() for errorSanitizer (fixes 500 logging) - After upgrade/install: fetch meta.xml from raw GitHub and overwrite installed file so store version (e.g. 1.1.0) is correct even when archive ZIP is cached/stale - Upgrade/install: discover ZIP top-level folder and match plugin folder case-insensitively - Improves redisManager/memcacheManager upgrade and all store installs
134 lines
4.6 KiB
Python
134 lines
4.6 KiB
Python
import subprocess
|
|
import time
|
|
import socket
|
|
import os
|
|
import smtplib
|
|
|
|
class CyberCPLogFileWriter:
|
|
fileName = "/home/cyberpanel/error-logs.txt"
|
|
|
|
@staticmethod
|
|
def get_current_timestamp():
|
|
"""Return current timestamp in same format used for log lines (for errorSanitizer etc)."""
|
|
return time.strftime("%m.%d.%Y_%H-%M-%S")
|
|
|
|
@staticmethod
|
|
def AddFromHeader(sender, message):
|
|
try:
|
|
import re
|
|
|
|
if not re.search('^From: ', message, re.MULTILINE):
|
|
message = 'From: {}\n{}'.format(sender, message)
|
|
|
|
return message
|
|
except BaseException as msg:
|
|
CyberCPLogFileWriter.writeToFile(str(msg) + ' [AddFromHeader]')
|
|
|
|
@staticmethod
|
|
def SendEmail(sender, receivers, message, subject=None, type=None):
|
|
try:
|
|
smtpPath = '/home/cyberpanel/smtpDetails'
|
|
|
|
if os.path.exists(smtpPath):
|
|
import json
|
|
|
|
mailSettings = json.loads(open(smtpPath, 'r').read())
|
|
smtpHost = mailSettings['smtpHost']
|
|
smtpPort = mailSettings['smtpPort']
|
|
smtpUserName = mailSettings['smtpUserName']
|
|
smtpPassword = mailSettings['smtpPassword']
|
|
|
|
smtpServer = smtplib.SMTP(str(smtpHost), int(smtpPort))
|
|
smtpServer.login(smtpUserName, smtpPassword)
|
|
|
|
##
|
|
|
|
if subject != None:
|
|
message = 'Subject: {}\n\n{}'.format(subject, message)
|
|
|
|
message = CyberCPLogFileWriter.AddFromHeader(sender, message)
|
|
smtpServer.sendmail(smtpUserName, receivers, message)
|
|
else:
|
|
smtpObj = smtplib.SMTP('localhost')
|
|
|
|
message = CyberCPLogFileWriter.AddFromHeader(sender, message)
|
|
smtpObj.sendmail(sender, receivers, message)
|
|
except BaseException as msg:
|
|
CyberCPLogFileWriter.writeToFile(str(msg))
|
|
|
|
@staticmethod
|
|
def writeToFile(message, email=None):
|
|
try:
|
|
print("[" + time.strftime("%m.%d.%Y_%H-%M-%S") + "] "+ message)
|
|
file = open(CyberCPLogFileWriter.fileName,'a')
|
|
file.writelines("[" + time.strftime(
|
|
"%m.%d.%Y_%H-%M-%S") + "] "+ message + "\n")
|
|
file.close()
|
|
|
|
## Send Email
|
|
|
|
emailPath = '/usr/local/CyberCP/emailDebug'
|
|
|
|
try:
|
|
if os.path.exists(emailPath):
|
|
SUBJECT = "CyberPanel log reporting"
|
|
adminEmailPath = '/home/cyberpanel/adminEmail'
|
|
adminEmail = open(adminEmailPath, 'r').read().rstrip('\n')
|
|
sender = 'root@%s' % (socket.gethostname())
|
|
TO = [adminEmail]
|
|
message = """\
|
|
From: %s
|
|
To: %s
|
|
Subject: %s
|
|
|
|
%s
|
|
""" % (
|
|
sender, ", ".join(TO), SUBJECT, '[%s] %s. \n' % (time.strftime("%m.%d.%Y_%H-%M-%S"), message))
|
|
|
|
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()
|
|
|
|
except BaseException as msg:
|
|
return "Can not write to error file."
|
|
|
|
@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")
|
|
file.close()
|
|
file.close()
|
|
except BaseException:
|
|
return "Can not write to error file!"
|
|
|
|
@staticmethod
|
|
def readLastNFiles(numberOfLines,fileName):
|
|
try:
|
|
|
|
lastFewLines = str(subprocess.check_output(["tail", "-n",str(numberOfLines),fileName]).decode("utf-8"))
|
|
|
|
return lastFewLines
|
|
|
|
except subprocess.CalledProcessError as msg:
|
|
return "File was empty"
|
|
|
|
@staticmethod
|
|
def statusWriter(tempStatusPath, mesg, append = None):
|
|
try:
|
|
if os.path.exists('/usr/local/CyberCP/debug'):
|
|
print(mesg)
|
|
if append == None:
|
|
statusFile = open(tempStatusPath, 'w')
|
|
else:
|
|
statusFile = open(tempStatusPath, 'a')
|
|
statusFile.writelines(mesg + '\n')
|
|
statusFile.close()
|
|
except BaseException as msg:
|
|
CyberCPLogFileWriter.writeToFile(str(msg) + ' [statusWriter]')
|