monitoring.scm 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018 Sou Bunnbu <iyzsong@member.fsf.org>
  3. ;;; Copyright © 2018, 2019 Gábor Boskovits <boskovits@gmail.com>
  4. ;;; Copyright © 2018, 2019, 2020 Oleg Pykhalov <go.wigust@gmail.com>
  5. ;;; Copyright © 2022 Marius Bakke <marius@gnu.org>
  6. ;;; Copyright © 2023 Bruno Victal <mirai@makinata.eu>
  7. ;;;
  8. ;;; This file is part of GNU Guix.
  9. ;;;
  10. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  11. ;;; under the terms of the GNU General Public License as published by
  12. ;;; the Free Software Foundation; either version 3 of the License, or (at
  13. ;;; your option) any later version.
  14. ;;;
  15. ;;; GNU Guix is distributed in the hope that it will be useful, but
  16. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;;; GNU General Public License for more details.
  19. ;;;
  20. ;;; You should have received a copy of the GNU General Public License
  21. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  22. (define-module (gnu services monitoring)
  23. #:use-module (gnu services)
  24. #:use-module (gnu services configuration)
  25. #:use-module (gnu services shepherd)
  26. #:use-module (gnu services web)
  27. #:use-module (gnu packages admin)
  28. #:use-module (gnu packages monitoring)
  29. #:use-module (gnu packages networking)
  30. #:use-module (gnu system shadow)
  31. #:use-module (guix gexp)
  32. #:use-module (guix packages)
  33. #:use-module (guix records)
  34. #:use-module (guix utils)
  35. #:use-module ((guix ui) #:select (display-hint G_))
  36. #:use-module (ice-9 match)
  37. #:use-module (ice-9 rdelim)
  38. #:use-module (srfi srfi-1)
  39. #:use-module (srfi srfi-26)
  40. #:use-module (srfi srfi-35)
  41. #:export (darkstat-configuration
  42. darkstat-service-type
  43. prometheus-node-exporter-configuration
  44. prometheus-node-exporter-configuration?
  45. prometheus-node-exporter-configuration-package
  46. prometheus-node-exporter-web-listen-address
  47. prometheus-node-exporter-service-type
  48. vnstat-configuration
  49. vnstat-configuration?
  50. vnstat-service-type
  51. vnstat-configuration-package
  52. vnstat-configuration-database-directory
  53. vnstat-configuration-5-minute-hours
  54. vnstat-configuration-64bit-interface-counters
  55. vnstat-configuration-always-add-new-interfaces?
  56. vnstat-configuration-bandwidth-detection?
  57. vnstat-configuration-bandwidth-detection-interval
  58. vnstat-configuration-boot-variation
  59. vnstat-configuration-check-disk-space?
  60. vnstat-configuration-create-directories?
  61. vnstat-configuration-daemon-group
  62. vnstat-configuration-daemon-user
  63. vnstat-configuration-daily-days
  64. vnstat-configuration-database-synchronous
  65. vnstat-configuration-database-write-ahead-logging?
  66. vnstat-configuration-hourly-days
  67. vnstat-configuration-log-file
  68. vnstat-configuration-max-bandwidth
  69. vnstat-configuration-max-bw
  70. vnstat-configuration-monthly-months
  71. vnstat-configuration-month-rotate
  72. vnstat-configuration-month-rotate-affects-years?
  73. vnstat-configuration-offline-save-interval
  74. vnstat-configuration-pid-file
  75. vnstat-configuration-poll-interval
  76. vnstat-configuration-rescan-database-on-save?
  77. vnstat-configuration-save-interval
  78. vnstat-configuration-save-on-status-change?
  79. vnstat-configuration-time-sync-wait
  80. vnstat-configuration-top-day-entries
  81. vnstat-configuration-trafficless-entries?
  82. vnstat-configuration-update-file-owner?
  83. vnstat-configuration-update-interval
  84. vnstat-configuration-use-logging
  85. vnstat-configuration-use-utc?
  86. vnstat-configuration-yearly-years
  87. zabbix-server-configuration
  88. zabbix-server-service-type
  89. zabbix-agent-configuration
  90. zabbix-agent-service-type
  91. zabbix-front-end-configuration
  92. zabbix-front-end-service-type
  93. %zabbix-front-end-configuration-nginx))
  94. ;;;
  95. ;;; darkstat
  96. ;;;
  97. (define-record-type* <darkstat-configuration>
  98. darkstat-configuration make-darkstat-configuration darkstat-configuration?
  99. (package darkstat-configuration-package
  100. (default darkstat))
  101. (interface darkstat-configuration-interface)
  102. (port darkstat-configuration-port
  103. (default "667"))
  104. (bind-address darkstat-configuration-bind-address
  105. (default "127.0.0.1"))
  106. (base darkstat-configuration-base
  107. (default "/")))
  108. (define %darkstat-accounts
  109. (list (user-account
  110. (name "darkstat")
  111. (group "darkstat")
  112. (system? #t)
  113. (comment "darkstat daemon user")
  114. (home-directory "/var/lib/darkstat")
  115. (shell (file-append shadow "/sbin/nologin")))
  116. (user-group
  117. (name "darkstat")
  118. (system? #t))))
  119. (define darkstat-shepherd-service
  120. (match-lambda
  121. (($ <darkstat-configuration>
  122. package interface port bind-address base)
  123. (shepherd-service
  124. (documentation "Network statistics gatherer.")
  125. (provision '(darkstat))
  126. (requirement '(networking))
  127. (start #~(make-forkexec-constructor
  128. (list #$(file-append package "/sbin/darkstat")
  129. "-i" #$interface
  130. "-p" #$port
  131. "-b" #$bind-address
  132. "--base" #$base
  133. "--syslog" "--no-daemon"
  134. "--chroot" "/var/lib/darkstat"
  135. "--user" "darkstat"
  136. "--import" "darkstat.db"
  137. "--export" "darkstat.db")))
  138. (stop #~(make-kill-destructor))))))
  139. (define darkstat-service-type
  140. (service-type
  141. (name 'darkstat)
  142. (description
  143. "Run @command{darkstat} to serve network traffic statistics reports over
  144. HTTP.")
  145. (extensions
  146. (list (service-extension account-service-type
  147. (const %darkstat-accounts))
  148. (service-extension shepherd-root-service-type
  149. (compose list darkstat-shepherd-service))))))
  150. ;;;
  151. ;;; Prometheus node exporter
  152. ;;;
  153. (define-record-type* <prometheus-node-exporter-configuration>
  154. prometheus-node-exporter-configuration
  155. make-prometheus-node-exporter-configuration
  156. prometheus-node-exporter-configuration?
  157. (package prometheus-node-exporter-configuration-package
  158. (default go-github-com-prometheus-node-exporter))
  159. (web-listen-address prometheus-node-exporter-web-listen-address
  160. (default ":9100"))
  161. (textfile-directory prometheus-node-exporter-textfile-directory
  162. (default "/var/lib/prometheus/node-exporter"))
  163. (extra-options prometheus-node-exporter-extra-options
  164. (default '())))
  165. (define %prometheus-node-exporter-accounts
  166. (list (user-account
  167. (name "prometheus-node-exporter")
  168. (group "prometheus-node-exporter")
  169. (system? #t)
  170. (comment "Prometheus node exporter daemon user")
  171. (home-directory "/var/empty")
  172. (shell (file-append shadow "/sbin/nologin")))
  173. (user-group
  174. (name "prometheus-node-exporter")
  175. (system? #t))))
  176. (define prometheus-node-exporter-shepherd-service
  177. (match-lambda
  178. (( $ <prometheus-node-exporter-configuration>
  179. package web-listen-address textfile-directory extra-options)
  180. (list
  181. (shepherd-service
  182. (documentation "Prometheus node exporter.")
  183. (provision '(prometheus-node-exporter))
  184. (requirement '(networking))
  185. (start #~(make-forkexec-constructor
  186. (list #$(file-append package "/bin/node_exporter")
  187. "--web.listen-address" #$web-listen-address
  188. #$@(if textfile-directory
  189. (list "--collector.textfile.directory"
  190. textfile-directory)
  191. '())
  192. #$@extra-options)
  193. #:user "prometheus-node-exporter"
  194. #:group "prometheus-node-exporter"
  195. #:log-file "/var/log/prometheus-node-exporter.log"))
  196. (stop #~(make-kill-destructor)))))))
  197. (define (prometheus-node-exporter-activation config)
  198. (with-imported-modules '((guix build utils))
  199. #~(let ((textfile-directory
  200. #$(prometheus-node-exporter-textfile-directory config)))
  201. (use-modules (guix build utils))
  202. (when textfile-directory
  203. (let ((user (getpw "prometheus-node-exporter")))
  204. #t
  205. (mkdir-p textfile-directory)
  206. (chown textfile-directory (passwd:uid user) (passwd:gid user))
  207. (chmod textfile-directory #o775))))))
  208. (define prometheus-node-exporter-service-type
  209. (service-type
  210. (name 'prometheus-node-exporter)
  211. (description
  212. "Run @command{node_exporter} to serve hardware and OS metrics to
  213. Prometheus.")
  214. (extensions
  215. (list
  216. (service-extension account-service-type
  217. (const %prometheus-node-exporter-accounts))
  218. (service-extension activation-service-type
  219. prometheus-node-exporter-activation)
  220. (service-extension shepherd-root-service-type
  221. prometheus-node-exporter-shepherd-service)))
  222. (default-value (prometheus-node-exporter-configuration))))
  223. ;;;
  224. ;;; vnstat daemon
  225. ;;;
  226. (define* (camelfy-field-name field-name #:key (dromedary? #f))
  227. (match (string-split (symbol->string field-name) #\-)
  228. ((head tail ...)
  229. (string-join (cons (if dromedary? head (string-upcase head 0 1))
  230. (map (cut string-upcase <> 0 1) tail)) ""))))
  231. (define (strip-trailing-?-character field-name)
  232. "Drop rightmost '?' character"
  233. (let ((str (symbol->string field-name)))
  234. (if (string-suffix? "?" str)
  235. (string->symbol (string-drop-right str 1))
  236. field-name)))
  237. (define (vnstat-serialize-string field-name value)
  238. #~(format #f "~a ~s~%"
  239. #$(camelfy-field-name field-name)
  240. #$value))
  241. (define vnstat-serialize-integer vnstat-serialize-string)
  242. (define (vnstat-serialize-boolean field-name value)
  243. #~(format #f "~a ~a~%"
  244. #$(camelfy-field-name (strip-trailing-?-character field-name))
  245. #$(if value 1 0)))
  246. (define (vnstat-serialize-alist field-name value)
  247. (generic-serialize-alist string-append
  248. (lambda (iface val)
  249. (vnstat-serialize-integer
  250. (format #f "MaxBW~a" iface) val))
  251. value))
  252. (define (vnstat-serialize-user-account field-name value)
  253. (vnstat-serialize-string field-name (user-account-name value)))
  254. (define (vnstat-serialize-user-group field-name value)
  255. (vnstat-serialize-string field-name (user-group-name value)))
  256. (define-maybe string (prefix vnstat-))
  257. (define-maybe integer (prefix vnstat-))
  258. (define-maybe boolean (prefix vnstat-))
  259. (define-maybe alist (prefix vnstat-))
  260. (define-maybe user-account (prefix vnstat-))
  261. (define-maybe user-group (prefix vnstat-))
  262. (define %vnstat-user
  263. (user-account
  264. (name "vnstat")
  265. (group "vnstat")
  266. (system? #t)
  267. (home-directory "/var/empty")
  268. (shell (file-append shadow "/sbin/nologin"))))
  269. (define %vnstat-group
  270. (user-group
  271. (name "vnstat")
  272. (system? #t)))
  273. ;; Documentation strings from vnstat.conf manpage adapted to texinfo.
  274. ;; vnstat checkout: v2.10, commit b3408af1c609aa6265d296cab7bfe59a61d7cf70
  275. ;; Do not reflow these strings or drop the initial \ escape as it makes it
  276. ;; harder to diff against the manpage.
  277. (define-configuration vnstat-configuration
  278. (package
  279. (file-like vnstat)
  280. "The vnstat package."
  281. empty-serializer)
  282. (database-directory
  283. (string "/var/lib/vnstat")
  284. "\
  285. Specifies the directory where the database is to be stored.
  286. A full path must be given and a leading '/' isn't required."
  287. (serializer
  288. (lambda (_ value)
  289. (vnstat-serialize-string 'database-dir value))))
  290. (5-minute-hours
  291. (maybe-integer 48)
  292. "\
  293. Data retention duration for the 5 minute resolution entries. The configuration
  294. defines for how many past hours entries will be stored. Set to @code{-1} for
  295. unlimited entries or to @code{0} to disable the data collection of this
  296. resolution.")
  297. (64bit-interface-counters
  298. (maybe-integer -2)
  299. "\
  300. Select interface counter handling. Set to @code{1} for defining that all interfaces
  301. use 64-bit counters on the kernel side and @code{0} for defining 32-bit counter. Set
  302. to @code{-1} for using the old style logic used in earlier versions where counter
  303. values within 32-bits are assumed to be 32-bit and anything larger is assumed to
  304. be a 64-bit counter. This may produce false results if a 64-bit counter is
  305. reset within the 32-bits. Set to @code{-2} for using automatic detection based on
  306. available kernel datastructures.")
  307. (always-add-new-interfaces?
  308. (maybe-boolean #t)
  309. "\
  310. Enable or disable automatic creation of new database entries for interfaces not
  311. currently in the database even if the database file already exists when the
  312. daemon is started. New database entries will also get created for new interfaces
  313. seen while the daemon is running. Pseudo interfaces @samp{lo}, @samp{lo0} and @samp{sit0} are always
  314. excluded from getting added.")
  315. (bandwidth-detection?
  316. (maybe-boolean #t)
  317. "\
  318. Try to automatically detect
  319. @var{max-bandwidth}
  320. value for each monitored interface. Mostly only ethernet interfaces support
  321. this feature.
  322. @var{max-bandwidth}
  323. will be used as fallback value if detection fails. Any interface specific
  324. @var{max-BW}
  325. configuration will disable the detection for the specified interface.
  326. In Linux, the detection is disabled for tun interfaces due to the
  327. Linux kernel always reporting 10 Mbit regardless of the used real interface.")
  328. (bandwidth-detection-interval
  329. (maybe-integer 5)
  330. "\
  331. How often in minutes interface specific detection of
  332. @var{max-bandwidth}
  333. is done for detecting possible changes when
  334. @var{bandwidth-detection}
  335. is enabled. Can be disabled by setting to @code{0}. Value range: @samp{0}..@samp{30}")
  336. (boot-variation
  337. (maybe-integer 15)
  338. "\
  339. Time in seconds how much the boot time reported by system kernel can variate
  340. between updates. Value range: @samp{0}..@samp{300}")
  341. (check-disk-space?
  342. (maybe-boolean #t)
  343. "\
  344. Enable or disable the availability check of at least some free disk space before
  345. a database write.")
  346. (create-directories?
  347. (maybe-boolean #t)
  348. "\
  349. Enable or disable the creation of directories when a configured path doesn't
  350. exist. This includes @var{database-directory}."
  351. (serializer
  352. (lambda (_ value)
  353. (if (maybe-value-set? value)
  354. (vnstat-serialize-boolean 'create-dirs value) ""))))
  355. ;; Note: Documentation for daemon-group and daemon-user adapted
  356. ;; for user-group and user-account record-types.
  357. (daemon-group
  358. (maybe-user-group %vnstat-group)
  359. "\
  360. Specify the group to which the daemon process should switch during startup.
  361. Set to @code{%unset-value} to disable group switching.")
  362. (daemon-user
  363. (maybe-user-account %vnstat-user)
  364. "\
  365. Specify the user to which the daemon process should switch during startup.
  366. Set to @code{%unset-value} to disable user switching.")
  367. (daily-days
  368. (maybe-integer 62)
  369. "\
  370. Data retention duration for the one day resolution entries. The configuration
  371. defines for how many past days entries will be stored. Set to @code{-1} for
  372. unlimited entries or to @code{0} to disable the data collection of this
  373. resolution.")
  374. (database-synchronous
  375. (maybe-integer -1)
  376. "\
  377. Change the setting of the SQLite \"synchronous\" flag which controls how much
  378. care is taken to ensure disk writes have fully completed when writing data to
  379. the database before continuing other actions. Higher values take extra steps
  380. to ensure data safety at the cost of slower performance. A value of @code{0} will
  381. result in all handling being left to the filesystem itself. Set to @code{-1} to
  382. select the default value according to database mode controlled by
  383. @var{database-write-ahead-logging}
  384. setting. See SQLite documentation for more details regarding values from @code{1}
  385. to @code{3}. Value range: @samp{-1}..@samp{3}")
  386. (database-write-ahead-logging?
  387. (maybe-boolean #f)
  388. "\
  389. Enable or disable SQLite Write-Ahead Logging mode for the database. See SQLite
  390. documentation for more details and note that support for read-only operations
  391. isn't available in older SQLite versions.")
  392. (hourly-days
  393. (maybe-integer 4)
  394. "\
  395. Data retention duration for the one hour resolution entries. The configuration
  396. defines for how many past days entries will be stored. Set to @code{-1} for
  397. unlimited entries or to @code{0} to disable the data collection of this
  398. resolution.")
  399. (log-file
  400. maybe-string
  401. "\
  402. Specify log file path and name to be used if @var{use-logging} is set to @code{1}.")
  403. (max-bandwidth
  404. maybe-integer
  405. "\
  406. Maximum bandwidth for all interfaces. If the interface specific traffic
  407. exceeds the given value then the data is assumed to be invalid and rejected.
  408. Set to 0 in order to disable the feature. Value range: @samp{0}..@samp{50000}")
  409. ;; documentation adapted for alist type
  410. (max-bw
  411. maybe-alist
  412. "\
  413. Same as
  414. @var{max-bandwidth}
  415. but can be used for setting individual limits
  416. for selected interfaces. This is an association list of interfaces
  417. as strings to integer values. For example,
  418. @lisp
  419. (max-bw
  420. `((\"eth0\" . 15000)
  421. (\"ppp0\" . 10000)))
  422. @end lisp
  423. @var{bandwidth-detection}
  424. is disabled on an interface specific level for each
  425. @var{max-bw}
  426. configuration. Value range: @samp{0}..@samp{50000}"
  427. (serializer
  428. (lambda (field-name value)
  429. (if (maybe-value-set? value)
  430. (vnstat-serialize-alist field-name value) ""))))
  431. (monthly-months
  432. (maybe-integer 25)
  433. "\
  434. Data retention duration for the one month resolution entries. The configuration
  435. defines for how many past months entries will be stored. Set to @code{-1} for
  436. unlimited entries or to @code{0} to disable the data collection of this
  437. resolution.")
  438. (month-rotate
  439. (maybe-integer 1)
  440. "\
  441. Day of month that months are expected to change. Usually set to
  442. 1 but can be set to alternative values for example for tracking
  443. monthly billed traffic where the billing period doesn't start on
  444. the first day. For example, if set to 7, days of February up to and
  445. including the 6th will count for January. Changing this option will
  446. not cause existing data to be recalculated. Value range: @samp{1}..@samp{28}")
  447. (month-rotate-affects-years?
  448. (maybe-boolean #f)
  449. "\
  450. Enable or disable
  451. @var{month-rotate}
  452. also affecting yearly data. Applicable only when
  453. @var{month-rotate}
  454. has a value greater than one.")
  455. (offline-save-interval
  456. (maybe-integer 30)
  457. "\
  458. How often in minutes cached interface data is saved to file when all monitored
  459. interfaces are offline. Value range:
  460. @var{save-interval}..@samp{60}")
  461. (pid-file
  462. (maybe-string "/var/run/vnstat/vnstatd.pid")
  463. "\
  464. Specify pid file path and name to be used.")
  465. (poll-interval
  466. (maybe-integer 5)
  467. "\
  468. How often in seconds interfaces are checked for status changes.
  469. Value range: @samp{2}..@samp{60}")
  470. (rescan-database-on-save?
  471. maybe-boolean
  472. "\
  473. Automatically discover added interfaces from the database and start monitoring.
  474. The rescan is done every
  475. @var{save-interval}
  476. or
  477. @var{offline-save-interval}
  478. minutes depending on the current activity state.")
  479. (save-interval
  480. (maybe-integer 5)
  481. "\
  482. How often in minutes cached interface data is saved to file.
  483. Value range: (
  484. @var{update-interval} / 60 )..@samp{60}")
  485. (save-on-status-change?
  486. (maybe-boolean #t)
  487. "\
  488. Enable or disable the additional saving to file of cached interface data
  489. when the availability of an interface changes, i.e., when an interface goes
  490. offline or comes online.")
  491. (time-sync-wait
  492. (maybe-integer 5)
  493. "\
  494. How many minutes to wait during daemon startup for system clock to sync if
  495. most recent database update appears to be in the future. This may be needed
  496. in systems without a real-time clock (RTC) which require some time after boot
  497. to query and set the correct time. @code{0} = wait disabled.
  498. Value range: @samp{0}..@samp{60}")
  499. (top-day-entries
  500. (maybe-integer 20)
  501. "\
  502. Data retention duration for the top day entries. The configuration
  503. defines how many of the past top day entries will be stored. Set to @code{-1} for
  504. unlimited entries or to @code{0} to disable the data collection of this
  505. resolution.")
  506. (trafficless-entries?
  507. (maybe-boolean #t)
  508. "\
  509. Create database entries even when there is no traffic during the entry's time
  510. period.")
  511. (update-file-owner?
  512. (maybe-boolean #t)
  513. "\
  514. Enable or disable the update of file ownership during daemon process startup.
  515. During daemon startup, only database, log and pid files will be modified if the
  516. user or group change feature (
  517. @var{daemon-user}
  518. or
  519. @var{daemon-group}
  520. ) is enabled and the files don't match the requested user or group. During manual
  521. database creation, this option will cause file ownership to be inherited from the
  522. database directory if the directory already exists. This option only has effect
  523. when the process is started as root or via sudo.")
  524. (update-interval
  525. (maybe-integer 20)
  526. "\
  527. How often in seconds the interface data is updated. Value range:
  528. @var{poll-interval}..@samp{300}")
  529. (use-logging
  530. (maybe-integer 2)
  531. "\
  532. Enable or disable logging. Accepted values are:
  533. @code{0} = disabled, @code{1} = logfile and @code{2} = syslog.")
  534. (use-utc?
  535. maybe-boolean
  536. "\
  537. Enable or disable using UTC as timezone in the database for all entries. When
  538. enabled, all entries added to the database will use UTC regardless of the
  539. configured system timezone. When disabled, the configured system timezone
  540. will be used. Changing this setting will not result in already existing
  541. data to be modified."
  542. (serializer
  543. (lambda (_ value)
  544. (if (maybe-value-set? value)
  545. (vnstat-serialize-boolean 'use-UTC value) ""))))
  546. (yearly-years
  547. (maybe-integer -1)
  548. "\
  549. Data retention duration for the one year resolution entries. The configuration
  550. defines for how many past years entries will be stored. Set to @code{-1} for
  551. unlimited entries or to @code{0} to disable the data collection of this
  552. resolution.")
  553. (prefix vnstat-))
  554. (define (vnstat-serialize-configuration config)
  555. (mixed-text-file
  556. "vnstat.conf"
  557. (serialize-configuration config vnstat-configuration-fields)))
  558. (define (vnstat-shepherd-service config)
  559. (let ((config-file (vnstat-serialize-configuration config)))
  560. (match-record config <vnstat-configuration> (package pid-file)
  561. (shepherd-service
  562. (documentation "Run vnstatd.")
  563. (requirement `(networking file-systems))
  564. (provision '(vnstatd))
  565. (start #~(make-forkexec-constructor
  566. (list #$(file-append package "/sbin/vnstatd")
  567. "--daemon"
  568. "--config" #$config-file)
  569. #:pid-file #$pid-file))
  570. (stop #~(make-kill-destructor))
  571. (actions
  572. (list (shepherd-configuration-action config-file)
  573. (shepherd-action
  574. (name 'reload)
  575. (documentation "Reload vnstatd.")
  576. (procedure
  577. #~(lambda (pid)
  578. (if pid
  579. (begin
  580. (kill pid SIGHUP)
  581. (format #t
  582. "Issued SIGHUP to vnstatd (PID ~a)."
  583. pid))
  584. (format #t "vnstatd is not running.")))))))))))
  585. (define (vnstat-account-service config)
  586. (match-record config <vnstat-configuration> (daemon-group daemon-user)
  587. (filter-map maybe-value (list daemon-group daemon-user))))
  588. (define vnstat-service-type
  589. (service-type
  590. (name 'vnstat)
  591. (description "vnStat network-traffic monitor service.")
  592. (extensions
  593. (list (service-extension shepherd-root-service-type
  594. (compose list vnstat-shepherd-service))
  595. (service-extension account-service-type
  596. vnstat-account-service)))
  597. (default-value (vnstat-configuration))))
  598. ;;;
  599. ;;; Zabbix server
  600. ;;;
  601. (define (uglify-field-name field-name)
  602. (apply string-append
  603. (map (lambda (str)
  604. (if (member (string->symbol str) '(ca db ssl))
  605. (string-upcase str)
  606. (string-capitalize str)))
  607. (string-split (string-delete #\?
  608. (symbol->string field-name))
  609. #\-))))
  610. (define (serialize-field field-name val)
  611. #~(format #f "~a=~a~%" #$(uglify-field-name field-name) #$val))
  612. (define (serialize-number field-name val)
  613. (serialize-field field-name (number->string val)))
  614. (define (serialize-list field-name val)
  615. #~(if (null? '#$val)
  616. ""
  617. #$(serialize-field field-name (string-join val ","))))
  618. (define (serialize-string field-name val)
  619. (if (or (eq? 'user field-name)
  620. (eq? 'group field-name)
  621. (and (string? val) (string=? val "")))
  622. ""
  623. (serialize-field field-name val)))
  624. (define include-files? list?)
  625. (define (serialize-include-files field-name val)
  626. #~(string-append #$@(map (cut serialize-field 'include <>) val)))
  627. (define extra-options? string?)
  628. (define (serialize-extra-options field-name val)
  629. #~(if (= 0 (string-length #$val)) "" #$(format #f "~a~%" val)))
  630. (define (nginx-server-configuration-list? val)
  631. (and (list? val) (and-map nginx-server-configuration? val)))
  632. (define (serialize-nginx-server-configuration-list field-name val)
  633. "")
  634. (define-configuration zabbix-server-configuration
  635. (zabbix-server
  636. (file-like zabbix-server)
  637. "The zabbix-server package.")
  638. (user
  639. (string "zabbix")
  640. "User who will run the Zabbix server.")
  641. (group
  642. (string "zabbix")
  643. "Group who will run the Zabbix server.")
  644. (db-host
  645. (string "127.0.0.1")
  646. "Database host name.")
  647. (db-name
  648. (string "zabbix")
  649. "Database name.")
  650. (db-user
  651. (string "zabbix")
  652. "Database user.")
  653. (db-password
  654. (string "")
  655. "Database password. Please, use @code{include-files} with
  656. @code{DBPassword=SECRET} inside a specified file instead.")
  657. (db-port
  658. (number 5432)
  659. "Database port.")
  660. (log-type
  661. (string "")
  662. "Specifies where log messages are written to:
  663. @itemize
  664. @item @code{system} - syslog.
  665. @item @code{file} - file specified with @code{log-file} parameter.
  666. @item @code{console} - standard output.
  667. @end itemize\n")
  668. (log-file
  669. (string "/var/log/zabbix/server.log")
  670. "Log file name for @code{log-type} @code{file} parameter.")
  671. (pid-file
  672. (string "/var/run/zabbix/zabbix_server.pid")
  673. "Name of PID file.")
  674. (ssl-ca-location
  675. (string "/etc/ssl/certs/ca-certificates.crt")
  676. "The location of certificate authority (CA) files for SSL server
  677. certificate verification.")
  678. (ssl-cert-location
  679. (string "/etc/ssl/certs")
  680. "Location of SSL client certificates.")
  681. (extra-options
  682. (extra-options "")
  683. "Extra options will be appended to Zabbix server configuration file.")
  684. (include-files
  685. (include-files '())
  686. "You may include individual files or all files in a directory in the
  687. configuration file."))
  688. (define (zabbix-server-account config)
  689. "Return the user accounts and user groups for CONFIG."
  690. (let ((zabbix-user (zabbix-server-configuration-user config))
  691. (zabbix-group (zabbix-server-configuration-group config)))
  692. (list (user-group (name zabbix-group) (system? #t))
  693. (user-account
  694. (name zabbix-user)
  695. (system? #t)
  696. (group zabbix-group)
  697. (comment "zabbix privilege separation user")
  698. (home-directory (string-append "/var/run/" zabbix-user))
  699. (shell (file-append shadow "/sbin/nologin"))))))
  700. (define (zabbix-server-config-file config)
  701. "Return the zabbix-server configuration file corresponding to CONFIG."
  702. (computed-file
  703. "zabbix_server.conf"
  704. #~(begin
  705. (call-with-output-file #$output
  706. (lambda (port)
  707. (format port "# Generated by 'zabbix-server-service'.~%")
  708. (format port #$(serialize-configuration
  709. config zabbix-server-configuration-fields)))))))
  710. (define (zabbix-server-activation config)
  711. "Return the activation gexp for CONFIG."
  712. (with-imported-modules '((guix build utils))
  713. #~(begin
  714. (use-modules (guix build utils)
  715. (ice-9 rdelim))
  716. (let ((user (getpw #$(zabbix-server-configuration-user config))))
  717. (for-each (lambda (file)
  718. (let ((directory (dirname file)))
  719. (mkdir-p directory)
  720. (chown directory (passwd:uid user) (passwd:gid user))
  721. (chmod directory #o755)))
  722. (list #$(zabbix-server-configuration-log-file config)
  723. #$(zabbix-server-configuration-pid-file config)
  724. "/etc/zabbix/maintenance.inc.php"))))))
  725. (define (zabbix-server-runtime-control-procedure zabbix-server config command)
  726. ;; XXX: This is duplicated from mcron; factorize.
  727. #~(lambda (_ . args)
  728. ;; Run 'zabbix_server' in a pipe so we can explicitly redirect its output
  729. ;; to 'current-output-port', which at this stage is bound to the client
  730. ;; connection.
  731. (let ((pipe (apply open-pipe* OPEN_READ #$zabbix-server
  732. "--config" #$config
  733. "-R" #$command args)))
  734. (let loop ()
  735. (match (read-line pipe 'concat)
  736. ((? eof-object?)
  737. (catch 'system-error
  738. (lambda ()
  739. (zero? (close-pipe pipe)))
  740. (lambda args
  741. ;; There's a race with the SIGCHLD handler, which could
  742. ;; call 'waitpid' before 'close-pipe' above does. If we
  743. ;; get ECHILD, that means we lost the race; in that case, we
  744. ;; cannot tell what the exit code was (FIXME).
  745. (or (= ECHILD (system-error-errno args))
  746. (apply throw args)))))
  747. (line
  748. (display line)
  749. (loop)))))))
  750. ;; Provide shepherd actions for common "zabbix_server -R" commands
  751. ;; mainly for a convenient way to use the correct configuration file.
  752. (define (zabbix-server-actions zabbix-server config)
  753. (list (shepherd-action
  754. (name 'reload-config-cache)
  755. (documentation "Reload the configuration cache.")
  756. (procedure (zabbix-server-runtime-control-procedure
  757. zabbix-server config "config_cache_reload")))
  758. (shepherd-action
  759. (name 'reload-snmp-cache)
  760. (documentation "Reload SNMP cache.")
  761. (procedure (zabbix-server-runtime-control-procedure
  762. zabbix-server config "snmp_cache_reload")))))
  763. (define (zabbix-server-shepherd-service config)
  764. "Return a <shepherd-service> for Zabbix server with CONFIG."
  765. (let ((zabbix-server
  766. (file-append (zabbix-server-configuration-zabbix-server config)
  767. "/sbin/zabbix_server"))
  768. (config-file (zabbix-server-config-file config)))
  769. (list (shepherd-service
  770. (provision '(zabbix-server))
  771. (requirement '(user-processes))
  772. (documentation "Run the Zabbix server daemon.")
  773. (actions (zabbix-server-actions zabbix-server config-file))
  774. (start #~(make-forkexec-constructor
  775. (list #$zabbix-server
  776. "--config" #$config-file
  777. "--foreground")
  778. #:user #$(zabbix-server-configuration-user config)
  779. #:group #$(zabbix-server-configuration-group config)
  780. #:pid-file #$(zabbix-server-configuration-pid-file config)
  781. #:environment-variables
  782. (list "SSL_CERT_DIR=/run/current-system/profile\
  783. /etc/ssl/certs"
  784. "SSL_CERT_FILE=/run/current-system/profile\
  785. /etc/ssl/certs/ca-certificates.crt")))
  786. (stop #~(make-kill-destructor
  787. ;; The server needs to finish database work on shutdown
  788. ;; which can take a while for big or busy databases.
  789. #:grace-period 60))))))
  790. (define zabbix-server-service-type
  791. (service-type
  792. (name 'zabbix-server)
  793. (extensions
  794. (list (service-extension shepherd-root-service-type
  795. zabbix-server-shepherd-service)
  796. (service-extension account-service-type
  797. zabbix-server-account)
  798. (service-extension activation-service-type
  799. zabbix-server-activation)))
  800. (default-value (zabbix-server-configuration))
  801. (description "Run the Zabbix server, a high-performance monitoring system
  802. that can collect data about machines from a variety of sources and provide the
  803. results in a Web interface.")))
  804. (define (generate-zabbix-server-documentation)
  805. (generate-documentation
  806. `((zabbix-server-configuration
  807. ,zabbix-server-configuration-fields))
  808. 'zabbix-server-configuration))
  809. (define-configuration zabbix-agent-configuration
  810. (zabbix-agent
  811. (file-like zabbix-agentd)
  812. "The zabbix-agent package.")
  813. (user
  814. (string "zabbix")
  815. "User who will run the Zabbix agent.")
  816. (group
  817. (string "zabbix")
  818. "Group who will run the Zabbix agent.")
  819. (hostname
  820. (string "")
  821. "Unique, case sensitive hostname which is required for active checks and
  822. must match hostname as configured on the server.")
  823. (log-type
  824. (string "")
  825. "Specifies where log messages are written to:
  826. @itemize
  827. @item @code{system} - syslog.
  828. @item @code{file} - file specified with @code{log-file} parameter.
  829. @item @code{console} - standard output.
  830. @end itemize\n")
  831. (log-file
  832. (string "/var/log/zabbix/agent.log")
  833. "Log file name for @code{log-type} @code{file} parameter.")
  834. (pid-file
  835. (string "/var/run/zabbix/zabbix_agent.pid")
  836. "Name of PID file.")
  837. (server
  838. (list '("127.0.0.1"))
  839. "List of IP addresses, optionally in CIDR notation, or hostnames of Zabbix
  840. servers and Zabbix proxies. Incoming connections will be accepted only from
  841. the hosts listed here.")
  842. (server-active
  843. (list '("127.0.0.1"))
  844. "List of IP:port (or hostname:port) pairs of Zabbix servers and Zabbix
  845. proxies for active checks. If port is not specified, default port is used.
  846. If this parameter is not specified, active checks are disabled.")
  847. (extra-options
  848. (extra-options "")
  849. "Extra options will be appended to Zabbix server configuration file.")
  850. (include-files
  851. (include-files '())
  852. "You may include individual files or all files in a directory in the
  853. configuration file."))
  854. (define (zabbix-agent-account config)
  855. "Return the user accounts and user groups for CONFIG."
  856. (let ((zabbix-user (zabbix-agent-configuration-user config))
  857. (zabbix-group (zabbix-agent-configuration-group config)))
  858. (list (user-group (name zabbix-group) (system? #t))
  859. (user-account
  860. (name zabbix-user)
  861. (system? #t)
  862. (group zabbix-group)
  863. (comment "zabbix privilege separation user")
  864. (home-directory (string-append "/var/run/" zabbix-user))
  865. (shell (file-append shadow "/sbin/nologin"))))))
  866. (define (zabbix-agent-activation config)
  867. "Return the activation gexp for CONFIG."
  868. (with-imported-modules '((guix build utils))
  869. #~(begin
  870. (use-modules (guix build utils)
  871. (ice-9 rdelim))
  872. (let ((user
  873. (getpw #$(zabbix-agent-configuration-user config))))
  874. (for-each (lambda (file)
  875. (let ((directory (dirname file)))
  876. (mkdir-p directory)
  877. (chown directory (passwd:uid user) (passwd:gid user))
  878. (chmod directory #o755)))
  879. (list #$(zabbix-agent-configuration-log-file config)
  880. #$(zabbix-agent-configuration-pid-file config)))))))
  881. (define (zabbix-agent-config-file config)
  882. "Return the zabbix-agent configuration file corresponding to CONFIG."
  883. (computed-file
  884. "zabbix_agent.conf"
  885. #~(begin
  886. (call-with-output-file #$output
  887. (lambda (port)
  888. (format port "# Generated by 'zabbix-agent-service'.~%")
  889. (format port #$(serialize-configuration
  890. config zabbix-agent-configuration-fields)))))))
  891. (define (zabbix-agent-arguments config)
  892. #~(let* ((config-file #$(zabbix-agent-config-file config))
  893. (agent #$(zabbix-agent-configuration-zabbix-agent config))
  894. (agent2? (file-exists? (string-append agent "/sbin/zabbix_agent2"))))
  895. (if agent2?
  896. (list (string-append agent "/sbin/zabbix_agent2")
  897. "-config" config-file
  898. "-foreground")
  899. (list (string-append agent "/sbin/zabbix_agentd")
  900. "--config" config-file
  901. "--foreground"))))
  902. (define (zabbix-agent-shepherd-service config)
  903. "Return a <shepherd-service> for Zabbix agent with CONFIG."
  904. (list (shepherd-service
  905. (provision '(zabbix-agent))
  906. (requirement '(user-processes))
  907. (documentation "Run Zabbix agent daemon.")
  908. (start #~(make-forkexec-constructor
  909. #$(zabbix-agent-arguments config)
  910. #:user #$(zabbix-agent-configuration-user config)
  911. #:group #$(zabbix-agent-configuration-group config)
  912. #:pid-file #$(zabbix-agent-configuration-pid-file config)
  913. #:environment-variables
  914. (list "SSL_CERT_DIR=/run/current-system/profile\
  915. /etc/ssl/certs"
  916. "SSL_CERT_FILE=/run/current-system/profile\
  917. /etc/ssl/certs/ca-certificates.crt"
  918. "PATH=/run/setuid-programs:\
  919. /run/current-system/profile/bin:/run/current-system/profile/sbin")))
  920. (stop #~(make-kill-destructor)))))
  921. (define zabbix-agent-service-type
  922. (service-type
  923. (name 'zabbix-agent)
  924. (extensions
  925. (list (service-extension shepherd-root-service-type
  926. zabbix-agent-shepherd-service)
  927. (service-extension account-service-type
  928. zabbix-agent-account)
  929. (service-extension activation-service-type
  930. zabbix-agent-activation)))
  931. (default-value (zabbix-agent-configuration))
  932. (description "Run the Zabbix agent, @command{zabbix_agentd}, which gathers
  933. information about the running system for the Zabbix monitoring server.")))
  934. (define (generate-zabbix-agent-documentation)
  935. (generate-documentation
  936. `((zabbix-agent-configuration
  937. ,zabbix-agent-configuration-fields))
  938. 'zabbix-agent-configuration))
  939. (define %zabbix-front-end-configuration-nginx
  940. (nginx-server-configuration
  941. (root #~(string-append #$zabbix-server:front-end "/share/zabbix/php"))
  942. (index '("index.php"))
  943. (locations
  944. (let ((php-location (nginx-php-location)))
  945. (list (nginx-location-configuration
  946. (inherit php-location)
  947. (body (append (nginx-location-configuration-body php-location)
  948. (list "
  949. fastcgi_param PHP_VALUE \"post_max_size = 16M
  950. max_execution_time = 300\";
  951. ")))))))
  952. (listen '("80"))))
  953. (define (zabbix-front-end-nginx-extension config)
  954. (match config
  955. (($ <zabbix-front-end-configuration> server nginx)
  956. (if (null? nginx)
  957. (list
  958. (nginx-server-configuration
  959. (inherit %zabbix-front-end-configuration-nginx)
  960. (root #~(string-append #$server:front-end "/share/zabbix/php"))))
  961. nginx))))
  962. (define-configuration zabbix-front-end-configuration
  963. (zabbix-server
  964. (file-like zabbix-server)
  965. "The Zabbix server package to use.")
  966. (nginx
  967. (list '())
  968. "List of @ref{nginx-server-configuration,@code{nginx-server-configuration}}
  969. blocks for the Zabbix front-end. When empty, a default that listens on port 80
  970. is used.")
  971. (db-host
  972. (string "localhost")
  973. "Database host name.")
  974. (db-port
  975. (number 5432)
  976. "Database port.")
  977. (db-name
  978. (string "zabbix")
  979. "Database name.")
  980. (db-user
  981. (string "zabbix")
  982. "Database user.")
  983. (db-password
  984. (string "")
  985. "Database password. Please, use @code{db-secret-file} instead.")
  986. (db-secret-file
  987. (string "")
  988. "Secret file which will be appended to @file{zabbix.conf.php} file. This
  989. file contains credentials for use by Zabbix front-end. You are expected to
  990. create it manually.")
  991. (zabbix-host
  992. (string "localhost")
  993. "Zabbix server hostname.")
  994. (zabbix-port
  995. (number 10051)
  996. "Zabbix server port."))
  997. (define (zabbix-front-end-config config)
  998. (match-record config <zabbix-front-end-configuration>
  999. (db-host db-port db-name db-user db-password db-secret-file
  1000. zabbix-host zabbix-port %location)
  1001. (mixed-text-file "zabbix.conf.php"
  1002. "\
  1003. <?php
  1004. // Zabbix GUI configuration file.
  1005. global $DB;
  1006. $DB['TYPE'] = 'POSTGRESQL';
  1007. $DB['SERVER'] = '" db-host "';
  1008. $DB['PORT'] = '" (number->string db-port) "';
  1009. $DB['DATABASE'] = '" db-name "';
  1010. $DB['USER'] = '" db-user "';
  1011. $DB['PASSWORD'] = " (let ((file (location-file %location))
  1012. (line (location-line %location))
  1013. (column (location-column %location)))
  1014. (if (string-null? db-password)
  1015. (if (string-null? db-secret-file)
  1016. (raise (make-compound-condition
  1017. (condition
  1018. (&message
  1019. (message
  1020. (format #f "no '~A' or '~A' field in your '~A' record"
  1021. 'db-secret-file 'db-password
  1022. 'zabbix-front-end-configuration))))
  1023. (condition
  1024. (&error-location
  1025. (location %location)))))
  1026. (string-append "trim(file_get_contents('"
  1027. db-secret-file "'));\n"))
  1028. (begin
  1029. (display-hint (G_ "~a:~a:~a: ~a:
  1030. Consider using @code{db-secret-file} instead of @code{db-password} for better
  1031. security.")
  1032. file line column
  1033. 'zabbix-front-end-configuration)
  1034. (format #f "'~a';~%" db-password))))
  1035. "
  1036. // Schema name. Used for IBM DB2 and PostgreSQL.
  1037. $DB['SCHEMA'] = '';
  1038. // Use IEEE754 compatible value range for 64-bit Numeric (float) history values.
  1039. $DB['DOUBLE_IEEE754'] = true;
  1040. $ZBX_SERVER = '" zabbix-host "';
  1041. $ZBX_SERVER_PORT = '" (number->string zabbix-port) "';
  1042. $ZBX_SERVER_NAME = '';
  1043. $IMAGE_FORMAT_DEFAULT = IMAGE_FORMAT_PNG;
  1044. ")))
  1045. (define %maintenance.inc.php
  1046. ;; Empty php file to allow us move zabbix-frontend configs to ‘/etc/zabbix’
  1047. ;; directory. See ‘install-front-end’ phase in
  1048. ;; (@ (gnu packages monitoring) zabbix-server) package.
  1049. "\
  1050. <?php
  1051. ")
  1052. (define (zabbix-front-end-activation config)
  1053. "Return the activation gexp for CONFIG."
  1054. #~(begin
  1055. (use-modules (guix build utils))
  1056. (mkdir-p "/etc/zabbix")
  1057. (call-with-output-file "/etc/zabbix/maintenance.inc.php"
  1058. (lambda (port)
  1059. (display #$%maintenance.inc.php port)))
  1060. (copy-file #$(zabbix-front-end-config config)
  1061. "/etc/zabbix/zabbix.conf.php")))
  1062. (define zabbix-front-end-service-type
  1063. (service-type
  1064. (name 'zabbix-front-end)
  1065. (extensions
  1066. (list (service-extension activation-service-type
  1067. zabbix-front-end-activation)
  1068. (service-extension nginx-service-type
  1069. zabbix-front-end-nginx-extension)
  1070. ;; Make sure php-fpm is instantiated.
  1071. (service-extension php-fpm-service-type
  1072. (const #t))))
  1073. (default-value (zabbix-front-end-configuration))
  1074. (description
  1075. "Run the zabbix-front-end web interface, which allows users to interact
  1076. with Zabbix server.")))
  1077. (define (generate-zabbix-front-end-documentation)
  1078. (generate-documentation
  1079. `((zabbix-front-end-configuration
  1080. ,zabbix-front-end-configuration-fields))
  1081. 'zabbix-front-end-configuration))