services: rottlog: Improve default weekly rotations.
[jackhill/guix/guix.git] / gnu / services / admin.scm
CommitLineData
92c03a87
JN
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
4d67ed70 3;;; Copyright © 2016 Ludovic Courtès <ludo@gnu.org>
92c03a87
JN
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 thye GNU General Public License
18;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
20(define-module (gnu services admin)
21 #:use-module (gnu packages admin)
22 #:use-module (gnu packages base)
23 #:use-module (gnu services)
24 #:use-module (gnu services mcron)
25 #:use-module (gnu services shepherd)
26 #:use-module (guix gexp)
27 #:use-module (guix packages)
28 #:use-module (guix records)
29 #:use-module (srfi srfi-1)
30 #:export (%default-rotations
31 %rotated-files
32 rottlog-configuration
33 rottlog-configuration?
34 rottlog-service
35 rottlog-service-type))
36
37;;; Commentary:
38;;;
39;;; This module implements configuration of rottlog by writing
40;;; /etc/rottlog/{rc,hourly|daily|weekly}. Example usage
41;;;
42;;; (mcron-service)
43;;; (service rottlog-service-type (rottlog-configuration))
44;;;
45;;; Code:
46
47(define %rotated-files
4d67ed70
LC
48 ;; Syslog files subject to rotation.
49 '("/var/log/messages" "/var/log/secure" "/var/log/maillog"))
92c03a87 50
4d67ed70
LC
51(define (syslog-rotation-config files)
52 #~(string-append #$(string-join files ",")
53 " {
92c03a87
JN
54 sharedscripts
55 postrotate
56 " #$coreutils "/bin/kill -HUP $(cat /var/run/syslog.pid) 2> /dev/null
57 endscript
58}
59"))
60
61(define (simple-rotation-config file)
62 (string-append file " {
63 sharedscripts
92c03a87
JN
64}
65"))
66
67(define %default-rotations
68 `(("weekly"
69 ,(computed-file "rottlog.weekly"
70 #~(call-with-output-file #$output
71 (lambda (port)
4d67ed70
LC
72 (display #$(syslog-rotation-config %rotated-files)
73 port)
92c03a87
JN
74 (display #$(simple-rotation-config
75 "/var/log/shepherd.log")
76 port)))))))
77
78(define (default-jobs rottlog)
79 (list #~(job '(next-hour '(0)) ;midnight
80 (lambda ()
81 (system* #$(file-append rottlog "/sbin/rottlog"))))
82 #~(job '(next-hour '(12)) ;noon
83 (lambda ()
84 (system* #$(file-append rottlog "/sbin/rottlog"))))))
85
86(define-record-type* <rottlog-configuration>
87 rottlog-configuration make-rottlog-configuration
88 rottlog-configuration?
89 (rottlog rottlog-rottlog ;package
90 (default rottlog))
91 (rc-file rottlog-rc-file ;file-like
92 (default (file-append rottlog "/etc/rc")))
93 (periodic-rotations rottlog-periodic-rotations ;list of (name file) tuples
94 (default %default-rotations))
95 (jobs rottlog-jobs ;list of <mcron-job>
96 (default #f)))
97
98(define (rottlog-etc config)
99 `(("rottlog" ,(file-union "rottlog"
100 (cons `("rc" ,(rottlog-rc-file config))
101 (rottlog-periodic-rotations config))))))
102
103(define (rottlog-jobs-or-default config)
104 (or (rottlog-jobs config)
105 (default-jobs (rottlog-rottlog config))))
106
107(define rottlog-service-type
108 (service-type
109 (name 'rottlog)
110 (extensions (list (service-extension etc-service-type rottlog-etc)
111 (service-extension mcron-service-type
112 rottlog-jobs-or-default)))))
113
114;;; admin.scm ends here