parent
5c1c941851
commit
920f3d2a7d
|
@ -14,6 +14,7 @@ import (
|
||||||
"github.com/AlecAivazis/survey/v2"
|
"github.com/AlecAivazis/survey/v2"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/database"
|
"github.com/crowdsecurity/crowdsec/pkg/database"
|
||||||
|
"github.com/crowdsecurity/crowdsec/pkg/database/ent"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||||
"github.com/crowdsecurity/machineid"
|
"github.com/crowdsecurity/machineid"
|
||||||
"github.com/enescakir/emoji"
|
"github.com/enescakir/emoji"
|
||||||
|
@ -90,6 +91,24 @@ func generateID(prefix string) (string, error) {
|
||||||
return prefix + suffix, nil
|
return prefix + suffix, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func displayLastHeartBeat(m *ent.Machine, fancy bool) string {
|
||||||
|
var hbDisplay string
|
||||||
|
|
||||||
|
if m.LastHeartbeat != nil {
|
||||||
|
lastHeartBeat := time.Now().UTC().Sub(*m.LastHeartbeat)
|
||||||
|
hbDisplay = lastHeartBeat.Truncate(time.Second).String()
|
||||||
|
if fancy && lastHeartBeat > 2*time.Minute {
|
||||||
|
hbDisplay = fmt.Sprintf("%s %s", emoji.Warning.String(), lastHeartBeat.Truncate(time.Second).String())
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
hbDisplay = "-"
|
||||||
|
if fancy {
|
||||||
|
hbDisplay = emoji.Warning.String() + " -"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return hbDisplay
|
||||||
|
}
|
||||||
|
|
||||||
func NewMachinesCmd() *cobra.Command {
|
func NewMachinesCmd() *cobra.Command {
|
||||||
/* ---- DECISIONS COMMAND */
|
/* ---- DECISIONS COMMAND */
|
||||||
var cmdMachines = &cobra.Command{
|
var cmdMachines = &cobra.Command{
|
||||||
|
@ -149,12 +168,7 @@ Note: This command requires database direct access, so is intended to be run on
|
||||||
} else {
|
} else {
|
||||||
validated = emoji.Prohibited.String()
|
validated = emoji.Prohibited.String()
|
||||||
}
|
}
|
||||||
lastHeartBeat := time.Now().UTC().Sub(*w.LastHeartbeat)
|
table.Append([]string{w.MachineId, w.IpAddress, w.UpdatedAt.Format(time.RFC3339), validated, w.Version, w.AuthType, displayLastHeartBeat(w, true)})
|
||||||
hbDisplay := lastHeartBeat.Truncate(time.Second).String()
|
|
||||||
if lastHeartBeat > 2*time.Minute {
|
|
||||||
hbDisplay = fmt.Sprintf("%s %s", emoji.Warning.String(), lastHeartBeat.Truncate(time.Second).String())
|
|
||||||
}
|
|
||||||
table.Append([]string{w.MachineId, w.IpAddress, w.UpdatedAt.Format(time.RFC3339), validated, w.Version, w.AuthType, hbDisplay})
|
|
||||||
}
|
}
|
||||||
table.Render()
|
table.Render()
|
||||||
} else if csConfig.Cscli.Output == "json" {
|
} else if csConfig.Cscli.Output == "json" {
|
||||||
|
@ -176,7 +190,7 @@ Note: This command requires database direct access, so is intended to be run on
|
||||||
} else {
|
} else {
|
||||||
validated = "false"
|
validated = "false"
|
||||||
}
|
}
|
||||||
err := csvwriter.Write([]string{w.MachineId, w.IpAddress, w.UpdatedAt.Format(time.RFC3339), validated, w.Version, w.AuthType, time.Now().UTC().Sub(*w.LastHeartbeat).Truncate(time.Second).String()})
|
err := csvwriter.Write([]string{w.MachineId, w.IpAddress, w.UpdatedAt.Format(time.RFC3339), validated, w.Version, w.AuthType, displayLastHeartBeat(w, false)})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed to write raw output : %s", err)
|
log.Fatalf("failed to write raw output : %s", err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue