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.agents.qpidpy;
22  
23  import java.io.File;
24  import java.io.RandomAccessFile;
25  import java.nio.channels.FileChannel;
26  import java.nio.channels.FileLock;
27  import org.apache.log4j.Level;
28  import org.miloss.fgsms.common.Logger;;
29  import org.apache.log4j.PropertyConfigurator;
30  import org.miloss.fgsms.agentcore.ConfigurationException;
31  import org.miloss.fgsms.agentcore.StatusHelper;
32  import org.miloss.fgsms.common.IpAddressUtility;
33  import org.miloss.fgsms.common.Utility;
34  import org.miloss.fgsms.services.interfaces.common.PolicyType;
35  
36  /**
37   * fgsms monitor for Qpid/MRG AMQP brokers based on C++. 
38   *
39   * @author AO fgsms monitor for Qpid/MRG AMQP brokers based on C++. This
40   * command runs a customized version of qpid-stats, parses the output and then
41   * records it in the fgsms database.
42   */
43  public class Mainv2 {
44  
45      static Logger log = Logger.getLogger("fgsms.QpidPython");
46      private boolean running = true;
47      private boolean done = false;
48      private File file;
49      private FileChannel channel;
50      private FileLock lock;
51  
52      private void closeLock() {
53          try {
54              lock.release();
55          } catch (Exception e) {
56          }
57          try {
58              channel.close();
59          } catch (Exception e) {
60          }
61      }
62  
63      private void deleteFile() {
64          try {
65              file.delete();
66          } catch (Exception e) {
67          }
68      }
69  
70      private void Run(String[] args) throws ConfigurationException {
71  
72          try {
73              file = new File("amqp.lck");
74              channel = new RandomAccessFile(file, "rw").getChannel();
75              lock = channel.tryLock();
76          } catch (Exception e) {
77              // already locked
78              closeLock();
79              System.out.println("Could not obtain the lock, this means that either this program is already running or something went wrong and the file amqp.lck needs to be deleted.");
80              return;
81          }
82          if (lock == null) {
83              closeLock();
84              System.out.println("Could not obtain the lock, this means that either this program is already running or something went wrong and the file amqp.lck needs to be deleted.");
85              return;
86          }
87  
88          Runtime.getRuntime().addShutdownHook(new RunWhenShuttingDown());
89          PropertyConfigurator.configure("log4j.properties");
90          long interval = 10000;
91          if (args.length == 1) {
92              try {
93                  interval = Long.parseLong(args[0]);
94                  if (interval < 10000) {
95                      interval = 10000;
96                  }
97              } catch (Exception ex) {
98              }
99          }
100 
101 
102         String url = IpAddressUtility.modifyURL("amqp://localhost", false);
103         qpidcmdws q = new qpidcmdws();
104         while (running) {
105             try {
106                 //create/fetch the policy
107                 //org.miloss.fgsms.agentcore.PolicyFetch.TryFetchPolicy(url, PolicyType.STATISTICAL, "unspecified", Utility.getHostName());
108 
109 //                AuxHelper.CheckStatisticalPolicyAndCreate(IpAddressUtility.modifyURL("amqp://localhost", false), config, false, AuxHelper.UNSPECIFIED, SLACommon.getHostName());
110 
111                 q.Fire(url);
112 
113 
114 
115                 //SLACommon.ProcessStatisticalSLARules2(IpAddressUtility.modifyURL("amqp://localhost", false), currentitems, false);
116 
117                 //    org.miloss.fgsms.agentcore.StatusHelperViaWebService.tryUpdateStatus(true, url, "OK", false, PolicyType.STATISTICAL, AuxHelper.UNSPECIFIED, Utility.getHostName());
118 
119 
120             } catch (Exception ex) {
121                 log.log(Level.ERROR, ex);
122                 // org.miloss.fgsms.agentcore.StatusHelperViaWebService.tryUpdateStatus(true, url, "OK", false, PolicyType.STATISTICAL, AuxHelper.UNSPECIFIED, Utility.getHostName());
123             }
124             if (running) {
125                 try {
126                     log.log(Level.INFO, "Sleeping " + interval + "ms until next interation....");
127                     Thread.sleep(interval);
128                 } catch (InterruptedException ex) {
129                     log.log(Level.ERROR, null, ex);
130                 }
131             }
132 
133         }
134         done = true;
135 
136     }
137 
138     //~ Inner Classes -----------------------------------------------------------------------------
139     public class RunWhenShuttingDown extends Thread {
140 
141         public void run() {
142             System.out.println("Control-C caught. Shutting down...");
143             //keepOn = false;
144             running = false;
145             while (!done) {
146                 try {
147                     Thread.sleep(1000);
148                 } catch (InterruptedException ex) {
149                 }
150             }
151             closeLock();
152             deleteFile();
153             try {
154                 boolean TryUpdateStatus = StatusHelper.tryUpdateStatus(false, IpAddressUtility.modifyURL("amqp://localhost", false), "Agent Stopped", false, PolicyType.STATISTICAL, null, Utility.getHostName());
155                 if (!TryUpdateStatus) {
156                     System.out.println("unable to send the last statue update, see log output for details ");
157                 }
158                 //TryUpdateStatus(false, null, null);
159             } catch (ConfigurationException ex) {
160                 System.out.println("unable to send the last statue update " + ex.getMessage());
161             }
162         }
163     }
164 
165     /**
166      * @param args the command line arguments
167      */
168     public static void main(String[] args) throws ConfigurationException {
169         new Mainv2().Run(args);
170 
171     }
172 }