-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
209 lines (189 loc) · 6.9 KB
/
main.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"fmt"
"github.com/alecthomas/kingpin/v2"
"github.com/prometheus/common/version"
log "github.com/sirupsen/logrus"
"math/rand"
"os"
"os/signal"
"strings"
"time"
)
//goland:noinspection SpellCheckingInspection
const (
applicationName = "cucm-perfmon-exporter" // application name
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" // map for random string
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
maxRandomSize = 10 // required size of random string
sleepBetweenSessions = 10 // sleep second between open new session or reconnect to server
)
var src = rand.NewSource(time.Now().UnixNano())
var (
help bool // show help?
toStopChannel chan bool // used for setup stop
monitors PerfMonService // registered service
Version string // for build data
Revision string // for build data
Branch string // for build data
BuildUser string // for build data
BuildDate string // for build data
rateRequest RateControl // for limit requests
)
// httpApplicationName define user agent name for API requests
func httpApplicationName() string {
return fmt.Sprintf("%s/%s", applicationName, version.Version)
}
// RandomString generate unique string
func RandomString() string {
sb := strings.Builder{}
sb.Grow(maxRandomSize)
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
for i, cache, remain := maxRandomSize-1, src.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = src.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
sb.WriteByte(letterBytes[idx])
i--
}
cache >>= letterIdxBits
remain--
}
return sb.String()
}
func monitoringOpenSession() (ret bool, err error) {
log.WithFields(log.Fields{FieldRoutine: "monitoringOpenSession"}).Trace("try open new monitor session")
defer duration(track(log.Fields{FieldRoutine: "monitoringOpenSession"}, "procedure ends"))
if err = monitors.OpenSession(); err != nil {
log.WithFields(log.Fields{FieldRoutine: "monitoringOpenSession"}).Error("problem open monitor session to target server")
return true, err
}
monitors.AddCounters()
prometheusCreateMetrics()
return false, nil
}
func track(fields log.Fields, msg string) (log.Fields, string, time.Time) {
return fields, msg, time.Now()
}
func duration(fields log.Fields, msg string, timeStart time.Time) {
timeEnd := time.Now()
log.WithFields(fields).WithField("duration", timeEnd.Sub(timeStart).Round(time.Millisecond).String()).Trace(msg)
}
// monitoringProcess start and run monitoring process
func monitoringProcess() {
var roundStartTime time.Time
var durationWait time.Duration
defer duration(track(log.Fields{FieldRoutine: "monitoringProcess"}, "procedure ends"))
log.WithFields(log.Fields{FieldRoutine: "monitoringProcess"}).Debug("start with configuration")
rateRequest.reset()
done := make(chan bool, 1)
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
log.WithFields(log.Fields{FieldRoutine: "monitoringProcess"}).Trace("read performance counters and description")
monitors = *NewPerfMonServers()
errMonitor := monitors.ListAllCounters()
if errMonitor != nil {
time.Sleep(2 * time.Second) // for finalize write to log
log.WithFields(log.Fields{FieldRoutine: "monitoringProcess"}).Fatal("problem collect counters from server")
}
log.WithFields(log.Fields{FieldRoutine: "monitoringProcess"}).Trace("start web server and gracefully shutdown GO routines")
srv := newWebServer(quit)
go gracefullyShutdown(srv, quit, done)
go runHttpServer(srv)
if toStopChannel == nil {
log.WithFields(log.Fields{FieldRoutine: "monitoringProcess"}).Fatal("problem start http listener")
return
}
requiredStop, err := monitoringOpenSession()
if err != nil && err.Error() == "" {
return
}
// processing cycle
for {
roundStartTime = time.Now()
if requiredStop {
log.WithFields(log.Fields{FieldRoutine: "monitoringProcess"}).Debug("close existing open routines")
monitors.CloseSession()
<-done
break
}
if !requiredStop {
if !monitors.ExistSession() {
log.WithFields(log.Fields{FieldRoutine: "monitoringProcess"}).Infof("session is closed wait %ds and try open new one", sleepBetweenSessions)
time.Sleep(time.Second * sleepBetweenSessions)
_, err = monitoringOpenSession()
}
if err == nil {
err = monitors.CollectSessionData()
}
if err != nil {
log.WithFields(log.Fields{FieldRoutine: "monitoringProcess"}).Info("problem read data close session")
monitors.CloseSession()
} else {
log.WithFields(log.Fields{FieldRoutine: "monitoringProcess"}).Trace("collect session data")
}
durationWait = time.Second*time.Duration(config.SleepBetweenRequest) - time.Now().Sub(roundStartTime)
if !monitors.ExistSession() {
durationWait = time.Second * 60 // wait for the next try to connect to the server
} else if durationWait < 1*time.Millisecond {
durationWait = 1 * time.Second // Wait time is too shor wait 1 second
}
select {
case <-toStopChannel:
requiredStop = true
log.WithFields(log.Fields{FieldRoutine: "monitoringProcess"}).Trace("request stop channel for monitoring")
break
case <-time.After(durationWait):
break
}
}
}
log.WithFields(log.Fields{FieldRoutine: "monitoringProcess"}).Debug("procedure ends")
}
func main() {
defer duration(track(log.Fields{FieldRoutine: "main"}, "program ends"))
exitCode := 0
version.Branch = Branch
if len(Branch) == 0 {
version.Branch = "Undefined"
}
version.Revision = Revision
if len(Revision) == 0 {
version.Revision = "Undefined"
}
version.BuildUser = BuildUser
if len(BuildUser) == 0 {
version.BuildUser = "Undefined"
}
version.BuildDate = BuildDate
if len(BuildDate) == 0 {
t := time.Now()
version.BuildDate = t.Format("20060102-15:04:05")
}
version.Version = Version
if len(Version) == 0 {
version.Version = "Undefined"
}
kingpin.Version(version.Print(applicationName))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
err := config.LoadFile(*configFile)
initLog()
if *showConfig {
fmt.Println(config.print())
log.WithFields(log.Fields{"ApplicationName": applicationName}).Info("show only configuration ane exit")
os.Exit(0)
}
if err == nil {
monitoringProcess()
} else {
log.Errorf("problem with configuration. Error: %s", err)
fmt.Printf("Program did not start due to configuration error! \r\n\tError: %s", err)
exitCode = 1
}
time.Sleep(time.Second * 2)
os.Exit(exitCode)
}