1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.miloss.fgsms.dependency;
23
24 import java.sql.Connection;
25 import java.sql.PreparedStatement;
26 import java.sql.ResultSet;
27 import java.sql.SQLException;
28
29 import org.miloss.fgsms.common.Utility;
30 import org.apache.log4j.Level;
31 import org.miloss.fgsms.common.Logger;;
32 import org.apache.log4j.PropertyConfigurator;
33 import org.miloss.fgsms.common.DBUtils;
34
35
36
37
38
39
40
41
42 public class DependencyScanner {
43
44 static final Logger log = Logger.getLogger("fgsms.DependencyScanner");
45
46 public static void main(String args[]) {
47 PropertyConfigurator.configure("log4j.properties");
48 new DependencyScanner().go(false, 0);
49 }
50
51 public void go(boolean pooled, long timestampAtLastRunTime) {
52
53 Connection con = null;
54 if (pooled) {
55 con = Utility.getPerformanceDBConnection();
56 } else {
57 con = Utility.getPerformanceDB_NONPOOLED_Connection();
58 }
59 long timer = System.currentTimeMillis();
60 long threadsSearched=0;
61 long dependenciesFound=0;
62 PreparedStatement cmd=null;
63 PreparedStatement c2=null;
64 ResultSet rs=null;
65 ResultSet r2=null;
66 try {
67 cmd = con.prepareStatement("select * from (select threadid,count(threadid) as t from rawdata where utcdatetime >= ? group by threadid ) as foo where foo.t > 3;");
68 cmd.setLong(1, timestampAtLastRunTime - (30 * 1000));
69 rs = cmd.executeQuery();
70 while (rs.next()) {
71 threadsSearched++;
72 try{
73 c2 = con.prepareCall("select url,monitorsource,soapaction from rawdata where threadid=? order by utcdatetime asc;");
74 c2.setString(1, rs.getString("threadid"));
75
76 r2 = c2.executeQuery();
77 record first = null;
78 record second = null;
79 while (r2.next()) {
80 if (first == null) {
81
82 first = new record();
83 first.url = r2.getString("url");
84 first.action = rs.getString("soapaction");
85 first.hostname = rs.getString("monitorsource");
86 if (r2.next()) {
87 if (second==null)
88 second=new record();
89 second.url = r2.getString("url");
90 second.action = rs.getString("soapaction");
91 second.hostname = rs.getString("monitorsource");
92 }
93 } else {
94 if (second==null)
95 second=new record();
96 second.url = r2.getString("url");
97 second.action = rs.getString("soapaction");
98 second.hostname = rs.getString("monitorsource");
99 }
100
101
102
103
104 if (first == null || second == null) {
105 continue;
106 }
107 if ((!first.url.equalsIgnoreCase(second.url) || !(first.action.equalsIgnoreCase(second.action))) && first.hostname.equalsIgnoreCase(second.hostname)) {
108 dependenciesFound++;
109 RecordDependency(first.url, first.action, second.url, second.action, con);
110 }
111
112
113 first = second;
114 second = null;
115 }
116 }catch (Exception ex){
117 log.log(Level.ERROR, null, ex);
118 } finally {
119 DBUtils.safeClose(r2);
120 DBUtils.safeClose(c2);
121 }
122
123 }
124 } catch (Exception ex) {
125 log.log(Level.ERROR, null, ex);
126 }
127 finally{
128 DBUtils.safeClose(rs);
129 DBUtils.safeClose(cmd);
130 DBUtils.safeClose(con);
131 }
132
133 log.log(Level.INFO, "Web Service Threads Searched: " + threadsSearched + " Dependencies Found: " + dependenciesFound + " took " + (System.currentTimeMillis()-timer) + " ms to run");
134 }
135
136 private void RecordDependency(String url, String action, String url0, String action0, Connection con) {
137 PreparedStatement cmd=null;
138 try {
139 cmd = con.prepareStatement("INSERT INTO dependencies(sourceurl, sourcesoapaction, destintationurl, destinationsoapaction) VALUES (?, ?, ?, ?);");
140 cmd.setString(1, url);
141 cmd.setString(2, action);
142 cmd.setString(3, url0);
143 cmd.setString(4, action0);
144 cmd.executeUpdate();
145 } catch (Exception ex) {
146 log.log(Level.ERROR, null, ex);
147 } finally {
148 DBUtils.safeClose(cmd);
149 }
150 }
151
152
153
154
155 class record {
156
157 public String url;
158 public String action;
159 public String hostname;
160 }
161 }
162
163
164
165
166
167
168
169
170
171