66import com .ibm .mq .MQTopic ;
77import com .ibm .mq .constants .MQConstants ;
88import com .ibm .mq .pcf .PCFMessage ;
9+ import com .ibm .mq .pcf .PCFMessageAgent ;
910import org .apache .logging .log4j .LogManager ;
1011import org .apache .logging .log4j .Logger ;
1112import ru .cinimex .exporter .mq .MQConnection ;
2425import java .util .ArrayList ;
2526import java .util .List ;
2627
28+ import static ru .cinimex .exporter .mq .MQConnection .createMQConnectionParams ;
29+
2730/**
2831 * Main class of mq exporter tool. Parses config, scans topics, starts subscribers.
2932 */
3033public class ExporterLauncher {
3134 private static final Logger logger = LogManager .getLogger (ExporterLauncher .class );
3235 private static final String TOPIC_STRING = "$SYS/MQ/INFO/QMGR/%s/Monitor/METADATA/CLASSES" ;
3336 private static final int GMO = MQConstants .MQGMO_WAIT | MQConstants .MQGMO_COMPLETE_MSG | MQConstants .MQGMO_SYNCPOINT ;
34- private static MQSubscriberManager manager ;
37+ private static MQSubscriberManager manager ;
3538 private static HTTPServer server ;
3639
37- public static void main (String [] args ) {
40+ public static void main (String [] args ) throws MQException , IOException {
3841 if (args .length == 0 ) {
3942 logger .error ("It seems like you forgot to specify path to the config file." );
4043 System .exit (1 );
4144 }
4245 Config config = new Config (args [0 ]);
46+ MQConnection .establish (config .getQmgrName (), createMQConnectionParams (config ));
4347
4448 createShutdownHook ();
4549 ArrayList <PCFElement > elements = getAllPublishedMetrics (config );
4650 ArrayList <MQObject .MQType > monitoringTypes = new ArrayList <>();
47- ArrayList <MQObject > objects = new ArrayList <>();
48-
49- if (config .getQueues () != null && !config .getQueues ().isEmpty ()) {
50- monitoringTypes .add (MQObject .MQType .QUEUE );
51- for (String queueName : config .getQueues ()) {
52- objects .add (new MQObject (queueName , MQObject .MQType .QUEUE ));
53- }
54- }
55- if (config .getChannels () != null && !config .getChannels ().isEmpty ()) {
56- monitoringTypes .add (MQObject .MQType .CHANNEL );
57- for (String channelName : config .getChannels ()) {
58- objects .add (new MQObject (channelName , MQObject .MQType .CHANNEL ));
59- }
60- }
61- if (config .getListeners () != null && !config .getListeners ().isEmpty ()) {
62- monitoringTypes .add (MQObject .MQType .LISTENER );
63- for (String listenerName : config .getListeners ()) {
64- objects .add (new MQObject (listenerName , MQObject .MQType .LISTENER ));
65- }
66- }
51+ List <MQObject > objects = getMonitoringObjects (config );
52+ monitoringTypes .add (MQObject .MQType .QUEUE );
53+ monitoringTypes .add (MQObject .MQType .CHANNEL );
54+ monitoringTypes .add (MQObject .MQType .LISTENER );
6755
6856 MetricsManager .initMetrics (elements , monitoringTypes );
6957 manager = new MQSubscriberManager (config );
@@ -83,27 +71,26 @@ public static void main(String[] args) {
8371 * @return - array, filled with metrics headers.
8472 */
8573 private static ArrayList <PCFElement > getAllPublishedMetrics (Config config ) {
86- MQConnection connection = new MQConnection ();
8774 MQTopic topic = null ;
8875 ArrayList <PCFElement > elements = new ArrayList <>();
8976 MQGetMessageOptions gmo = new MQGetMessageOptions ();
9077 gmo .options = GMO ;
9178 gmo .waitInterval = 30000 ;
9279 try {
93- connection . establish ( config .getQmgrName (), MQConnection . createMQConnectionParams ( config ) );
94- topic = connection .createTopic (String .format (TOPIC_STRING , config . getQmgrName () ));
80+ String qmgrName = config .getQmgrName ();
81+ topic = MQConnection .createTopic (String .format (TOPIC_STRING , qmgrName ));
9582 MQMessage msg = getEmptyMessage ();
9683 topic .get (msg , gmo );
9784 PCFMessage pcfResponse = new PCFMessage (msg );
9885 List <PCFClass > classes = PCFDataParser .getPCFClasses (pcfResponse );
9986 for (PCFClass pcfClass : classes ) {
100- topic = connection .createTopic (pcfClass .getTopicString ());
87+ topic = MQConnection .createTopic (pcfClass .getTopicString ());
10188 msg = getEmptyMessage ();
10289 topic .get (msg , gmo );
10390 pcfResponse = new PCFMessage (msg );
10491 List <PCFType > types = PCFDataParser .getPCFTypes (pcfResponse );
10592 for (PCFType type : types ) {
106- topic = connection .createTopic (type .getTopicString ());
93+ topic = MQConnection .createTopic (type .getTopicString ());
10794 msg = getEmptyMessage ();
10895 topic .get (msg , gmo );
10996 pcfResponse = new PCFMessage (msg );
@@ -118,14 +105,87 @@ private static ArrayList<PCFElement> getAllPublishedMetrics(Config config) {
118105 if (topic != null && topic .isOpen ()) {
119106 topic .close ();
120107 }
121- connection .close ();
122108 } catch (MQException e ) {
123109 logger .error ("Error occurred during disconnecting from topic {}. Error: " , topic .toString (), e );
124110 }
125111 }
126112 return elements ;
127113 }
128114
115+ private static List <MQObject > getMonitoringObjects (Config config ) throws MQException , IOException {
116+ List <MQObject > objects = new ArrayList <>();
117+
118+ for (MQObject .MQType type : MQObject .MQType .values ()) {
119+ switch (type ) {
120+ case QUEUE :
121+ objects .addAll (inquireMQObjectsByPatterns (config .getQueues ().get ("include" ), type , MQConstants .MQCACF_Q_NAMES ));
122+ objects .removeAll (inquireMQObjectsByPatterns (config .getQueues ().get ("exclude" ), type , MQConstants .MQCACF_Q_NAMES ));
123+ break ;
124+ case CHANNEL :
125+ objects .addAll (inquireMQObjectsByPatterns (config .getChannels ().get ("include" ), type , MQConstants .MQCACH_CHANNEL_NAMES ));
126+ objects .removeAll (inquireMQObjectsByPatterns (config .getChannels ().get ("exclude" ), type , MQConstants .MQCACH_CHANNEL_NAMES ));
127+ break ;
128+ case LISTENER :
129+ objects .addAll (inquireMQObjectsByPatterns (config .getListeners ().get ("include" ), type , MQConstants .MQCACH_LISTENER_NAME ));
130+ objects .removeAll (inquireMQObjectsByPatterns (config .getListeners ().get ("exclude" ), type , MQConstants .MQCACH_LISTENER_NAME ));
131+ break ;
132+ }
133+ }
134+ return objects ;
135+ }
136+
137+ private static List <MQObject > inquireMQObjectsByPatterns (List <String > rules , MQObject .MQType type , int getValueParam ) throws MQException , IOException {
138+
139+ List <MQObject > objects = new ArrayList <>();
140+ if (rules != null && !rules .isEmpty ()) {
141+ PCFMessageAgent agent = new PCFMessageAgent ();
142+ agent .connect (MQConnection .getQueueManager ());
143+ for (String rule : rules ) {
144+ PCFMessage pcfCommand = preparePCFCommand (type , rule );
145+ PCFMessage [] pcfResponse = agent .send (pcfCommand );
146+ if (!type .equals (MQObject .MQType .LISTENER )) {
147+ String [] names = (String []) pcfResponse [0 ].getParameterValue (getValueParam );
148+
149+ for (int index = 0 ; index < names .length ; index ++) {
150+ String objName = names [index ].trim ();
151+ if (!objName .startsWith ("AMQ." )) {
152+ objects .add (new MQObject (objName , type ));
153+ }
154+ }
155+ } else {
156+ for (int index = 0 ; index < pcfResponse .length ; index ++) {
157+ objects .add (new MQObject (pcfResponse [index ].getParameterValue (MQConstants .MQCACH_LISTENER_NAME ).toString ().trim (), type ));
158+ }
159+ }
160+ }
161+ agent .disconnect ();
162+ }
163+
164+ return objects ;
165+ }
166+
167+ private static PCFMessage preparePCFCommand (MQObject .MQType type , String name ) {
168+ PCFMessage command ;
169+ switch (type ) {
170+ case QUEUE :
171+ command = new PCFMessage (MQConstants .MQCMD_INQUIRE_Q_NAMES );
172+ command .addParameter (MQConstants .MQCA_Q_NAME , name );
173+ command .addParameter (MQConstants .MQIA_Q_TYPE , MQConstants .MQQT_LOCAL );
174+ break ;
175+ case CHANNEL :
176+ command = new PCFMessage (MQConstants .MQCMD_INQUIRE_CHANNEL_NAMES );
177+ command .addParameter (MQConstants .MQCACH_CHANNEL_NAME , name );
178+ break ;
179+ case LISTENER :
180+ command = new PCFMessage (MQConstants .MQCMD_INQUIRE_LISTENER );
181+ command .addParameter (MQConstants .MQCACH_LISTENER_NAME , name );
182+ break ;
183+ default :
184+ throw new IllegalStateException ("Unexpected value: " + type );
185+ }
186+ return command ;
187+ }
188+
129189 private static void createShutdownHook () {
130190 Runtime .getRuntime ().addShutdownHook (new Thread (() -> {
131191 logger .info ("Exporter finishes all activities..." );
@@ -142,6 +202,7 @@ private static void createShutdownHook() {
142202 logger .debug ("Stopping HTTP server..." );
143203 server .stop ();
144204 }
205+ MQConnection .close ();
145206 logger .info ("Goodbye!" );
146207 LogManager .shutdown ();
147208 }));
0 commit comments