diff --git a/README.md b/README.md index 171bbe8..f5e5af0 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -pyDash - v1.2 +pyDash - v1.3 ====== A small web monitoring dashboard for your linux pc/server writen in Python and Django + Chart.js. @@ -9,7 +9,7 @@ The dashboard is built using only Python libraries available in the main Python Current dependencies: - == Django 1.6.1 - - >= Python 2.6 + - == Python 2.x __[View Demo](http://pydash.hostechs.com/)__ diff --git a/main/views.py b/main/views.py index ad1d9ef..ec3b24f 100755 --- a/main/views.py +++ b/main/views.py @@ -71,16 +71,16 @@ def get_ipaddress(): Get the IP Address """ try: - pipe = os.popen(" ip addr | grep -A3 'state UP' | awk '{printf \"%s,\",$2}'|awk -F, '{print $1, $2, $3}'") - data = pipe.read().strip().split('\n') + pipe = os.popen(" ip addr | grep -A3 'LOWER_UP' | awk '{printf \"%s,\",$2}'|awk -F,, '{print $1, $2, $3}'") + data = pipe.read().strip().split(' ') pipe.close() - data = [i.split(None, 3) for i in data] + data = [n for n in data if not n.startswith(('lo', '127'))] + data = [i.split(',', 3) for i in data] + + itf = [] for e in data: - if len(e) > 3: - itf = dict(zip([iter(e[0].strip(':'))])) - else: - itf = [e[0].strip(':')] + itf.append(e[0].strip(':')) ips = {'interface': itf, 'itfip': data} @@ -255,7 +255,7 @@ def get_cpu_usage(): """ Get the CPU usage and running processes """ - try: + try: pipe = os.popen("ps aux --sort -%cpu,-rss") data = pipe.read().strip().split('\n') pipe.close() @@ -282,17 +282,38 @@ def get_cpu_usage(): return data + def get_load(): """ Get load average """ try: - data = os.getloadavg()[0] + data = os.getloadavg()[0] except Exception, err: - data = str(err) + data = str(err) 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}' | 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) + if data[0] == []: + data = [['No', 'data', 'available']] + + return data + + @login_required(login_url='/login/') def getall(request): diff --git a/pydash/settings.py b/pydash/settings.py index 4e56a0a..0238db5 100644 --- a/pydash/settings.py +++ b/pydash/settings.py @@ -36,7 +36,7 @@ TIME_JS_REFRESH = 30000 TIME_JS_REFRESH_LONG = 120000 TIME_JS_REFRESH_NET = 2000 -VERSION = 1.2 +VERSION = 1.3 ALLOWED_HOSTS = ['*'] diff --git a/pydash/urls.py b/pydash/urls.py index 4ef5663..60eff05 100644 --- a/pydash/urls.py +++ b/pydash/urls.py @@ -23,7 +23,8 @@ urlpatterns = patterns('', 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/loadaverage/$', 'usage.views.loadaverage', name='loadaverage'), + url(r'^info/getnetstat/$', 'usage.views.getnetstat', name='getnetstat') ) urlpatterns += patterns('', diff --git a/templates/main.html b/templates/main.html index 29935e9..2d54372 100644 --- a/templates/main.html +++ b/templates/main.html @@ -42,6 +42,7 @@
  • RAM
  • Load
  • Users
  • +
  • Netstat
  • Network
  • Processes
  • @@ -250,32 +251,59 @@
    -
    -
    -
    -

    Online

    -
    -
    -
    - -
    - - - - - - - - - - -
    UserTTYLogged in from
    -
    - -
    - -
    - +
    +
    +
    +

    Online

    +
    +
    +
    + +
    + + + + + + + + + + +
    UserTTYLogged in from
    +
    + +
    + +
    + +
    +
    +
    +

    Netstat

    +
    +
    +
    + +
    + + + + + + + + + + + +
    CountLocal IPLocal PortForeign
    +
    + +
    + +
    +
    @@ -357,6 +385,7 @@ function get_os_data(url, element) { var dashboard = {}; + dashboard.getUptime = function() { get_os_data('/info/uptime/', "#get-uptime"); } @@ -380,6 +409,16 @@ dashboard.getUsers = function() { $("#get-users").html( tr ); }); } +dashboard.getNetstat = function() { + $.getJSON('/info/getnetstat/', function(data) { + var tr=''; + $.each( data, function( index, item){ + tr+=''+item[0]+''+item[1]+''+item[2]+''+item[3]+''; + }) + tr+=''; + $("#get-netstat").html( tr ); + }); + } dashboard.getProc = function() { $.getJSON('/info/proc/', function(data) { var tr=''; @@ -476,6 +515,7 @@ $(function() { window.setInterval('dashboard.getUptime()', {{ time_refresh_long }}); window.setInterval('dashboard.getDisk()', {{ time_refresh_long }}); window.setInterval('dashboard.getUsers()', {{ time_refresh_long }}); + window.setInterval('dashboard.getNetstat()', {{ time_refresh_long }}); }); $(function pageLoad() { @@ -488,6 +528,7 @@ $(function pageLoad() { dashboard.getUptime(); dashboard.getDisk(); dashboard.getUsers(); + dashboard.getNetstat(); dashboard.getIps(); }); diff --git a/usage/views.py b/usage/views.py index 92a528a..dc53442 100755 --- a/usage/views.py +++ b/usage/views.py @@ -33,6 +33,24 @@ time_refresh = TIME_JS_REFRESH time_refresh_long = TIME_JS_REFRESH_LONG time_refresh_net = TIME_JS_REFRESH_NET + +@login_required(login_url='/login/') +def getnetstat(request): + """ + Return netstat output + """ + try: + net_stat = get_netstat() + except Exception: + 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 uptime(request): """ @@ -49,6 +67,7 @@ def uptime(request): response.write(data) return response + @login_required(login_url='/login/') def getdisk(request): """