syslog.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //go:build !windows && !plan9
  2. package main
  3. import (
  4. "bufio"
  5. "flag"
  6. "fmt"
  7. "log"
  8. "log/syslog"
  9. "os"
  10. "strings"
  11. "time"
  12. )
  13. func main() {
  14. var ip = flag.String("ip", "localhost:514", "ip:port")
  15. var cycle = flag.Int("cycle", 1000, "repeat number")
  16. var cycleSpan = flag.Int("sleep", 1, "sleep millisecond after one cycle")
  17. var span = flag.Int("span", 1, "sleep 1 millisecond every span row")
  18. var protocol = flag.String("protocol", "udp", "udp/tcp")
  19. var incTime = flag.Int("inctime", 1, "Increase seconds")
  20. flag.Parse()
  21. filePath := os.Args[ len(os.Args)-1 ]
  22. readFile, err := os.Open(filePath)
  23. if err != nil {
  24. log.Fatal(err)
  25. }
  26. fileScanner := bufio.NewScanner(readFile)
  27. fileScanner.Split(bufio.ScanLines)
  28. var fileLines []string
  29. for fileScanner.Scan() {
  30. fileLines = append(fileLines, fileScanner.Text())
  31. }
  32. _ = readFile.Close()
  33. /*for _, line := range fileLines {
  34. fmt.Println(line)
  35. }
  36. fmt.Println(fileLines)
  37. */
  38. sysLog, err := syslog.Dial(*protocol, *ip, syslog.LOG_WARNING|syslog.LOG_DAEMON, "demo")
  39. if err != nil {
  40. log.Fatal(err)
  41. }
  42. // 11-Jul-2023 16:00:01 DNS systemd: Started Session 1370 of user root.
  43. format := "02-Jan-2006 15:04:05"
  44. total := 0
  45. loop := 0
  46. for i:=0 ; i<*cycle ; i++ {
  47. incDuration := time.Second*time.Duration(i*(*incTime))
  48. for k, line := range fileLines {
  49. if line != "" {
  50. fields := strings.Fields(line)
  51. if len(fields) > 2 {
  52. timeStr := fields[0] + " " + fields[1]
  53. line = strings.Join(fields[2:], " ")
  54. t, err := time.Parse(format, timeStr)
  55. if err != nil {
  56. log.Fatal(err)
  57. }
  58. t = t.Add(incDuration)
  59. line = t.Format(format) + " " + line
  60. }
  61. }
  62. if err = sysLog.Emerg(line); err != nil {
  63. log.Fatal(err)
  64. }
  65. if *span > 0 && (k % *span == 0) {
  66. time.Sleep(time.Duration(*span) * time.Millisecond)
  67. }
  68. total++
  69. }
  70. loop++
  71. fmt.Printf("send %d , total %d row .\n", loop, total)
  72. if *cycleSpan > 0 {
  73. time.Sleep(time.Duration(*cycleSpan) * time.Millisecond)
  74. }
  75. }
  76. }