From d2a7cb6b8761a7dc83e208ab7094f52e03ebed89 Mon Sep 17 00:00:00 2001 From: Mika Havela Date: Fri, 3 Jul 2009 09:05:10 +0000 Subject: Functionallity to create new DB. Still missing validation on userinput. --- rrdtool-controller.lua | 5 +++-- rrdtool-model.lua | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/rrdtool-controller.lua b/rrdtool-controller.lua index 7aef79c..9d00065 100644 --- a/rrdtool-controller.lua +++ b/rrdtool-controller.lua @@ -29,8 +29,9 @@ function listrrd(self) end function createrrd(self) - return controllerfunctions.handle_form(self, self.model.createnewrrd, self.model.write_file, self.clientdata, "Save", "Edit Interfaces file", "File saved") - + return controllerfunctions.handle_form(self, self.model.createnewrrd, function(value) + return self.model.savenewrrd(self, value, sessiondata.userinfo.userid) + end, self.clientdata, "Save", "Create new RRD", "File saved") end function delete(self) diff --git a/rrdtool-model.lua b/rrdtool-model.lua index dfa4475..2163ec0 100644 --- a/rrdtool-model.lua +++ b/rrdtool-model.lua @@ -104,7 +104,7 @@ end function createnewrrd() local newdb = {} - newdb.name = cfe({label="Name",value="mydb.rrd",descr="The name of the RRD you want to create. RRD files should end with the extension .rrd. However, RRDtool will accept any filename."}) + newdb.filename = cfe({label="Name",value="mydb.rrd",descr="The name of the RRD you want to create. RRD files should end with the extension .rrd. However, RRDtool will accept any filename."}) newdb.start = cfe({label="Start",value="",descr="Specifies the time in seconds since 1970-01-01 UTC when the first value should be added to the RRD. RRDtool will not accept any data timed before or at the time specified.\ (Your system saids that your current time is '" .. tostring(os.time(os.date("*t"))) .. "' in the format you should specify above. The startvalue should be smaller than your current time.)\ (Default: now - 10s)"}) @@ -139,3 +139,38 @@ function rrd_info(self, path, userid) end return cfe({ value=success, label="rrdtool info ".. tostring(path) , errtxt=errtxt }) end + +function savenewrrd(self, configfile, userid) + path=tostring(databases).."/"..configfile.value.filename.value + configfile.errtxt = "Failed to create file" + local path = configfile.value.filename.value + if not string.find(path, "/") then + path = databases .. "/" .. path + end + if (posix.stat(path)) then + configfile.value.filename.errtxt = "File already exists" + else + local start,step + if (#configfile.value.start.value > 0) then + start = "--start " .. tostring(configfile.value.start.value) .. " " + else + start = "" + end + if (#configfile.value.step.value > 0) then + step = "--step " .. tostring(configfile.value.step.value) .. " " + else + step = "" + end + local f = io.popen( "/usr/bin/rrdtool create ".. + tostring(path) .. " " .. + tostring(start) .. + tostring(step) .. + tostring(configfile.value.ds.value) .. " " .. + tostring(configfile.value.rra.value) .. " 2>&1") + success = f:read("*a") or "" + f:close() + configfile.errtxt = tostring(success) + end + + return configfile +end -- cgit v1.2.3