View Javadoc
1   /**
2    * This Source Code Form is subject to the terms of the Mozilla Public
3    * License, v. 2.0. If a copy of the MPL was not distributed with this
4    * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5    *
6    * If it is not possible or desirable to put the notice in a particular
7    * file, then You may include the notice in a location (such as a LICENSE
8    * file in a relevant directory) where a recipient would be likely to look
9    * for such a notice.
10  
11   * 
12   */
13   
14  /*  ---------------------------------------------------------------------------
15   *  U.S. Government, Department of the Army
16   *  Army Materiel Command
17   *  Research Development Engineering Command
18   *  Communications Electronics Research Development and Engineering Center
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   * @author AO
37   */
38  public class FgsmsServerCrashAlerters {
39  
40      boolean running = false;
41      /**
42       * This program will loop forever and periodically test to see if the PCS
43       * service is available and thus also prove that the database is running. If
44       * this assertion fails, an email is dispatched.
45       *
46       * @param args the command line arguments
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 }