From 19b01914edfeb7acc3e276ed2e0b045da8caedfe Mon Sep 17 00:00:00 2001 From: Florian N Date: Sun, 16 Feb 2014 19:31:56 -0500 Subject: [PATCH 01/10] readme --- README.md | 2 +- usage/views.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 171bbe8..1c1386a 100644 --- a/README.md +++ b/README.md @@ -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/usage/views.py b/usage/views.py index 92a528a..bdbbcb7 100755 --- a/usage/views.py +++ b/usage/views.py @@ -49,6 +49,7 @@ def uptime(request): response.write(data) return response + @login_required(login_url='/login/') def getdisk(request): """ From b55048fe16df333b5a38d980a70ac3cb897ec8a9 Mon Sep 17 00:00:00 2001 From: Mehmet INCE Date: Mon, 17 Feb 2014 14:04:01 +0200 Subject: [PATCH 02/10] Netstat module added. --- main/views.py | 21 ++++++++-- pydash/urls.py | 3 +- templates/main.html | 93 ++++++++++++++++++++++++++++++++------------- usage/views.py | 12 ++++++ 4 files changed, 99 insertions(+), 30 deletions(-) diff --git a/main/views.py b/main/views.py index ad1d9ef..5878bd8 100755 --- a/main/views.py +++ b/main/views.py @@ -282,17 +282,32 @@ 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(): + try: + pipe = os.popen("netstat -tlnp|grep LISTEN|awk '{print $4, $5, $7}'") + data = pipe.read().strip().split('\n') + pipe.close() + data = [i.split(None, 3) for i in data] + except Exception, err: + data = str(err) + if data[0] == []: + data = [['No', 'data', 'available']] + #for i in data: + # temp = i.split() + # print temp[3] + temp[4] + return data @login_required(login_url='/login/') def getall(request): 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..f9bd6cd 100644 --- a/templates/main.html +++ b/templates/main.html @@ -42,6 +42,7 @@
  • RAM
  • Load
  • Users
  • +
  • Netstat
  • Network
  • Processes
  • @@ -250,32 +251,58 @@
    -
    -
    -
    -

    Online

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

    Online

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

    Netstat

    +
    +
    +
    + +
    + + + + + + + + + + +
    Local AddressForeign AddressPID/Program Name
    +
    + +
    + +
    +
    @@ -357,6 +384,7 @@ function get_os_data(url, element) { var dashboard = {}; + dashboard.getUptime = function() { get_os_data('/info/uptime/', "#get-uptime"); } @@ -380,6 +408,17 @@ 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]+''; + }) + tr+=''; + console.log(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 bdbbcb7..0a8a366 100755 --- a/usage/views.py +++ b/usage/views.py @@ -33,6 +33,18 @@ 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 + """ + net_stat = get_netstat() + 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): """ From 9b136441bf264611b2b797e72a5a63ba0245775f Mon Sep 17 00:00:00 2001 From: Florian N Date: Mon, 17 Feb 2014 10:27:11 -0500 Subject: [PATCH 03/10] v 1.3 --- main/views.py | 18 ++++++++++++------ pydash/settings.py | 2 +- templates/main.html | 1 - usage/views.py | 6 +++++- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/main/views.py b/main/views.py index 5878bd8..b59f2d6 100755 --- a/main/views.py +++ b/main/views.py @@ -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() @@ -295,19 +295,25 @@ def get_load(): return data def get_netstat(): + """ + Get ports and applications + """ try: - pipe = os.popen("netstat -tlnp|grep LISTEN|awk '{print $4, $5, $7}'") + pipe = os.popen("netstat -tlnp |" + "grep 'LISTEN' |" + "awk '{print $4, $5, $7}'") data = pipe.read().strip().split('\n') pipe.close() + data = [i.split(None, 3) for i in data] + except Exception, err: data = str(err) + if data[0] == []: - data = [['No', 'data', 'available']] - #for i in data: - # temp = i.split() - # print temp[3] + temp[4] + 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/templates/main.html b/templates/main.html index f9bd6cd..edbbdc0 100644 --- a/templates/main.html +++ b/templates/main.html @@ -415,7 +415,6 @@ dashboard.getNetstat = function() { tr+=''+item[0]+''+item[1]+''+item[2]+''; }) tr+=''; - console.log(tr); $("#get-netstat").html( tr ); }); } diff --git a/usage/views.py b/usage/views.py index 0a8a366..79b23d5 100755 --- a/usage/views.py +++ b/usage/views.py @@ -38,7 +38,11 @@ def getnetstat(request): """ Return netstat output """ - net_stat = get_netstat() + try: + net_stat = get_netstat() + except Exception: + net_stat = None + data = json.dumps(net_stat) response = HttpResponse() response['Content-Type'] = "text/javascript" From baf0c7984ca7cbd63e2cfefb24b8e81e3ff781df Mon Sep 17 00:00:00 2001 From: Florian N Date: Mon, 17 Feb 2014 10:27:34 -0500 Subject: [PATCH 04/10] v 1.3 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1c1386a..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. From 96f86840fec4b353c57bc4879198c9b685052773 Mon Sep 17 00:00:00 2001 From: Florian N Date: Mon, 17 Feb 2014 13:41:11 -0500 Subject: [PATCH 05/10] fix #10 --- main/views.py | 14 +++++++------- usage/views.py | 2 ++ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/main/views.py b/main/views.py index b59f2d6..096f54a 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 '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} diff --git a/usage/views.py b/usage/views.py index 79b23d5..dc53442 100755 --- a/usage/views.py +++ b/usage/views.py @@ -33,6 +33,7 @@ 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): """ @@ -49,6 +50,7 @@ def getnetstat(request): response.write(data) return response + @login_required(login_url='/login/') def uptime(request): """ From 9c090fd80cca8c172fb81c049e1d6079db6eeca5 Mon Sep 17 00:00:00 2001 From: Florian N Date: Mon, 17 Feb 2014 13:46:54 -0500 Subject: [PATCH 06/10] fix #10 --- main/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/views.py b/main/views.py index 096f54a..43e5d33 100755 --- a/main/views.py +++ b/main/views.py @@ -71,7 +71,7 @@ def get_ipaddress(): Get the IP Address """ try: - pipe = os.popen(" ip addr | grep -A3 'UP' | awk '{printf \"%s,\",$2}'|awk -F,, '{print $1, $2, $3}'") + 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() From 9a3ec9383e1eb1ec3b493a55d54c5a683c353e6f Mon Sep 17 00:00:00 2001 From: Florian N Date: Mon, 17 Feb 2014 19:03:45 -0500 Subject: [PATCH 07/10] netstat to ss --- main/.#views.py | 1 + main/views.py | 3 ++- templates/main.html | 4 ++-- 3 files changed, 5 insertions(+), 3 deletions(-) create mode 120000 main/.#views.py diff --git a/main/.#views.py b/main/.#views.py new file mode 120000 index 0000000..d645fd0 --- /dev/null +++ b/main/.#views.py @@ -0,0 +1 @@ +root@pydash.hostechs.com.30609 \ No newline at end of file diff --git a/main/views.py b/main/views.py index 43e5d33..6bc5dd3 100755 --- a/main/views.py +++ b/main/views.py @@ -299,11 +299,12 @@ def get_netstat(): Get ports and applications """ try: - pipe = os.popen("netstat -tlnp |" + "grep 'LISTEN' |" + "awk '{print $4, $5, $7}'") + pipe = os.popen("ss -tlnp | awk '{print $4, $5, $6}'") data = pipe.read().strip().split('\n') pipe.close() data = [i.split(None, 3) for i in data] + del data[0] except Exception, err: data = str(err) diff --git a/templates/main.html b/templates/main.html index edbbdc0..39264df 100644 --- a/templates/main.html +++ b/templates/main.html @@ -289,8 +289,8 @@ - - + + From 5915bd06eb57def01ab88626d0bdf99403600252 Mon Sep 17 00:00:00 2001 From: Florian N Date: Mon, 17 Feb 2014 19:04:36 -0500 Subject: [PATCH 08/10] netstat to ss --- main/.#views.py | 1 - 1 file changed, 1 deletion(-) delete mode 120000 main/.#views.py diff --git a/main/.#views.py b/main/.#views.py deleted file mode 120000 index d645fd0..0000000 --- a/main/.#views.py +++ /dev/null @@ -1 +0,0 @@ -root@pydash.hostechs.com.30609 \ No newline at end of file From ffde36ff1dc17d62e19867762bd0109d7ba05115 Mon Sep 17 00:00:00 2001 From: Florian N Date: Mon, 17 Feb 2014 22:49:39 -0500 Subject: [PATCH 09/10] fix ss output --- main/views.py | 3 +-- templates/main.html | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/main/views.py b/main/views.py index 6bc5dd3..ea74bdd 100755 --- a/main/views.py +++ b/main/views.py @@ -299,12 +299,11 @@ def get_netstat(): Get ports and applications """ try: - pipe = os.popen("ss -tlnp | awk '{print $4, $5, $6}'") + pipe = os.popen("ss -tnp | grep ESTAB| awk '{print $4, $5}'") data = pipe.read().strip().split('\n') pipe.close() data = [i.split(None, 3) for i in data] - del data[0] except Exception, err: data = str(err) diff --git a/templates/main.html b/templates/main.html index 39264df..d69046a 100644 --- a/templates/main.html +++ b/templates/main.html @@ -291,7 +291,6 @@ - @@ -412,7 +411,7 @@ dashboard.getNetstat = function() { $.getJSON('/info/getnetstat/', function(data) { var tr=''; $.each( data, function( index, item){ - tr+=''; + tr+=''; }) tr+=''; $("#get-netstat").html( tr ); From 3ef9e50e32995a248cab10cd991d4826eb510b49 Mon Sep 17 00:00:00 2001 From: Florian N Date: Tue, 18 Feb 2014 18:50:07 -0500 Subject: [PATCH 10/10] change ss data --- main/views.py | 4 ++-- templates/main.html | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/main/views.py b/main/views.py index ea74bdd..ec3b24f 100755 --- a/main/views.py +++ b/main/views.py @@ -299,11 +299,11 @@ def get_netstat(): Get ports and applications """ try: - pipe = os.popen("ss -tnp | grep ESTAB| awk '{print $4, $5}'") + 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, 3) for i in data] + data = [i.split(None, 4) for i in data] except Exception, err: data = str(err) diff --git a/templates/main.html b/templates/main.html index d69046a..2d54372 100644 --- a/templates/main.html +++ b/templates/main.html @@ -289,7 +289,9 @@
    Local AddressForeign AddressLocalForeign PID/Program Name
    Local ForeignPID/Program Name
    '+item[0]+''+item[1]+''+item[2]+'
    '+item[0]+''+item[1]+'
    - + + + @@ -411,7 +413,7 @@ dashboard.getNetstat = function() { $.getJSON('/info/getnetstat/', function(data) { var tr=''; $.each( data, function( index, item){ - tr+=''; + tr+=''; }) tr+=''; $("#get-netstat").html( tr );
    LocalCountLocal IPLocal Port Foreign
    '+item[0]+''+item[1]+'
    '+item[0]+''+item[1]+''+item[2]+''+item[3]+'