1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.miloss.fgsms.alerting;
22
23 import java.io.FileInputStream;
24 import java.util.Properties;
25 import javax.mail.Message;
26 import javax.mail.Session;
27 import javax.mail.Transport;
28 import javax.mail.internet.InternetAddress;
29 import javax.mail.internet.MimeMessage;
30 import org.miloss.fgsms.agentcore.ConfigLoader;
31 import org.miloss.fgsms.common.Utility;
32 import org.miloss.fgsms.services.interfaces.policyconfiguration.GlobalPolicy;
33
34
35
36
37
38 public class FgsmsServerCrashAlerters {
39
40 boolean running = false;
41
42
43
44
45
46
47
48 public static void main(String[] args) throws Exception {
49 new FgsmsServerCrashAlerters().runMain();
50 }
51 static ThreadRunner runner = null;
52
53 public static void start(String[] args) {
54 runner = new ThreadRunner();
55 t = new Thread(runner);
56 t.start();
57 }
58 static Thread t;
59
60 public static void stop(String[] args) {
61 if (t != null && t.isAlive() && runner != null) {
62 runner.m.running = false;
63 try {
64 t.join();
65 } catch (InterruptedException ex) {
66 ex.printStackTrace();
67 }
68 }
69 }
70
71 protected void runMain() throws Exception {
72 Properties p = new Properties();
73 running = true;
74 try {
75 FileInputStream fis = new FileInputStream("mail.properties");
76 p.load(fis);
77 fis.close();
78 } catch (Exception ex) {
79 ex.printStackTrace();
80 }
81 if (p == null || p.isEmpty()) {
82 System.err.println("couldn't load main.properties");
83 }
84
85
86 org.miloss.fgsms.agentcore.ConfigLoader cf = new ConfigLoader();
87
88
89 boolean istriggered = false;
90 while (running) {
91 try {
92 GlobalPolicy policy = org.miloss.fgsms.agentcore.PolicyFetch.TryFetchGlobalPolicy();
93 if (policy == null) {
94 System.out.println(System.currentTimeMillis() + " Null policy" + Utility.listStringtoString(cf.getPCS_URLS()));
95 if (!istriggered) {
96 triggerEmailAlert(p, "Couldn't reach any of the PCS urls at " + Utility.listStringtoString(cf.getPCS_URLS()));
97 System.out.println(System.currentTimeMillis() + " Email sent");
98 istriggered = true;
99 }
100 } else {
101 System.out.println(System.currentTimeMillis() + " Success");
102 istriggered = false;
103 }
104 } catch (Exception ex) {
105 ex.printStackTrace();
106 }
107 Thread.sleep(10000);
108 }
109 }
110
111 private void triggerEmailAlert(Properties p, String message) throws Exception {
112
113 Session mailSession = Session.getDefaultInstance(p);
114 Message simpleMessage = new MimeMessage(mailSession);
115
116
117 InternetAddress from = new InternetAddress(p.getProperty("sendfrom"));
118
119 simpleMessage.setFrom(from);
120
121 try {
122 simpleMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(p.getProperty("sendto")));
123 simpleMessage.setSubject("fgsms Server is down! ");
124
125 simpleMessage.setContent(
126 "server is down!" + message, "text/html; charset=ISO-8859-1");
127 Transport.send(simpleMessage);
128 } catch (Exception ex) {
129 ex.printStackTrace();
130 }
131
132 }
133 }