-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSendSMSSTShttp.java
More file actions
399 lines (342 loc) · 11.4 KB
/
SendSMSSTShttp.java
File metadata and controls
399 lines (342 loc) · 11.4 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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package Jvakt;
/*
* 2025-06-09 V.05 Michael Ekdal Changed the order of the createStatement to accomodate changes in postgresql-42.7.6.jar
* 2024-09-02 V.04 Michael Ekdal Added sleep between SMS sends
* 2024-09-02 V.03 Michael Ekdal Increased time out and removed space from the URL string
* 2024-07-09 V.02 Michael Ekdal Added more info when reporting in status to Jvakt server.
* 2024-02-05 V.01 Michael Ekdal SendSMSSTShttp created
*/
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.*;
public class SendSMSSTShttp {
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 String expected = "";
static String reply = "";
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;
static String config = null;
static File configF;
static String body = "Jvakt: ";
static int serrors = 0;
static List<String> listTo;
static String toSMSSTSW;
static String toSMS;
static String SMShost;
static String SMSport;
static int SMSporti;
static Socket sock = null;
static OutputStreamWriter osw;
static InputStreamReader isr;
static String value = "";
static InetAddress inet;
static String agent = null;
static String hosturl;
static String SMSuser = "jVakt";
static String SMSpwd = "xxxx";
static int tout = 5000 ;
// public static void main(String[] args ) throws IOException, UnknownHostException {
public static void main(String[] args ) {
String version = "SendSMSSTShttp ";
version += getVersion()+".05";
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();
listTo = new ArrayList<String>(); // All SMS numbers.
String[] toAddr = toSMSSTSW.split("\\,");
for(int i=0 ; i<toAddr.length;i++) {
// System.out.println(toAddr[i]);
listTo.add(toAddr[i]);
}
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.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?
while (rs.next()) {
// swHits = true;
swTiming = false;
swFound = true;
//--
for (int i = 1; i <= 9; i++) {
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")) {
errors++;
}
else if (rs.getInt("prio") >= 30 && rs.getString("status").contentEquals("ERR")) {
warnings++;
}
else if (rs.getString("status").contentEquals("INFO") || rs.getString("status").contentEquals("OK")) {
infos++;
}
else if (rs.getString("status").startsWith("TOut")) {
warnings++;
}
else if (rs.getString("status").contentEquals("OK")) {
infos++;
}
else {
infos++;
}
}
}
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());
}
if (errors > 0) {
errors = errors / 7;
body = body + "Errors: " + errors + " ";
}
if (warnings > 0) {
warnings = warnings / 7;
body = body + "Warnings: " + warnings + " ";
}
if (errors == 0 && warnings == 0) {
body = body + "all is OKAY! ";
}
if (infos > 0) {
infos = infos / 7;
body = body + "Infos: " + infos + " ";
}
if (!swDB) {
body = "\n - Jvakt Database not accessible ! -\n";
swMail = true;
}
if (!swServer) {
if (swDB) {
body = "";
}
body = body + "- Jvakt Server not accessible ! ";
swMail = true;
}
// System.out.println("\n" + body );
if (swMail && !swDormant) {
if (sendSMS()) try {sendSTS(true);} catch (IOException e) { e.printStackTrace(); }
else try {sendSTS(false);} catch (IOException e) { e.printStackTrace();}
}
// try {sendSTS(true);} 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");
// int jvporti = Integer.parseInt(jvport);
jvhost = prop.getProperty("jvhost");
toSMSSTSW = prop.getProperty("toSMSSTS");
SMSuser = prop.getProperty("SMSuser");
SMSpwd = prop.getProperty("SMSpwd");
if (SMSpwd.startsWith("==y")) {
byte[] decodedBytes = Base64.getDecoder().decode(SMSpwd.substring(3));
String decodedString = new String(decodedBytes);
SMSpwd=decodedString;
}
SMShost = prop.getProperty("SMShost");
SMSport = prop.getProperty("SMSport");
SMSporti = Integer.parseInt(SMSport);
String mode = prop.getProperty("mode");
if (!mode.equalsIgnoreCase("active")) swDormant = true;
input.close();
} catch (IOException ex) {
// ex.printStackTrace();
}
}
// Connects to the SMS terminal and sends the text
static boolean sendSMS() {
boolean swOK = false;
System.out.println("--- Sending Jvakt Status SMS ---\n");
// First set the default cookie manager.
java.net.CookieManager cm = new java.net.CookieManager(null, CookiePolicy.ACCEPT_ALL);
java.net.CookieHandler.setDefault(cm);
Date cacheexpiration;
if (body.length() > 140 ) body = body.substring(0, 139);
body = body.replace('_', '-'); // replace _ with - because SMS creates a �
body = body.replace('Å', 'A');
body = body.replace('Ä', 'A');
body = body.replace('Ö', 'O');
body = body.replace('å', 'a');
body = body.replace('ä', 'a');
body = body.replace('ö', 'o');
body = body.trim();
body = body.replaceAll("[^a-zA-Z0-9.,:-></]" , " ");
body = body.replaceAll("\\s+","%20");
// System.out.println("Sending body: "+body );
// Loop on all phone numbers
for(Object object : listTo) {
// String element = (String) object;
// swOK = false;
toSMS = (String) object;
System.out.println("--- SMS to:"+toSMS +" Body: " + body );
// Connect to SMS-Server
try {
// hosturl ="http://www.ekdal.se/";
hosturl ="http://"+SMShost+":"+SMSport+"/cgi-bin/sms_send?username="+SMSuser+"&password="+SMSpwd+"&number="+toSMS+"&text="+body;
System.out.println("\n-- URL : "+hosturl);
URL url = new URL(hosturl);
URLConnection con = url.openConnection(); // new
con.addRequestProperty("User-Agent", "Mozilla");
con.setReadTimeout(tout);
con.setConnectTimeout(tout);
if (con.getExpiration() > 0) {
cacheexpiration = new Date(con.getExpiration());
System.out.println("-- Cache expiration "+cacheexpiration);
} else System.out.println("-- Cache expiration 0");
BufferedReader httpin = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
System.out.println(" read line" );
while ((inputLine = httpin.readLine()) != null) {
System.out.println(inputLine);
swOK = true;
if (inputLine.toUpperCase().indexOf("OK") >= 0 ) {
System.out.println("-- SMS sent! ");
swOK = true;
}
else {
System.out.println("-- SMS failed! ");
swOK = false;
}
}
httpin.close();
}
catch (Exception e) { System.out.println(e); }
try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace();}
}
if (swOK) {
System.out.println("RETURN true");
try {sendSTS(true);} catch (IOException e) { e.printStackTrace();}
return true;
}
else {
System.out.println("Failure sending SMS message");
try {sendSTS(false);} catch (IOException e) { e.printStackTrace();}
return false;
}
}
// 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-SendSMSSTShttp");
if (STS) {
jmsg.setBody("The SendSMSSTShttp program is working. "+configF.getCanonicalPath());
jmsg.setRptsts("OK");
}
else {
jmsg.setBody("The SendSMSSTShttp 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 Jvakt-SendSMSSTShttp ---");
jm.close();
}
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;
}
}