summaryrefslogtreecommitdiffstats
path: root/opennhrp-model.lua
blob: f8092835534f9a69e4d5a5ff40c55c00ae8819f3 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
module(..., package.seeall)

require("fs")
require("procps")
require("getopts")
require("format")
require("daemoncontrol")
require("validator")

local configfile = "/etc/opennhrp/opennhrp.conf"
local processname = "opennhrp"
local baseurl = "/etc/opennhrp/"

local type_status = {
	['incomplete']="The protocol address is being resolved",
	['negative']="This protocol address is not available",
	['cached']="Protocol address was resolved successfully",
	['route']="This is a dynamic shortcut",
	['dynamic']="This entry is from a node that connected to us",
	['local']="Local interface address",
	['static']="Static mapping from configuration file (e.g. address of core)",
	}

local function get_version()
	local cmd_output_result, cmd_output_error
	local cmd = "/sbin/apk_version -vs " .. processname .." 2>/dev/null"
	local f = io.popen( cmd )
	local cmdresult = f:read("*l")
	if (cmdresult) and (#cmdresult > 0) then
		cmd_output_result = string.match(cmdresult,"^%S*") or "Unknown"
	else
		cmd_output_error = "Program not installed"
	end	
	f:close()
	return cmd_output_result,cmd_output_error
end

local function autostarts()
	local cmd_output_result, cmd_output_error
	local cmd = "/sbin/rc_status | egrep '^S' | egrep '" .. processname .."' 2>/dev/null"
	local f = io.popen( cmd )
	local cmdresult = f:read("*a")
	if (cmdresult) and (#cmdresult > 0) then
		cmd_output_result = "Process will autostart at next boot (at sequence '" .. string.match(cmdresult,"^%a+(%d%d)") .. "')"
	else
		cmd_output_error = "Not programmed to autostart"
	end	
	f:close()
	return cmd_output_result,cmd_output_error
end

local function opennhrpctl_show()
	local cmd_output_result={}
	local cmd_output_result_table={}
	local cmd_output_error, opennhrpstatus
	local cmd = "/usr/sbin/opennhrpctl show 2>/dev/null"
	local f = io.popen( cmd )
	for line in f:lines() do
		if string.find(line, "^Status:") then
			opennhrpstatus=line
		else
			table.insert(cmd_output_result, line)
		end
	end
	f:close()
	local cnt = 0
	for k,v in pairs(cmd_output_result) do
		if string.find(v,"^Interface") then
			cnt = cnt + 1
			cmd_output_result_table[cnt] = {}
		end
		if ( cnt > 0 ) and (v ~= "") then
			local k = string.match(v,"^(.-):%s?.*")
			cmd_output_result_table[cnt][k]=string.match(v,"^.-:%s?(.*)")
			if (string.lower(k) == "type") then
				local tooltip = string.match(v,"^.-:%s?(.*)")
				cmd_output_result_table[cnt]["Tooltip"] = type_status[tooltip]
				local typestatus = string.lower(string.match(v,"^.-:%s?(.*)"))
				cmd_output_result_table[cnt]['type_descr']=(type_status[typestatus] or "")
			end
		end
	end

	return cmd_output_result_table,opennhrpstatus,cmd_output_error
end


	



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


function getstatus()
	local status = {}
	local value, errtxt = get_version()
	status.version = cfe({ name = "version",
		label="Program version",
		value=value,
		errtxt=errtxt,
		 })

	status.status = cfe({ name="status",
		label="Program status",
		value=procps.pidof(processname),
		})
	local autostart_sequense, autostart_errtxt = autostarts()
	status.autostart = cfe({ name="autostart",
		label="Autostart sequence",
		value=autostart_sequense,
		errtxt=autostart_errtxt,
		})

	local opennhrpctl_show, opennhrpctl_status = opennhrpctl_show()
	status.stats = cfe({ name="stats",
		label="Programstatus reports",
		value=opennhrpctl_status,
		})

	local peers_list = {}
	for k,v in pairs(opennhrpctl_show) do
		if (v.Interface) and not (peers_list[v.Interface]) then
			peers_list[v.Interface] = {}
		end
		table.insert(peers_list[v.Interface], v)
	end
	status.show = cfe({ name="show",
		label="Peers",
		option=peers_list,
		})

	return status
end