Patchwork gnu: rottlog: rotate messages daily.

login
register
mail settings
Submitter Jan Nieuwenhuizen
Date Sept. 14, 2016, 6:30 p.m.
Message ID <874m5itjv5.fsf@gnu.org>
Download mbox | patch
Permalink /patch/15638/
State New
Headers show

Comments

Jan Nieuwenhuizen - Sept. 14, 2016, 6:30 p.m.
Ludovic Courtès writes:

> I would add /var/log/{secure,shepherd.log}, but weekly is probably
> enough.

Ok.  I implemented this by changing the rottlog package that already
modifies etc/weekly.  I'm not sure if sending SIGHUP to syslog is
ok for shepherd.log or that would need to be a kill 1?  See attached.

>> * gnu/services/admin.scm: New file.
>> * gnu/local.mk (GNU_SYSTEM_MODULES): Add it.
>
> I think we’re pretty much there!

Silly me, asking if we already ad log rotation ;-)

>> +  (rc-file rottlog-rc-file)
>                              ^
> With: (default (file-append rottlog "/etc/rc")).

OK.

>> +  (periods rottlog-periods)
>
> Maybe s/periods/periodic-rotations/ ?
> Also with default rotation.

Sure, done.

>> +  (jobs rottlog-jobs))
>
> We should have a sane default here, like running it twice a day (which
> can’t hurt because “The logfiles cannot be modified multiple times per
> period”, says the manual.)

OK, great!

> It’s a good idea to mark the expected types of each field in the margin
> (until we have a type system ;-)).

Ah yes.  Done.

> You can remove it and use ‘file-union’ from (gnu services), which is
> roughly the same (it expects a list of tuples instead of a list of
> pairs.)

Ok (I hope...it works what I have, but the apparent need I found for
having "rottlog" twice confuses me).

>> +(define* (rottlog-service
>
> This can be omitted.  It’s enough to expose ‘rottlog-service-type’ and
> ‘rottlog-configuration’.

Hmm.  I removed all the defaulting here and then removed the function
alltogether, but then I cannot seem to do

  (services (cons* (mcron-service) (rottlog-service) %base-services)))

anymore.  Is that right?

I'm feeling a bit stubborn keeping this but I thought that's what we
want and my test showed me that this is needed.  So I kept a minimal of
this function.  Please just remove+enlighten me how to use it if this
can really go :-)

> The last thing that’s missing is a “Log Rotation” section in guix.texi,
> with cross-references to the rottlog manual, like “Scheduled Job
> Execution” does.  Could you look into it?

Added.

Greetings,
Jan
Ludovic Courtès - Sept. 20, 2016, 5:15 a.m.
Hello Jan!

Jan Nieuwenhuizen <janneke@gnu.org> skribis:

> Ludovic Courtès writes:
>
>> I would add /var/log/{secure,shepherd.log}, but weekly is probably
>> enough.
>
> Ok.  I implemented this by changing the rottlog package that already
> modifies etc/weekly.

The current ‘tweak-rc-weekly’ phase “fixes a bug” in etc/weekly, but I
don’t think we should do customization in the package itself.  Instead,
it would be more appropriate to generate/provide the configuration files
that we need, without further modifying the package itself.  WDYT?

> I'm not sure if sending SIGHUP to syslog is ok for shepherd.log or
> that would need to be a kill 1?  See attached.

GuixSD runs syslogd from Inetutils, so it’s not PID 1, fortunately.  ;-)

However, shepherd.log is written by PID_1, indeed, and we shouldn’t send
SIGHUP to PID 1.  However, I don’t think anything bad will happen if
rottlog moves shepherd.log and Shepherd doesn’t reopen this file.

>>> +(define* (rottlog-service
>>
>> This can be omitted.  It’s enough to expose ‘rottlog-service-type’ and
>> ‘rottlog-configuration’.
>
> Hmm.  I removed all the defaulting here and then removed the function
> alltogether, but then I cannot seem to do
>
>   (services (cons* (mcron-service) (rottlog-service) %base-services)))
>
> anymore.  Is that right?

Yes.  Instead, you have to write:

  (services (cons* (service rottlog-service-type
                            (rottlog-configuration …))
                   …))

> I'm feeling a bit stubborn keeping this but I thought that's what we
> want and my test showed me that this is needed.  So I kept a minimal of
> this function.  Please just remove+enlighten me how to use it if this
> can really go :-)

I hope the above sheds some light on this.  WDYT of this idea?

> From e8e489db62337c6e8ef03e745a56938566e078c8 Mon Sep 17 00:00:00 2001
> From: Jan Nieuwenhuizen <janneke@gnu.org>
> Date: Wed, 14 Sep 2016 19:04:38 +0200
> Subject: [PATCH 1/2] gnu: update rottlog: install guix-specific etc/weekly.
>
> * gnu/packages/admin.scm (rottlog): Install guix-specific etc/weekly
> for rotating /var/log/{messages,secureshepherd.log}.

As discussed above, I think this is the wrong place for this.

