Listen for events and forward to external security scanning services.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

syslog.go 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package main
  2. /*
  3. syslog - This file includes all of the logic necessary to interact with syslog.
  4. This is extrapolated out so that a SyslogActioner interface can be
  5. passed to functions. Doing this allows testing by mock classes to be created
  6. that can be passed to functions.
  7. Since this is a wrapper around the log/syslog library, this does not need
  8. testing.
  9. */
  10. import (
  11. "fmt"
  12. "log"
  13. "log/syslog"
  14. "net"
  15. )
  16. // SyslogActioner is an interface for an SyslogActions class. Having
  17. // this as an interface allows us to pass in a dummy class for testing that
  18. // just returns mocked data.
  19. type SyslogActioner interface {
  20. Connect() error
  21. Info(string)
  22. }
  23. // SyslogActions is a class that handles all interactions directly with Syslog.
  24. // See the comment on SyslogActioner for rationale.
  25. type SyslogActions struct {
  26. logger *syslog.Writer
  27. Options SyslogOptions
  28. }
  29. // SyslogOptions is a class to convey all of the configurable options for the
  30. // SyslogActions class.
  31. type SyslogOptions struct {
  32. Host string
  33. Protocol string
  34. Retry bool
  35. Port string
  36. }
  37. // Info is the main method for the SyslogActioner class, it writes an
  38. // info-level message to the syslog stream.
  39. func (s SyslogActions) Info(writeMe string) {
  40. log.Println("Logged: ", writeMe)
  41. s.logger.Info(writeMe)
  42. }
  43. // Connect is the method that establishes the connection to the syslog server
  44. // over the network.
  45. func (s *SyslogActions) Connect() error {
  46. var err error
  47. address := net.JoinHostPort(s.Options.Host, s.Options.Port)
  48. if Debug {
  49. log.Printf("Opening %q syslog socket to %q\n", s.Options.Protocol, s.Options.Host)
  50. }
  51. s.logger, err = syslog.Dial(s.Options.Protocol, address, syslog.LOG_INFO, "osel")
  52. if err != nil {
  53. log.Printf("error opening syslog socket to %s: %s\n", s.Options.Host, err)
  54. if s.Options.Retry {
  55. for err != nil {
  56. log.Println("retrying")
  57. s.logger, err = syslog.Dial(s.Options.Protocol, address, syslog.LOG_INFO, "osel")
  58. }
  59. }
  60. return fmt.Errorf("error opening syslog socket to %s: %s", s.Options.Host, err)
  61. }
  62. if Debug {
  63. log.Println("Successfully connected to syslog host", s.Options.Host)
  64. }
  65. return nil
  66. }