This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
/
monitor.go
154 lines (129 loc) · 4.68 KB
/
monitor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2014 The jenkins-client-launcher Authors. All rights reserved.
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
package environment
import (
"github.com/jkellerer/jenkins-client-launcher/launcher/util"
"github.com/jkellerer/jenkins-client-launcher/launcher/modes"
"fmt"
"encoding/xml"
"time"
)
const (
NodeMonitoringURI = "computer/%s/api/xml"
)
// The interval when the jenkins node is monitored.
var nodeMonitoringInterval = time.Second * 15
// The max number of offline results in a row until a reconnect is forced.
var maxOfflineCountBeforeRestart = int16(4)
type JenkinsNodeStatus struct {
DisplayName string `xml:"displayName"`
Idle bool `xml:"idle"`
Offline bool `xml:"offline"`
TemporarilyOffline bool `xml:"temporarilyOffline"`
}
// Returns the current offline and idle status of this Jenkins node from the Jenkins server.
func GetJenkinsNodeStatus(config *util.Config) (*JenkinsNodeStatus, error) {
if response, err := config.CIGet(fmt.Sprintf(NodeMonitoringURI, config.ClientName)); err == nil {
defer response.Body.Close()
if response.StatusCode == 200 {
status := &JenkinsNodeStatus{}
err = xml.NewDecoder(response.Body).Decode(status)
return status, err
} else {
return nil, fmt.Errorf(response.Status)
}
} else {
return nil, err
}
}
// Implements a monitor that issues a rest call on jenkins to see whether the node is online within jenkins.
type JenkinsNodeMonitor struct {
ticker *time.Ticker
onlineShown bool
offlineCount int16
}
func (self *JenkinsNodeMonitor) IsConfigAcceptable(config *util.Config) (bool) {
if config.ClientMonitorStateOnServer && !config.HasCIConnection() {
util.GOut("monitor", "No Jenkins URI defined. Cannot monitor this node within Jenkins.");
return false;
}
return true;
}
func (self *JenkinsNodeMonitor) Name() string {
return "Jenkins Node Monitor"
}
func (self *JenkinsNodeMonitor) Prepare(config *util.Config) {
if self.ticker != nil {
self.ticker.Stop()
}
if config.ClientMonitorStateOnServer {
maxOfflineCountBeforeRestart = config.ClientMonitorStateOnServerMaxFailures
self.ticker = time.NewTicker(nodeMonitoringInterval)
go func() {
// Run in schedule
for _ = range self.ticker.C {
self.monitor(config)
}
}()
} else {
// Setting IDLE to always true if active monitoring is disabled.
util.NodeIsIdle.Set(true)
}
}
// Checks if both, this side and the remote side show the node as connected and increments a offline count if not.
// Forces a restart of the connector when offline count reaches the threshold.
func (self *JenkinsNodeMonitor) monitor(config *util.Config) {
if self.isThisSideConnected(config) {
if connected, idle, serverReachable := self.isServerSideConnected(config); connected {
util.NodeIsIdle.Set(idle)
self.offlineCount = 0
if !self.onlineShown {
util.GOut("monitor", "Node is online in Jenkins.")
self.onlineShown = true
}
} else {
util.NodeIsIdle.Set(true)
if serverReachable {
self.offlineCount++
}
if self.offlineCount > 3 * maxOfflineCountBeforeRestart {
self.offlineCount = maxOfflineCountBeforeRestart
}
if self.offlineCount == maxOfflineCountBeforeRestart {
self.forceReconnect(config)
}
util.GOut("monitor", "WARN: Node is OFFLINE in Jenkins.")
self.onlineShown = false
}
} else {
util.NodeIsIdle.Set(true)
self.offlineCount = 0
if self.onlineShown {
util.GOut("monitor", "WARN: Node went OFFLINE locally.")
self.onlineShown = false
}
}
}
// Checks if the run mode is in started state.
// Also updates the global "util.NodeIsIdle" state to true if run mode is not in started state.
func (self *JenkinsNodeMonitor) isThisSideConnected(config *util.Config) bool {
return modes.GetConfiguredMode(config).Status().Get() == modes.ModeStarted
}
// Checks if Jenkins shows this node as connected and returns the node's IDLE state as second return value.
func (self *JenkinsNodeMonitor) isServerSideConnected(config *util.Config) (connected bool, idle bool, serverReachable bool) {
if status, err := GetJenkinsNodeStatus(config); err == nil {
return !status.Offline, status.Idle, true
} else {
util.GOut("monitor", "ERROR: Failed to monitor node %v using %v. Cause: %v", config.ClientName, config.CIHostURI, err)
return false, true, false
}
}
// Forces a reconnect with Jenkins by stopping the current mode.
func (self *JenkinsNodeMonitor) forceReconnect(config *util.Config) {
if self.isThisSideConnected(config) {
util.GOut("monitor", "WARN: This node appears dead in Jenkins, forcing a reconnect.")
modes.GetConfiguredMode(config).Stop()
}
}
// Registering the monitor.
var _ = RegisterPreparer(new(JenkinsNodeMonitor))