summaryrefslogtreecommitdiffstats
path: root/ospf-model.lua
blob: 383050b9bae4b6d3cb175e4216ab2e33e45c40e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
module(..., package.seeall)

-- Load libraries
modelfunctions = require("modelfunctions")
format = require("acf.format")

-- Set variables
local path="PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin "

local configfile = "/etc/quagga/ospfd.conf"
local processname = "ospfd"
local packagename = "quagga"

-- ################################################################################
-- LOCAL FUNCTIONS
local function parseconfigfile()
	local conf = {}
	local f = io.open(configfile, "r")
	local line, key, _, k, v
	
	if not f then
		return nil
	end

	for line in f:lines() do
		line = string.gsub(line, "%s*#.*", "")
		local k,v = string.match(line, "^%s*(%S*)%s+(.*)")
		if k then
			conf[k] = v
		end
	end
	f:close()
	return conf
end

local function telnetshowip()
	local output = {}
	local configfile = parseconfigfile() or {}
	local cmd = (configfile.password or "") .. "\nshow ip ospf route\nquit\n"
	local result, errtxt = modelfunctions.run_executable({"nc", "localhost", "ospfd"}, false, cmd)
	if result == "" then
		result = "Failed to find routes"
	end
	local startout, stopout
	for line in string.gmatch(result, "([^\n]*)\n?") do
		table.insert(output,line)
		if (string.find(line, "show ip ospf route")) then
			startout = #output+1
		elseif (string.find(line, "> quit")) then
			stopout = #output-1
		end
	end
	return table.concat(output,"\n",startout,stopout)
end
-- ################################################################################
-- PUBLIC FUNCTIONS

function get_startstop(self, clientdata)       
        return modelfunctions.get_startstop(processname)
end

function startstop_service(self, startstop, action)        
        return modelfunctions.startstop_service(startstop, action)
end

function getstatus()
	return modelfunctions.getstatus(processname, packagename, "OSPF Status")
end

function getconfigfile()
	return modelfunctions.getfiledetails(configfile)
end

function setconfigfile(self, filedetails)
	return modelfunctions.setfiledetails(self, filedetails, {configfile})
end

function getdetails()
	local status = {}
	status.showip = cfe({ label="OSPF routes" })
	status.showip.value = telnetshowip()
	return cfe({ type="group", value=status, label="OSPF Details" })
end