1
2
3
4
5
6 package org.miloss.fgsms.statistics.jobs;
7
8 import java.sql.Connection;
9 import java.sql.PreparedStatement;
10 import java.sql.ResultSet;
11 import java.sql.SQLException;
12 import java.util.ArrayList;
13 import java.util.Calendar;
14 import java.util.GregorianCalendar;
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.UUID;
18 import org.apache.log4j.Level;
19 import org.miloss.fgsms.agentcore.MessageProcessor;
20 import org.miloss.fgsms.common.DBUtils;
21 import org.miloss.fgsms.common.Utility;
22 import org.miloss.fgsms.services.interfaces.common.PolicyType;
23 import org.miloss.fgsms.services.interfaces.dataaccessservice.OperationalRecord;
24 import static org.miloss.fgsms.statistics.FgsmsStatsv2.SERVICE_NAME;
25 import static org.miloss.fgsms.statistics.FgsmsStatsv2.allitems;
26 import static org.miloss.fgsms.statistics.FgsmsStatsv2.log;
27 import static org.miloss.fgsms.statistics.FgsmsStatsv2.myUrl;
28
29
30
31
32
33 public class MachineProcessJob extends BaseJob implements Runnable {
34
35 List<Long> periods;
36
37 public MachineProcessJob(List<Long> periods) {
38 this.periods = periods;
39 }
40
41 @Override
42 public void run() {
43
44 UUID random = UUID.randomUUID();
45 MessageProcessor.getSingletonObject().processMessageInput(SERVICE_NAME, 0, myUrl, "machine/process", "system", random.toString(), new HashMap(), "", this.getClass().getCanonicalName(), "", "");
46 Connection ConfigCon = Utility.getConfigurationDBConnection();
47 Connection PerfCon = Utility.getPerformanceDBConnection();
48 try {
49 doWorkMachinesProcesses(ConfigCon, PerfCon, periods);
50 MessageProcessor.getSingletonObject().processMessageOutput(random.toString(), "success", 0, false, System.currentTimeMillis(), new HashMap());
51 } catch (Exception ex) {
52 MessageProcessor.getSingletonObject().processMessageOutput(random.toString(), "error " + ex.getMessage(), 0, true, System.currentTimeMillis(), new HashMap());
53
54 } finally {
55 DBUtils.safeClose(PerfCon);
56 DBUtils.safeClose(ConfigCon);
57 }
58 }
59
60 private void doWorkMachinesProcesses(Connection ConfigCon, Connection PerfCon, List<Long> periods) throws Exception {
61 PreparedStatement com = null;
62 ResultSet rs = null;
63 try {
64 long now = System.currentTimeMillis();
65
66 if (ConfigCon == null || PerfCon == null) {
67 log.log(Level.ERROR, "doWorkMachinesProcesses database unavailable");
68 return;
69 }
70 com = ConfigCon.prepareStatement("select uri,policytype from servicepolicies where policytype=? or policytype=?;");
71 com.setInt(1, PolicyType.MACHINE.ordinal());
72 com.setInt(2, PolicyType.PROCESS.ordinal());
73 rs = com.executeQuery();
74 while (rs.next()) {
75
76 log.log(Level.INFO, "calculating statistics for " + rs.getString("uri"));
77 String t = allitems;
78
79 for (int i = 0; i < periods.size(); i++) {
80 insertRow(PerfCon, rs.getString("uri"), t, periods.get(i));
81
82 PreparedStatement up = PerfCon.prepareStatement("UPDATE agg2 set "
83 + " avail=?, "
84 + " timestampepoch=?, "
85 + " sla=?,"
86 + "avgcpu =?, avgmem =? , avgthread =?, avgfile =?"
87 + "WHERE uri=? and soapaction=? and timerange=?;");
88
89 double avail = getAvailability(now, periods.get(i), rs.getString("uri"), t, PerfCon, ConfigCon);
90 long sla = getSLACount(rs.getString("uri"), periods.get(i), PerfCon);
91 up.setDouble(1, avail);
92 up.setLong(2, now);
93 up.setLong(3, getSLACount(rs.getString("uri"), periods.get(i), PerfCon));
94
95 up.setDouble(4, getAvgCPU(rs.getString("uri"), periods.get(i), PerfCon));
96 up.setLong(5, getAvgMem(rs.getString("uri"), periods.get(i), PerfCon));
97 up.setLong(6, getAvgThread(rs.getString("uri"), periods.get(i), PerfCon));
98 if (rs.getInt("policytype") == PolicyType.PROCESS.ordinal()) {
99 up.setLong(7, getAvgFile(rs.getString("uri"), periods.get(i), PerfCon));
100 } else {
101 up.setLong(7, -1);
102 }
103 up.setString(8, rs.getString("uri"));
104 up.setString(9, t);
105 up.setLong(10, periods.get(i));
106 up.executeUpdate();
107 log.log(Level.DEBUG, "Updated stats for service " + rs.getString("uri") + " action " + t);
108 up.close();
109 }
110 }
111
112 now = System.currentTimeMillis() - now;
113 log.log(Level.INFO, "Statistics calculations took " + now + "ms");
114
115 } catch (Exception ex) {
116 log.log(Level.ERROR, null, ex);
117 throw ex;
118 } finally {
119 DBUtils.safeClose(rs);
120 DBUtils.safeClose(com);
121
122 }
123 }
124
125 private double getAvgCPU(final String url, final Long ts, Connection con) {
126 double r = 0;
127 PreparedStatement cmd = null;
128 ResultSet rs = null;
129 try {
130 cmd = con.prepareStatement("select avg(percentcpu) from rawdatamachineprocess where uri=? and utcdatetime > ?");
131 cmd.setString(1, url);
132 cmd.setLong(2, System.currentTimeMillis() - ts);
133 rs = cmd.executeQuery();
134 if (rs.next()) {
135 r = rs.getLong(1);
136 }
137
138 } catch (Exception ex) {
139 log.log(Level.ERROR, null, ex);
140 } finally {
141 DBUtils.safeClose(rs);
142 DBUtils.safeClose(cmd);
143 }
144 return r;
145 }
146
147 private long getAvgMem(final String url, final Long ts, Connection con) {
148 Double r = Double.valueOf(0);
149 PreparedStatement cmd = null;
150 ResultSet rs = null;
151 try {
152 cmd = con.prepareStatement("select avg(memoryused) from rawdatamachineprocess where uri=? and utcdatetime > ?");
153 cmd.setString(1, url);
154 cmd.setLong(2, System.currentTimeMillis() - ts);
155 rs = cmd.executeQuery();
156 if (rs.next()) {
157 r = rs.getDouble(1);
158 }
159
160 } catch (Exception ex) {
161 log.log(Level.ERROR, null, ex);
162 } finally {
163 DBUtils.safeClose(rs);
164 DBUtils.safeClose(cmd);
165 }
166 return r.longValue();
167 }
168
169 private long getAvgThread(final String url, final Long ts, Connection con) {
170 Double r = Double.valueOf(0);
171 PreparedStatement cmd = null;
172 ResultSet rs = null;
173
174 try {
175 cmd = con.prepareStatement("select avg(threads) from rawdatamachineprocess where uri=? and utcdatetime > ?");
176 cmd.setString(1, url);
177 cmd.setLong(2, System.currentTimeMillis() - ts);
178 rs = cmd.executeQuery();
179 if (rs.next()) {
180 r = rs.getDouble(1);
181 }
182 rs.close();
183 cmd.close();
184
185 } catch (Exception ex) {
186 log.log(Level.ERROR, null, ex);
187 } finally {
188 try {
189 if (rs != null && !rs.isClosed()) {
190 rs.close();
191 }
192 } catch (Throwable ex) {
193 }
194 try {
195 if (cmd != null && !cmd.isClosed()) {
196 cmd.close();
197 }
198 } catch (Throwable ex) {
199 }
200 }
201 return r.longValue();
202 }
203
204 private long getAvgFile(final String url, final Long ts, Connection con) {
205 Double r = Double.valueOf(0);
206 PreparedStatement cmd = null;
207 ResultSet rs = null;
208 try {
209 cmd = con.prepareStatement("select avg(openfiles) from rawdatamachineprocess where uri=? and utcdatetime > ?");
210 cmd.setString(1, url);
211 cmd.setLong(2, System.currentTimeMillis() - ts);
212 rs = cmd.executeQuery();
213 if (rs.next()) {
214 r = rs.getDouble(1);
215 }
216 } catch (Exception ex) {
217 log.log(Level.ERROR, null, ex);
218 } finally {
219 DBUtils.safeClose(rs);
220 DBUtils.safeClose(cmd);
221 }
222 return r.longValue();
223 }
224
225 }