diff --git a/main/views.py b/main/views.py index eb32022..0d8150f 100755 --- a/main/views.py +++ b/main/views.py @@ -1,35 +1,35 @@ -#The MIT License (MIT) +# The MIT License (MIT) # -#Copyright (c) 2014 Florian Neagu - michaelneagu@gmail.com - https://github.com/k3oni/ +# Copyright (c) 2014 Florian Neagu - michaelneagu@gmail.com - https://github.com/k3oni/ # -#Permission is hereby granted, free of charge, to any person obtaining a copy -#of this software and associated documentation files (the "Software"), to deal -#in the Software without restriction, including without limitation the rights -#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -#copies of the Software, and to permit persons to whom the Software is -#furnished to do so, subject to the following conditions: +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: # -#The above copyright notice and this permission notice shall be included in all -#copies or substantial portions of the Software. +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. # -#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -#SOFTWARE. - -import platform, os, multiprocessing, json - +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +import platform +import os +import multiprocessing from datetime import timedelta from django.contrib.auth.decorators import login_required -from django.core import serializers from django.shortcuts import render_to_response -from django.http import HttpResponseRedirect, HttpResponse +from django.http import HttpResponseRedirect from django.template import RequestContext -from django.utils.translation import ugettext_lazy as _ + from pydash.settings import TIME_JS_REFRESH, TIME_JS_REFRESH_LONG, TIME_JS_REFRESH_NET, VERSION time_refresh = TIME_JS_REFRESH @@ -37,6 +37,7 @@ time_refresh_long = TIME_JS_REFRESH_LONG time_refresh_net = TIME_JS_REFRESH_NET version = VERSION + @login_required(login_url='/login/') def index(request): """ @@ -48,20 +49,21 @@ def index(request): def chunks(get, n): - return [get[i:i+n] for i in range(0, len(get), n)] + return [get[i:i + n] for i in range(0, len(get), n)] + def get_uptime(): """ Get uptime """ - try: - with open('/proc/uptime', 'r') as f: - uptime_seconds = float(f.readline().split()[0]) - uptime_time = str(timedelta(seconds = uptime_seconds)) - data = uptime_time.split('.', 1)[0] - - except Exception,err: - data = str(err) + try: + with open('/proc/uptime', 'r') as f: + uptime_seconds = float(f.readline().split()[0]) + uptime_time = str(timedelta(seconds=uptime_seconds)) + data = uptime_time.split('.', 1)[0] + + except Exception, err: + data = str(err) return data @@ -73,7 +75,7 @@ def get_ipaddress(): data = [] try: eth = os.popen("ip addr | grep LOWER_UP | awk '{print $2}'") - iface = eth.read().strip().replace(':','').split('\n') + iface = eth.read().strip().replace(':', '').split('\n') eth.close() del iface[0] @@ -82,7 +84,7 @@ def get_ipaddress(): data1 = pipe.read().strip().split('\n') pipe.close() if len(data1) == 2: - data1.append('unavailable', 'unavailable') + data1.append('unavailable') if len(data1) == 3: data1.append('unavailable') data1[0] = i @@ -92,199 +94,207 @@ def get_ipaddress(): data = ips - except Exception,err: - data = str(err) + except Exception, err: + data = str(err) return data + def get_cpus(): """ Get the number of CPUs and model/type """ try: - pipe = os.popen("cat /proc/cpuinfo |" + "grep 'model name'") - data = pipe.read().strip().split(':')[-1] - pipe.close() - - if not data: - pipe = os.popen("cat /proc/cpuinfo |" + "grep 'Processor'") - data = pipe.read().strip().split(':')[-1] - pipe.close() - - cpus = multiprocessing.cpu_count() - - data = {'cpus': cpus, 'type': data} - + pipe = os.popen("cat /proc/cpuinfo |" + "grep 'model name'") + data = pipe.read().strip().split(':')[-1] + pipe.close() + + if not data: + pipe = os.popen("cat /proc/cpuinfo |" + "grep 'Processor'") + data = pipe.read().strip().split(':')[-1] + pipe.close() + + cpus = multiprocessing.cpu_count() + + data = {'cpus': cpus, 'type': data} + except Exception, err: - data = str(err) - + data = str(err) + return data - + + def get_users(): """ Get the current logged in users """ try: - pipe = os.popen("who |" + "awk '{print $1, $2, $6}'") - data = pipe.read().strip().split('\n') - pipe.close() - - if data == [""]: - data = None - else: - data = [i.split(None, 3) for i in data] - + pipe = os.popen("who |" + "awk '{print $1, $2, $6}'") + data = pipe.read().strip().split('\n') + pipe.close() + + if data == [""]: + data = None + else: + data = [i.split(None, 3) for i in data] + except Exception, err: - data = str(err) - + data = str(err) + return data - + + def get_traffic(request): """ Get the traffic for the specified interface """ try: - pipe = os.popen("cat /proc/net/dev |" + "grep " + request + "| awk '{print $1, $9}'") - data = pipe.read().strip().split(':',1)[-1] - pipe.close() - - if not data[0].isdigit(): - pipe = os.popen("cat /proc/net/dev |" + "grep " + request + "| awk '{print $2, $10}'") - data = pipe.read().strip().split(':',1)[-1] - pipe.close() - - data = data.split() - - traffic_in = int(data[0]) - traffic_out = int(data[1]) - - all_traffic = {'traffic_in': traffic_in, 'traffic_out': traffic_out} - - data = all_traffic - - except Exception,err: - data = str(err) - + pipe = os.popen("cat /proc/net/dev |" + "grep " + request + "| awk '{print $1, $9}'") + data = pipe.read().strip().split(':', 1)[-1] + pipe.close() + + if not data[0].isdigit(): + pipe = os.popen("cat /proc/net/dev |" + "grep " + request + "| awk '{print $2, $10}'") + data = pipe.read().strip().split(':', 1)[-1] + pipe.close() + + data = data.split() + + traffic_in = int(data[0]) + traffic_out = int(data[1]) + + all_traffic = {'traffic_in': traffic_in, 'traffic_out': traffic_out} + + data = all_traffic + + except Exception, err: + data = str(err) + return data + def get_platform(): """ Get the OS name, hostname and kernel """ try: - osname = " ".join(platform.linux_distribution()) - uname = platform.uname() - - if osname == ' ': - osname = uname[0] - - data = {'osname': osname, 'hostname': uname[1], 'kernel': uname[2] } - - except Exception,err: - data = str(err) + osname = " ".join(platform.linux_distribution()) + uname = platform.uname() + + if osname == ' ': + osname = uname[0] + + data = {'osname': osname, 'hostname': uname[1], 'kernel': uname[2]} + + except Exception, err: + data = str(err) return data - + + def get_disk(): """ Get disk usage """ try: - pipe = os.popen("df -Ph | " + "grep -v Filesystem | " + "awk '{print $1, $2, $3, $4, $5, $6}'") - data = pipe.read().strip().split('\n') - pipe.close() - - data = [i.split(None, 6) for i in data] - - except Exception,err: - data = str(err) - + pipe = os.popen("df -Ph | " + "grep -v Filesystem | " + "awk '{print $1, $2, $3, $4, $5, $6}'") + data = pipe.read().strip().split('\n') + pipe.close() + + data = [i.split(None, 6) for i in data] + + except Exception, err: + data = str(err) + return data + def get_disk_rw(): """ Get the disk reads and writes """ try: - pipe = os.popen("cat /proc/partitions | grep -v 'major' | awk '{print $4}'") - data = pipe.read().strip().split('\n') - pipe.close() - - rws = [] - for i in data: - if i.isalpha(): - pipe = os.popen("cat /proc/diskstats | grep -w '" + i + "'|awk '{print $4, $8}'") - rw = pipe.read().strip().split() - pipe.close() - - rws.append([i, rw[0], rw[1]]) - - if not rws: - pipe = os.popen("cat /proc/diskstats | grep -w '" + data[0] + "'|awk '{print $4, $8}'") - rw = pipe.read().strip().split() - pipe.close() - - rws.append([data[0], rw[0], rw[1]]) - - data = rws - - except Exception,err: - data = str(err) - + pipe = os.popen("cat /proc/partitions | grep -v 'major' | awk '{print $4}'") + data = pipe.read().strip().split('\n') + pipe.close() + + rws = [] + for i in data: + if i.isalpha(): + pipe = os.popen("cat /proc/diskstats | grep -w '" + i + "'|awk '{print $4, $8}'") + rw = pipe.read().strip().split() + pipe.close() + + rws.append([i, rw[0], rw[1]]) + + if not rws: + pipe = os.popen("cat /proc/diskstats | grep -w '" + data[0] + "'|awk '{print $4, $8}'") + rw = pipe.read().strip().split() + pipe.close() + + rws.append([data[0], rw[0], rw[1]]) + + data = rws + + except Exception, err: + data = str(err) + return data + def get_mem(): """ Get memory usage """ try: - pipe = os.popen("free -tmo | " + "grep 'Mem' | " + "awk '{print $2,$4}'") - data = pipe.read().strip().split() - pipe.close() - - allmem = int(data[0]) - freemem = int(data[1]) - - percent = (100 - ((freemem * 100) / allmem)) - usage = (allmem - freemem) - - mem_usage = {'usage': usage, 'free': freemem, 'percent': percent} - - data = mem_usage - - except Exception,err: - data = str(err) - + pipe = os.popen("free -tmo | " + "grep 'Mem' | " + "awk '{print $2,$4}'") + data = pipe.read().strip().split() + pipe.close() + + allmem = int(data[0]) + freemem = int(data[1]) + + percent = (100 - ((freemem * 100) / allmem)) + usage = (allmem - freemem) + + mem_usage = {'usage': usage, 'free': freemem, 'percent': percent} + + data = mem_usage + + except Exception, err: + data = str(err) + return data - + + def get_cpu_usage(): """ Get the CPU usage and running processes """ - try: - pipe = os.popen("ps aux --sort -%cpu,-rss") - data = pipe.read().strip().split('\n') - pipe.close() - - usage = [i.split(None, 10) for i in data] - del usage[0] - - total_usage = [] - - for element in usage: - usage_cpu = element[2] - total_usage.append(usage_cpu) - - total_usage = sum(float(i) for i in total_usage) - - total_free = ((100 * int(get_cpus()['cpus'])) - float(total_usage)) - - cpu_used = {'free': total_free, 'used': float(total_usage), 'all': usage} - - data = cpu_used - - except Exception,err: - data = str(err) + try: + pipe = os.popen("ps aux --sort -%cpu,-rss") + data = pipe.read().strip().split('\n') + pipe.close() + + usage = [i.split(None, 10) for i in data] + del usage[0] + + total_usage = [] + + for element in usage: + usage_cpu = element[2] + total_usage.append(usage_cpu) + + total_usage = sum(float(i) for i in total_usage) + + total_free = ((100 * int(get_cpus()['cpus'])) - float(total_usage)) + + cpu_used = {'free': total_free, 'used': float(total_usage), 'all': usage} + + data = cpu_used + + except Exception, err: + data = str(err) return data @@ -300,28 +310,29 @@ def get_load(): return data + def get_netstat(): """ Get ports and applications """ try: - pipe = os.popen("ss -tnp | grep ESTAB | awk '{print $4, $5}'| sed 's/::ffff://g' | awk -F: '{print $1, $2}' | awk 'NF > 0' | sort -n | uniq -c") + pipe = os.popen( + "ss -tnp | grep ESTAB | awk '{print $4, $5}'| sed 's/::ffff://g' | awk -F: '{print $1, $2}' " + "| awk 'NF > 0' | sort -n | uniq -c") data = pipe.read().strip().split('\n') pipe.close() - + data = [i.split(None, 4) for i in data] - + except Exception, err: data = str(err) - + return data @login_required(login_url='/login/') def getall(request): - return render_to_response('main.html', {'time_refresh': time_refresh, - 'time_refresh_long': time_refresh_long, - 'time_refresh_net': time_refresh_net, - 'version': version - }, context_instance=RequestContext(request)) + 'time_refresh_long': time_refresh_long, + 'time_refresh_net': time_refresh_net, + 'version': version}, context_instance=RequestContext(request)) diff --git a/pydash/settings.py b/pydash/settings.py index b8942ff..ea5095a 100644 --- a/pydash/settings.py +++ b/pydash/settings.py @@ -10,9 +10,10 @@ https://docs.djangoproject.com/en/1.6/ref/settings/ # Build paths inside the project like this: os.path.join(BASE_DIR, ...) import os + BASE_DIR = os.path.dirname(os.path.dirname(__file__)) -#SITE_ID = 1 +# SITE_ID = 1 # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/ @@ -93,7 +94,7 @@ TEMPLATE_LOADERS = ( 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', # 'django.template.loaders.eggs.Loader', - ) +) TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates'),) @@ -118,19 +119,19 @@ LOGGING = { 'filters': { 'require_debug_false': { '()': 'django.utils.log.RequireDebugFalse' - } + } }, 'handlers': { 'mail_admins': { 'level': 'ERROR', - 'filters': ['require_debug_false'], + 'filters': ['require_debug_false'], 'class': 'django.utils.log.AdminEmailHandler' } }, 'loggers': { - 'django.request': { + 'django.request': { 'handlers': ['mail_admins'], - 'level': 'ERROR', + 'level': 'ERROR', 'propagate': True, }, } diff --git a/pydash/urls.py b/pydash/urls.py index 9841377..7597e6d 100644 --- a/pydash/urls.py +++ b/pydash/urls.py @@ -1,34 +1,35 @@ from django.conf.urls import patterns, url from django.conf import settings -#from django.contrib import admin -#admin.autodiscover() +# from django.contrib import admin +# admin.autodiscover() urlpatterns = patterns('', - # Examples: - # url(r'^$', 'pydash.views.home', name='home'), - # url(r'^blog/', include('blog.urls')), + # Examples: + # url(r'^$', 'pydash.views.home', name='home'), + # url(r'^blog/', include('blog.urls')), - #url(r'^admin/', include(admin.site.urls)), - url(r'^$', 'main.views.index', name='index'), - url(r'^login/$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}, name='login'), - url(r'^logout/$', 'django.contrib.auth.views.logout', {'template_name': 'logout.html'}, name='logout'), - url(r'^main/$', 'main.views.getall', name='main'), - url(r'^info/uptime/$', 'usage.views.uptime', name='uptime'), - url(r'^info/memory/$', 'usage.views.memusage', name='memusage'), - url(r'^info/cpuusage/$', 'usage.views.cpuusage', name='cpuusage'), - url(r'^info/getdisk/$', 'usage.views.getdisk', name='getdisk'), - url(r'^info/getusers/$', 'usage.views.getusers', name='getusers'), - url(r'^info/getips/$', 'usage.views.getips', name='getips'), - url(r'^info/gettraffic/$', 'usage.views.gettraffic', name='gettraffic'), - url(r'^info/proc/$', 'usage.views.getproc', name='getproc'), - url(r'^info/getdiskio/$', 'usage.views.getdiskio', name='getdiskio'), - url(r'^info/loadaverage/$', 'usage.views.loadaverage', name='loadaverage'), - url(r'^info/platform/([\w\-\.]+)/$', 'usage.views.platform', name='platform'), - url(r'^info/getcpus/([\w\-\.]+)/$', 'usage.views.getcpus', name='getcpus'), - url(r'^info/getnetstat/$', 'usage.views.getnetstat', name='getnetstat') -) + # url(r'^admin/', include(admin.site.urls)), + url(r'^$', 'main.views.index', name='index'), + url(r'^login/$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}, + name='login'), + url(r'^logout/$', 'django.contrib.auth.views.logout', {'template_name': 'logout.html'}, + name='logout'), + url(r'^main/$', 'main.views.getall', name='main'), + url(r'^info/uptime/$', 'usage.views.uptime', name='uptime'), + url(r'^info/memory/$', 'usage.views.memusage', name='memusage'), + url(r'^info/cpuusage/$', 'usage.views.cpuusage', name='cpuusage'), + url(r'^info/getdisk/$', 'usage.views.getdisk', name='getdisk'), + url(r'^info/getusers/$', 'usage.views.getusers', name='getusers'), + url(r'^info/getips/$', 'usage.views.getips', name='getips'), + url(r'^info/gettraffic/$', 'usage.views.gettraffic', name='gettraffic'), + url(r'^info/proc/$', 'usage.views.getproc', name='getproc'), + url(r'^info/getdiskio/$', 'usage.views.getdiskio', name='getdiskio'), + url(r'^info/loadaverage/$', 'usage.views.loadaverage', name='loadaverage'), + url(r'^info/platform/([\w\-\.]+)/$', 'usage.views.platform', name='platform'), + url(r'^info/getcpus/([\w\-\.]+)/$', 'usage.views.getcpus', name='getcpus'), + url(r'^info/getnetstat/$', 'usage.views.getnetstat', name='getnetstat')) urlpatterns += patterns('', - (r'^static/(?P.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}), -) + (r'^static/(?P.*)$', 'django.views.static.serve', + {'document_root': settings.STATIC_ROOT})) diff --git a/usage/views.py b/usage/views.py index 3a58ac4..77e1554 100755 --- a/usage/views.py +++ b/usage/views.py @@ -1,34 +1,33 @@ -#The MIT License (MIT) +# The MIT License (MIT) # -#Copyright (c) 2014 Florian Neagu - michaelneagu@gmail.com - https://github.com/k3oni/ +# Copyright (c) 2014 Florian Neagu - michaelneagu@gmail.com - https://github.com/k3oni/ # -#Permission is hereby granted, free of charge, to any person obtaining a copy -#of this software and associated documentation files (the "Software"), to deal -#in the Software without restriction, including without limitation the rights -#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -#copies of the Software, and to permit persons to whom the Software is -#furnished to do so, subject to the following conditions: +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: # -#The above copyright notice and this permission notice shall be included in all -#copies or substantial portions of the Software. +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. # -#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -#SOFTWARE. +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. import json -from django.shortcuts import render_to_response -from django.http import HttpResponse, HttpResponseRedirect -from django.template import RequestContext -from django.contrib.auth.decorators import login_required + +from django.http import HttpResponse from main.views import * from pydash.settings import TIME_JS_REFRESH, TIME_JS_REFRESH_LONG, TIME_JS_REFRESH_NET + time_refresh = TIME_JS_REFRESH time_refresh_long = TIME_JS_REFRESH_LONG time_refresh_net = TIME_JS_REFRESH_NET @@ -40,16 +39,17 @@ def getnetstat(request): Return netstat output """ try: - net_stat = get_netstat() + net_stat = get_netstat() except Exception: - net_stat = None - + net_stat = None + data = json.dumps(net_stat) response = HttpResponse() response['Content-Type'] = "text/javascript" response.write(data) return response - + + @login_required(login_url='/login/') def platform(request, name): """ @@ -59,52 +59,56 @@ def platform(request, name): hostname = getplatform['hostname'] osname = getplatform['osname'] kernel = getplatform['kernel'] - + + data = {} + if name == 'hostname': - try: - data = hostname - except Exception: - data = None + try: + data = hostname + except Exception: + data = None if name == 'osname': - try: - data = osname - except Exception: - data = None - + try: + data = osname + except Exception: + data = None + if name == 'kernel': - try: - data = kernel - except Exception: - data = None + try: + data = kernel + except Exception: + data = None - data = json.dumps(data) + data = json.dumps(data) response = HttpResponse() response['Content-Type'] = "text/javascript" response.write(data) return response - + + @login_required(login_url='/login/') def getcpus(request, name): """ Return the CPU number and type/model """ - getcpus = get_cpus() - cputype = getcpus['type'] - cpucount = getcpus['cpus'] - + cpus = get_cpus() + cputype = cpus['type'] + cpucount = cpus['cpus'] + data = {} + if name == 'type': - try: - data = cputype - except Exception: - data = None - + try: + data = cputype + except Exception: + data = None + if name == 'count': - try: - data = cpucount - except Exception: - data = None - + try: + data = cpucount + except Exception: + data = None + data = json.dumps(data) response = HttpResponse() response['Content-Type'] = "text/javascript" @@ -118,10 +122,10 @@ def uptime(request): Return uptime """ try: - up_time = get_uptime() + up_time = get_uptime() except Exception: - up_time = None - + up_time = None + data = json.dumps(up_time) response = HttpResponse() response['Content-Type'] = "text/javascript" @@ -135,26 +139,27 @@ def getdisk(request): Return the disk usage """ try: - getdisk = get_disk() + diskusage = get_disk() except Exception: - getdisk = None - - data = json.dumps(getdisk) + diskusage = None + + data = json.dumps(diskusage) response = HttpResponse() response['Content-Type'] = "text/javascript" response.write(data) return response + @login_required(login_url='/login/') def getips(request): """ Return the IPs and interfaces """ try: - get_ips = get_ipaddress() + get_ips = get_ipaddress() except Exception: - get_ips = None - + get_ips = None + data = json.dumps(get_ips['itfip']) response = HttpResponse() response['Content-Type'] = "text/javascript" @@ -168,10 +173,10 @@ def getusers(request): Return online users """ try: - online_users = get_users() + online_users = get_users() except Exception: - online_users = None - + online_users = None + data = json.dumps(online_users) response = HttpResponse() response['Content-Type'] = "text/javascript" @@ -179,23 +184,24 @@ def getusers(request): return response -@login_required(login_url='/login/') +@login_required(login_url='/login/') def getproc(request): """ Return the running processes """ try: - processes = get_cpu_usage() - processes = processes['all'] + processes = get_cpu_usage() + processes = processes['all'] except Exception: - processes = None - + processes = None + data = json.dumps(processes) response = HttpResponse() response['Content-Type'] = "text/javascript" response.write(data) return response + @login_required(login_url='/login/') def cpuusage(request): """ @@ -203,20 +209,20 @@ def cpuusage(request): """ try: cpu_usage = get_cpu_usage() - + except Exception: cpu_usage = 0 - + cpu = [ - { - "value": cpu_usage['free'], - "color": "#0AD11B" - }, - { - "value": cpu_usage['used'], - "color": "#F7464A" - } - ] + { + "value": cpu_usage['free'], + "color": "#0AD11B" + }, + { + "value": cpu_usage['used'], + "color": "#F7464A" + } + ] data = json.dumps(cpu) response = HttpResponse() @@ -224,6 +230,7 @@ def cpuusage(request): response.write(data) return response + @login_required(login_url='/login/') def memusage(request): """ @@ -231,7 +238,6 @@ def memusage(request): """ datasets_free = [] datasets_used = [] - cookie_memory = {} try: mem_usage = get_mem() @@ -250,7 +256,7 @@ def memusage(request): datasets = json.loads(cookies) datasets_free = datasets[0] datasets_used = datasets[1] - + if len(datasets_free) > 10: while datasets_free: del datasets_free[0] @@ -298,7 +304,7 @@ def memusage(request): } ] } - + cookie_memory = [datasets_free, datasets_used] data = json.dumps(memory) response = HttpResponse() @@ -307,7 +313,8 @@ def memusage(request): response.write(data) return response -@login_required(login_url='/login/') + +@login_required(login_url='/login/') def loadaverage(request): """ Return Load Average numeric @@ -350,15 +357,15 @@ def loadaverage(request): 'labels': [""] * 10, 'datasets': [ { - "fillColor" : "rgba(151,187,205,0.5)", - "strokeColor" : "rgba(151,187,205,1)", - "pointColor" : "rgba(151,187,205,1)", + "fillColor": "rgba(151,187,205,0.5)", + "strokeColor": "rgba(151,187,205,1)", + "pointColor": "rgba(151,187,205,1)", "pointStrokeColor": "#fff", "data": datasets } ] } - + data = json.dumps(load) response = HttpResponse() response['Content-Type'] = "text/javascript" @@ -376,14 +383,12 @@ def gettraffic(request): datasets_in_i = [] datasets_out = [] datasets_out_o = [] - json_traffic = [] - cookie_traffic = {} label = "KBps" try: - intf = get_ipaddress() - intf = intf['interface'][0] - + intf = get_ipaddress() + intf = intf['interface'][0] + traffic = get_traffic(intf) except Exception: traffic = 0 @@ -402,8 +407,8 @@ def gettraffic(request): datasets = json.loads(cookies) datasets_in = datasets[0] datasets_out = datasets[1] - datasets_in_i = datasets[2] - datasets_out_o = datasets[3] + datasets_in_i = datasets[2] + datasets_out_o = datasets[3] if len(datasets_in) > 10: while datasets_in: @@ -437,14 +442,14 @@ def gettraffic(request): datasets_out_o.append(float(traffic['traffic_out'])) del datasets_out_o[0] - dataset_in = (float(((datasets_in_i[1] - datasets_in_i[0]) / 1024 ) / ( time_refresh_net / 1000 ))) - dataset_out = (float(((datasets_out_o[1] - datasets_out_o[0]) / 1024 ) / ( time_refresh_net / 1000 ))) - + dataset_in = (float(((datasets_in_i[1] - datasets_in_i[0]) / 1024) / (time_refresh_net / 1000))) + dataset_out = (float(((datasets_out_o[1] - datasets_out_o[0]) / 1024) / (time_refresh_net / 1000))) + if dataset_in > 1024 or dataset_out > 1024: - dataset_in = (float(dataset_in / 1024 )) - dataset_out = (float(dataset_out / 1024 )) - label = "MBps" - + dataset_in = (float(dataset_in / 1024)) + dataset_out = (float(dataset_out / 1024)) + label = "MBps" + if len(datasets_in) <= 9: datasets_in.append(dataset_in) if len(datasets_in) == 10: @@ -501,13 +506,10 @@ def getdiskio(request): datasets_in_i = [] datasets_out = [] datasets_out_o = [] - json_diskrw = [] - cookie_diskrw = {} try: - diskrw = get_disk_rw() - diskrw = diskrw[0] - + diskrw = get_disk_rw() + diskrw = diskrw[0] except Exception: diskrw = 0 @@ -525,8 +527,8 @@ def getdiskio(request): datasets = json.loads(cookies) datasets_in = datasets[0] datasets_out = datasets[1] - datasets_in_i = datasets[2] - datasets_out_o = datasets[3] + datasets_in_i = datasets[2] + datasets_out_o = datasets[3] if len(datasets_in) > 10: while datasets_in: @@ -560,9 +562,9 @@ def getdiskio(request): datasets_out_o.append(int(diskrw[2])) del datasets_out_o[0] - dataset_in = (int((datasets_in_i[1] - datasets_in_i[0]) / ( time_refresh_net / 1000 ))) - dataset_out = (int((datasets_out_o[1] - datasets_out_o[0]) / ( time_refresh_net / 1000 ))) - + dataset_in = (int((datasets_in_i[1] - datasets_in_i[0]) / (time_refresh_net / 1000))) + dataset_out = (int((datasets_out_o[1] - datasets_out_o[0]) / (time_refresh_net / 1000))) + if len(datasets_in) <= 9: datasets_in.append(dataset_in) if len(datasets_in) == 10: