diff --git a/e b/e index a7c3758de5..a9601e9269 160000 --- a/e +++ b/e @@ -1 +1 @@ -Subproject commit a7c3758de5988c97e3f36b934f7b44d9ac8200f0 +Subproject commit a9601e92690f804a89965ca94af76a6d9604944f diff --git a/lib/constants/constants.go b/lib/constants/constants.go index c521e70261..144a5d6c8a 100644 --- a/lib/constants/constants.go +++ b/lib/constants/constants.go @@ -670,6 +670,10 @@ const ( AnnotationLogo = "gravitational.io/logo" // AnnotationSize contains image size in bytes. AnnotationSize = "gravitational.io/size" + + // GravityCLITag is used to tag gravity cli command log entries in the + // system journal. + GravityCLITag = "gravity-cli" ) var ( diff --git a/lib/report/system.go b/lib/report/system.go index bbb9c38e3e..2528ff61cc 100644 --- a/lib/report/system.go +++ b/lib/report/system.go @@ -23,6 +23,7 @@ import ( "path/filepath" "time" + "github.com/gravitational/gravity/lib/constants" "github.com/gravitational/gravity/lib/defaults" "github.com/gravitational/gravity/lib/utils" "github.com/gravitational/trace" @@ -40,6 +41,7 @@ func NewSystemCollector(since time.Duration) Collectors { add(syslogExportLogs(since)) add(systemFileLogs()...) add(planetLogs(since)...) + add(gravityCLILog(since)) return collectors } @@ -191,3 +193,15 @@ func fetchEtc(name string) CollectorFunc { ) }) } + +// gravityCLILog fetches gravity cli log. +func gravityCLILog(since time.Duration) Collector { + var script = fmt.Sprintf(` +#!/bin/bash +/bin/journalctl --no-pager -t %s`, constants.GravityCLITag) + if since != 0 { + script = fmt.Sprintf(`%s --since="%s"`, script, time.Now().Add(-since).Format(JournalDateFormat)) + } + script = fmt.Sprintf("%s | /bin/gzip -f", script) + return Script("gravity-cli.log.gz", script) +} diff --git a/lib/utils/syslog.go b/lib/utils/syslog.go new file mode 100644 index 0000000000..6cdd673205 --- /dev/null +++ b/lib/utils/syslog.go @@ -0,0 +1,37 @@ +/* +Copyright 2020 Gravitational, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package utils + +import ( + "log/syslog" + + "github.com/gravitational/trace" +) + +// SyslogWrite writes the message to the system log with the specified priority +// and tag. +func SyslogWrite(priority syslog.Priority, message, tag string) error { + w, err := syslog.New(priority, tag) + if err != nil { + return trace.Wrap(err) + } + defer w.Close() + if _, err := w.Write([]byte(message)); err != nil { + return trace.Wrap(err) + } + return nil +} diff --git a/tool/gravity/cli/run.go b/tool/gravity/cli/run.go index 21c430af4f..376df7bfbf 100644 --- a/tool/gravity/cli/run.go +++ b/tool/gravity/cli/run.go @@ -20,6 +20,7 @@ import ( "bufio" "context" "fmt" + "log/syslog" "net/http" "os" "os/exec" @@ -57,6 +58,10 @@ func ConfigureEnvironment() error { // Run parses CLI arguments and executes an appropriate gravity command func Run(g *Application) error { log.Debugf("Executing: %v.", os.Args) + if err := utils.SyslogWrite(syslog.LOG_INFO, strings.Join(os.Args, " "), constants.GravityCLITag); err != nil { + log.WithError(err).Warn("Failed to write to system logs.") + } + err := ConfigureEnvironment() if err != nil { return trace.Wrap(err)