summaryrefslogtreecommitdiffstats
path: root/zebra-model.lua
blob: cba6eb96817723cf1016bffe46da22f74e9ca7f9 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
local mymodule = {}

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

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

local configfile = "/etc/quagga/zebra.conf"
local processname = "zebra"
local packagename = "quagga"
local portnumber = 2601

-- ################################################################################
-- 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

-- Use socket.protect to create a function that will not throw exceptions and cleans itself up
local send_to_event_socket = socket.protect(function(cmd)
	-- connect to freeswitch
	local conn = socket.try(socket.connect("127.0.0.1", portnumber))
	conn:settimeout(1) -- timeout of 1 second for response to each command
	-- create a try function that closes 'conn' on error
	local try = socket.newtry(function() conn:close() end)
	-- do everything reassured conn will be closed
	local out = {}
	local o
	repeat
		o,e = conn:receive()
		out[#out+1] = o
	until o == nil
	for i,c in ipairs(cmd) do
		try(conn:send(c.."\n"))
		repeat
			o,e = conn:receive()
			out[#out+1] = o
		until o == nil
	end
	conn:close()
	return table.concat(out, "\n") or ""
end)

local function telnetshowip()
	local output = {}
	local configfile = parseconfigfile() or {}
	local cmd = {configfile.password, "show ip route", "quit"}
	local result = send_to_event_socket(cmd)
	local validoutput
	if not result or 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, "^Password:")) then
			startout = #output+1
		elseif (string.find(line, "^Codes:")) then
			startout = #output
		elseif (string.find(line, "^quit")) then
			stopout = #output-1
		end
	end
	return table.concat(output,"\n",startout,stopout)
end

-- ################################################################################
-- PUBLIC FUNCTIONS

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

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

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

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

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

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

return mymodule