2017-10-24 19:16:36 +05:00
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django . shortcuts import render , redirect
from django . http import HttpResponse
import plogical . CyberCPLogFileWriter as logging
from loginSystem . views import loadLoginPage
import json
import subprocess
2018-01-13 21:41:22 +05:00
import psutil
import shlex
import socket
2018-08-18 00:39:10 +05:00
from plogical . acl import ACLManager
2018-11-08 12:11:42 +05:00
import os
2018-11-09 22:01:28 +05:00
from plogical . virtualHostUtilities import virtualHostUtilities
import time
import serverStatusUtil
from plogical . processUtilities import ProcessUtilities
2018-12-13 04:23:08 +05:00
from plogical . httpProc import httpProc
2017-10-24 19:16:36 +05:00
# Create your views here.
def serverStatusHome ( request ) :
try :
userID = request . session [ ' userID ' ]
return render ( request , ' serverStatus/index.html ' )
except KeyError :
return redirect ( loadLoginPage )
def litespeedStatus ( request ) :
try :
userID = request . session [ ' userID ' ]
2018-08-18 00:39:10 +05:00
currentACL = ACLManager . loadedACL ( userID )
2017-10-24 19:16:36 +05:00
2018-08-18 00:39:10 +05:00
if currentACL [ ' admin ' ] == 1 :
pass
else :
return ACLManager . loadError ( )
2017-10-24 19:16:36 +05:00
processList = ProcessUtilities . getLitespeedProcessNumber ( )
2018-11-09 22:01:28 +05:00
OLS = 0
if ProcessUtilities . decideServer ( ) == ProcessUtilities . OLS :
OLS = 1
2017-10-24 19:16:36 +05:00
try :
versionInformation = subprocess . check_output ( [ " /usr/local/lsws/bin/lshttpd " , " -v " ] ) . split ( " \n " )
lsversion = versionInformation [ 0 ]
modules = versionInformation [ 1 ]
counter = 0
loadedModules = [ ]
for items in versionInformation :
if counter == 0 or counter == 1 :
counter = counter + 1
continue
else :
loadedModules . append ( items )
except subprocess . CalledProcessError , msg :
logging . CyberCPLogFileWriter . writeToFile ( str ( msg ) + " [litespeedStatus] " )
2018-11-09 22:01:28 +05:00
return render ( request , " serverStatus/litespeedStatus.html " , { " processList " : processList , " liteSpeedVersionStatus " : " For some reaons not able to load version details, see CyberCP main log file. " , ' OLS ' : OLS } )
2017-10-24 19:16:36 +05:00
if ( processList != 0 ) :
dataForHtml = { " processList " : processList , " lsversion " : lsversion , " modules " : modules ,
2018-11-09 22:01:28 +05:00
" loadedModules " : loadedModules , ' OLS ' : OLS }
2017-10-24 19:16:36 +05:00
return render ( request , " serverStatus/litespeedStatus.html " , dataForHtml )
else :
dataForHtml = { " lsversion " : lsversion , " modules " : modules ,
2018-11-09 22:01:28 +05:00
" loadedModules " : loadedModules , ' OLS ' : OLS }
2017-10-24 19:16:36 +05:00
return render ( request , " serverStatus/litespeedStatus.html " , dataForHtml )
except KeyError , msg :
logging . CyberCPLogFileWriter . writeToFile ( str ( msg ) + " [litespeedStatus] " )
return redirect ( loadLoginPage )
def stopOrRestartLitespeed ( request ) :
try :
userID = request . session [ ' userID ' ]
2018-08-18 00:39:10 +05:00
currentACL = ACLManager . loadedACL ( userID )
2017-10-24 19:16:36 +05:00
2018-08-18 00:39:10 +05:00
if currentACL [ ' admin ' ] == 1 :
pass
else :
return ACLManager . loadErrorJson ( ' reboot ' , 0 )
2017-10-24 19:16:36 +05:00
data = json . loads ( request . body )
reboot = data [ ' reboot ' ]
if reboot == 1 :
if ProcessUtilities . restartLitespeed ( ) == 1 :
status = { " reboot " : 1 , " shutdown " : 0 }
else :
status = { " reboot " : 0 , " shutdown " : 0 , " error_message " : " Please see CyberCP main log file. " }
else :
if ProcessUtilities . stopLitespeed ( ) == 1 :
status = { " reboot " : 0 , " shutdown " : 1 }
else :
status = { " reboot " : 0 , " shutdown " : 0 , " error_message " : " Please see CyberCP main log file. " }
final_json = json . dumps ( status )
return HttpResponse ( final_json )
except KeyError , msg :
logging . CyberCPLogFileWriter . writeToFile ( str ( msg ) + " [stopOrRestartLitespeed] " )
return HttpResponse ( " Not Logged in as admin " )
def cyberCPMainLogFile ( request ) :
try :
2018-08-18 00:39:10 +05:00
userID = request . session [ ' userID ' ]
2017-10-24 19:16:36 +05:00
2018-08-18 00:39:10 +05:00
currentACL = ACLManager . loadedACL ( userID )
2017-10-24 19:16:36 +05:00
2018-08-18 00:39:10 +05:00
if currentACL [ ' admin ' ] == 1 :
pass
else :
return ACLManager . loadError ( )
2017-10-24 19:16:36 +05:00
return render ( request , ' serverStatus/cybercpmainlogfile.html ' )
except KeyError , msg :
logging . CyberCPLogFileWriter . writeToFile ( str ( msg ) + " [cyberCPMainLogFile] " )
return redirect ( loadLoginPage )
def getFurtherDataFromLogFile ( request ) :
try :
2018-08-18 00:39:10 +05:00
userID = request . session [ ' userID ' ]
currentACL = ACLManager . loadedACL ( userID )
2017-10-24 19:16:36 +05:00
2018-08-18 00:39:10 +05:00
if currentACL [ ' admin ' ] == 1 :
pass
2018-06-30 15:29:56 +05:00
else :
2018-08-18 00:39:10 +05:00
return ACLManager . loadErrorJson ( ' logstatus ' , 0 )
fewLinesOfLogFile = logging . CyberCPLogFileWriter . readLastNFiles ( 50 , logging . CyberCPLogFileWriter . fileName )
fewLinesOfLogFile = str ( fewLinesOfLogFile )
status = { " logstatus " : 1 , " logsdata " : fewLinesOfLogFile }
final_json = json . dumps ( status )
return HttpResponse ( final_json )
2017-10-24 19:16:36 +05:00
except KeyError , msg :
status = { " logstatus " : 0 , " error " : " Could not fetch data from log file, please see CyberCP main log file through command line. " }
logging . CyberCPLogFileWriter . writeToFile ( str ( msg ) + " [getFurtherDataFromLogFile] " )
2018-01-13 21:41:22 +05:00
return HttpResponse ( " Not Logged in as admin " )
def services ( request ) :
try :
userID = request . session [ ' userID ' ]
2018-08-18 00:39:10 +05:00
currentACL = ACLManager . loadedACL ( userID )
2018-01-13 21:41:22 +05:00
2018-08-18 00:39:10 +05:00
if currentACL [ ' admin ' ] == 1 :
pass
else :
return ACLManager . loadError ( )
2018-11-09 22:01:28 +05:00
data = { }
if ProcessUtilities . decideServer ( ) == ProcessUtilities . OLS :
data [ ' serverName ' ] = ' OpenLiteSpeed '
else :
data [ ' serverName ' ] = ' LiteSpeed Ent '
2019-01-08 18:09:24 +00:00
dockerInstallPath = ' /usr/bin/docker '
if not os . path . exists ( dockerInstallPath ) :
data [ ' isDocker ' ] = False
else :
data [ ' isDocker ' ] = True
2018-11-09 22:01:28 +05:00
return render ( request , ' serverStatus/services.html ' , data )
2018-01-13 21:41:22 +05:00
except KeyError :
return redirect ( loadLoginPage )
def servicesStatus ( request ) :
try :
userID = request . session [ ' userID ' ]
lsStatus = [ ]
sqlStatus = [ ]
dnsStatus = [ ]
ftpStatus = [ ]
mailStatus = [ ]
2019-01-08 18:09:24 +00:00
dockerStatus = [ ]
2018-01-13 21:41:22 +05:00
processlist = subprocess . check_output ( [ ' ps ' , ' -A ' ] )
def getServiceStats ( service ) :
if service in processlist :
return 1
else :
return 0
def getMemStats ( service ) :
memCount = 0
for proc in psutil . process_iter ( ) :
if service in proc . name ( ) :
process = psutil . Process ( proc . pid )
memCount + = process . memory_info ( ) . rss
return memCount
### [1] status [2] mem
lsStatus . append ( getServiceStats ( ' litespeed ' ) )
if getServiceStats ( ' litespeed ' ) :
lsStatus . append ( getMemStats ( ' litespeed ' ) )
else :
lsStatus . append ( 0 )
2019-01-08 18:09:24 +00:00
# Docker status
dockerStatus . append ( getServiceStats ( ' docker ' ) )
2018-01-13 21:41:22 +05:00
# mysql status
s = socket . socket ( socket . AF_INET , socket . SOCK_STREAM )
result = s . connect_ex ( ( ' 127.0.0.1 ' , 3306 ) )
if result == 0 :
sqlStatus . append ( 1 )
else :
sqlStatus . append ( 0 )
s . close ( )
if getServiceStats ( ' mysql ' ) :
sqlStatus . append ( getMemStats ( ' mysql ' ) )
else :
sqlStatus . append ( 0 )
dnsStatus . append ( getServiceStats ( ' pdns ' ) )
if getServiceStats ( ' pdns ' ) :
dnsStatus . append ( getMemStats ( ' pdns ' ) )
else :
dnsStatus . append ( 0 )
ftpStatus . append ( getServiceStats ( ' pure-ftpd ' ) )
if getServiceStats ( ' pure-ftpd ' ) :
ftpStatus . append ( getMemStats ( ' pure-ftpd ' ) )
else :
ftpStatus . append ( 0 )
mailStatus . append ( getServiceStats ( ' postfix ' ) )
if getServiceStats ( ' postfix ' ) :
mailStatus . append ( getMemStats ( ' postfix ' ) )
else :
mailStatus . append ( 0 )
json_data = { ' status ' :
{ ' litespeed ' : lsStatus [ 0 ] ,
' mysql ' : sqlStatus [ 0 ] ,
' powerdns ' : dnsStatus [ 0 ] ,
' pureftp ' : ftpStatus [ 0 ] ,
2019-01-08 18:09:24 +00:00
' postfix ' : mailStatus [ 0 ] ,
' docker ' : dockerStatus [ 0 ] } ,
2018-01-13 21:41:22 +05:00
' memUsage ' :
{ ' litespeed ' : lsStatus [ 1 ] ,
' mysql ' : sqlStatus [ 1 ] ,
' powerdns ' : dnsStatus [ 1 ] ,
' pureftp ' : ftpStatus [ 1 ] ,
' postfix ' : mailStatus [ 1 ] } }
return HttpResponse ( json . dumps ( json_data ) )
except KeyError :
return redirect ( loadLoginPage )
def servicesAction ( request ) :
try :
2018-08-18 00:39:10 +05:00
userID = request . session [ ' userID ' ]
currentACL = ACLManager . loadedACL ( userID )
2018-01-13 21:41:22 +05:00
2018-08-18 00:39:10 +05:00
if currentACL [ ' admin ' ] == 1 :
pass
else :
return ACLManager . loadErrorJson ( ' serviceAction ' , 0 )
2018-01-13 21:41:22 +05:00
try :
if request . method == ' POST ' :
data = json . loads ( request . body )
service = data [ ' service ' ]
action = data [ ' action ' ]
if action not in [ " stop " , " start " , " restart " ] :
final_dic = { ' serviceAction ' : 0 , " error_message " : " Invalid Action " }
final_json = json . dumps ( final_dic )
return HttpResponse ( final_json )
else :
pass
2019-01-08 18:09:24 +00:00
if service not in [ " lsws " , " mysql " , " pdns " , " pure-ftpd " , " docker " ] :
2018-01-13 21:41:22 +05:00
final_dic = { ' serviceAction ' : 0 , " error_message " : " Invalid Service " }
final_json = json . dumps ( final_dic )
return HttpResponse ( final_json )
else :
2018-11-08 12:11:42 +05:00
if service == ' pure-ftpd ' :
if os . path . exists ( " /etc/lsb-release " ) :
service = ' pure-ftpd-mysql '
else :
service = ' pure-ftpd '
2018-05-29 20:20:05 +05:00
command = ' sudo systemctl %s %s ' % ( action , service )
2018-01-13 21:41:22 +05:00
cmd = shlex . split ( command )
res = subprocess . call ( cmd )
p = subprocess . Popen ( cmd , stdout = subprocess . PIPE )
result = p . communicate ( ) [ 0 ]
if res != 0 :
final_dic = { ' serviceAction ' : 0 , " error_message " : " Error while performing action " }
final_json = json . dumps ( final_dic )
return HttpResponse ( final_json )
else :
final_dic = { ' serviceAction ' : 1 , " error_message " : 0 }
final_json = json . dumps ( final_dic )
return HttpResponse ( final_json )
except BaseException , msg :
final_dic = { ' serviceAction ' : 0 , ' error_message ' : str ( msg ) }
final_json = json . dumps ( final_dic )
return HttpResponse ( final_json )
except KeyError , msg :
final_dic = { ' serviceAction ' : 0 , ' error_message ' : str ( msg ) }
final_json = json . dumps ( final_dic )
return HttpResponse ( final_json )
2018-11-09 22:01:28 +05:00
def switchTOLSWS ( request ) :
try :
userID = request . session [ ' userID ' ]
currentACL = ACLManager . loadedACL ( userID )
if currentACL [ ' admin ' ] == 1 :
pass
else :
return ACLManager . loadErrorJson ( ' status ' , 0 )
data = json . loads ( request . body )
execPath = " sudo /usr/local/CyberCP/bin/python " + virtualHostUtilities . cyberPanel + " /serverStatus/serverStatusUtil.py "
execPath = execPath + " switchTOLSWS --licenseKey " + data [ ' licenseKey ' ]
subprocess . Popen ( shlex . split ( execPath ) )
time . sleep ( 2 )
data_ret = { ' status ' : 1 , ' error_message ' : " None " , }
json_data = json . dumps ( data_ret )
return HttpResponse ( json_data )
except BaseException , msg :
data_ret = { ' status ' : 0 , ' error_message ' : str ( msg ) }
json_data = json . dumps ( data_ret )
return HttpResponse ( json_data )
def switchTOLSWSStatus ( request ) :
try :
command = ' sudo cat ' + serverStatusUtil . ServerStatusUtil . lswsInstallStatusPath
output = subprocess . check_output ( shlex . split ( command ) )
if output . find ( ' [404] ' ) > - 1 :
data_ret = { ' abort ' : 1 , ' requestStatus ' : output , ' installed ' : 0 }
json_data = json . dumps ( data_ret )
return HttpResponse ( json_data )
elif output . find ( ' [200] ' ) > - 1 :
data_ret = { ' abort ' : 1 , ' requestStatus ' : output , ' installed ' : 1 }
json_data = json . dumps ( data_ret )
return HttpResponse ( json_data )
else :
data_ret = { ' abort ' : 0 , ' requestStatus ' : output , ' installed ' : 0 }
json_data = json . dumps ( data_ret )
return HttpResponse ( json_data )
except BaseException , msg :
data_ret = { ' abort ' : 1 , ' requestStatus ' : str ( msg ) , ' installed ' : 0 }
json_data = json . dumps ( data_ret )
return HttpResponse ( json_data )
def licenseStatus ( request ) :
try :
userID = request . session [ ' userID ' ]
try :
currentACL = ACLManager . loadedACL ( userID )
if currentACL [ ' admin ' ] == 1 :
pass
else :
return ACLManager . loadErrorJson ( ' status ' , 0 )
command = ' sudo cat /usr/local/lsws/conf/serial.no '
serial = subprocess . check_output ( shlex . split ( command ) )
command = ' sudo /usr/local/lsws/bin/lshttpd -V '
expiration = subprocess . check_output ( shlex . split ( command ) )
final_dic = { ' status ' : 1 , " erroMessage " : 0 , ' lsSerial ' : serial , ' lsexpiration ' : expiration }
final_json = json . dumps ( final_dic )
return HttpResponse ( final_json )
except BaseException , msg :
final_dic = { ' status ' : 0 , ' erroMessage ' : str ( msg ) }
final_json = json . dumps ( final_dic )
return HttpResponse ( final_json )
except KeyError , msg :
final_dic = { ' status ' : 0 , ' erroMessage ' : str ( msg ) }
final_json = json . dumps ( final_dic )
return HttpResponse ( final_json )
def changeLicense ( request ) :
try :
userID = request . session [ ' userID ' ]
try :
currentACL = ACLManager . loadedACL ( userID )
if currentACL [ ' admin ' ] == 1 :
pass
else :
return ACLManager . loadErrorJson ( ' status ' , 0 )
data = json . loads ( request . body )
newKey = data [ ' newKey ' ]
command = ' sudo chown -R cyberpanel:cyberpanel /usr/local/lsws/conf '
subprocess . call ( shlex . split ( command ) )
serialPath = ' /usr/local/lsws/conf/serial.no '
serialFile = open ( serialPath , ' w ' )
serialFile . write ( newKey )
serialFile . close ( )
command = ' sudo chown -R lsadm:lsadm /usr/local/lsws/conf '
subprocess . call ( shlex . split ( command ) )
command = ' sudo /usr/local/lsws/bin/lshttpd -r '
subprocess . call ( shlex . split ( command ) )
command = ' sudo /usr/local/lsws/bin/lswsctrl restart '
subprocess . call ( shlex . split ( command ) )
final_dic = { ' status ' : 1 , " erroMessage " : ' None ' }
final_json = json . dumps ( final_dic )
return HttpResponse ( final_json )
2018-12-13 04:23:08 +05:00
except BaseException , msg :
final_dic = { ' status ' : 0 , ' erroMessage ' : str ( msg ) }
final_json = json . dumps ( final_dic )
return HttpResponse ( final_json )
except KeyError , msg :
final_dic = { ' status ' : 0 , ' erroMessage ' : str ( msg ) }
final_json = json . dumps ( final_dic )
return HttpResponse ( final_json )
def topProcesses ( request ) :
try :
userID = request . session [ ' userID ' ]
currentACL = ACLManager . loadedACL ( userID )
if currentACL [ ' admin ' ] == 1 :
pass
else :
return ACLManager . loadError ( )
templateName = " serverStatus/topProcesses.html "
proc = httpProc ( request , templateName )
return proc . renderPre ( )
except KeyError , msg :
logging . CyberCPLogFileWriter . writeToFile ( str ( msg ) + " [litespeedStatus] " )
return redirect ( loadLoginPage )
def topProcessesStatus ( request ) :
try :
with open ( " /home/cyberpanel/top " , " w " ) as outfile :
subprocess . call ( " sudo top -n1 -b " , shell = True , stdout = outfile )
data = open ( ' /home/cyberpanel/top ' , ' r ' ) . readlines ( )
json_data = " [ "
checker = 0
counter = 0
loadAVG = data [ 0 ] . split ( ' ' )
loadAVG = filter ( lambda a : a != ' ' , loadAVG )
loadNow = data [ 2 ] . split ( ' ' )
loadNow = filter ( lambda a : a != ' ' , loadNow )
memory = data [ 3 ] . split ( ' ' )
memory = filter ( lambda a : a != ' ' , memory )
swap = data [ 4 ] . split ( ' ' )
swap = filter ( lambda a : a != ' ' , swap )
processes = data [ 1 ] . split ( ' ' )
processes = filter ( lambda a : a != ' ' , processes )
for items in data :
counter = counter + 1
if counter < = 7 :
continue
points = items . split ( ' ' )
points = filter ( lambda a : a != ' ' , points )
dic = { ' PID ' : points [ 0 ] , ' User ' : points [ 1 ] , ' VIRT ' : points [ 4 ] ,
' RES ' : points [ 5 ] , ' S ' : points [ 7 ] , ' CPU ' : points [ 8 ] , ' MEM ' : points [ 9 ] ,
' Time ' : points [ 10 ] , ' Command ' : points [ 11 ]
}
if checker == 0 :
json_data = json_data + json . dumps ( dic )
checker = 1
else :
json_data = json_data + ' , ' + json . dumps ( dic )
json_data = json_data + ' ] '
data = { }
data [ ' status ' ] = 1
data [ ' error_message ' ] = ' None '
data [ ' data ' ] = json_data
## CPU
data [ ' cpuNow ' ] = loadNow [ 1 ]
data [ ' cpuOne ' ] = loadAVG [ - 3 ] . rstrip ( ' , ' )
data [ ' cpuFive ' ] = loadAVG [ - 2 ] . rstrip ( ' , ' )
data [ ' cpuFifteen ' ] = loadAVG [ - 1 ]
## CPU Time spent
data [ ' ioWait ' ] = loadNow [ 9 ] + ' % '
data [ ' idleTime ' ] = loadNow [ 7 ] + ' % '
data [ ' hwInterrupts ' ] = loadNow [ 11 ] + ' % '
data [ ' Softirqs ' ] = loadNow [ 13 ] + ' % '
## Memory
data [ ' totalMemory ' ] = str ( int ( float ( memory [ 3 ] ) / 1024 ) ) + ' MB '
data [ ' freeMemory ' ] = str ( int ( float ( memory [ 5 ] ) / 1024 ) ) + ' MB '
data [ ' usedMemory ' ] = str ( int ( float ( memory [ 7 ] ) / 1024 ) ) + ' MB '
data [ ' buffCache ' ] = str ( int ( float ( memory [ 9 ] ) / 1024 ) ) + ' MB '
## Swap
data [ ' swapTotalMemory ' ] = str ( int ( float ( swap [ 2 ] ) / 1024 ) ) + ' MB '
data [ ' swapFreeMemory ' ] = str ( int ( float ( swap [ 4 ] ) / 1024 ) ) + ' MB '
data [ ' swapUsedMemory ' ] = str ( int ( float ( swap [ 6 ] ) / 1024 ) ) + ' MB '
data [ ' swapBuffCache ' ] = str ( int ( float ( swap [ 8 ] ) / 1024 ) ) + ' MB '
## Processes
data [ ' totalProcesses ' ] = processes [ 1 ]
data [ ' runningProcesses ' ] = processes [ 3 ]
data [ ' sleepingProcesses ' ] = processes [ 5 ]
data [ ' stoppedProcesses ' ] = processes [ 7 ]
data [ ' zombieProcesses ' ] = processes [ 9 ]
## CPU Details
command = ' sudo cat /proc/cpuinfo '
output = subprocess . check_output ( shlex . split ( command ) ) . splitlines ( )
import psutil
data [ ' cores ' ] = psutil . cpu_count ( )
for items in output :
if items . find ( ' model name ' ) > - 1 :
modelName = items . split ( ' : ' ) [ 1 ] . strip ( ' ' )
index = modelName . find ( ' CPU ' )
data [ ' modelName ' ] = modelName [ 0 : index ]
elif items . find ( ' cpu MHz ' ) > - 1 :
data [ ' cpuMHZ ' ] = items . split ( ' : ' ) [ 1 ] . strip ( ' ' )
elif items . find ( ' cache size ' ) > - 1 :
data [ ' cacheSize ' ] = items . split ( ' : ' ) [ 1 ] . strip ( ' ' )
break
final_json = json . dumps ( data )
return HttpResponse ( final_json )
except BaseException , msg :
data_ret = { ' status ' : 0 , ' error_message ' : str ( msg ) }
json_data = json . dumps ( data_ret )
return HttpResponse ( json_data )
def killProcess ( request ) :
try :
userID = request . session [ ' userID ' ]
try :
currentACL = ACLManager . loadedACL ( userID )
if currentACL [ ' admin ' ] == 1 :
pass
else :
return ACLManager . loadErrorJson ( ' status ' , 0 )
data = json . loads ( request . body )
pid = data [ ' pid ' ]
ProcessUtilities . executioner ( ' sudo kill ' + pid )
proc = httpProc ( request , None )
return proc . ajax ( 1 , None )
2018-11-09 22:01:28 +05:00
except BaseException , msg :
final_dic = { ' status ' : 0 , ' erroMessage ' : str ( msg ) }
final_json = json . dumps ( final_dic )
return HttpResponse ( final_json )
except KeyError , msg :
final_dic = { ' status ' : 0 , ' erroMessage ' : str ( msg ) }
final_json = json . dumps ( final_dic )
return HttpResponse ( final_json )