-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathperformance_monitoring_service.go
189 lines (172 loc) · 6.15 KB
/
performance_monitoring_service.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
package main
import (
"encoding/xml"
"errors"
"fmt"
log "github.com/sirupsen/logrus"
"strings"
)
// PerfMonService struct hold CUCM API and list of CUCM cluster server names
type PerfMonService struct {
monitors []ClusterHostMonitorData // monitors list of CUCM cluster server names
client *ApiMonitorClient // client API client with prepared http.Client
}
// openSessionResponse response for open session to server
type openSessionResponse struct {
XMLName xml.Name `xml:"perfmonOpenSessionResponse"`
Text string `xml:",chardata"`
Ns1 string `xml:"ns1,attr"`
OpenSessionId string `xml:"perfmonOpenSessionReturn"`
}
// NewPerfMonServers Create new performance client for all servers
func NewPerfMonServers() *PerfMonService {
p := PerfMonService{
monitors: make([]ClusterHostMonitorData, 0),
client: NewApiMonitorClient(),
}
for _, r := range config.MonitorNames {
p.monitors = append(p.monitors, *NewClusterHostMonitorData(r))
}
log.WithFields(p.logFields("NewPerfMonServers")).Trace("create monitor service")
return &p
}
// OpenSession open PerfMon API session and store session ID
func (s *PerfMonService) OpenSession() (err error) {
log.WithFields(s.logFields("OpenSession")).Trace("open new session")
defer duration(track(s.logFields("OpenSession"), "procedure ends"))
req := " <soap:perfmonOpenSession/>"
body, err := s.client.processRequest("OpenSession", req)
if err != nil {
log.WithFields(s.logFields("OpenSession")).Errorf("session request fail with message %s", err)
return err
}
var data openSessionResponse
err = xml.Unmarshal([]byte(body), &data)
if err != nil {
log.WithFields(s.logFields("OpenSession")).Errorf("problem convert XML body to struct. Error: %s", err)
return err
}
s.client.session = data.OpenSessionId
log.WithFields(s.logFields("OpenSession", s.client.session)).Infof("open new monitoring session")
return nil
}
// AddCounters add PerfMon session counters
func (s *PerfMonService) AddCounters() {
log.WithFields(s.logFields("AddCounters", s.client.session)).Trace("register counters for session")
if !s.client.isSessionOpen() {
log.WithFields(s.logFields("AddCounters")).Debug("session not open")
return
}
cnt := 0
for _, mon := range s.monitors {
if mon.AddCounters(s.client) != nil {
log.WithFields(s.logFields("AddCounters", s.client.session)).Errorf("problem register counter toi session for monitor %s", mon.server)
} else {
cnt++
}
}
if cnt == len(s.monitors) {
log.WithFields(s.logFields("AddCounters", s.client.session)).Info("success register counters for session")
}
}
// CloseSession close actual API session and remove all Prometheus metrics
func (s *PerfMonService) CloseSession() {
log.WithFields(s.logFields("CloseSession")).Trace("close existing session")
defer duration(track(s.logFields("CloseSession"), "procedure ends"))
if !s.client.isSessionOpen() {
log.WithFields(s.logFields("CloseSession")).Debug("not any open session")
return
}
req := fmt.Sprintf("<soap:perfmonCloseSession><soap:SessionHandle>%s</soap:SessionHandle></soap:perfmonCloseSession>", s.client.session)
_, _ = s.client.processRequest("CloseSession", req)
log.WithFields(s.logFields("CloseSession", s.client.session)).Debug("current session is closed")
s.client.session = ""
prometheusRemoveMetrics()
}
// ExistSession is open API session
func (s *PerfMonService) ExistSession() bool {
return s.client.isSessionOpen()
}
func (s *PerfMonService) CollectSessionData() (err error) {
log.WithFields(s.logFields("CollectSessionData", s.client.session)).Trace("collect session data")
defer duration(track(s.logFields("CollectSessionData"), "procedure ends"))
if !s.client.isSessionOpen() {
log.WithFields(s.logFields("CollectSessionData")).Debug("session not open")
return errors.New("session not exist for open data")
}
req := fmt.Sprintf("<soap:perfmonCollectSessionData><soap:SessionHandle>%s</soap:SessionHandle></soap:perfmonCollectSessionData>", s.client.session)
body, err := s.client.processRequest("CollectSessionData", req)
if err != nil {
log.WithFields(s.logFields("CollectSessionData")).Errorf("request return error message %s", err)
return err
}
var data SessionData
err = xml.Unmarshal([]byte(body), &data)
if err != nil {
log.WithFields(s.logFields("CollectSessionData", s.client.session)).Errorf("problem convert XML body to required struct. Error: %s", err)
return err
}
data.processData()
return nil
}
// ListAllCounters collect counters for all servers in cluster
func (s *PerfMonService) ListAllCounters() (err error) {
log.WithFields(s.logFields("ListAllCounters")).Trace("collect all counters")
defer duration(track(s.logFields("ListAllCounters"), "procedure ends"))
err = nil
for r := range s.monitors {
e := s.monitors[r].ListCounters(s.client)
if e != nil {
err = e
}
e = s.monitors[r].ReadCounterDescription(s.client)
if e != nil {
err = e
}
}
return err
}
func (s *PerfMonService) GetCounterDetails(name string) (details *CounterDetails, err error) {
for srv, server := range s.monitors {
for g, group := range server.counterList.group {
for c, counter := range group.counterName {
if counter.name == name {
return &s.monitors[srv].counterList.group[g].counterName[c], nil
}
}
}
}
log.WithFields(s.logFields("GetCounterDetails")).Errorf("not found details for counter %s", name)
details = &CounterDetails{name: name, description: fmt.Sprintf("Description for %s not exists", name)}
return details, fmt.Errorf("problem found required counter [%s] on any server", name)
}
func (s *PerfMonService) print() string {
return ""
}
func (s *PerfMonService) logFields(operation ...string) log.Fields {
names := strings.Builder{}
delimiter := ""
for _, r := range s.monitors {
names.WriteString(delimiter)
names.WriteString(r.server)
delimiter = ";"
}
var f log.Fields
if len(operation) == 2 {
f = log.Fields{
FieldMonitorName: names.String(),
FieldRoutine: operation[0],
FieldSessionId: operation[1],
}
} else if len(operation) == 1 {
f = log.Fields{
FieldMonitorName: names.String(),
FieldRoutine: operation[0],
}
} else {
f = log.Fields{
FieldMonitorName: names.String(),
}
}
return f
}