Added Services Page, Bug fix to Website Creation!

This commit is contained in:
usmannasir
2018-01-13 21:41:22 +05:00
parent db9195540e
commit 024783b2a3
30 changed files with 723 additions and 36 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

View File

@@ -276,4 +276,165 @@ app.controller('readCyberCPLogFile', function($scope,$http) {
/* Java script code to read log file ends here */
/* Java script code to read log file ends here */
/* Java script code to read log file ends here */
/* Services */
app.controller('servicesManager', function($scope,$http) {
$scope.services = false;
$scope.btnDisable = false;
$scope.actionLoader = false;
function getServiceStatus(){
$scope.btnDisable = true;
url = "/serverstatus/servicesStatus";
$http.post(url).then(ListInitialDatas, cantLoadInitialDatas);
function ListInitialDatas(response) {
if (response.data.status.litespeed) {
$scope.olsStatus = "Running";
$scope.olsStats = true;
$scope.olsStart = false;
$scope.olsStop = true;
$scope.olsMem = Math.round(parseInt(response.data.memUsage.litespeed) / 1048576) + " MB";
}
else {
$scope.olsStatus = "Stopped";
$scope.olsStats = false;
$scope.olsStart = true;
$scope.olsStop = false;
}
// Update SQL stats
if (response.data.status.mysql) {
$scope.sqlStatus = "Running";
$scope.sqlStats = true;
$scope.sqlStart = false;
$scope.sqlStop = true;
$scope.sqlMem = Math.round(parseInt(response.data.memUsage.mysql) / 1048576) + " MB";
}
else {
$scope.sqlStatus = "Stopped";
$scope.sqlStats = false;
$scope.sqlStart = true;
$scope.sqlStop = false;
}
// Update DNS stats
if (response.data.status.powerdns) {
$scope.dnsStatus = "Running";
$scope.dnsStats = true;
$scope.dnsStart = false;
$scope.dnsStop = true;
$scope.dnsMem = Math.round(parseInt(response.data.memUsage.powerdns) / 1048576) + " MB";
}
else {
$scope.dnsStatus = "Stopped";
$scope.dnsStats = false;
$scope.dnsStart = true;
$scope.dnsStop = false;
}
// Update FTP stats
if (response.data.status.pureftp) {
$scope.ftpStatus = "Running";
$scope.ftpStats = true;
$scope.ftpStart = false;
$scope.ftpStop = true;
$scope.ftpMem = Math.round(parseInt(response.data.memUsage.pureftp) / 1048576) + " MB";
}
else {
$scope.ftpStatus = "Stopped";
$scope.ftpStats = false;
$scope.ftpStart = true;
$scope.ftpStop = false;
}
$scope.services = true;
$scope.btnDisable = false;
}
function cantLoadInitialDatas(response) {
$scope.couldNotConnect = true;
}
};
getServiceStatus();
$scope.serviceAction = function(serviceName, action){
$scope.ActionProgress = true;
$scope.btnDisable = true;
$scope.ActionSuccessfull = false;
$scope.ActionFailed = false;
$scope.couldNotConnect = false;
$scope.actionLoader = true;
url = "/serverstatus/servicesAction";
var data = {
service:serviceName,
action:action
};
var config = {
headers : {
'X-CSRFToken': getCookie('csrftoken')
}
};
$http.post(url, data,config).then(ListInitialDatas, cantLoadInitialDatas);
function ListInitialDatas(response) {
console.log(response.data);
if(response.data.serviceAction == 1){
setTimeout(function() {
getServiceStatus();
setTimeout(function() {
$scope.ActionSuccessfull = true;
$scope.ActionFailed = false;
$scope.couldNotConnect = false;
$scope.actionLoader = false;
$scope.btnDisable = false;
},1000)
}, 3000);
}
else{
setTimeout(function() {
getServiceStatus();
setTimeout(function() {
$scope.ActionSuccessfull = false;
$scope.ActionFailed = true;
$scope.couldNotConnect = false;
$scope.actionLoader = false;
$scope.btnDisable = false;
},1000)
}, 5000);
}
}
function cantLoadInitialDatas(response) {
$scope.ActionSuccessfull = false;
$scope.ActionFailed = false;
$scope.couldNotConnect = true;
$scope.actionLoader = false;
$scope.btnDisable = false;
}
}
});

View File

@@ -0,0 +1,145 @@
{% extends "baseTemplate/index.html" %}
{% load i18n %}
{% block title %}{% trans "Services - CyberPanel" %}{% endblock %}
{% block content %}
{% load static %}
{% get_current_language as LANGUAGE_CODE %}
<!-- Current language: {{ LANGUAGE_CODE }} -->
<style>
.serviceImg { text-align:center; }
.serviceImg img { bottom: 63px; width: 100px; margin-top: -65px; background: white; padding: 10px; height: 100px;}
.service-panel { margin-top: 63px; border: 0; background: #38b4bf; }
.service-panel .panel-body { height: 188px; }
.serviceDetails { color: white; text-align:center; margin-top: 15px;}
.serviceActionBtn { margin: 10px; }
</style>
<div ng-controller="servicesManager" class="container">
<div id="page-title">
<h2>Services <img ng-show="actionLoader" src="/static/images/loading.gif"></h2>
<p>Show stats for services and actions (Start, Stop, Restart)</p>
</div>
<div >
<div class="row">
<div class="col-sm-6 col-md-6">
<div class="panel panel-default service-panel">
<div class="panel-body">
<div class="serviceImg">
<img src="{% static 'images/litespeed.png' %}" alt="OpenLitespeed" class="img-circle">
</div>
<div class="serviceDetails">
<div class="serviceHeading">
<h5><b>OpenLitespeed</b></h5>
<span class="help-block" ng-bind="olsStatus">Stopped</span>
</div>
<div class="serviceActionBtn">
<button type="button" class="btn btn-success" ng-disabled="btnDisable" ng-show="olsStart" ng-click="serviceAction('lsws','start')" data-toggle="tooltip" title="Start!"><i class="glyph-icon icon-play" aria-hidden="true"></i></button>
<button type="button" class="btn btn-warning" ng-disabled="btnDisable" ng-show="olsStop" ng-click="serviceAction('lsws','stop')" data-toggle="tooltip" title="Stop!"><i class="glyph-icon icon-pause" aria-hidden="true"></i></button>
<button type="button" class="btn btn-default" ng-disabled="btnDisable" ng-click="serviceAction('lsws','restart')" data-toggle="tooltip" title="Restart!"><i class="glyph-icon icon-refresh" aria-hidden="true"></i></button>
</div>
<div ng-show="olsStats">
<p>Memory Usage: <span ng-bind="olsMem"></span></p>
</div>
</div>
</div>
</div>
</div>
<div class="col-sm-6 col-md-6">
<div class="panel panel-default service-panel">
<div class="panel-body">
<div class="serviceImg">
<img src="{% static 'images/mariadb.png' %}" alt="MariaDB" class="img-circle">
</div>
<div class="serviceDetails">
<div class="serviceHeading">
<h5><b>MariaDB</b></h5>
<span class="help-block" ng-bind="sqlStatus">Stopped</span>
</div>
<div class="serviceActionBtn">
<button type="button" class="btn btn-success" ng-disabled="btnDisable" ng-show="sqlStart" ng-click="serviceAction('mysql','start')" data-toggle="tooltip" title="Start!"><i class="glyph-icon icon-play" aria-hidden="true"></i></button>
<button type="button" class="btn btn-warning" ng-disabled="btnDisable" ng-show="sqlStop" ng-click="serviceAction('mysql','stop')" data-toggle="tooltip" title="Stop!"><i class="glyph-icon icon-pause" aria-hidden="true"></i></button>
<button type="button" class="btn btn-default" ng-disabled="btnDisable" ng-click="serviceAction('mysql','restart')" data-toggle="tooltip" title="Restart!"><i class="glyph-icon icon-refresh" aria-hidden="true"></i></button>
</div>
<div ng-show="sqlStats">
<p>Memory Usage: <span ng-bind="sqlMem"></span></p>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6 col-md-6">
<div class="panel panel-default service-panel">
<div class="panel-body">
<div class="serviceImg">
<img src="{% static 'images/powerdns.png' %}" alt="PowerDNS" class="img-circle">
</div>
<div class="serviceDetails">
<div class="serviceHeading">
<h5><b>PowerDNS</b></h5>
<span class="help-block" ng-bind="dnsStatus">Stopped</span>
</div>
<div class="serviceActionBtn">
<button type="button" class="btn btn-success" ng-disabled="btnDisable" ng-show="dnsStart" ng-click="serviceAction('pdns','start')" data-toggle="tooltip" title="Start!"><i class="glyph-icon icon-play" aria-hidden="true"></i></button>
<button type="button" class="btn btn-warning" ng-disabled="btnDisable" ng-show="dnsStop" ng-click="serviceAction('pdns','stop')" data-toggle="tooltip" title="Stop!"><i class="glyph-icon icon-pause" aria-hidden="true"></i></button>
<button type="button" class="btn btn-default" ng-disabled="btnDisable" ng-click="serviceAction('pdns','restart')" data-toggle="tooltip" title="Restart!"><i class="glyph-icon icon-refresh" aria-hidden="true"></i></button>
</div>
<div ng-show="dnsStats">
<p>Memory Usage: <span ng-bind="dnsMem"></span></p>
</div>
</div>
</div>
</div>
</div>
<div class="col-sm-6 col-md-6">
<div class="panel panel-default service-panel">
<div class="panel-body">
<div class="serviceImg">
<img src="{% static 'images/pureftpd.png' %}" alt="PureFTPd" class="img-circle">
</div>
<div class="serviceDetails">
<div class="serviceHeading">
<h5><b>PureFTPd</b></h5>
<span class="help-block" ng-bind="dnsStatus">Stopped</span>
</div>
<div class="serviceActionBtn">
<button type="button" class="btn btn-success" ng-disabled="btnDisable" ng-show="ftpStart" ng-click="serviceAction('pure-ftpd','start')" data-toggle="tooltip" title="Start!"><i class="glyph-icon icon-play" aria-hidden="true"></i></button>
<button type="button" class="btn btn-warning" ng-disabled="btnDisable" ng-show="ftpStop" ng-click="serviceAction('pure-ftpd','stop')" data-toggle="tooltip" title="Stop!"><i class="glyph-icon icon-pause" aria-hidden="true"></i></button>
<button type="button" class="btn btn-default" ng-disabled="btnDisable" ng-click="serviceAction('pure-ftpd','restart')" data-toggle="tooltip" title="Restart!"><i class="glyph-icon icon-refresh" aria-hidden="true"></i></button>
</div>
<div ng-show="ftpStats">
<p>Memory Usage: <span ng-bind="ftpMem"></span></p>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label"></label>
<div class="col-sm-4">
<div ng-show="ActionFailed" class="alert alert-danger">
<p class="ng-binding">Action Failed</p>
</div>
<div ng-show="ActionSuccessfull" class="alert alert-success ng-hide">
<p class="ng-binding">Action Completed</p>
</div>
<div ng-show="couldNotConnect" class="alert alert-danger ng-hide">
<p>Could not connect to server. Please refresh this page.</p>
</div>
</div>
</div>
</div>
</div>
{% endblock %}

View File

@@ -8,6 +8,8 @@ urlpatterns = [
url(r'^cyberCPMainLogFile', views.cyberCPMainLogFile, name='cyberCPMainLogFile'),
url(r'^getFurtherDataFromLogFile',views.getFurtherDataFromLogFile,name='getFurtherDataFromLogFile'),
url(r'^servicesStatus', views.servicesStatus, name='servicesStatus'),
url(r'^servicesAction', views.servicesAction, name='servicesAction'),
url(r'^services', views.services, name='services'),
]

View File

@@ -9,6 +9,9 @@ from loginSystem.views import loadLoginPage
import json
import subprocess
from loginSystem.models import Administrator
import psutil
import shlex
import socket
# Create your views here.
@@ -149,4 +152,169 @@ def getFurtherDataFromLogFile(request):
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]")
return HttpResponse("Not Logged in as admin")
return HttpResponse("Not Logged in as admin")
def services(request):
try:
userID = request.session['userID']
admin = Administrator.objects.get(pk=userID)
if admin.type == 3:
return HttpResponse("You don't have enough priviliges to access this page.")
return render(request, 'serverStatus/services.html')
except KeyError:
return redirect(loadLoginPage)
def servicesStatus(request):
try:
userID = request.session['userID']
admin = Administrator.objects.get(pk=userID)
if admin.type == 3:
final = {'error': 1, "error_message": "Not enough privilege"}
final_json = json.dumps(final)
return HttpResponse(final_json)
lsStatus = []
sqlStatus = []
dnsStatus = []
ftpStatus = []
mailStatus = []
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)
# 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],
'postfix': mailStatus[0]},
'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:
val = request.session['userID']
admin = Administrator.objects.get(pk=val)
if admin.type == 3:
final = {'serviceAction': 0, "error_message": "Not enough privilege"}
final_json = json.dumps(final)
return HttpResponse(final_json)
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
if service not in ["lsws", "mysql", "pdns", "pure-ftpd"]:
final_dic = {'serviceAction': 0, "error_message": "Invalid Service"}
final_json = json.dumps(final_dic)
return HttpResponse(final_json)
else:
command = 'sudo systemctl %s %s' % (action, service)
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)