admin.scm 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
  3. ;;; Copyright © 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (gnu services admin)
  20. #:use-module (gnu packages admin)
  21. #:use-module (gnu packages base)
  22. #:use-module (gnu services)
  23. #:use-module (gnu services mcron)
  24. #:use-module (gnu services shepherd)
  25. #:use-module (guix gexp)
  26. #:use-module (guix packages)
  27. #:use-module (guix records)
  28. #:use-module (srfi srfi-1)
  29. #:use-module (ice-9 vlist)
  30. #:export (%default-rotations
  31. %rotated-files
  32. log-rotation
  33. log-rotation?
  34. log-rotation-frequency
  35. log-rotation-files
  36. log-rotation-options
  37. log-rotation-post-rotate
  38. rottlog-configuration
  39. rottlog-configuration?
  40. rottlog-service
  41. rottlog-service-type))
  42. ;;; Commentary:
  43. ;;;
  44. ;;; This module implements configuration of rottlog by writing
  45. ;;; /etc/rottlog/{rc,hourly|daily|weekly}. Example usage
  46. ;;;
  47. ;;; (mcron-service)
  48. ;;; (service rottlog-service-type)
  49. ;;;
  50. ;;; Code:
  51. (define-record-type* <log-rotation> log-rotation make-log-rotation
  52. log-rotation?
  53. (files log-rotation-files) ;list of strings
  54. (frequency log-rotation-frequency ;symbol
  55. (default 'weekly))
  56. (post-rotate log-rotation-post-rotate ;#f | gexp
  57. (default #f))
  58. (options log-rotation-options ;list of strings
  59. (default '())))
  60. (define %rotated-files
  61. ;; Syslog files subject to rotation.
  62. '("/var/log/messages" "/var/log/secure" "/var/log/debug"
  63. "/var/log/maillog"))
  64. (define %default-rotations
  65. (list (log-rotation ;syslog files
  66. (files %rotated-files)
  67. ;; Restart syslogd after rotation.
  68. (options '("sharedscripts"))
  69. (post-rotate #~(let ((pid (call-with-input-file "/var/run/syslog.pid"
  70. read)))
  71. (kill pid SIGHUP))))
  72. (log-rotation
  73. (files '("/var/log/guix-daemon.log")))))
  74. (define (log-rotation->config rotation)
  75. "Return a string-valued gexp representing the rottlog configuration snippet
  76. for ROTATION."
  77. (define post-rotate
  78. (let ((post (log-rotation-post-rotate rotation)))
  79. (and post
  80. (program-file "rottlog-post-rotate.scm" post))))
  81. #~(let ((post #$post-rotate))
  82. (string-append (string-join '#$(log-rotation-files rotation) ",")
  83. " {"
  84. #$(string-join (log-rotation-options rotation)
  85. "\n " 'prefix)
  86. (if post
  87. (string-append "\n postrotate\n " post
  88. "\n endscript\n")
  89. "")
  90. "\n}\n")))
  91. (define (log-rotations->/etc-entries rotations)
  92. "Return the list of /etc entries for ROTATIONS, a list of <log-rotation>."
  93. (define (frequency-file frequency rotations)
  94. (computed-file (string-append "rottlog." (symbol->string frequency))
  95. #~(call-with-output-file #$output
  96. (lambda (port)
  97. (for-each (lambda (str)
  98. (display str port))
  99. (list #$@(map log-rotation->config
  100. rotations)))))))
  101. (let* ((frequencies (delete-duplicates
  102. (map log-rotation-frequency rotations)))
  103. (table (fold (lambda (rotation table)
  104. (vhash-consq (log-rotation-frequency rotation)
  105. rotation table))
  106. vlist-null
  107. rotations)))
  108. (map (lambda (frequency)
  109. `(,(symbol->string frequency)
  110. ,(frequency-file frequency
  111. (vhash-foldq* cons '() frequency table))))
  112. frequencies)))
  113. (define (default-jobs rottlog)
  114. (list #~(job '(next-hour '(0)) ;midnight
  115. #$(file-append rottlog "/sbin/rottlog"))
  116. #~(job '(next-hour '(12)) ;noon
  117. #$(file-append rottlog "/sbin/rottlog"))))
  118. (define-record-type* <rottlog-configuration>
  119. rottlog-configuration make-rottlog-configuration
  120. rottlog-configuration?
  121. (rottlog rottlog-rottlog ;package
  122. (default rottlog))
  123. (rc-file rottlog-rc-file ;file-like
  124. (default (file-append rottlog "/etc/rc")))
  125. (rotations rottlog-rotations ;list of <log-rotation>
  126. (default %default-rotations))
  127. (jobs rottlog-jobs ;list of <mcron-job>
  128. (default #f)))
  129. (define (rottlog-etc config)
  130. `(("rottlog"
  131. ,(file-union "rottlog"
  132. (cons `("rc" ,(rottlog-rc-file config))
  133. (log-rotations->/etc-entries
  134. (rottlog-rotations config)))))))
  135. (define (rottlog-jobs-or-default config)
  136. (or (rottlog-jobs config)
  137. (default-jobs (rottlog-rottlog config))))
  138. (define rottlog-service-type
  139. (service-type
  140. (name 'rottlog)
  141. (description
  142. "Periodically rotate log files using GNU@tie{}Rottlog and GNU@tie{}mcron.
  143. Old log files are removed or compressed according to the configuration.")
  144. (extensions (list (service-extension etc-service-type rottlog-etc)
  145. (service-extension mcron-service-type
  146. rottlog-jobs-or-default)
  147. ;; Add Rottlog to the global profile so users can access
  148. ;; the documentation.
  149. (service-extension profile-service-type
  150. (compose list rottlog-rottlog))))
  151. (compose concatenate)
  152. (extend (lambda (config rotations)
  153. (rottlog-configuration
  154. (inherit config)
  155. (rotations (append (rottlog-rotations config)
  156. rotations)))))
  157. (default-value (rottlog-configuration))))
  158. ;;; admin.scm ends here