NetworkDiagnosticServer.go 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package networkdiagnostic
  2. import (
  3. networkDiagnostic "cfTest/networkdiagnostic/pb"
  4. "context"
  5. "github.com/go-ping/ping"
  6. "github.com/sirupsen/logrus"
  7. "gopkg.in/natefinch/lumberjack.v2"
  8. "runtime"
  9. "time"
  10. )
  11. var logger *logrus.Logger
  12. type NetworkDiagnosticServer struct {
  13. networkDiagnostic.UnimplementedNetworkDiagnosticServiceServer
  14. }
  15. func (s *NetworkDiagnosticServer) NetworkLatencyProbe(ctx context.Context, probeRequest *networkDiagnostic.NetworkLatencyProbeRequest) (*networkDiagnostic.NetworkLatencyProbeReply, error) {
  16. logger.Println("domainName Received: ", probeRequest.GetDomainName())
  17. logger.Println("execCount Received: ", probeRequest.GetExecCount())
  18. logger.Println("interval Received: ", probeRequest.GetInterval())
  19. // 业务请求 -> ping
  20. pinger, err := ping.NewPinger(probeRequest.GetDomainName())
  21. if err != nil {
  22. return &networkDiagnostic.NetworkLatencyProbeReply{
  23. Error: err.Error(),
  24. }, nil
  25. }
  26. pinger.Count = int(probeRequest.GetExecCount())
  27. pinger.Timeout = 5 * time.Second
  28. pinger.Interval = time.Duration(probeRequest.GetInterval()) * time.Second
  29. if runtime.GOOS == "windows" {
  30. pinger.SetPrivileged(true)
  31. }
  32. err = pinger.Run()
  33. if err != nil {
  34. return &networkDiagnostic.NetworkLatencyProbeReply{
  35. Error: err.Error(),
  36. }, nil
  37. }
  38. status := pinger.Statistics()
  39. // response
  40. return &networkDiagnostic.NetworkLatencyProbeReply{
  41. Latency: float64(status.AvgRtt.Milliseconds()),
  42. PacketsReceive: int32(status.PacketsRecv),
  43. PacketsSend: int32(status.PacketsSent),
  44. IpAddr: status.IPAddr.IP.String(),
  45. }, nil
  46. }
  47. // logger
  48. func Init() {
  49. initLogger()
  50. }
  51. func initLogger() {
  52. setupLogger()
  53. // 在初始化函数中对包级别的变量进行初始化
  54. logger = logrus.StandardLogger()
  55. logger.Println("启动NetworkDiagnosticServer, 初始化日志框架...")
  56. }
  57. func setupLogger() {
  58. // 创建一个新的logrus实例
  59. log := logrus.New()
  60. // 设置日志输出为文件,并使用lumberjack库来实现每天生成一个新的日志文件,并保存旧日志文件
  61. log.SetOutput(&lumberjack.Logger{
  62. Filename: "../../logs/networkDiagnosticServer/" + generateLogFileName(), // 日志文件路径,使用日期占位符%Y-%m-%d
  63. MaxSize: 20, // 每个日志文件的最大尺寸,单位:MB
  64. MaxBackups: 3, // 最多保留的旧日志文件数
  65. MaxAge: 30, // 最多保留的旧日志文件天数(这里设置为30天)
  66. LocalTime: true, // 使用本地时间(默认为UTC时间)
  67. Compress: true, // 是否压缩旧日志文件
  68. })
  69. // 设置日志格式为JSON格式
  70. log.SetFormatter(&logrus.JSONFormatter{})
  71. // 设置日志级别为Debug
  72. log.SetLevel(logrus.DebugLevel)
  73. // 将logrus实例设置为全局的默认日志记录器
  74. logrus.StandardLogger().SetOutput(log.Out)
  75. }
  76. func generateLogFileName() string {
  77. currentDate := time.Now().Format("01-02-2006")
  78. return "networkDiagnostic_" + currentDate + ".log"
  79. }
  80. func GetLogger() *logrus.Logger {
  81. return logger
  82. }