> From 59213cce5d6d4e41f9b6f321e3fef056cccc7c22 Mon Sep 17 00:00:00 2001
> From: Jan Nieuwenhuizen <janneke@gnu.org>
> Date: Thu, 8 Sep 2016 01:20:43 +0200
> Subject: [PATCH 2/2] gnu: services: add rottlog.
>
> * gnu/services/admin.scm: New file.
> * gnu/local.mk (GNU_SYSTEM_MODULES): Add it.
> * doc/guix.texi (Log Rotation): Document it.

[...]

> +  (periodic-rotations rottlog-periodic-rotations ; list of (name file) tuples
> +                      (default `(("weekly"
> +                                  ,(file-append rottlog "/etc/weekly")))))

Here I would suggest adding somewhere:


  (define %rotated-files
    '("/var/log/messages" …))

  (define (syslog-rotation-config file)
    (string-append file " {
    …
    kill -HUP …
    …")))

  (define (simple-rotation-config file)
    ;; Same as above, but without “kill -HUP”.
    …)

  (define %default-rotations
    `(("weekly" . ,(plain-file "rottlog.weekly"
                               (string-append (string-join
                                               (map syslog-rotation-config
                                                    '("/var/log/messages"…)))
                                              (simple-rotation-config
                                               "shepherd.log"))))))

Does that make sense?

> +  (jobs rottlog-jobs                             ; list of <mcron-job>
> +        (default
> +          (list #~(job
> +                   '(next-hour '(0))
> +                   (lambda ()
> +                     (system (string-append #$rottlog "/sbin/rottlog"))))
> +                #~(job
> +                   '(next-hour '(12))
> +                   (lambda ()
> +                     (system (string-append #$rottlog "/sbin/rottlog"))))))))

Please move (list …) to a global variable, to avoid code duplication
when the macro is expanded.

Use ‘system*’ instead of ‘system’ (the latter does “/bin/sh -c”, which
is unnecessary here.)

Also, this should use the ‘rottlog’ package of <rottlog-configuration>,
which cannot be referred to from the default value, which is a constant.
Thus, you may have to change the default to #f, and generate the default
value upon #f.

TIA!

Ludo’.

Patch

From 59213cce5d6d4e41f9b6f321e3fef056cccc7c22 Mon Sep 17 00:00:00 2001
From: Jan Nieuwenhuizen <janneke@gnu.org>
Date: Thu, 8 Sep 2016 01:20:43 +0200
Subject: [PATCH 2/2] gnu: services: add rottlog.

* gnu/services/admin.scm: New file.
* gnu/local.mk (GNU_SYSTEM_MODULES): Add it.
* doc/guix.texi (Log Rotation): Document it.
---
 doc/guix.texi          | 69 +++++++++++++++++++++++++++++++++++++++++++-
 gnu/local.mk           |  1 +
 gnu/services/admin.scm | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 147 insertions(+), 1 deletion(-)
 create mode 100644 gnu/services/admin.scm

diff --git a/doc/guix.texi b/doc/guix.texi
index d5ece55..c3cfe9e 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -26,7 +26,8 @@  Copyright @copyright{} 2016 Ben Woodcroft@*
 Copyright @copyright{} 2016 Chris Marusich@*
 Copyright @copyright{} 2016 Efraim Flashner@*
 Copyright @copyright{} 2016 John Darrington@*
-Copyright @copyright{} 2016 ng0
+Copyright @copyright{} 2016 ng0@*
+Copyright @copyright{} 2016 Jan Nieuwenhuizen
 
 Permission is granted to copy, distribute and/or modify this document
 under the terms of the GNU Free Documentation License, Version 1.3 or
@@ -213,6 +214,7 @@  Services
 
 * Base Services::               Essential system services.
 * Scheduled Job Execution::     The mcron service.
+* Log Rotation::                The rottlog service.
 * Networking Services::         Network setup, SSH daemon, etc.
 * X Window::                    Graphical display.
 * Desktop Services::            D-Bus and desktop services.
@@ -7622,6 +7624,7 @@  declaration.
 @menu
 * Base Services::               Essential system services.
 * Scheduled Job Execution::     The mcron service.
+* Log Rotation::                The rottlog service.
 * Networking Services::         Network setup, SSH daemon, etc.
 * X Window::                    Graphical display.
 * Desktop Services::            D-Bus and desktop services.
@@ -8096,6 +8099,70 @@  specifications,, mcron, GNU@tie{}mcron}).
 @end deftp
 
 
+@node Log Rotation
+@subsubsection Log Rotation
+
+@cindex rottlog
+@cindex log rotation
+The @code{(gnu services admin)} module provides an interface to
+GNU@tie{}rottlog, a log rotator @pxref{rottlog,,,GNU Rot[t]log Manual}.
+
+The example below defines an operating system that provides
+log rotation.
+
+@lisp
+(use-modules (guix) (gnu) (gnu services admin) (gnu services mcron))
+(use-package-modules base idutils)
+
+(operating-system
+  ;; @dots{}
+  (services (cons* (mcron-service)
+                   (rottlog-service) 
+                   %base-services)))
+@end lisp
+
+@deffn {Scheme Procedure} rottlog-service [@var{config}]
+Return an rottlog service running @var{rottlog} that rotates log files according
+to configuration @var{config}.
+
+This is a shorthand for:
+@example
+(service rottlog-service-type
+         (rottlog-configuration (rottlog rottlog)
+                                (rc-file rc-file)
+                                (periodic-rotations periodic-rotationsn)
+                                (jobs jobs))
+@end example
+@end deffn
+
+@defvr {Scheme Variable} rottlog-service-type
+This is the type of the @code{rottlog} service, whose value is an
+@code{rottlog-configuration} object.
+
+This service type can define @var{mcron}-jobs (@pxref{Scheduled Job
+Execution}) to run the rottlog service.
+@end defvr
+
+@deftp {Data Type} rottlog-configuration
+Data type representing the configuration of rottlog.
+
+@table @asis
+@item @code{rottlog} (default: @var{rottlog})
+The rottlog package to use.
+
+@item @code{rc-file} (default: (file-append rottlog "/etc/rc")
+The rottlog @var{rc-file} to use.
+
+@item @code{periodic-rotations} (default: `(("weekly" ,(file-append rottlog "/etc/weekly")))
+An alist of rottlog configuration file specifications.
+
+@item @code{jobs}
+This is a list of gexps (@pxref{G-Expressions}), where each gexp
+corresponds to an mcron job specification (@pxref{Syntax, mcron job
+specifications,, mcron, GNU@tie{}mcron}).
+@end table
+@end deftp
+
 @node Networking Services
 @subsubsection Networking Services
 
diff --git a/gnu/local.mk b/gnu/local.mk
index 0da41f7..baa10f9 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -381,6 +381,7 @@  GNU_SYSTEM_MODULES =				\
   %D%/packages/zip.scm				\
 						\
   %D%/services.scm				\
+  %D%/services/admin.scm			\
   %D%/services/avahi.scm			\
   %D%/services/base.scm				\
   %D%/services/databases.scm			\
diff --git a/gnu/services/admin.scm b/gnu/services/admin.scm
new file mode 100644
index 0000000..b3a7906
--- /dev/null
+++ b/gnu/services/admin.scm
@@ -0,0 +1,78 @@ 
+;;; GNU Guix --- Functional package management for GNU
+;;; Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
+;;;
+;;; This file is part of GNU Guix.
+;;;
+;;; GNU Guix is free software; you can redistribute it and/or modify it
+;;; under the terms of the GNU General Public License as published by
+;;; the Free Software Foundation; either version 3 of the License, or (at
+;;; your option) any later version.
+;;;
+;;; GNU Guix is distributed in the hope that it will be useful, but
+;;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;;; GNU General Public License for more details.
+;;;
+;;; You should have received a copy of thye GNU General Public License
+;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.
+
+(define-module (gnu services admin)
+  #:use-module (gnu packages admin)
+  #:use-module (gnu packages base)
+  #:use-module (gnu services)
+  #:use-module (gnu services mcron)
+  #:use-module (gnu services shepherd)
+  #:use-module (guix gexp)
+  #:use-module (guix records)
+  #:export (rottlog-configuration
+            rottlog-configuration?
+            rottlog-service
+            rottlog-service-type))
+
+;;; Commentary:
+;;;
+;;; This module implements configuration of rottlog by writing
+;;; /etc/rottlog/{rc,hourly|daily|weekly}.  Example usage
+;;; 
+;;;     (mcron-service)
+;;;     (rottlog-service)
+;;;
+;;; Code:
+
+(define-record-type* <rottlog-configuration>
+  rottlog-configuration make-rottlog-configuration
+  rottlog-configuration?
+  (rottlog            rottlog-rottlog            ; package
+                      (default rottlog))
+  (rc-file rottlog-rc-file                       ; file
+                      (default (file-append rottlog "/etc/rc")))
+  (periodic-rotations rottlog-periodic-rotations ; list of (name file) tuples
+                      (default `(("weekly"
+                                  ,(file-append rottlog "/etc/weekly")))))
+  (jobs rottlog-jobs                             ; list of <mcron-job>
+        (default
+          (list #~(job
+                   '(next-hour '(0))
+                   (lambda ()
+                     (system (string-append #$rottlog "/sbin/rottlog"))))
+                #~(job
+                   '(next-hour '(12))
+                   (lambda ()
+                     (system (string-append #$rottlog "/sbin/rottlog"))))))))
+
+(define (rottlog-etc config)
+  `(("rottlog" ,(file-union "rottlog"
+                            (cons `("rc" ,(rottlog-rc-file config))
+                                  (rottlog-periodic-rotations config))))))
+
+(define rottlog-service-type
+  (service-type (name 'rottlog)
+                (extensions
+                 (list
+                  (service-extension etc-service-type rottlog-etc)
+                  (service-extension mcron-service-type rottlog-jobs)))))
+
+(define* (rottlog-service #:optional (config (rottlog-configuration)))
+  (service rottlog-service-type config))
+
+;;; admin.scm ends here
-- 
2.10.0