main.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package main
  4. import (
  5. "dust-monitor/parser"
  6. "dust-monitor/pb"
  7. "dust-monitor/tcp_packet"
  8. "dust-monitor/timetask"
  9. "flag"
  10. "fmt"
  11. "net"
  12. "os"
  13. )
  14. var (
  15. configFile = flag.String("config", "conf/common.yaml", "config file location")
  16. version = flag.Bool("version", false, "print the version")
  17. GitCommit = "library-import"
  18. Version = "library-import"
  19. )
  20. func showVersion() {
  21. fmt.Println("Version: ", Version)
  22. fmt.Println("GitCommit:", GitCommit)
  23. }
  24. func prepare(filename string) {
  25. // 加载配置
  26. err := parser.LoadConfig(filename)
  27. if err != nil {
  28. fmt.Printf("get conf failed, err: %+v\n\n", err)
  29. os.Exit(1)
  30. }
  31. // 注册处理函数
  32. // parser.Register(parser.MysqlHandler, parser.RedisHandler, parser.LoggerHandler)
  33. //parser.Register(parser.LoggerHandler, parser.MysqlHandler, parser.RedisHandler)
  34. parser.Register(parser.LoggerHandler, parser.InfluxdbHandler)
  35. // 执行注册的处理函数
  36. parser.Handle()
  37. // 建立rpc客户端
  38. conns := pb.SetupClients()
  39. for _, conn := range conns {
  40. defer conn.Close()
  41. }
  42. timetask.Init()
  43. }
  44. func runDeviceTcp(ip string, port int) {
  45. service := fmt.Sprintf("%s:%d", ip, port)
  46. tcpAddr, err := net.ResolveTCPAddr("tcp4", service)
  47. if err != nil {
  48. fmt.Println("resolve tcp addr err:", err)
  49. os.Exit(1)
  50. }
  51. listener, err := net.ListenTCP("tcp", tcpAddr)
  52. if err != nil {
  53. fmt.Println("listen tcp err:", err)
  54. os.Exit(2)
  55. }
  56. for {
  57. fmt.Printf("listen:%v\n", listener.Addr().String())
  58. conn, err := listener.Accept()
  59. if err != nil {
  60. continue
  61. }
  62. go tcp_packet.HandleSocketClient(conn)
  63. }
  64. }
  65. func main() {
  66. flag.Parse()
  67. if *version {
  68. showVersion()
  69. os.Exit(1)
  70. }
  71. prepare(*configFile)
  72. runDeviceTcp(parser.Conf.DustMonitor.ServiceIp, parser.Conf.DustMonitor.ServicePort)
  73. return
  74. }