6
6
import com .ibm .mq .MQTopic ;
7
7
import com .ibm .mq .constants .MQConstants ;
8
8
import com .ibm .mq .pcf .PCFMessage ;
9
+ import org .apache .logging .log4j .LogManager ;
10
+ import org .apache .logging .log4j .Logger ;
9
11
import ru .cinimex .exporter .mq .MQConnection ;
10
12
import ru .cinimex .exporter .mq .MQObject ;
11
13
import ru .cinimex .exporter .mq .MQSubscriberManager ;
25
27
* Main class of mq exporter tool. Parses config, scans topics, starts subscribers.
26
28
*/
27
29
public class ExporterLauncher {
30
+ private static final Logger logger = LogManager .getLogger (ExporterLauncher .class );
28
31
private static final String topicString = "$SYS/MQ/INFO/QMGR/%s/Monitor/METADATA/CLASSES" ;
29
32
private static final int getMsgOpt = MQConstants .MQGMO_WAIT | MQConstants .MQGMO_COMPLETE_MSG | MQConstants .MQGMO_SYNCPOINT ;
30
33
31
- public static void main (String [] args ) throws MQException , IOException {
34
+ public static void main (String [] args ) {
32
35
if (args .length == 0 ) {
33
- System . err . println ("It seems that you forgot to specify the path to the config file." );
36
+ logger . error ("It seems like you forgot to specify path to the config file." );
34
37
System .exit (1 );
35
38
}
36
39
Config config = new Config (args [0 ]);
37
- try {
38
- ArrayList <PCFElement > elements = getAllPublishedMetrics (config );
39
- ArrayList <MQObject .MQType > monitoringTypes = new ArrayList <>();
40
- ArrayList <MQObject > objects = new ArrayList <>();
41
40
42
- if (config .sendPCFCommands ()) {
43
- if (config .getQueues () != null && config .getQueues ().size () > 0 ) {
44
- monitoringTypes .add (MQObject .MQType .QUEUE );
45
- for (String queueName : config .getQueues ()) {
46
- objects .add (new MQObject (queueName , MQObject .MQType .QUEUE ));
47
- }
41
+ ArrayList <PCFElement > elements = getAllPublishedMetrics (config );
42
+ ArrayList <MQObject .MQType > monitoringTypes = new ArrayList <>();
43
+ ArrayList <MQObject > objects = new ArrayList <>();
44
+
45
+ if (config .sendPCFCommands ()) {
46
+ if (config .getQueues () != null && config .getQueues ().size () > 0 ) {
47
+ monitoringTypes .add (MQObject .MQType .QUEUE );
48
+ for (String queueName : config .getQueues ()) {
49
+ objects .add (new MQObject (queueName , MQObject .MQType .QUEUE ));
50
+ logger .debug ("Queue {} was added for additional monitoring." , queueName );
48
51
}
49
- if (config .getChannels () != null && config .getChannels ().size () > 0 ) {
50
- monitoringTypes .add (MQObject .MQType .CHANNEL );
51
- for (String channelName : config .getChannels ()) {
52
- objects .add (new MQObject (channelName , MQObject .MQType .CHANNEL ));
53
- }
52
+ }
53
+ if (config .getChannels () != null && config .getChannels ().size () > 0 ) {
54
+ monitoringTypes .add (MQObject .MQType .CHANNEL );
55
+ for (String channelName : config .getChannels ()) {
56
+ objects .add (new MQObject (channelName , MQObject .MQType .CHANNEL ));
57
+ logger .debug ("Channel {} was added for additional monitoring." , channelName );
54
58
}
55
- if (config .getListeners () != null && config .getListeners ().size () > 0 ) {
56
- monitoringTypes .add (MQObject .MQType .LISTENER );
57
- for (String listenerName : config .getListeners ()) {
58
- objects .add (new MQObject (listenerName , MQObject .MQType .LISTENER ));
59
- }
59
+ }
60
+ if (config .getListeners () != null && config .getListeners ().size () > 0 ) {
61
+ monitoringTypes .add (MQObject .MQType .LISTENER );
62
+ for (String listenerName : config .getListeners ()) {
63
+ objects .add (new MQObject (listenerName , MQObject .MQType .LISTENER ));
64
+ logger .debug ("Listener {} was added for additional monitoring." , listenerName );
60
65
}
61
66
}
62
- MetricsManager .initMetrics (elements , monitoringTypes );
63
- MQSubscriberManager manager = new MQSubscriberManager (config .getQmgrHost (), config .getQmgrPort (), config .getQmgrChannel (), config .getQmgrName (), config .getUser (), config .getPassword (), config .useMqscp ());
64
- manager .runSubscribers (elements , objects , config .sendPCFCommands (), config .usePCFWildcards (), config .getScrapeInterval ());
67
+ }
68
+ MetricsManager .initMetrics (elements , monitoringTypes );
69
+ MQSubscriberManager manager = new MQSubscriberManager (config .getQmgrHost (), config .getQmgrPort (), config .getQmgrChannel (), config .getQmgrName (), config .getUser (), config .getPassword (), config .useMqscp ());
70
+ manager .runSubscribers (elements , objects , config .sendPCFCommands (), config .usePCFWildcards (), config .getScrapeInterval ());
71
+ try {
65
72
new HTTPServer (new InetSocketAddress ("0.0.0.0" , config .getEndpPort ()), config .getEndpURL (), Registry .getRegistry (), false );
66
- } catch (Exception e ) {
67
- System . err . println ( e . getMessage () );
73
+ } catch (IOException e ) {
74
+ logger . error ( "Error occurred during expanding endpoint for Prometheus: " , e );
68
75
}
69
76
}
70
77
@@ -81,9 +88,8 @@ private static ArrayList<PCFElement> getAllPublishedMetrics(Config config) {
81
88
MQGetMessageOptions gmo = new MQGetMessageOptions ();
82
89
gmo .options = getMsgOpt ;
83
90
gmo .waitInterval = 30000 ;
84
- connection .establish (config .getQmgrHost (), config .getQmgrPort (), config .getQmgrChannel (), config .getQmgrName (), config .getUser (), config .getPassword (), config .useMqscp ());
85
-
86
91
try {
92
+ connection .establish (config .getQmgrHost (), config .getQmgrPort (), config .getQmgrChannel (), config .getQmgrName (), config .getUser (), config .getPassword (), config .useMqscp ());
87
93
topic = connection .createTopic (String .format (topicString , config .getQmgrName ()));
88
94
MQMessage msg = getEmptyMessage ();
89
95
topic .get (msg , gmo );
@@ -103,16 +109,16 @@ private static ArrayList<PCFElement> getAllPublishedMetrics(Config config) {
103
109
elements .addAll (PCFDataParser .getPCFElements (pcfResponse ));
104
110
}
105
111
}
106
- } catch (Exception e ) {
107
- System . err . println ( e . getMessage () );
112
+ } catch (MQException | IOException e ) {
113
+ logger . error ( "Failed!" , e );
108
114
} finally {
109
115
try {
110
116
if (topic != null && topic .isOpen ()) {
111
117
topic .close ();
112
118
}
113
119
connection .close ();
114
120
} catch (MQException e ) {
115
- System . err . println ( String . format ( "Error occured during disconnecting from topic %s . Error: %s " , topic .toString (), e . getStackTrace ()) );
121
+ logger . error ( "Error occurred during disconnecting from topic {} . Error: " , topic .toString (), e );
116
122
}
117
123
}
118
124
return elements ;
0 commit comments