From c46e6f868149e04378f379655341f3098f493c68 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 15 Feb 2019 06:12:31 +0100 Subject: Adding container log command. Signed-off-by: Daniel Baumann --- lib/container/log | 144 ++++++++++++++++++++++++++++++++++++++++ share/bash-completion/container | 37 +++++++++++ share/man/container-log.1.txt | 91 +++++++++++++++++++++++++ share/man/container.1.txt | 3 + 4 files changed, 275 insertions(+) create mode 100755 lib/container/log create mode 100644 share/man/container-log.1.txt diff --git a/lib/container/log b/lib/container/log new file mode 100755 index 0000000..41ac087 --- /dev/null +++ b/lib/container/log @@ -0,0 +1,144 @@ +#!/bin/sh + +# container-tools - Manage systemd-nspawn containers +# Copyright (C) 2014-2019 Daniel Baumann +# +# SPDX-License-Identifier: GPL-3.0+ +# +# This program 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. +# +# This program 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 the GNU General Public License +# along with this program. If not, see . + +set -e + +COMMAND="$(basename ${0})" + +LOG="/var/log/container-tools/container.log" + +Parameters () +{ + GETOPT_LONGOPTIONS="name:,date:," + GETOPT_OPTIONS="n:,d:," + + PARAMETERS="$(getopt --longoptions ${GETOPT_LONGOPTIONS} --name=${COMMAND} --options ${GETOPT_OPTIONS} --shell sh -- ${@})" + + if [ "${?}" != "0" ] + then + echo "'${COMMAND}': getopt exit" >&2 + exit 1 + fi + + eval set -- "${PARAMETERS}" + + while true + do + case "${1}" in + -d|--date) + DATE="${2}" + shift 2 + ;; + + -n|--name) + NAME="${2}" + shift 2 + ;; + + -u|--user) + USER="${2}" + shift 2 + ;; + + --) + shift 1 + break + ;; + + *) + echo "'${COMMAND}': getopt error" >&2 + exit 1 + ;; + esac + done +} + +Usage () +{ + echo "Usage: container ${COMMAND} [-n|--name NAME] [-d|--date DATE|today|today-N|yesterday] [-u|--user USER]" >&2 + exit 1 +} + +Parameters "${@}" + +case "${DATE}" in + today-*) + DAYS="$(echo ${DATE} | awk -F- '{ print $2 }')" + + START="$(date -d "${DAYS} day ago" '+%Y-%m-%d')" + END="$(date +%Y-%m-%d)" + + DATE="(${START}" + + while true + do + DATE="${DATE}|${START}" + + if [ "${START}" = "${END}" ] + then + break + fi + + START="$(date +%Y-%m-%d --date "${START} +1 day")" + done + + DATE="${DATE})" + ;; + + today) + DATE="$(date +%Y-%m-%d)" + ;; + + yesterday) + DATE="$(date -d '1 day ago' '+%Y-%m-%d')" + ;; +esac + +if ls "${LOG}"-*.gz > /dev/null 2>&1 +then + LOGS="$(ls ${LOG}-*.gz) ${LOG}" +else + LOGS="${LOG}" +fi + +# FIXME: user + +for LOG in ${LOGS} +do + case "${LOG}" in + *.gz) + GREP="zgrep" + ;; + + *) + GREP="grep" + ;; + esac + + case "${NAME}" in + "") + ${GREP} -E "^${DATE}" "${LOG}" || true + ;; + + *) + ${GREP} -E "^${DATE}" "${LOG}" | grep " ${NAME}" || true + ;; + esac +done diff --git a/share/bash-completion/container b/share/bash-completion/container index 4289961..3f1f271 100644 --- a/share/bash-completion/container +++ b/share/bash-completion/container @@ -143,6 +143,43 @@ _container() esac ;; + log) + case "${prev}" in + -d|--date) + opts="$(if ls /var/log/container-tools/container.log-*.gz > /dev/null 2>&1; + then \ + zcat /var/log/container-tools/container.log-*.gz | awk '{ print $1 }' | sort -u; \ + fi; \ + if [ -e /var/log/container-tools/container.log ]; \ + then \ + awk '{ print $1 }' /var/log/container-tools/container.log | sort -u; \ + fi) \ + today yesterday" + COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) + return 0 + ;; + + -n|--name) + opts="$(if ls /var/log/container-tools/container.log-*.gz > /dev/null 2>&1; + then \ + zgrep -E '(\-n|\-\-name) ' /var/log/container-tools/container.log-*.gz | sed -e 's|.*-n ||' -e 's|.*--name ||' -e 's| .*$||' | sort -u; \ + fi; \ + if [ -e /var/log/container-tools/container.log ]; \ + then \ + grep -E '(\-n|\-\-name) ' /var/log/container-tools/container.log | sed -e 's|.*-n ||' -e 's|.*--name ||' -e 's| .*$||' | sort -u; \ + fi)" + COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) + return 0 + ;; + + *) + opts="-d --date -n --name" + COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) + return 0 + ;; + esac + ;; + move|mv) case "${prev}" in -o|--old) diff --git a/share/man/container-log.1.txt b/share/man/container-log.1.txt new file mode 100644 index 0000000..ea13f5b --- /dev/null +++ b/share/man/container-log.1.txt @@ -0,0 +1,91 @@ +// container-tools - Manage systemd-nspawn containers +// Copyright (C) 2014-2019 Daniel Baumann +// +// SPDX-License-Identifier: GPL-3.0+ +// +// This program 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. +// +// This program 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 the GNU General Public License +// along with this program. If not, see . + +CONTAINER-LOG(1) +================ +:doctype: manpage +:man manual: Open Infrastructure +:man source: container-tools +:man version: {revnumber} + + +NAME +---- +container-log - Show container log + + +SYNOPSIS +-------- +*container log* ['OPTIONS'] + + +DESCRIPTION +----------- +The container log command shows the container log. + + +OPTIONS +------- +The following container options are available: + +*-d, --date*:: + Show only log entries of the specified date as 'YYYY-MM-DD' or a date range like 'today-7'. + +*-n, --name*:: + Show only log entries for the specified container. + +EXAMPLES +-------- +*Show all log entries:*:: + sudo container log + +*Show only log entries of today:*:: + sudo container log -d today + +*Show only log entries of yesterday:*:: + sudo container log -d yesterday + +*Show all log entries of the example.net container:*:: + sudo container log -n example.net + +*Show only log entries of the example.net container of the last 30 days:*:: + sudo container log -d today-30 -n example.net + + +SEE ALSO +-------- +container-tools(7), +container(1). + + +HOMEPAGE +-------- +More information about container-tools and the Open Infrastructure project can be found on the homepage at https://open-infrastructure.net. + + +CONTACT +------- +Bug reports, feature requests, help, patches, support and everything else +are welcome on the Open Infrastructure Software Mailing List . + +Debian specific bugs can also be reported in the Debian Bug Tracking System at https://bugs.debian.org. + + +AUTHORS +------- +container-tools was written by Daniel Baumann and others. diff --git a/share/man/container.1.txt b/share/man/container.1.txt index efe4e3b..7170041 100644 --- a/share/man/container.1.txt +++ b/share/man/container.1.txt @@ -85,6 +85,9 @@ The following container commands are available: *list*:: List container on the system, see container-list(1). +*log*:: + Show container log, see container-log(1). + *status*:: Show container status, see container-status(1). -- cgit v1.2.3