status.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package logic
  2. import (
  3. "time"
  4. "github.com/gravitl/netmaker/logic"
  5. "github.com/gravitl/netmaker/models"
  6. )
  7. func getNodeStatusOld(node *models.Node) {
  8. // On CE check only last check-in time
  9. if node.IsStatic {
  10. if !node.StaticNode.Enabled {
  11. node.Status = models.OfflineSt
  12. return
  13. }
  14. node.Status = models.OnlineSt
  15. return
  16. }
  17. if time.Since(node.LastCheckIn) > time.Minute*10 {
  18. node.Status = models.OfflineSt
  19. return
  20. }
  21. node.Status = models.OnlineSt
  22. }
  23. func GetNodeStatus(node *models.Node, defaultEnabledPolicy bool) {
  24. if node.IsStatic {
  25. if !node.StaticNode.Enabled {
  26. node.Status = models.OfflineSt
  27. return
  28. }
  29. // check extclient connection from metrics
  30. ingressMetrics, err := GetMetrics(node.StaticNode.IngressGatewayID)
  31. if err != nil || ingressMetrics == nil || ingressMetrics.Connectivity == nil {
  32. node.Status = models.UnKnown
  33. return
  34. }
  35. if metric, ok := ingressMetrics.Connectivity[node.StaticNode.ClientID]; ok {
  36. if metric.Connected {
  37. node.Status = models.OnlineSt
  38. return
  39. } else {
  40. node.Status = models.OfflineSt
  41. return
  42. }
  43. }
  44. node.Status = models.UnKnown
  45. return
  46. }
  47. if time.Since(node.LastCheckIn) > models.LastCheckInThreshold {
  48. node.Status = models.OfflineSt
  49. return
  50. }
  51. host, err := logic.GetHost(node.HostID.String())
  52. if err != nil {
  53. node.Status = models.UnKnown
  54. return
  55. }
  56. vlt, err := logic.VersionLessThan(host.Version, "v0.30.0")
  57. if err != nil {
  58. node.Status = models.UnKnown
  59. return
  60. }
  61. if vlt {
  62. getNodeStatusOld(node)
  63. return
  64. }
  65. metrics, err := logic.GetMetrics(node.ID.String())
  66. if err != nil {
  67. return
  68. }
  69. if metrics == nil || metrics.Connectivity == nil || len(metrics.Connectivity) == 0 {
  70. if time.Since(node.LastCheckIn) < models.LastCheckInThreshold {
  71. node.Status = models.OnlineSt
  72. return
  73. }
  74. if node.LastCheckIn.IsZero() {
  75. node.Status = models.OfflineSt
  76. return
  77. }
  78. }
  79. // if node.IsFailOver {
  80. // if time.Since(node.LastCheckIn) < models.LastCheckInThreshold {
  81. // node.Status = models.OnlineSt
  82. // return
  83. // }
  84. // }
  85. // If all Peers are able to reach me and and the peer is not able to reached by any peer then return online
  86. /* 1. FailOver Exists
  87. a. check connectivity to failover Node - if no connection return warning
  88. b. if getting failedover and still no connection to any of the peers - then show error
  89. c. if getting failedOver and has connections to some peers - show warning
  90. 2. FailOver Doesn't Exist
  91. a. check connectivity to pu
  92. */
  93. // failoverNode, exists := FailOverExists(node.Network)
  94. // if exists && failoverNode.FailedOverBy != uuid.Nil {
  95. // // check connectivity to failover Node
  96. // if metric, ok := metrics.Connectivity[failoverNode.ID.String()]; ok {
  97. // if time.Since(failoverNode.LastCheckIn) < models.LastCheckInThreshold {
  98. // if metric.Connected {
  99. // node.Status = models.OnlineSt
  100. // return
  101. // } else {
  102. // checkPeerConnectivity(node, metrics)
  103. // return
  104. // }
  105. // }
  106. // } else {
  107. // node.Status = models.OnlineSt
  108. // return
  109. // }
  110. // }
  111. checkPeerConnectivity(node, metrics, defaultEnabledPolicy)
  112. }
  113. func checkPeerStatus(node *models.Node, defaultAclPolicy bool) {
  114. peerNotConnectedCnt := 0
  115. metrics, err := logic.GetMetrics(node.ID.String())
  116. if err != nil {
  117. return
  118. }
  119. if metrics == nil || metrics.Connectivity == nil {
  120. if time.Since(node.LastCheckIn) < models.LastCheckInThreshold {
  121. node.Status = models.OnlineSt
  122. return
  123. }
  124. }
  125. for peerID, metric := range metrics.Connectivity {
  126. peer, err := logic.GetNodeByID(peerID)
  127. if err != nil {
  128. continue
  129. }
  130. if !defaultAclPolicy {
  131. allowed, _ := logic.IsNodeAllowedToCommunicate(*node, peer, false)
  132. if !allowed {
  133. continue
  134. }
  135. }
  136. if time.Since(peer.LastCheckIn) > models.LastCheckInThreshold {
  137. continue
  138. }
  139. if metric.Connected {
  140. continue
  141. }
  142. if peer.Status == models.ErrorSt {
  143. continue
  144. }
  145. peerNotConnectedCnt++
  146. }
  147. if peerNotConnectedCnt == 0 {
  148. node.Status = models.OnlineSt
  149. return
  150. }
  151. if len(metrics.Connectivity) > 0 && peerNotConnectedCnt == len(metrics.Connectivity) {
  152. node.Status = models.ErrorSt
  153. return
  154. }
  155. node.Status = models.WarningSt
  156. }
  157. func checkPeerConnectivity(node *models.Node, metrics *models.Metrics, defaultAclPolicy bool) {
  158. peerNotConnectedCnt := 0
  159. for peerID, metric := range metrics.Connectivity {
  160. peer, err := logic.GetNodeByID(peerID)
  161. if err != nil {
  162. continue
  163. }
  164. if !defaultAclPolicy {
  165. allowed, _ := logic.IsNodeAllowedToCommunicate(*node, peer, false)
  166. if !allowed {
  167. continue
  168. }
  169. }
  170. if time.Since(peer.LastCheckIn) > models.LastCheckInThreshold {
  171. continue
  172. }
  173. if metric.Connected {
  174. continue
  175. }
  176. // check if peer is in error state
  177. checkPeerStatus(&peer, defaultAclPolicy)
  178. if peer.Status == models.ErrorSt || peer.Status == models.WarningSt {
  179. continue
  180. }
  181. peerNotConnectedCnt++
  182. }
  183. if peerNotConnectedCnt > len(metrics.Connectivity)/2 {
  184. node.Status = models.WarningSt
  185. return
  186. }
  187. if len(metrics.Connectivity) > 0 && peerNotConnectedCnt == len(metrics.Connectivity) {
  188. node.Status = models.ErrorSt
  189. return
  190. }
  191. node.Status = models.OnlineSt
  192. }