From 56a1ef7ebe6ae60bd4722f979ec50684221d94fb Mon Sep 17 00:00:00 2001 From: Ted Trask Date: Sat, 6 Sep 2008 15:11:19 +0000 Subject: Added dynamic network activity page to health controller. git-svn-id: svn://svn.alpinelinux.org/acf/alpine-baselayout/trunk@1452 ab2d0c66-481e-0410-8bed-d214d4d58bed --- alpine-baselayout.roles | 2 +- health-controller.lua | 17 +++++++ health-model.lua | 25 ++++++++++ health-networkstats-ajax.lsp | 10 ++++ health-networkstats-html.lsp | 115 +++++++++++++++++++++++++++++++++++++++++++ health.menu | 1 + syslog-model.lua | 3 +- 7 files changed, 170 insertions(+), 3 deletions(-) create mode 100644 health-networkstats-ajax.lsp create mode 100644 health-networkstats-html.lsp diff --git a/alpine-baselayout.roles b/alpine-baselayout.roles index 993f050..adc0e41 100644 --- a/alpine-baselayout.roles +++ b/alpine-baselayout.roles @@ -1,5 +1,5 @@ CREATE=interfaces:create,interfaces:editintfile -READ=health:storage,health:proc,health:network,health:modules,interfaces:status,interfaces:read,logfiles:status,logfiles:view,logfiles:download,logfiles:tail,syslog:status,syslog:loginfo +READ=health:storage,health:proc,health:network,health:modules,health:networkstats,interfaces:status,interfaces:read,logfiles:status,logfiles:view,logfiles:download,logfiles:tail,syslog:status,syslog:loginfo UPDATE=interfaces:update,interfaces:ifup,interfaces:ifdown,skins:update,skins:read,syslog:startstop,syslog:config,syslog:expert DELETE=interfaces:delete,logfiles:delete ALL=health:system,hostname:read diff --git a/health-controller.lua b/health-controller.lua index 6ebc00e..ef08301 100644 --- a/health-controller.lua +++ b/health-controller.lua @@ -23,3 +23,20 @@ end proc = function (self ) return self.model:get_proc() end + +networkstats = function(self) + local retval = self.model.get_networkstats() + if self.clientdata.viewtype then + self.conf.viewtype = "ajax" + else + local intf = self:new("alpine-baselayout/interfaces") + local interfaces = intf.model.get_addresses() + intf:destroy() + for i,intf in ipairs(interfaces.value) do + if retval.value[intf.interface] then + retval.value[intf.interface].ipaddr = intf.ipaddr + end + end + end + return retval +end diff --git a/health-model.lua b/health-model.lua index 0646f5e..854e2cc 100644 --- a/health-model.lua +++ b/health-model.lua @@ -94,3 +94,28 @@ get_proc = function (self) proc.memory = cfe({ value=fs.read_file("/proc/meminfo"), label="Memory" }) return cfe({ type="group", value=proc }) end + +get_networkstats = function () + local stats = cfe({ type="structure", value={}, label="Network Stats", timestamp=os.time() }) + local result = querycmd("/bin/ip -s link show") + -- parse the result + local i=0 + local intf = "" + for line in string.gmatch(result, "[^\n]*\n?") do + if i==0 then + intf = string.match(line, "^%x+:%s+(%w+):") + if not intf or intf == "" then + break + end + stats.value[intf] = {} + elseif i==3 then + local bytes, packets = string.match(line, "^%s+(%d+)%s+(%d+)") + stats.value[intf].RX = {bytes=bytes, packets=packets} + elseif i==5 then + local bytes, packets = string.match(line, "^%s+(%d+)%s+(%d+)") + stats.value[intf].TX = {bytes=bytes, packets=packets} + end + i=(i+1)%6 + end + return stats +end diff --git a/health-networkstats-ajax.lsp b/health-networkstats-ajax.lsp new file mode 100644 index 0000000..e8d176e --- /dev/null +++ b/health-networkstats-ajax.lsp @@ -0,0 +1,10 @@ +<% local view, viewlibrary, page_info = ... %> +<% + require("json") +%> +Status: 200 OK +Content-Type: "application/json" +<% io.write("\n") %> +<% + print(json.encode(view)) +%> diff --git a/health-networkstats-html.lsp b/health-networkstats-html.lsp new file mode 100644 index 0000000..e994afb --- /dev/null +++ b/health-networkstats-html.lsp @@ -0,0 +1,115 @@ +<% local view, viewlibrary, page_info = ... %> +<% require("viewfunctions") %> +<% require("json") %> + + + + + + + +

Network Statistics

+
+
+
Display Options
+<% for intf,val in pairs(view.value) do + local ipaddr = "" + if val.ipaddr then ipaddr = " ("..val.ipaddr..")" end %> +
checked="checked"><%= intf.." RX"..ipaddr %>
+
checked="checked"><%= intf.." TX"..ipaddr %>
+<% end %> +
Start / Stop
+
+ + +
+
diff --git a/health.menu b/health.menu index ab57366..a595728 100644 --- a/health.menu +++ b/health.menu @@ -2,5 +2,6 @@ System 01General_health System system System 01General_health Storage storage System 01General_health Network network +System 01General_health Network_Activity networkstats System 01General_health Modules modules System 01General_health Proc proc diff --git a/syslog-model.lua b/syslog-model.lua index 273cc27..25969fe 100644 --- a/syslog-model.lua +++ b/syslog-model.lua @@ -202,8 +202,7 @@ end function update_filedetails (filedetails) -- Validation before writing - filedetails.value.filecontent.value = format.dostounix(filedetails.value.filecontent.value) - filedetails.value.filecontent.value = filedetails.value.filecontent.value:gsub("\n+$", "") + filedetails.value.filecontent.value = string.gsub(format.dostounix(filedetails.value.filecontent.value), "\n+$", "") local configcontent = getopts.getoptsfromfile(filedetails.value.filecontent.value, "", "SYSLOGD_OPTS", true) or {} local config = makeconfig(configcontent) local success, errtxt -- cgit v1.2.3