You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

configgraph.lua 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. --[[
  2. Copyright (c) 2022, Vsevolod Stakhov <vsevolod@rspamd.com>
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ]]--
  13. local rspamd_logger = require "rspamd_logger"
  14. local rspamd_util = require "rspamd_util"
  15. local rspamd_regexp = require "rspamd_regexp"
  16. local argparse = require "argparse"
  17. -- Define command line options
  18. local parser = argparse()
  19. :name "rspamadm configgraph"
  20. :description "Produces graph of Rspamd includes"
  21. :help_description_margin(30)
  22. parser:option "-c --config"
  23. :description "Path to config file"
  24. :argname("<file>")
  25. :default(rspamd_paths["CONFDIR"] .. "/" .. "rspamd.conf")
  26. parser:flag "-a --all"
  27. :description('Show all nodes, not just existing ones')
  28. local function process_filename(fname)
  29. local cdir = rspamd_paths['CONFDIR'] .. '/'
  30. fname = fname:gsub(cdir, '')
  31. return fname
  32. end
  33. local function output_dot(opts, nodes, adjacency)
  34. rspamd_logger.messagex("digraph rspamd {")
  35. for k, node in pairs(nodes) do
  36. local attrs = { "shape=box" }
  37. local skip = false
  38. if node.exists then
  39. if node.priority >= 10 then
  40. attrs[#attrs + 1] = "color=red"
  41. elseif node.priority > 0 then
  42. attrs[#attrs + 1] = "color=blue"
  43. end
  44. else
  45. if opts.all then
  46. attrs[#attrs + 1] = "style=dotted"
  47. else
  48. skip = true
  49. end
  50. end
  51. if not skip then
  52. rspamd_logger.messagex("\"%s\" [%s];", process_filename(k),
  53. table.concat(attrs, ','))
  54. end
  55. end
  56. for _, adj in ipairs(adjacency) do
  57. local attrs = {}
  58. local skip = false
  59. if adj.to.exists then
  60. if adj.to.merge then
  61. attrs[#attrs + 1] = "arrowhead=diamond"
  62. attrs[#attrs + 1] = "label=\"+\""
  63. elseif adj.to.priority > 1 then
  64. attrs[#attrs + 1] = "color=red"
  65. end
  66. else
  67. if opts.all then
  68. attrs[#attrs + 1] = "style=dotted"
  69. else
  70. skip = true
  71. end
  72. end
  73. if not skip then
  74. rspamd_logger.messagex("\"%s\" -> \"%s\" [%s];", process_filename(adj.from),
  75. adj.to.short_path, table.concat(attrs, ','))
  76. end
  77. end
  78. rspamd_logger.messagex("}")
  79. end
  80. local function load_config_traced(opts)
  81. local glob_traces = {}
  82. local adjacency = {}
  83. local nodes = {}
  84. local function maybe_match_glob(file)
  85. for _, gl in ipairs(glob_traces) do
  86. if gl.re:match(file) then
  87. return gl
  88. end
  89. end
  90. return nil
  91. end
  92. local function add_dep(from, node, args)
  93. adjacency[#adjacency + 1] = {
  94. from = from,
  95. to = node,
  96. args = args
  97. }
  98. end
  99. local function process_node(fname, args)
  100. local node = nodes[fname]
  101. if not node then
  102. node = {
  103. path = fname,
  104. short_path = process_filename(fname),
  105. exists = rspamd_util.file_exists(fname),
  106. merge = args.duplicate and args.duplicate == 'merge',
  107. priority = args.priority or 0,
  108. glob = args.glob,
  109. try = args.try,
  110. }
  111. nodes[fname] = node
  112. end
  113. return node
  114. end
  115. local function trace_func(cur_file, included_file, args, parent)
  116. if args.glob then
  117. glob_traces[#glob_traces + 1] = {
  118. re = rspamd_regexp.import_glob(included_file, ''),
  119. parent = cur_file,
  120. args = args,
  121. seen = {},
  122. }
  123. else
  124. local node = process_node(included_file, args)
  125. if opts.all or node.exists then
  126. local gl_parent = maybe_match_glob(included_file)
  127. if gl_parent and not gl_parent.seen[cur_file] then
  128. add_dep(gl_parent.parent, nodes[cur_file], gl_parent.args)
  129. gl_parent.seen[cur_file] = true
  130. end
  131. add_dep(cur_file, node, args)
  132. end
  133. end
  134. end
  135. local _r, err = rspamd_config:load_ucl(opts['config'], trace_func)
  136. if not _r then
  137. rspamd_logger.errx('cannot parse %s: %s', opts['config'], err)
  138. os.exit(1)
  139. end
  140. output_dot(opts, nodes, adjacency)
  141. end
  142. local function handler(args)
  143. local res = parser:parse(args)
  144. load_config_traced(res)
  145. end
  146. return {
  147. handler = handler,
  148. description = parser._description,
  149. name = 'configgraph'
  150. }