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 */ 017 package org.apache.geronimo.monitoring.console; 018 019 import java.sql.Connection; 020 import java.sql.PreparedStatement; 021 import java.sql.ResultSet; 022 import java.sql.Statement; 023 import java.text.DecimalFormat; 024 import java.text.Format; 025 import java.text.SimpleDateFormat; 026 import java.util.ArrayList; 027 import java.util.Date; 028 import java.util.HashMap; 029 import java.util.Iterator; 030 import java.util.TreeMap; 031 032 import org.apache.geronimo.monitoring.console.util.DBManager; 033 034 public class GraphsBuilder { 035 private String ip = new String(); 036 private int timeFrame; 037 private int snapCount; 038 private MRCConnector mrc = new MRCConnector(); 039 private Connection con; 040 041 // constructor 042 public GraphsBuilder(Connection con) { 043 // TODO: Database pull stuff may go here... based on server ID...\ 044 this.con = con; 045 } 046 047 public StatsGraph buildOneDB(int graph_id) throws Exception { 048 con = (new DBManager()).getConnection(); 049 StatsGraph graph = null; 050 PreparedStatement pStmt = null; 051 ResultSet rsServer = null; 052 pStmt = con 053 .prepareStatement("SELECT * from graphs WHERE enabled=1 AND graph_id=" 054 + graph_id); 055 ResultSet rs = pStmt.executeQuery(); 056 if (rs.next()) { 057 String mBeanName = rs.getString("mbean"); 058 String dataName1 = rs.getString("dataname1"); 059 String dataName2 = rs.getString("dataname2"); 060 String graphName1 = rs.getString("dataname1"); 061 String graphName2 = rs.getString("dataname2"); 062 timeFrame = rs.getInt("timeframe"); 063 String server_id = rs.getString("server_id"); 064 String xlabel = rs.getString("xlabel"); 065 String ylabel = rs.getString("ylabel"); 066 String data1operation = rs.getString("data1operation"); 067 String data2operation = rs.getString("data2operation"); 068 String operation = rs.getString("operation"); 069 String color = rs.getString("color"); 070 float warninglevel1 = rs.getFloat("warninglevel1"); 071 String description = rs.getString("description"); 072 boolean showArchive = rs.getInt("archive") == 1 ? true : 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, protocol); 094 HashMap<String, ArrayList<Object>> DataList = new HashMap<String, ArrayList<Object>>(); 095 096 DataList.put(graphName1, new ArrayList<Object>()); 097 if ((dataName2 != null) && !dataName2.equals("time") 098 && !dataName2.equals("null") && !dataName2.equals("")) { 099 DataList.put(graphName2, new ArrayList<Object>()); 100 } 101 if ((timeFrame / 1440 >= 30)) 102 snapCount = 17; 103 else { 104 if ((timeFrame / 1440) == 7) { 105 snapCount = 16; 106 } else if ((int) ((timeFrame / (mrc.getSnapshotDuration() / 60000))) <= 12) 107 snapCount = (int) ((timeFrame / (mrc 108 .getSnapshotDuration() / 60000))); 109 else 110 snapCount = 12; 111 } 112 113 ArrayList<Object> snapshot_time = new ArrayList<Object>(); 114 115 ArrayList<Object> PrettyTime = new ArrayList<Object>(); 116 117 String prettyTimeFrame = new String(); 118 DecimalFormat fmt = new DecimalFormat("0.##"); 119 if (timeFrame / 60 > 24) { 120 prettyTimeFrame = fmt.format((float) (timeFrame / 1440)) 121 + " day"; 122 } else { 123 if (timeFrame > 60) { 124 prettyTimeFrame = fmt.format((float) timeFrame / 60) 125 + " hour"; 126 } else { 127 prettyTimeFrame = fmt.format(timeFrame) + " minute"; 128 } 129 } 130 131 int skipCount = (int) ((timeFrame / (mrc.getSnapshotDuration() / 60000))) 132 / (snapCount); 133 134 snapCount = snapCount + 2; 135 TreeMap<Long, Long> snapshotList1 = mrc 136 .getSpecificStatistics(mBeanName, dataName1, snapCount, 137 skipCount, showArchive); 138 TreeMap<Long, Long> snapshotList2 = new TreeMap<Long, Long>(); 139 if ((dataName2 != null) && !dataName2.equals("time") 140 && !dataName2.equals("null") && !dataName2.equals("")) { 141 snapshotList2 = mrc.getSpecificStatistics(mBeanName, 142 dataName2, snapCount, skipCount, showArchive); 143 } 144 // Check if snapshotList is empty 145 if (snapshotList1.size() == 0) { 146 snapshotList1.put(System.currentTimeMillis(), new Long(0)); 147 /* 148 * If there are not enough snapshots available to fill the 149 * requested number, insert some with values of 0 and the 150 * proper times. 151 */ 152 while (snapshotList1.size() < snapCount) { 153 // Temporary, always is first element (oldest) 154 Long timeFix = snapshotList1.firstKey(); 155 // Calculate appropriate time, add it to the submap, 156 // then 157 // add 158 // that to the tempMap 159 snapshotList1 160 .put( 161 (timeFix - (mrc.getSnapshotDuration() * skipCount)), 162 new Long(0)); 163 } 164 } 165 if (snapshotList2.size() == 0) { 166 snapshotList2.put(System.currentTimeMillis(), new Long(0)); 167 while (snapshotList2.size() < snapCount) { 168 // Temporary, always is first element (oldest) 169 Long timeFix = snapshotList2.firstKey(); 170 snapshotList2 171 .put( 172 (timeFix - (mrc.getSnapshotDuration() * skipCount)), 173 new Long(0)); 174 } 175 } 176 177 for (Iterator<Long> it = snapshotList1.keySet().iterator(); it 178 .hasNext();) { 179 Long current = it.next(); 180 snapshot_time.add(current); 181 ArrayList<Object> ArrayListTemp = DataList.get(graphName1); 182 ArrayListTemp.add(snapshotList1.get(current)); 183 DataList.put(graphName1, ArrayListTemp); 184 if ((dataName2 != null) && !dataName2.equals("time") 185 && !dataName2.equals("null") 186 && !dataName2.equals("")) { 187 ArrayList<Object> ArrayListTemp2 = DataList 188 .get(graphName2); 189 ArrayListTemp2.add(snapshotList2.get(current)); 190 DataList.put(graphName2, ArrayListTemp2); 191 } 192 PrettyTime.add((Long) current / 1000); 193 } 194 195 if (dataName2.equals("time")) { 196 graph = (new StatsGraph(graph_id, ip + " - " + xlabel 197 + " - " + prettyTimeFrame, description, xlabel, 198 ylabel, data1operation.charAt(0), DataList 199 .get(graphName1), operation, data2operation 200 .charAt(0), PrettyTime, snapshot_time, 201 (int) (mrc.getSnapshotDuration() / 1000), 202 timeFrame, color, warninglevel1, warninglevel1)); 203 } else if (!dataName2.equals("time") && (dataName2 != null) 204 && !dataName2.equals("null") && !dataName2.equals("")) { 205 graph = (new StatsGraph(graph_id, ip + " - " + xlabel 206 + " - " + prettyTimeFrame, description, xlabel, 207 ylabel, data1operation.charAt(0), DataList 208 .get(graphName1), operation, data2operation 209 .charAt(0), DataList.get(graphName2), 210 snapshot_time, 211 (int) (mrc.getSnapshotDuration() / 1000), 212 timeFrame, color, warninglevel1, warninglevel1)); 213 } else if (dataName2 == null || dataName2.equals("null") 214 || dataName2.equals("")) { 215 graph = (new StatsGraph(graph_id, ip + " - " + xlabel 216 + " - " + prettyTimeFrame, description, xlabel, 217 ylabel, data1operation.charAt(0), DataList 218 .get(graphName1), operation, snapshot_time, 219 (int) (mrc.getSnapshotDuration() / 1000), 220 timeFrame, color, warninglevel1, warninglevel1)); 221 } else { 222 System.out.println("Using Null call."); 223 graph = (new StatsGraph()); 224 } 225 } 226 } 227 228 // check to see if graph was successfully populated 229 if (graph != null) { 230 // get the current date 231 Format formatter = null; 232 formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 233 Date date = new Date(System.currentTimeMillis()); 234 String currentTime = formatter.format(date); 235 // the graph was successfully operated on, 236 // so update the last_seen attribute 237 DBManager dbManager = new DBManager(); 238 Connection conn = dbManager.getConnection(); 239 Statement stmt = conn.createStatement(); 240 stmt.executeUpdate("UPDATE GRAPHS SET LAST_SEEN='" + currentTime 241 + "' WHERE GRAPH_ID=" + graph_id); 242 conn.close(); 243 } 244 return graph; 245 } 246 }