1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
3 ;;; Copyright © 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
5 ;;; This file is part of GNU Guix.
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.
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.
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/>.
20 (define-module (gnu services admin)
21 #:use-module (gnu packages admin)
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
35 log-rotation-frequency
38 log-rotation-post-rotate
41 rottlog-configuration?
43 rottlog-service-type))
47 ;;; This module implements configuration of rottlog by writing
48 ;;; /etc/rottlog/{rc,hourly|daily|weekly}. Example usage
51 ;;; (service rottlog-service-type)
55 (define-record-type* <log-rotation> log-rotation make-log-rotation
57 (files log-rotation-files) ;list of strings
58 (frequency log-rotation-frequency ;symbol
60 (post-rotate log-rotation-post-rotate ;#f | gexp
62 (options log-rotation-options ;list of strings
65 (define %rotated-files
66 ;; Syslog files subject to rotation.
67 '("/var/log/messages" "/var/log/secure" "/var/log/debug"
70 (define %default-rotations
71 (list (log-rotation ;syslog files
72 (files %rotated-files)
74 ;; Restart syslogd after rotation.
75 (options '("sharedscripts"))
76 (post-rotate #~(let ((pid (call-with-input-file "/var/run/syslog.pid"
80 (files '("/var/log/guix-daemon.log")))))
82 (define (log-rotation->config rotation)
83 "Return a string-valued gexp representing the rottlog configuration snippet
86 (let ((post (log-rotation-post-rotate rotation)))
88 (program-file "rottlog-post-rotate.scm" post))))
90 #~(let ((post #$post-rotate))
91 (string-append (string-join '#$(log-rotation-files rotation) ",")
93 #$(string-join (log-rotation-options rotation)
96 (string-append "\n postrotate\n " post
101 (define (log-rotations->/etc-entries rotations)
102 "Return the list of /etc entries for ROTATIONS, a list of <log-rotation>."
103 (define (frequency-file frequency rotations)
104 (computed-file (string-append "rottlog." (symbol->string frequency))
105 #~(call-with-output-file #$output
107 (for-each (lambda (str)
109 (list #$@(map log-rotation->config
112 (let* ((frequencies (delete-duplicates
113 (map log-rotation-frequency rotations)))
114 (table (fold (lambda (rotation table)
115 (vhash-consq (log-rotation-frequency rotation)
119 (map (lambda (frequency)
120 `(,(symbol->string frequency)
121 ,(frequency-file frequency
122 (vhash-foldq* cons '() frequency table))))
125 (define (default-jobs rottlog)
126 (list #~(job '(next-hour '(0)) ;midnight
127 #$(file-append rottlog "/sbin/rottlog"))
128 #~(job '(next-hour '(12)) ;noon
129 #$(file-append rottlog "/sbin/rottlog"))))
131 (define-record-type* <rottlog-configuration>
132 rottlog-configuration make-rottlog-configuration
133 rottlog-configuration?
134 (rottlog rottlog-rottlog ;package
136 (rc-file rottlog-rc-file ;file-like
137 (default (file-append rottlog "/etc/rc")))
138 (rotations rottlog-rotations ;list of <log-rotation>
139 (default %default-rotations))
140 (jobs rottlog-jobs ;list of <mcron-job>
143 (define (rottlog-etc config)
145 ,(file-union "rottlog"
146 (cons `("rc" ,(rottlog-rc-file config))
147 (log-rotations->/etc-entries
148 (rottlog-rotations config)))))))
150 (define (rottlog-jobs-or-default config)
151 (or (rottlog-jobs config)
152 (default-jobs (rottlog-rottlog config))))
154 (define rottlog-service-type
158 "Periodically rotate log files using GNU@tie{}Rottlog and GNU@tie{}mcron.
159 Old log files are removed or compressed according to the configuration.")
160 (extensions (list (service-extension etc-service-type rottlog-etc)
161 (service-extension mcron-service-type
162 rottlog-jobs-or-default)
164 ;; Add Rottlog to the global profile so users can access
165 ;; the documentation.
166 (service-extension profile-service-type
167 (compose list rottlog-rottlog))))
168 (compose concatenate)
169 (extend (lambda (config rotations)
170 (rottlog-configuration
172 (rotations (append (rottlog-rotations config)
174 (default-value (rottlog-configuration))))
176 ;;; admin.scm ends here