Mercurial > prosody-modules
comparison mod_audit/mod_audit.lua @ 5299:e3a3a6c86a9f
mod_audit: Add a command to print the audit log on the command-line
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 01 Apr 2023 13:22:12 +0100 |
parents | 12f7d8b901e0 |
children | 0091b7de2763 |
comparison
equal
deleted
inserted
replaced
5298:12f7d8b901e0 | 5299:e3a3a6c86a9f |
---|---|
121 end | 121 end |
122 | 122 |
123 function moduleapi.audit(module, user, event_type, extra) | 123 function moduleapi.audit(module, user, event_type, extra) |
124 audit(module.host, user, "mod_" .. module:get_name(), event_type, extra); | 124 audit(module.host, user, "mod_" .. module:get_name(), event_type, extra); |
125 end | 125 end |
126 | |
127 function module.command(_arg) | |
128 local arg = require "util.argparse".parse(_arg, { value_params = { "limit", "user" } }); | |
129 local host = arg[1]; | |
130 if not host then | |
131 print("EE: Please supply the host for which you want to show events"); | |
132 return 1; | |
133 elseif not prosody.hosts[host] then | |
134 print("EE: Unknown host: "..host); | |
135 return 1; | |
136 end | |
137 | |
138 require "core.storagemanager".initialize_host(host); | |
139 local store = stores[host]; | |
140 local c = 0; | |
141 | |
142 local results, err = store:find(nil, { | |
143 with = arg.user; | |
144 limit = arg.limit and tonumber(arg.limit) or nil; | |
145 }) | |
146 if not results then | |
147 print("EE: Failed to query audit log: "..tostring(err)); | |
148 return 1; | |
149 end | |
150 | |
151 local colspec = { | |
152 { title = "Date", key = "when", width = 19, mapper = function (when) return os.date("%Y-%m-%d %R:%S", when); end }; | |
153 { title = "Source", key = "source", width = 18 }; | |
154 { title = "Event", key = "event_type", width = 22 }; | |
155 }; | |
156 | |
157 if not arg.global then | |
158 table.insert(colspec, { | |
159 title = "User", key = "username", width = 30, | |
160 mapper = function (user) | |
161 if user == "@" then return ""; end | |
162 if user:sub(-#host-1, -1) == ("@"..host) then | |
163 return (user:gsub("@.+$", "")); | |
164 end | |
165 end; | |
166 }); | |
167 | |
168 if attach_ips then | |
169 table.insert(colspec, { | |
170 title = "IP", key = "ip", width = "28"; | |
171 }); | |
172 end | |
173 if attach_location then | |
174 table.insert(colspec, { | |
175 title = "Location", key = "country", width = 2; | |
176 }); | |
177 end | |
178 end | |
179 | |
180 local width = tonumber(os.getenv("COLUMNS")) or 80; | |
181 local row = require "util.human.io".table(colspec, width); | |
182 | |
183 print(string.rep("-", width)); | |
184 print(row()); | |
185 print(string.rep("-", width)); | |
186 for _, entry, when, user in results do | |
187 c = c + 1; | |
188 print(row({ | |
189 when = when; | |
190 source = entry.attr.source; | |
191 event_type = entry.attr.type:gsub("%-", " "); | |
192 username = user; | |
193 ip = entry:get_child_text("remote-ip"); | |
194 location = entry:find("location@country"); | |
195 })); | |
196 end | |
197 print(string.rep("-", width)); | |
198 print(("%d records displayed"):format(c)); | |
199 end |