-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMonAS400msgq.java
More file actions
266 lines (226 loc) · 8.24 KB
/
MonAS400msgq.java
File metadata and controls
266 lines (226 loc) · 8.24 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package Jvakt;
/*
* 2024-05-09 V.56 Michael Ekdal Added opportunity to change the default sts and sev in the MonAS400msgq.csv file
* 2022-11-15 V.55 Michael Ekdal Fixed: When -dormant, "All reports will be forced to be 30 or higher"
* 2022-11-15 V.55 Michael Ekdal Tested: Used with Java 17.
* 2022-06-23 V.54 Michael Ekdal Added getVersion() to get at consistent version throughout all classes.
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.InetAddress;
/*
2021-01-13 V1.0 Ekdal: Monitors a msgq and auto response is possible.
*/
import java.util.*;
import com.ibm.as400.access.*;
public class MonAS400msgq {
static Date now;
static AS400 as400;
static String config = null;
static File configF;
// @SuppressWarnings("unchecked")
public static void main(String[] args) throws IOException {
now = new Date();
String version = getVersion()+".56";
// Displays help
if (args.length == 0) {
System.out.println("\n*** MonAS400msgq "+version+" ***" +
"\n*** by Michael Ekdal Perstorp Sweden. ***");
System.out.println("\n\nThe parameters and their meaning are:\n"+
"\n-q \tThe name of the AS400 message queue, like \" /qsys.lib/itoctools.lib/xxx.msgq\" "+
"\n-ah \tHostname of the AS400 server."+
"\n-us \tThe AS400 user."+
"\n-pw \tThe AS400 password."+
"\n-config \tThe dir of the input files. Like: \"-config c:\\Temp\" (MonAS400msgq.csv must be present) "+
"\n-jh \tHostname of the Jvakt server."+
"\n-jp \tThe port of the Jvakt server."+
"\n-dormant \tAll reports will be forced to be 30 or higher. No autoreply will be made.");
System.exit(12);
}
String msgq = null;
String ashost = null;
String asuser = null;
String aspw = null;
String agent = "";
InetAddress inet;
String jvhost = "127.0.0.1";
String jport = "1956";
int jvport = 1956;
String msg;
String msgL;
String rpy;
String jobName;
String msgId;
int severity;
String[] words;
String s = "";
boolean swAutoreply = false;
boolean swDormant = false;
int sevDefault = 1;
String stsDefault = "ERR";
int sev;
String sts;
// Enumeration<QueuedMessage> q = null;
byte [] msgkey = null;
// reads command line arguments
for (int i = 0; i < args.length; i++) {
if (args[i].equalsIgnoreCase("-q")) msgq = args[++i];
if (args[i].equalsIgnoreCase("-ah")) ashost = args[++i];
if (args[i].equalsIgnoreCase("-us")) asuser = args[++i];
if (args[i].equalsIgnoreCase("-pw")) aspw = args[++i];
if (args[i].equalsIgnoreCase("-jh")) jvhost = args[++i];
if (args[i].equalsIgnoreCase("-jp")) jport = args[++i];
if (args[i].equalsIgnoreCase("-config")) config = args[++i];
if (args[i].equalsIgnoreCase("-dormant")) swDormant = true;
}
// checks the mandatory parameters
if (ashost == null) {
System.out.println("\n\n\t *** -ah is missing (host)");
System.exit(8);
}
if (msgq == null ) {
System.out.println("\n\n\t *** -q is missing (msgq)");
System.exit(8);
}
// Read filter file
if (config == null ) {
configF = new File("MonAS400msgq.csv");
}
else {
// System.out.println("config is "+config);
configF = new File(config,"MonAS400msgq.csv");
}
System.out.println("--- Jvakt MonAS400msgq ("+version+") --- "+now+" ---");
// Importing filer file.
BufferedReader inokay;
inokay = new BufferedReader(new FileReader(configF));
int ecount = 0;
String[] Ftab = new String[1000];
while((s = inokay.readLine())!= null) {
if (s.startsWith("#")) continue;
if (s.length() == 0) continue;
Ftab[ecount++] = s.toUpperCase();
words = s.toUpperCase().split(";",4);
if (words[0].startsWith("*")) {
sevDefault = Integer.parseInt(words[1]);
stsDefault = words[2];
}
}
inokay.close();
System.out.println(" Imported "+ecount+" rows from "+configF);
System.out.println(" Default values for unhandled messages are "+sevDefault+" and "+stsDefault);
if (swDormant) System.out.println(" Dormant mode on");
// System.out.println( " count: " + ecount);
try {
inet = InetAddress.getLocalHost();
// System.out.println(" Inet: "+inet);
agent = inet.toString();
}
catch (Exception e) { System.out.println(e); }
jvport = Integer.parseInt(jport);
if (args.length == 0) System.exit(4);
// As400
System.out.println(" AS400 connecting to host " + ashost);
as400 = new AS400(ashost,asuser,aspw);
System.out.println(" Connected to system " + as400.getSystemName());
// System.out.println(" CCSID " + as400.getCcsid());
System.out.println(" Connecting to Msgq " + msgq );
MessageQueue queue = new MessageQueue(as400, msgq);
System.out.println(" Connected to Msgq " + queue.getPath());
QueuedMessage msge = null;
// Starts a never ending loop
for (;;) {
// System.out.println("Queue length: "+queue.getLength());
if (swAutoreply) {
try { Thread.sleep(60000); } catch (InterruptedException e) { e.printStackTrace();}
swAutoreply = false;
}
try { msge = queue.receive(null,0,queue.OLD,queue.ANY); } catch( Exception e ) { e.printStackTrace();}
if (msge == null) {
try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace();}
continue;
}
if (swDormant) {
sev = 51;
sts = "INFO";
}
else {
sev = sevDefault;
sts = stsDefault;
}
rpy = "";
swAutoreply = false;
msgkey = msge.getKey();
msg = msge.getText();
jobName = msge.getFromJobName();
severity = msge.getSeverity();
msgId = msge.getID();
// System.out.println("GetType "+msge.getType());
// if (msgId.length()<1) { System.out.println(new Date()+" msgId empty"); continue; }
// System.out.println("* Msge: "+msgId + " "+severity+ " " + jobName + " - " + msg );
msgL = jobName+" "+msgId+" "+msg;
for ( int k = 0; k < ecount ; k++) { // check if any scan string is present in the line.
words = Ftab[k].split(";",4);
// System.out.println("Split length "+ words.length +" "+ words[0] + " "+words[1]+ " " + words[2] + " " + words[3] );
if (msgL.toUpperCase().indexOf(words[0]) >= 0) {
if (words[1].startsWith("X")) {
sev = -1;
sts = "X";
}
else {
sev = Integer.parseInt(words[1]);
if (swDormant && sev < 30) sev = 30;
sts = words[2];
}
if (words.length>3) rpy = words[3];
else rpy = "";
System.out.println(new Date()+" - id:" + msgId + " sev:"+sev+" sts:"+sts+ " job:" + jobName + " msg:" + msg + " * String: " + words[0] );
if (!swDormant && msge.getType() == msge.INQUIRY && rpy.length()>0) {
try { queue.reply(msgkey, rpy, false); } catch( Exception e ) { e.printStackTrace();}
msg="Autoreply "+rpy+" -> "+msg;
swAutoreply = true;
break;
}
}
}
if (sev < 0 ) continue; // no Jvakt update. continue with the next message.
try {
Message jmsg = new Message();
SendMsg jm = new SendMsg(jvhost, jvport);
String openResponse = jm.open();
System.out.println("Open connection to Jvakt server "+jvhost+":"+jvport+" Response: "+openResponse);
while (openResponse.startsWith("failed")) {
System.out.println("Connection to Jvakt server "+jvhost+":"+jvport+" failed");
try { Thread.sleep(60000); } catch (InterruptedException e) { e.printStackTrace();}
openResponse = jm.open();
}
jmsg.setId(ashost+"-"+jobName+"-"+msgId);
jmsg.setPrio(sev);
jmsg.setRptsts(sts);
jmsg.setBody(msg);
jmsg.setType("I");
jmsg.setAgent(agent);
System.out.println(" Jvakt-> "+jmsg.getType() +" "+ jmsg.getId() +" "+ jmsg.getPrio() +" "+ jmsg.getRptsts() +" - "+ jmsg.getBody() +" "+ jmsg.getAgent() );
if (jm.sendMsg(jmsg)) { System.out.println("-- Rpt Delivered --"); }
else { System.out.println("-- Rpt Failed --"); }
jm.close();
System.out.println("-- Read next entry in the message queue --");
}
catch( Exception e ) { e.printStackTrace(); System.exit(12);}
}
}
static private String getVersion() {
String version = "0";
try {
Class<?> c1 = Class.forName("Jvakt.Version",false,ClassLoader.getSystemClassLoader());
Version ver = new Version();
version = ver.getVersion();
}
catch (java.lang.ClassNotFoundException ex) {
version = "?";
}
return version;
}
}