Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

[5.5.x] Collect gravity cli history #1858

Merged
merged 2 commits into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion e
Submodule e updated from a7c375 to bf2405
4 changes: 4 additions & 0 deletions lib/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
14 changes: 14 additions & 0 deletions lib/report/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -40,6 +41,7 @@ func NewSystemCollector(since time.Duration) Collectors {
add(syslogExportLogs(since))
add(systemFileLogs()...)
add(planetLogs(since)...)
add(gravityCLILog(since))

return collectors
}
Expand Down Expand Up @@ -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)
}
37 changes: 37 additions & 0 deletions lib/utils/syslog.go
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 5 additions & 0 deletions tool/gravity/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bufio"
"context"
"fmt"
"log/syslog"
"net/http"
"os"
"os/exec"
Expand Down Expand Up @@ -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)
Expand Down