-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSendMailSTS.java
More file actions
384 lines (336 loc) · 12 KB
/
SendMailSTS.java
File metadata and controls
384 lines (336 loc) · 12 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package Jvakt;
/*
* 2025-06-09 V.57 Michael Ekdal Changed the order of the createStatement to accomodate changes in postgresql-42.7.6.jar
* 2024-07-09 V.56 Michael Ekdal Added more info when reporting in status to Jvakt server.
* 2023-01-09 V.55 Michael Ekdal Added send of the status to Jvakt server
* 2022-06-23 V.54 Michael Ekdal Added getVersion() to get at consistent version throughout all classes.
*/
import java.io.*;
import java.net.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
import Jvakt.Message;
import jakarta.mail.*;
import jakarta.mail.Authenticator;
import jakarta.mail.PasswordAuthentication;
public class SendMailSTS {
static String DBUrl = "jdbc:postgresql://localhost:5433/Jvakt";
static Connection conn = null;
static boolean swDelete;
static boolean swTiming;
static boolean swFound;
static boolean swShDay; // set when the scheduled day is active
static boolean swDormant = false;
static boolean swDB = true;
static boolean swServer = true;
static java.sql.Date zDate;
static java.sql.Timestamp zD;
static java.sql.Timestamp zTs;
static java.sql.Time zT;
static Long lhou, lmin, Lsec, Lrptdat, Lchktim;
static int errors = 0;
static int warnings = 0;
static int infos = 0;
static int resolved = 0;
static String ebody = "";
static String wbody = "";
static String rbody = "";
static Date now;
static InetAddress inet;
static String agent = null;
static String tblStr = "<TABLE COLS=2 BORDER=4 cellpadding=\"3\" width=\"100%\" >";
static String tblEnd = "</TABLE>";
static String hdrStrG = "<TH BGCOLOR=\"#00FF00\"><FONT SIZE=5>"; // Green
static String hdrStrY = "<TH BGCOLOR=\"#FFFF00\"><FONT SIZE=5>"; // Yellow
static String hdrStrR = "<TH BGCOLOR=\"#FF6600\"><FONT SIZE=5>"; // Red
static String hdrStrM = "<TH BGCOLOR=\"#FF00FF\"><FONT SIZE=5>"; // Magenta
static String hdrEnd = "</TH>";
static String rowStr = "<TR>";
static String rowEnd = "</TR>";
static String boxStrG = "<TD BGCOLOR=\"#00FF00\">"; // Green
static String boxStrY = "<TD BGCOLOR=\"#FFFF00\">"; // Yellow
static String boxStrR = "<TD BGCOLOR=\"#FF6600\">"; // Red
static String boxStrM = "<TD BGCOLOR=\"#FF00FF\">"; // Magenta
static String boxStrB = "<TD BGCOLOR=\"#CCEEFF\">"; // Light blue
static String boxStr = "<TD>";
static String boxEnd = "</TD>";
static String database = "jVakt";
static String dbuser = "jVakt";
static String dbpassword = "??";
static String dbhost = "localhost";
static String dbport = "5433";
static String jvhost = "localhost";
static String jvport = "1956";
static int port = 1956;
//Declare recipient's & sender's e-mail id.
static String PtoEmail;
static String PfromEmail = "";
static String Puname = "";
static String Ppwd = "";
static String Psmtphost = "";
static String Psmtpport = "";
static int smtpporti;
static String config = null;
static File configF;
public static void main(String[] args ) throws IOException, UnknownHostException {
String version = "SendMailSTS ";
version += getVersion()+".57";
String subject = "";
String body = "";
String value = "";
//Declare recipient's & sender's e-mail id.
final String toEmail;
final String fromEmail;
// final String uname;
final String pwd;
final String smtphost;
// final String smtpport;
for (int i=0; i<args.length; i++) {
if (args[i].equalsIgnoreCase("-config")) config = args[++i];
}
if (config == null ) configF = new File("Jvakt.properties");
else configF = new File(config,"Jvakt.properties");
System.out.println("----- Jvakt: "+new Date()+" Version: "+version+" - config file: "+configF);
boolean swMail = false;
getProps();
toEmail = PtoEmail;
fromEmail = PfromEmail;
// uname = Puname;
pwd = Ppwd;
smtphost = Psmtphost;
// smtpport = Psmtpport;
// System.out.println(s);
//create Authenticator object to pass in Session.getInstance argument
Authenticator auth = new Authenticator() {
//override the getPasswordAuthentication method
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(fromEmail, pwd);
}
};
//Set properties and their values
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.ssl.trust", "*");
props.put("mail.smtp.host", smtphost);
props.put("mail.smtp.port", smtpporti);
Statement stmt = null;
String s;
// boolean swHits;
// String cause = "";
swServer = true;
// try {
port = Integer.parseInt(jvport);
SendMsg jm = new SendMsg(jvhost, port); // kollar om JvaktServer är tillgänglig.
// System.out.println(jm.open());
if (jm.open().startsWith("DORMANT")) swDormant = true;
else swDormant = false;
jm.close();
// }
// catch (IOException e1) {
// swServer = false;
// System.err.println(e1);
// System.err.println(e1.getMessage());
// }
// System.out.println("swServer :" + swServer);
try {
Class.forName("org.postgresql.Driver").newInstance();
DBUrl = "jdbc:postgresql://"+dbhost+":"+dbport+"/"+database;
// System.out.println(DBUrl);
conn = DriverManager.getConnection(DBUrl,dbuser,dbpassword);
conn.setAutoCommit(true);
s = new String("select * from console order by credat desc;");
// System.out.println(s);
// stmt = conn.createStatement(ResultSet.CONCUR_UPDATABLE,ResultSet.TYPE_SCROLL_INSENSITIVE);
// stmt = conn.createStatement(ResultSet.CONCUR_UPDATABLE,ResultSet.TYPE_FORWARD_ONLY,ResultSet.CLOSE_CURSORS_AT_COMMIT );
stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY,ResultSet.CLOSE_CURSORS_AT_COMMIT );
stmt.setFetchSize(1000);
ResultSet rs = stmt.executeQuery(s);
// swHits = false; // is there already a record?
body = tblStr;
while (rs.next()) {
// swHits = true;
swTiming = false;
swFound = true;
body = body +rowStr;
//--
for (int i = 1; i <= 9; i++) {
if (i==1) continue; // not interested in showing count
if (i==5) continue; // not interested in showing condat
if (i==9) continue; // not interested in showing agent
value = rs.getString (i);
if (rs.getInt("prio") < 30 && rs.getString("status").contentEquals("ERR")) {
body = body + boxStrM + value + boxEnd;
errors++;
}
else if (rs.getInt("prio") >= 30 && rs.getString("status").contentEquals("ERR")) {
body = body + boxStrR + value + boxEnd;
warnings++;
}
else if (rs.getString("status").contentEquals("INFO")) {
body = body + boxStrB + value + boxEnd;
infos++;
}
else if (rs.getString("status").startsWith("TOut")) {
body = body + boxStrY + value + boxEnd;
warnings++;
}
else if (rs.getString("status").contentEquals("OK")) {
body = body + boxStrG + value + boxEnd;
infos++;
}
else {
body = body + boxStrB + value + boxEnd;
infos++;
}
}
body = body + rowEnd;
//--
}
swMail = true;
rs.close();
stmt.close();
conn.close();
}
catch (SQLException e) {
System.err.println("*** SQLExeption");
swDB = false;
System.err.println(e);
System.err.println(e.getMessage());
}
catch (Exception e) {
swDB = false;
System.err.println(e);
System.err.println(e.getMessage());
}
// finally {
// subject = "Status: ";
// subject = "";
// }
subject = "* STATUS * ";
if (errors > 0) {
errors = errors / 6;
subject = subject + "Errors: " + errors + " ";
}
if (warnings > 0) {
warnings = warnings / 6;
subject = subject + "Warnings: " + warnings + " ";
}
if (errors == 0 && warnings == 0) {
subject = subject + "Jvakt: all is OKAY! ";
}
if (infos > 0) {
infos = infos / 6;
subject = subject + "Infos: " + infos + " ";
}
body = body + tblEnd;
if (!swDB) {
subject = "\n - Jvakt Database not accessible ! -\n";
body = "<br><p> - Jvakt Database not accessible ! -</p>";
swMail = true;
}
if (!swServer) {
if (swDB) {
subject = "";
body = "";
}
subject = subject + "\n - Jvakt Server not accessible ! - \n";
body = body + "<br><p> - Jvakt Server not accessible ! -</p>";
swMail = true;
}
if (errors == 0 && warnings == 0 && infos == 0 ) body = body + "<br><p> All is OKAY! There is nothing to report!</p>";
else body = body + "<br><p> - On-call Duty -<br>Only rows with Prio less than 30 is to be handeled out of office hours !</p>";
now = new Date();
subject = subject + " -- " + now;
System.out.println("-> Mail receivers: " + toEmail );
System.out.println("-> Mail subject : " + subject );
System.out.println("-> Mail body : " + body );
if (swMail && !swDormant) {
Session session = Session.getInstance(props, auth);
if (EmailUtil.sendEmail(session, toEmail,subject, body, fromEmail)) {
try {sendSTS(true);} catch (IOException e) { e.printStackTrace();}
}
else {
try {sendSTS(false);} catch (IOException e) { e.printStackTrace();}
}
}
}
static void getProps() {
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream(configF);
prop.load(input);
// get the property value and print it out
database = prop.getProperty("database");
dbuser = prop.getProperty("dbuser");
dbpassword = prop.getProperty("dbpassword");
if (dbpassword.startsWith("==y")) {
byte[] decodedBytes = Base64.getDecoder().decode(dbpassword.substring(3));
String decodedString = new String(decodedBytes);
dbpassword=decodedString;
}
dbhost = prop.getProperty("dbhost");
dbport = prop.getProperty("dbport");
jvport = prop.getProperty("jvport");
jvhost = prop.getProperty("jvhost");
PtoEmail = prop.getProperty("toSts");
PfromEmail= prop.getProperty("fromEmail");
Puname = prop.getProperty("smtpuser");
Ppwd = prop.getProperty("smtppwd");
if (Ppwd.startsWith("==y")) {
byte[] decodedBytes = Base64.getDecoder().decode(Ppwd.substring(3));
String decodedString = new String(decodedBytes);
Ppwd=decodedString;
}
Psmtphost = prop.getProperty("smtphost");
Psmtpport = prop.getProperty("smtpport");
smtpporti = Integer.parseInt(Psmtpport);
String mode = prop.getProperty("mode");
if (!mode.equalsIgnoreCase("active")) swDormant = true;
input.close();
} catch (IOException ex) {
// ex.printStackTrace();
}
}
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;
}
// sends status to the Jvakt server
static protected void sendSTS( boolean STS) throws IOException {
System.out.println("--- Connecting to "+jvhost+":"+jvport);
Message jmsg = new Message();
SendMsg jm = new SendMsg(jvhost, port);
// System.out.println(jm.open());
jm.open();
jmsg.setId("Jvakt-SendMailSTS");
if (STS) {
jmsg.setBody("The SendMailSTS program is working. "+configF.getCanonicalPath());
jmsg.setRptsts("OK");
}
else {
jmsg.setBody("The SendMailSTS program is not working! "+configF.getCanonicalPath());
jmsg.setRptsts("ERR");
}
jmsg.setType("T");
inet = InetAddress.getLocalHost();
// System.out.println("-- Inet: "+inet);
agent = inet.toString();
jmsg.setAgent(agent);
if (!jm.sendMsg(jmsg)) System.out.println("--- Rpt to Jvakt Failed for SendMailSTS ---");
jm.close();
}
}