summaryrefslogtreecommitdiffstats
path: root/dnscache-model.lua
diff options
context:
space:
mode:
Diffstat (limited to 'dnscache-model.lua')
-rw-r--r--dnscache-model.lua203
1 files changed, 203 insertions, 0 deletions
diff --git a/dnscache-model.lua b/dnscache-model.lua
new file mode 100644
index 0000000..d67f80d
--- /dev/null
+++ b/dnscache-model.lua
@@ -0,0 +1,203 @@
+module(..., package.seeall)
+
+-- Load libraries
+require("fs")
+require("procps")
+require("getopts")
+require("format")
+require("posix")
+require("daemoncontrol")
+require("validator")
+require("socket")
+require("processinfo")
+
+-- Set variables
+local configfile = "/etc/quagga/bgpd.conf"
+local processname = "bgpd"
+local packagename = "quagga"
+local baseurl = "/etc/quagga/"
+local descr = {
+}
+
+-- ################################################################################
+-- LOCAL FUNCTIONS
+
+local function get_routes()
+ local cmd_output_result, cmd_output_error
+ local cmd = "/sbin/route -n 2>/dev/null"
+ local f = io.popen( cmd )
+ local cmd_output_result = f:read("*a")
+ return cmd_output_result,cmd_output_error
+end
+
+local function get_iproute()
+ local cmd_output_result, cmd_output_error
+ local cmd = "/bin/ip route 2>/dev/null"
+ local f = io.popen( cmd )
+ local cmd_output_result = f:read("*a")
+ return cmd_output_result,cmd_output_error
+end
+
+local function get_bgpinfo()
+ local cmd_output_result, cmd_output_error
+ local cmd = "/usr/sbin/bgpd --version 2>/dev/null"
+ local f = io.popen( cmd )
+ local cmd_output_result = f:read("*a")
+ -- bgpdport=179
+ return cmd_output_result,cmd_output_error
+end
+
+local function process_status_text(procname)
+ local t = procps.pidof(procname)
+ if (t) and (#t > 0) then
+ return "Enabled"
+ else
+ return "Disabled"
+ end
+end
+
+-- ################################################################################
+-- PUBLIC FUNCTIONS
+
+function startstop_service ( self, action )
+ local cmd = action.value
+ local cmdresult,cmdmessage,cmderror,cmdaction = daemoncontrol.daemoncontrol(processname, cmd)
+ action.descr=cmdmessage
+ action.errtxt=cmderror
+ return cmdresult,action
+end
+
+function getstatus()
+ local status = {}
+
+ local value, errtxt = processinfo.package_version(packagename)
+ status.version = cfe({ name = "version",
+ label="Program version",
+ value=value,
+ errtxt=errtxt,
+ })
+
+ status.status = cfe({ name="status",
+ label="Program status",
+ value=process_status_text(processname),
+ })
+
+ local autostart_sequense, autostart_errtxt = processinfo.process_botsequence(processname)
+ status.autostart = cfe({ name="autostart",
+ label="Autostart sequence",
+ value=autostart_sequense,
+ errtxt=autostart_errtxt,
+ })
+
+ status.iproute = cfe({ name="iproute",
+ label="ip route",
+ value=get_iproute(),
+ })
+
+ status.routes = cfe({ name="routes",
+ label="route -n",
+ value=get_routes(),
+ })
+
+ status.bgpstats = cfe({ name="bgpstats",
+ label="BGP version",
+ value=get_bgpinfo(),
+ })
+
+ return status
+end
+
+function get_logfile ()
+ local file = {}
+ local cmdtxt = "cat /var/log/messages | grep " .. processname
+ local cmd, error = io.popen(cmdtxt ,r)
+ local cmdoutput = cmd:read("*a")
+ cmd:close()
+
+ file["filename"] = cfe({
+ name="filename",
+ label="File name",
+ value=cmdtxt,
+ })
+
+ file["filecontent"] = cfe({
+ type="longtext",
+ name="filecontent",
+ label="File content",
+ value=cmdoutput,
+ })
+
+ return file
+end
+
+function getconfig()
+ local config = {}
+ return config
+end
+
+function get_filedetails(self,num)
+ local path
+ if (num == "2") then
+ path = configfile2
+ else
+ path = configfile
+ end
+ local file = {}
+ local filedetails = {}
+ local config = {}
+ local filenameerrtxt
+ if (path) and (fs.is_file(path)) then
+ filedetails = fs.stat(path)
+ config = getconfig(path)
+ else
+ config = {}
+ config.filename = {}
+ config["filename"]["errtxt"]="Config file '".. path .. "' is missing!"
+ end
+
+ file["filename" .. (num or "")] = cfe({
+ name="filename" .. (num or ""),
+ label="File name",
+ value=path,
+ errtxt=filenameerrtxt
+ })
+ file["filesize" .. (num or "")] = cfe({
+ name="filesize" .. (num or ""),
+ label="File size",
+ value=filedetails.size or 0,
+ })
+ file["mtime" .. (num or "")] = cfe({
+ name="mtime" .. (num or ""),
+ label="File date",
+ value=filedetails.mtime or "---",
+ })
+ file["filecontent" .. (num or "")] = cfe({
+ type="longtext",
+ name="filecontent" .. (num or ""),
+ label="File content",
+ value=fs.read_file(path),
+ })
+
+ -- Sum all errors into one cfe
+ local sumerrors = ""
+ for k,v in pairs(config) do
+ if (config[k]) and (config[k]["errtxt"]) and (config[k]["errtxt"] ~= "") then
+ sumerrors = sumerrors .. config[k]["errtxt"] .. "\n"
+ end
+ end
+ if (sumerrors ~= "") then
+ file["sumerrors" .. (num or "")] = cfe ({
+ name="sumerrors" .. (num or ""),
+ label = "Configuration errors",
+ errtxt = string.match(sumerrors, "(.-)\n$"),
+ })
+ end
+
+ return file
+end
+function update_filecontent (self, modifications)
+ local path = configfile
+ local file_result,err = fs.write_file(path, format.dostounix(modifications))
+ return file_result
+end
+