2
0

stat-genconfig 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. #!/usr/bin/env ucode
  2. /*
  3. Luci statistics - collectd configuration generator
  4. (c) 2008-2022 Jo-Philipp Wich <jo@mein.io>
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. */
  10. 'use strict';
  11. import { lsdir, open } from 'fs';
  12. import { cursor } from 'uci';
  13. const uci = cursor();
  14. const sections = uci.get_all('luci_statistics');
  15. const plugins = {
  16. collectd: [
  17. [ 'BaseDir', 'Include', 'PIDFile', 'PluginDir', 'TypesDB', 'Interval', 'ReadThreads', 'Hostname' ],
  18. [],
  19. []
  20. ],
  21. logfile: [
  22. [ 'LogLevel', 'File' ],
  23. [ 'Timestamp' ],
  24. []
  25. ],
  26. };
  27. function parse_units(ustr) {
  28. let val = 0;
  29. // unit map
  30. const map = {
  31. y : 60 * 60 * 24 * 366,
  32. m : 60 * 60 * 24 * 31,
  33. w : 60 * 60 * 24 * 7,
  34. d : 60 * 60 * 24,
  35. h : 60 * 60,
  36. min: 60
  37. };
  38. // parse input string
  39. for (let spec in match(lc(ustr), /([0-9.]+)([a-z]*)/g)) {
  40. let num = +spec[1];
  41. let mul = map[spec[2]] ?? map[substr(spec[2], 0, 1)] ?? 1;
  42. val += num * mul;
  43. }
  44. return int(val);
  45. }
  46. const preprocess = {
  47. RRATimespans: function(val) {
  48. return join(' ', map(type(val) == 'array' ? val : split(val, /\s+/), parse_units));
  49. }
  50. };
  51. function _bool(s, n, nopad) {
  52. if (s == '1')
  53. return `${nopad ? '' : '\t'}${n} true\n`;
  54. if (s == '0')
  55. return `${nopad ? '' : '\t'}${n} false\n`;
  56. return '';
  57. }
  58. function _string(s, n, nopad) {
  59. if (s) {
  60. if (n == 'Port' || n == 'Irq' || match(s, /[^0-9]/)) {
  61. if (!match(s, /[^\w]/) && n != 'Port' && n != 'Irq')
  62. return `${nopad ? '' : '\t'}${n} ${trim(s)}\n`;
  63. else
  64. return `${nopad ? '' : '\t'}${n} "${trim(s)}"\n`;
  65. }
  66. else {
  67. return `${nopad ? '' : '\t'}${n} ${trim(s)}\n`;
  68. }
  69. }
  70. return '';
  71. }
  72. function _expand(s, n, nopad) {
  73. let str = "";
  74. if (type(s) == 'string') {
  75. for (let v in split(s, /\s+/))
  76. str += _string(v, n, nopad);
  77. }
  78. else if (type(s) == 'array') {
  79. for (let v in s)
  80. str += _string(v, n, nopad);
  81. }
  82. return str;
  83. }
  84. function _list_expand(c, l, nopad) {
  85. let str = '';
  86. for (let n in l) {
  87. if (c[n]) {
  88. if (preprocess[n])
  89. c[n] = preprocess[n](c[n]);
  90. let m = match(n, /^(\w+)ses$/);
  91. let k;
  92. if (m)
  93. k = `${m[1]}s`;
  94. else
  95. k = replace(n, /^(\w+)s$/, '$1');
  96. str += _expand(c[n], k, nopad);
  97. }
  98. }
  99. return str;
  100. }
  101. function config_generic(c, singles, bools, lists, nopad) {
  102. let str = '';
  103. if (c) {
  104. for (let key in singles) {
  105. if (preprocess[key])
  106. c[key] = preprocess[key](c[key]);
  107. str += _string(c[key], key, nopad);
  108. }
  109. for (let key in bools) {
  110. if (preprocess[key])
  111. c[key] = preprocess[key](c[key]);
  112. str += _bool(c[key], key, nopad);
  113. }
  114. if (lists)
  115. str += _list_expand(c, lists, nopad);
  116. }
  117. return str;
  118. }
  119. function config_exec(c) {
  120. let str = "";
  121. for (let k, s in sections) {
  122. for (let key, type in { Exec: 'collectd_exec_input', NotificationExec: 'collectd_exec_notify' }) {
  123. if (s['.type'] == type) {
  124. let cmd = replace(trim(s.cmdline), /\s+/g, '" "');
  125. let user = s.cmduser ?? 'nobody';
  126. let group = s.cmdgroup;
  127. if (cmd)
  128. str += `\t${key} "${user}${group ? `:${group}` : ''}" "${cmd}"\n`;
  129. }
  130. }
  131. }
  132. return str;
  133. }
  134. function config_curl(c) {
  135. let str = "";
  136. for (let k, s in sections) {
  137. if (s['.type'] == 'collectd_curl_page') {
  138. str += `\t<Page "${s.name}">\n`
  139. + `\t\tURL "${s.url}"\n`
  140. + `\t\tMeasureResponseTime true\n`
  141. + `\t</Page>\n`;
  142. }
  143. }
  144. return str;
  145. }
  146. function config_iptables(c) {
  147. let str = "";
  148. for (let k, s in sections) {
  149. for (let type, verb in { collectd_iptables_match: 'Chain', collectd_iptables_match6: 'Chain6' }) {
  150. if (s['.type'] == type) {
  151. let tname = `${s.table}`;
  152. let chain = `${s.chain}`;
  153. if (match(tname, /^\S+$/) && match(chain, /^\S+$/)) {
  154. str += `\t${verb} "${tname}" "${chain}"`;
  155. let rule = `${s.rule}`;
  156. if (match(rule, /^\S+$/)) {
  157. str += ` "${rule}"`;
  158. let name = `${s.name}`;
  159. if (match(name, /^\S+$/))
  160. str += ` "${name}"`;
  161. }
  162. }
  163. str += '\n';
  164. }
  165. }
  166. }
  167. return str;
  168. }
  169. function config_network(c) {
  170. let str = '';
  171. for (let k, s in sections) {
  172. for (let key, type in { Listen: 'collectd_network_listen', Server: 'collectd_network_server' }) {
  173. if (s['.type'] == type && s.host) {
  174. if (s.port)
  175. str += `\t${key} "${s.host}" "${s.port}"\n`;
  176. else
  177. str += `\t${key} "${s.host}"\n`;
  178. }
  179. }
  180. }
  181. return str
  182. + _string(c.MaxPacketSize, 'MaxPacketSize')
  183. + _string(c.TimeToLive, 'TimeToLive')
  184. + _bool(c.Forward, 'Forward')
  185. + _bool(c.ReportStats, 'ReportStats')
  186. ;
  187. }
  188. function config_mqtt(c) {
  189. let str = "";
  190. for (let k, s in sections) {
  191. if (s['.type'] === 'collectd_mqtt_block') {
  192. const isPublish = s['blocktype'] === 'Publish';
  193. str += isPublish ? `\t<Publish "${s.name}">\n` : `\t<Subscribe "${s.name}">\n`;
  194. str += `\t\tHost "${s.Host}"\n`;
  195. str += s['Port'] ? `\t\tPort ${s.Port}\n` : '';
  196. str += s['User'] ? `\t\tUser "${s.User}"\n` : '';
  197. str += s['Password'] ? `\t\tPassword "${s.Password}"\n` : '';
  198. str += s['Qos'] ? `\t\tQos ${s.Qos}\n` : '';
  199. str += s['Prefix'] ? `\t\tPrefix ${s.Prefix}\n` : '';
  200. str += s['Retain'] ? `\t\tRetain ${s.Retain}\n` : '';
  201. str += s['StoreRates'] ? `\t\tRetain ${s.StoreRates}\n` : '';
  202. str += s['CleanSession'] ? `\t\tRetain ${s.CleanSession}\n` : '';
  203. str += s['Topic'] ? `\t\tTopic "${s.Topic}"\n` : '';
  204. str += isPublish ? `\t</Publish>\n` : `\t</Subscribe>\n`;
  205. }
  206. }
  207. return str;
  208. }
  209. function section(plugin) {
  210. let config = sections[`collectd_${plugin}`] ?? sections.collectd;
  211. if (config && (plugin == 'collectd' || config.enable == '1')) {
  212. let params;
  213. if (type(plugins[plugin]) == 'function')
  214. params = plugins[plugin](config);
  215. else
  216. params = config_generic(config, ...plugins[plugin], plugin == 'collectd');
  217. if (plugin != 'collectd')
  218. print(`LoadPlugin ${plugin}\n${length(params) ? `<Plugin ${plugin}>\n${params}</Plugin>\n` : ''}\n`);
  219. else
  220. print(`${params ?? ''}\n`);
  221. }
  222. }
  223. let plugin_dir = '/usr/share/luci/statistics/plugins';
  224. for (let filename in lsdir(plugin_dir)) {
  225. let name = replace(filename, /\.json$/, '');
  226. switch (name) {
  227. case 'exec': plugins[name] = config_exec; break;
  228. case 'iptables': plugins[name] = config_iptables; break;
  229. case 'curl': plugins[name] = config_curl; break;
  230. case 'network': plugins[name] = config_network; break;
  231. case 'mqtt': plugins[name] = config_mqtt; break;
  232. default:
  233. plugins[name] = json(open(`${plugin_dir}/${filename}`))?.legend;
  234. }
  235. }
  236. section('collectd');
  237. section('logfile');
  238. for (let plugin in plugins)
  239. if (plugin != 'collectd' && plugin != 'logfile')
  240. section(plugin);