-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
139 lines (116 loc) · 3.8 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
package main
/*
Copyright (c) IBM Corporation 2016, 2019
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific
Contributors:
Mark Taylor - Initial Contribution
*/
import (
"os"
"strings"
"time"
ibmmq "github.com/ibm-messaging/mq-golang/v5/ibmmq"
mqmetric "github.com/ibm-messaging/mq-golang/v5/mqmetric"
log "github.com/sirupsen/logrus"
)
var BuildStamp string
var GitCommit string
var BuildPlatform string
var discoverConfig mqmetric.DiscoverConfig
// add to elastic integration
var configuration = Configuration{}
var secrets = Secrets{}
// Print this via the logger rather than direct to stdout so it can be
// avoided if someone is using the stdout stream as the JSON input to a parser
func printInfo(title string, stamp string, commit string, buildPlatform string) {
log.Infoln(title)
if stamp != "" {
log.Infoln("Build : " + stamp)
}
if commit != "" {
log.Infoln("Commit Level : " + commit)
}
if buildPlatform != "" {
log.Infoln("Build Platform: " + buildPlatform)
}
log.Println("")
}
func main() {
// add to elastic integration
loadVars()
loadSecrets()
var err error
initConfig()
printInfo("Starting IBM MQ metrics exporter for JSON", BuildStamp, GitCommit, BuildPlatform)
if config.cf.QMgrName == "" {
log.Errorln("Must provide a queue manager name to connect to.")
os.Exit(72)
}
d, err := time.ParseDuration(config.interval)
if err != nil || d.Seconds() <= 1 {
log.Errorln("Invalid or too short value for interval parameter: ", err)
os.Exit(1)
}
if config.cf.CC.UseResetQStats {
log.Warnln("Warning: Data from 'RESET QSTATS' has been requested. Ensure no other monitoring applications are also using that command.")
}
log.Infoln("Starting IBM MQ metrics exporter for JSON")
// Connect and open standard queues
err = mqmetric.InitConnection(config.cf.QMgrName, config.cf.ReplyQ, &config.cf.CC)
if err == nil {
log.Infoln("Connected to queue manager ", config.cf.QMgrName)
defer mqmetric.EndConnection()
} else {
if mqe, ok := err.(mqmetric.MQMetricError); ok {
mqrc := mqe.MQReturn.MQRC
if mqrc == ibmmq.MQRC_STANDBY_Q_MGR {
log.Errorln(err)
os.Exit(30) // This is the same as the strmqm return code for "active instance running elsewhere"
}
}
}
if err == nil {
mqmetric.ChannelInitAttributes()
mqmetric.QueueInitAttributes()
mqmetric.TopicInitAttributes()
mqmetric.SubInitAttributes()
mqmetric.QueueManagerInitAttributes()
mqmetric.UsageInitAttributes()
}
// What metrics can the queue manager provide? Find out, and
// subscribe.
if err == nil {
// Do we need to expand wildcarded queue names
// or use the wildcard as-is in the subscriptions
wildcardResource := true
if config.cf.MetaPrefix != "" {
wildcardResource = false
}
discoverConfig.MonitoredQueues.ObjectNames = config.cf.MonitoredQueues
discoverConfig.MonitoredQueues.UseWildcard = wildcardResource
discoverConfig.MetaPrefix = config.cf.MetaPrefix
discoverConfig.MonitoredQueues.SubscriptionSelector = strings.ToUpper(config.cf.QueueSubscriptionSelector)
err = mqmetric.DiscoverAndSubscribe(discoverConfig)
// Also get the static attributes for any configured channels
mqmetric.RediscoverAttributes(ibmmq.MQOT_CHANNEL, config.cf.MonitoredChannels)
}
// Go into main loop for sending data to stdout
// This program runs forever
if err == nil {
for {
Collect()
time.Sleep(d)
}
}
if err != nil {
log.Fatal(err)
}
os.Exit(0)
}