chiark / gitweb /
journal: add call to determine current journal file disk usage
authorLennart Poettering <lennart@poettering.net>
Fri, 7 Sep 2012 21:20:28 +0000 (23:20 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 7 Sep 2012 21:20:28 +0000 (23:20 +0200)
Makefile.am
TODO
man/journalctl.xml
man/sd-journal.xml
man/sd_journal_get_usage.xml [new file with mode: 0644]
src/journal/journal-file.c
src/journal/journalctl.c
src/journal/libsystemd-journal.sym
src/journal/sd-journal.c
src/systemd/sd-journal.h

index f88d193b4136c0dd786043d247cba94ebb85c90c..7ccec6713379b005cf4403d14bfea42d0a76dd62 100644 (file)
@@ -513,6 +513,7 @@ MANPAGES = \
        man/sd_journal_get_cutoff_realtime_usec.3 \
        man/sd_journal_get_cursor.3 \
        man/sd_journal_get_fd.3 \
+       man/sd_journal_get_usage.3 \
        man/sd_journal_add_match.3 \
        man/sd_journal_seek_head.3
 
diff --git a/TODO b/TODO
index 30e2f8e24840d6967a6f212d5603065cc29c0b91..de7639e863d970971235f96337814e2fabe3e172 100644 (file)
--- a/TODO
+++ b/TODO
@@ -126,8 +126,6 @@ Features:
 
 * add _SYSTEMD_USER_UNIT= field to journal entries
 
-* journal: expose current disk usage
-
 * dracut-shutdown needs to be ordered before unmounting /boot
 
 * wiki: document new logind LockSessions() call
index 296e3fd2ff01e01cc43e6f0ba8fc13fa270e672e..9800cf30ed260ecfd8b4650f06f6a8a23e7f2d2c 100644 (file)
                                 accessed.</para></listitem>
                         </varlistentry>
 
+                        <varlistentry>
+                                <term><option>--disk-usage</option></term>
+
+                                <listitem><para>Shows the current disk
+                                usage of all
+                                journal files.</para></listitem>
+                        </varlistentry>
+
                         <varlistentry>
                                 <term><option>--setup-keys</option></term>
 
index b6241a1767c200acc50ef5bbfc8f98af67905ba1..773689c692ad33761939746dd9db763f79e2e675 100644 (file)
@@ -78,7 +78,8 @@
                 <citerefentry><refentrytitle>sd_journal_add_match</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
                 <citerefentry><refentrytitle>sd_journal_seek_head</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
                 <citerefentry><refentrytitle>sd_journal_get_cursor</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
-                <citerefentry><refentrytitle>sd_journal_cutoff_realtime_usec</refentrytitle><manvolnum>3</manvolnum></citerefentry>
+                <citerefentry><refentrytitle>sd_journal_cutoff_realtime_usec</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
+                <citerefentry><refentrytitle>sd_journal_get_usage</refentrytitle><manvolnum>3</manvolnum></citerefentry>
                 and
                 <citerefentry><refentrytitle>sd_journal_get_fd</refentrytitle><manvolnum>3</manvolnum></citerefentry>
                 for more information about the functions
                         <citerefentry><refentrytitle>sd_journal_seek_head</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
                         <citerefentry><refentrytitle>sd_journal_get_cursor</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
                         <citerefentry><refentrytitle>sd_journal_cutoff_realtime_usec</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
+                        <citerefentry><refentrytitle>sd_journal_get_usage</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
                         <citerefentry><refentrytitle>journalctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
                         <citerefentry><refentrytitle>sd-id128</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
                         <citerefentry><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry>
diff --git a/man/sd_journal_get_usage.xml b/man/sd_journal_get_usage.xml
new file mode 100644 (file)
index 0000000..14eb1e2
--- /dev/null
@@ -0,0 +1,104 @@
+<?xml version='1.0'?> <!--*-nxml-*-->
+<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
+        "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
+
+<!--
+  This file is part of systemd.
+
+  Copyright 2012 Lennart Poettering
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd 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
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+-->
+
+<refentry id="sd_journal_get_usage">
+
+        <refentryinfo>
+                <title>sd_journal_get_usage</title>
+                <productname>systemd</productname>
+
+                <authorgroup>
+                        <author>
+                                <contrib>Developer</contrib>
+                                <firstname>Lennart</firstname>
+                                <surname>Poettering</surname>
+                                <email>lennart@poettering.net</email>
+                        </author>
+                </authorgroup>
+        </refentryinfo>
+
+        <refmeta>
+                <refentrytitle>sd_journal_get_usage</refentrytitle>
+                <manvolnum>3</manvolnum>
+        </refmeta>
+
+        <refnamediv>
+                <refname>sd_journal_get_usage</refname>
+                <refpurpose>Journal disk usage</refpurpose>
+        </refnamediv>
+
+        <refsynopsisdiv>
+                <funcsynopsis>
+                        <funcsynopsisinfo>#include &lt;systemd/sd-journal.h&gt;</funcsynopsisinfo>
+
+                        <funcprototype>
+                                <funcdef>int <function>sd_journal_get_usage</function></funcdef>
+                                <paramdef>sd_journal* <parameter>j</parameter></paramdef>
+                                <paramdef>uint64_t* <parameter>bytes</parameter></paramdef>
+                        </funcprototype>
+
+                </funcsynopsis>
+        </refsynopsisdiv>
+
+        <refsect1>
+                <title>Description</title>
+
+                <para><function>sd_journal_get_usage()</function>
+                determines the total disk space currently used up by
+                journal files. If
+                <literal>SD_JOURNAL_LOCAL_ONLY</literal> has been
+                passed when opening the journal files this value will
+                only reflect the size of journal files of the local
+                host, otherwise of all hosts.</para>
+        </refsect1>
+
+        <refsect1>
+                <title>Return Value</title>
+
+                <para><function>sd_journal_get_usage()</function>
+                returns 0 on success or a negative errno-style error
+                code.</para>
+        </refsect1>
+
+        <refsect1>
+                <title>Notes</title>
+
+                <para>The <function>sd_journal_get_usage()</function>
+                interface is available as shared library, which can be
+                compiled and linked to with the
+                <literal>libsystemd-journal</literal>
+                <citerefentry><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry>
+                file.</para>
+        </refsect1>
+
+        <refsect1>
+                <title>See Also</title>
+
+                <para>
+                        <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
+                        <citerefentry><refentrytitle>sd-journal</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
+                        <citerefentry><refentrytitle>sd_journal_open</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
+                </para>
+        </refsect1>
+
+</refentry>
index 697e7f3606896add66c83255386667d80ad84716..06de2acc50dc9d74317438c9b66ec789c3c1a82c 100644 (file)
@@ -1906,6 +1906,8 @@ fail:
 void journal_file_print_header(JournalFile *f) {
         char a[33], b[33], c[33];
         char x[FORMAT_TIMESTAMP_MAX], y[FORMAT_TIMESTAMP_MAX];
+        struct stat st;
+        char bytes[FORMAT_BYTES_MAX];
 
         assert(f);
 
@@ -1970,6 +1972,9 @@ void journal_file_print_header(JournalFile *f) {
         if (JOURNAL_HEADER_CONTAINS(f->header, n_entry_arrays))
                 printf("Entry Array Objects: %llu\n",
                        (unsigned long long) le64toh(f->header->n_entry_arrays));
+
+        if (fstat(f->fd, &st) >= 0)
+                printf("Disk usage: %s\n", format_bytes(bytes, sizeof(bytes), (off_t) st.st_blocks * 512ULL));
 }
 
 int journal_file_open(
index 1a6464d5e7ac9d72864524149910517b1409c39f..e2600542fc74caf5b2dc4dab0062f485ff713f63 100644 (file)
@@ -72,7 +72,8 @@ static enum {
         ACTION_NEW_ID128,
         ACTION_PRINT_HEADER,
         ACTION_SETUP_KEYS,
-        ACTION_VERIFY
+        ACTION_VERIFY,
+        ACTION_DISK_USAGE,
 } arg_action = ACTION_SHOW;
 
 static int help(void) {
@@ -96,6 +97,7 @@ static int help(void) {
                "Commands:\n"
                "     --new-id128         Generate a new 128 Bit ID\n"
                "     --header            Show journal header information\n"
+               "     --disk-usage        Show total disk usage\n"
 #ifdef HAVE_GCRYPT
                "     --setup-keys        Generate new FSS key pair\n"
                "       --interval=TIME   Time interval for changing the FSS sealing key\n"
@@ -118,7 +120,8 @@ static int parse_argv(int argc, char *argv[]) {
                 ARG_SETUP_KEYS,
                 ARG_INTERVAL,
                 ARG_VERIFY,
-                ARG_VERIFY_KEY
+                ARG_VERIFY_KEY,
+                ARG_DISK_USAGE
         };
 
         static const struct option options[] = {
@@ -141,6 +144,7 @@ static int parse_argv(int argc, char *argv[]) {
                 { "interval",     required_argument, NULL, ARG_INTERVAL     },
                 { "verify",       no_argument,       NULL, ARG_VERIFY       },
                 { "verify-key",   required_argument, NULL, ARG_VERIFY_KEY   },
+                { "disk-usage",   no_argument,       NULL, ARG_DISK_USAGE   },
                 { NULL,           0,                 NULL, 0                }
         };
 
@@ -224,6 +228,10 @@ static int parse_argv(int argc, char *argv[]) {
                         arg_action = ACTION_VERIFY;
                         break;
 
+                case ARG_DISK_USAGE:
+                        arg_action = ACTION_DISK_USAGE;
+                        break;
+
 #ifdef HAVE_GCRYPT
                 case ARG_SETUP_KEYS:
                         arg_action = ACTION_SETUP_KEYS;
@@ -746,6 +754,19 @@ int main(int argc, char *argv[]) {
                 goto finish;
         }
 
+        if (arg_action == ACTION_DISK_USAGE) {
+                uint64_t bytes;
+                char sbytes[FORMAT_BYTES_MAX];
+
+                r = sd_journal_get_usage(j, &bytes);
+                if (r < 0)
+                        goto finish;
+
+                printf("Journals take up %s on disk.\n", format_bytes(sbytes, sizeof(sbytes), bytes));
+                r = 0;
+                goto finish;
+        }
+
 #ifdef HAVE_ACL
         if (access("/var/log/journal", F_OK) < 0 && geteuid() != 0 && in_group("adm") <= 0) {
                 log_error("Unprivileged users can't see messages unless persistent log storage is enabled. Users in the group 'adm' can always see messages.");
index 27fdcdda0e25d1c80ca864ef217fa11453cf29d4..7dfae2625fa51b38e534ae3d3dcada0d0559c674 100644 (file)
@@ -70,3 +70,8 @@ global:
         sd_journal_perror;
         sd_journal_perror_with_location;
 } LIBSYSTEMD_JOURNAL_187;
+
+LIBSYSTEMD_JOURNAL_190 {
+global:
+        sd_journal_get_usage;
+} LIBSYSTEMD_JOURNAL_188;
index 0f7c02ce69c9d368d0d4346c1a8e1fd296d8893b..b4d35eebafd7612f7b8cd128bc0bc996d810b624 100644 (file)
@@ -2062,6 +2062,29 @@ void journal_print_header(sd_journal *j) {
         }
 }
 
+_public_ int sd_journal_get_usage(sd_journal *j, uint64_t *bytes) {
+        Iterator i;
+        JournalFile *f;
+        uint64_t sum = 0;
+
+        if (!j)
+                return -EINVAL;
+        if (!bytes)
+                return -EINVAL;
+
+        HASHMAP_FOREACH(f, j->files, i) {
+                struct stat st;
+
+                if (fstat(f->fd, &st) < 0)
+                        return -errno;
+
+                sum += (uint64_t) st.st_blocks * 512ULL;
+        }
+
+        *bytes = sum;
+        return 0;
+}
+
 /* _public_ int sd_journal_query_unique(sd_journal *j, const char *field) { */
 /*         if (!j) */
 /*                 return -EINVAL; */
index de1c8f38f266303a98f8f5b044c6c70badc6ccf0..6c18c894253e7294df19b0c83f060b83c109570e 100644 (file)
@@ -108,6 +108,8 @@ int sd_journal_get_cursor(sd_journal *j, char **cursor);
 int sd_journal_get_cutoff_realtime_usec(sd_journal *j, uint64_t *from, uint64_t *to);
 int sd_journal_get_cutoff_monotonic_usec(sd_journal *j, const sd_id128_t boot_id, uint64_t *from, uint64_t *to);
 
+int sd_journal_get_usage(sd_journal *j, uint64_t *bytes);
+
 /* int sd_journal_query_unique(sd_journal *j, const char *field);      /\* missing *\/ */
 /* int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_t *l); /\* missing *\/ */
 /* void sd_journal_restart_unique(sd_journal *j);                      /\* missing *\/ */