001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.monitoring.console;
017:
018: import java.sql.Connection;
019: import java.sql.PreparedStatement;
020: import java.sql.ResultSet;
021: import java.sql.Statement;
022: import java.text.DecimalFormat;
023: import java.text.Format;
024: import java.text.SimpleDateFormat;
025: import java.util.ArrayList;
026: import java.util.Date;
027: import java.util.HashMap;
028: import java.util.Iterator;
029: import java.util.TreeMap;
030:
031: import org.apache.geronimo.monitoring.console.util.DBManager;
032:
033: public class GraphsBuilder {
034: private String ip = new String();
035: private int timeFrame;
036: private int snapCount;
037: private MRCConnector mrc = new MRCConnector();
038: private Connection con;
039:
040: // constructor
041: public GraphsBuilder(Connection con) {
042: // TODO: Database pull stuff may go here... based on server ID...\
043: this .con = con;
044: }
045:
046: public StatsGraph buildOneDB(int graph_id) throws Exception {
047: con = (new DBManager()).getConnection();
048: StatsGraph graph = null;
049: PreparedStatement pStmt = null;
050: ResultSet rsServer = null;
051: pStmt = con
052: .prepareStatement("SELECT * from graphs WHERE enabled=1 AND graph_id="
053: + graph_id);
054: ResultSet rs = pStmt.executeQuery();
055: if (rs.next()) {
056: String mBeanName = rs.getString("mbean");
057: String dataName1 = rs.getString("dataname1");
058: String dataName2 = rs.getString("dataname2");
059: String graphName1 = rs.getString("dataname1");
060: String graphName2 = rs.getString("dataname2");
061: timeFrame = rs.getInt("timeframe");
062: String server_id = rs.getString("server_id");
063: String xlabel = rs.getString("xlabel");
064: String ylabel = rs.getString("ylabel");
065: String data1operation = rs.getString("data1operation");
066: String data2operation = rs.getString("data2operation");
067: String operation = rs.getString("operation");
068: String color = rs.getString("color");
069: float warninglevel1 = rs.getFloat("warninglevel1");
070: String description = rs.getString("description");
071: boolean showArchive = rs.getInt("archive") == 1 ? true
072: : false;
073:
074: pStmt = con
075: .prepareStatement("SELECT * from servers WHERE enabled=1 AND server_id="
076: + server_id);
077: rsServer = pStmt.executeQuery();
078: if (rsServer.next()) {
079: ip = rsServer.getString("ip");
080: String username = rsServer.getString("username");
081: String password = rsServer.getString("password");
082: int port = rsServer.getInt("port");
083: int protocol = rsServer.getInt("protocol");
084: // close the connection before calling the MRCConnector because
085: // it opens another
086: // connection to the db to update the SERVERS.last_seen
087: // attribute
088: try {
089: con.close();
090: } catch (Exception e) {
091: throw e;
092: }
093: mrc = new MRCConnector(ip, username, password, port,
094: protocol);
095: snapCount = timeFrame
096: / java.lang.Integer
097: .valueOf(java.lang.Long
098: .toString((mrc
099: .getSnapshotDuration() / new Long(
100: 60000))));
101: HashMap<String, ArrayList<Object>> DataList = new HashMap<String, ArrayList<Object>>();
102:
103: DataList.put(graphName1, new ArrayList<Object>());
104: if ((dataName2 != null) && !dataName2.equals("time")
105: && !dataName2.equals("null")
106: && !dataName2.equals("")) {
107: DataList.put(graphName2, new ArrayList<Object>());
108: }
109: if ((timeFrame / 1440 >= 30))
110: snapCount = 17;
111: else {
112: if (((timeFrame / 1440) == 7)
113: && ((timeFrame / 60) > 24)
114: && snapCount >= 14) {
115: snapCount = 16;
116: } else {
117: snapCount = 12; // default for anything else
118: }
119: }
120:
121: ArrayList<Object> snapshot_time = new ArrayList<Object>();
122:
123: ArrayList<Object> PrettyTime = new ArrayList<Object>();
124:
125: String prettyTimeFrame = new String();
126: DecimalFormat fmt = new DecimalFormat("0.##");
127: if (timeFrame / 60 > 24) {
128: prettyTimeFrame = fmt
129: .format((float) (timeFrame / 1440))
130: + " day";
131: } else {
132: if (timeFrame > 60) {
133: prettyTimeFrame = fmt
134: .format((float) timeFrame / 60)
135: + " hour";
136: } else {
137: prettyTimeFrame = fmt.format(timeFrame)
138: + " minute";
139: }
140: }
141:
142: int skipCount = (int) ((timeFrame / (mrc
143: .getSnapshotDuration() / 60000)))
144: / (snapCount - 2);
145: snapCount = snapCount + 2;
146: TreeMap<Long, Long> snapshotList1 = mrc
147: .getSpecificStatistics(mBeanName, dataName1,
148: snapCount, skipCount, showArchive);
149: TreeMap<Long, Long> snapshotList2 = new TreeMap<Long, Long>();
150: if ((dataName2 != null) && !dataName2.equals("time")
151: && !dataName2.equals("null")
152: && !dataName2.equals("")) {
153: snapshotList2 = mrc.getSpecificStatistics(
154: mBeanName, dataName2, snapCount, skipCount,
155: showArchive);
156: }
157: // Check if snapshotList is empty
158: if (snapshotList1.size() == 0) {
159: snapshotList1.put(System.currentTimeMillis(),
160: new Long(0));
161: /*
162: * If there are not enough snapshots available to fill the
163: * requested number, insert some with values of 0 and the
164: * proper times.
165: */
166: while (snapshotList1.size() < snapCount) {
167: // Temporary, always is first element (oldest)
168: Long timeFix = snapshotList1.firstKey();
169: // Calculate appropriate time, add it to the submap,
170: // then
171: // add
172: // that to the tempMap
173: snapshotList1.put((timeFix - (mrc
174: .getSnapshotDuration() * skipCount)),
175: new Long(0));
176: }
177: }
178: if (snapshotList2.size() == 0) {
179: snapshotList2.put(System.currentTimeMillis(),
180: new Long(0));
181: while (snapshotList2.size() < snapCount) {
182: // Temporary, always is first element (oldest)
183: Long timeFix = snapshotList2.firstKey();
184: snapshotList2.put((timeFix - (mrc
185: .getSnapshotDuration() * skipCount)),
186: new Long(0));
187: }
188: }
189:
190: for (Iterator<Long> it = snapshotList1.keySet()
191: .iterator(); it.hasNext();) {
192: Long current = it.next();
193: snapshot_time.add(current);
194: ArrayList<Object> ArrayListTemp = DataList
195: .get(graphName1);
196: ArrayListTemp.add(snapshotList1.get(current));
197: DataList.put(graphName1, ArrayListTemp);
198: if ((dataName2 != null)
199: && !dataName2.equals("time")
200: && !dataName2.equals("null")
201: && !dataName2.equals("")) {
202: ArrayList<Object> ArrayListTemp2 = DataList
203: .get(graphName2);
204: ArrayListTemp2.add(snapshotList2.get(current));
205: DataList.put(graphName2, ArrayListTemp2);
206: }
207: PrettyTime.add((Long) current / 1000);
208: }
209:
210: if (dataName2.equals("time")) {
211: graph = (new StatsGraph(graph_id, ip + " - "
212: + xlabel + " - " + prettyTimeFrame,
213: description, xlabel, ylabel, data1operation
214: .charAt(0), DataList
215: .get(graphName1), operation,
216: data2operation.charAt(0), PrettyTime,
217: snapshot_time, (int) (mrc
218: .getSnapshotDuration() / 1000),
219: timeFrame, color, warninglevel1,
220: warninglevel1));
221: } else if (!dataName2.equals("time")
222: && (dataName2 != null)
223: && !dataName2.equals("null")
224: && !dataName2.equals("")) {
225: graph = (new StatsGraph(graph_id, ip + " - "
226: + xlabel + " - " + prettyTimeFrame,
227: description, xlabel, ylabel, data1operation
228: .charAt(0), DataList
229: .get(graphName1), operation,
230: data2operation.charAt(0), DataList
231: .get(graphName2), snapshot_time,
232: (int) (mrc.getSnapshotDuration() / 1000),
233: timeFrame, color, warninglevel1,
234: warninglevel1));
235: } else if (dataName2 == null
236: || dataName2.equals("null")
237: || dataName2.equals("")) {
238: graph = (new StatsGraph(graph_id, ip + " - "
239: + xlabel + " - " + prettyTimeFrame,
240: description, xlabel, ylabel, data1operation
241: .charAt(0), DataList
242: .get(graphName1), operation,
243: snapshot_time, (int) (mrc
244: .getSnapshotDuration() / 1000),
245: timeFrame, color, warninglevel1,
246: warninglevel1));
247: } else {
248: System.out.println("Using Null call.");
249: graph = (new StatsGraph());
250: }
251: }
252: }
253:
254: // check to see if graph was successfully populated
255: if (graph != null) {
256: // get the current date
257: Format formatter = null;
258: formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
259: Date date = new Date(System.currentTimeMillis());
260: String currentTime = formatter.format(date);
261: // the graph was successfully operated on,
262: // so update the last_seen attribute
263: DBManager dbManager = new DBManager();
264: Connection conn = dbManager.getConnection();
265: Statement stmt = conn.createStatement();
266: stmt.executeUpdate("UPDATE GRAPHS SET LAST_SEEN='"
267: + currentTime + "' WHERE GRAPH_ID=" + graph_id);
268: conn.close();
269: }
270: return graph;
271: }
272: }
|