alerts_table.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "sort"
  6. "strconv"
  7. "time"
  8. log "github.com/sirupsen/logrus"
  9. "github.com/crowdsecurity/crowdsec/pkg/models"
  10. )
  11. func alertsTable(out io.Writer, alerts *models.GetAlertsResponse, printMachine bool) {
  12. t := newTable(out)
  13. t.SetRowLines(false)
  14. header := []string{"ID", "value", "reason", "country", "as", "decisions", "created_at"}
  15. if printMachine {
  16. header = append(header, "machine")
  17. }
  18. t.SetHeaders(header...)
  19. for _, alertItem := range *alerts {
  20. displayVal := *alertItem.Source.Scope
  21. if len(alertItem.Decisions) > 1 {
  22. displayVal = fmt.Sprintf("%s (%d %ss)", *alertItem.Source.Scope, len(alertItem.Decisions), *alertItem.Decisions[0].Scope)
  23. } else if *alertItem.Source.Value != "" {
  24. displayVal += ":" + *alertItem.Source.Value
  25. }
  26. row := []string{
  27. strconv.Itoa(int(alertItem.ID)),
  28. displayVal,
  29. *alertItem.Scenario,
  30. alertItem.Source.Cn,
  31. alertItem.Source.GetAsNumberName(),
  32. DecisionsFromAlert(alertItem),
  33. *alertItem.StartAt,
  34. }
  35. if printMachine {
  36. row = append(row, alertItem.MachineID)
  37. }
  38. t.AddRow(row...)
  39. }
  40. t.Render()
  41. }
  42. func alertDecisionsTable(out io.Writer, alert *models.Alert) {
  43. foundActive := false
  44. t := newTable(out)
  45. t.SetRowLines(false)
  46. t.SetHeaders("ID", "scope:value", "action", "expiration", "created_at")
  47. for _, decision := range alert.Decisions {
  48. parsedDuration, err := time.ParseDuration(*decision.Duration)
  49. if err != nil {
  50. log.Error(err)
  51. }
  52. expire := time.Now().UTC().Add(parsedDuration)
  53. if time.Now().UTC().After(expire) {
  54. continue
  55. }
  56. foundActive = true
  57. scopeAndValue := *decision.Scope
  58. if *decision.Value != "" {
  59. scopeAndValue += ":" + *decision.Value
  60. }
  61. t.AddRow(
  62. strconv.Itoa(int(decision.ID)),
  63. scopeAndValue,
  64. *decision.Type,
  65. *decision.Duration,
  66. alert.CreatedAt,
  67. )
  68. }
  69. if foundActive {
  70. fmt.Printf(" - Active Decisions :\n")
  71. t.Render() // Send output
  72. }
  73. }
  74. func alertEventTable(out io.Writer, event *models.Event) {
  75. fmt.Fprintf(out, "\n- Date: %s\n", *event.Timestamp)
  76. t := newTable(out)
  77. t.SetHeaders("Key", "Value")
  78. sort.Slice(event.Meta, func(i, j int) bool {
  79. return event.Meta[i].Key < event.Meta[j].Key
  80. })
  81. for _, meta := range event.Meta {
  82. t.AddRow(
  83. meta.Key,
  84. meta.Value,
  85. )
  86. }
  87. t.Render() // Send output
  88. }