Search in sources :

Example 6 with DBInstanceInfo

use of com.yahoo.dba.perf.myperf.common.DBInstanceInfo in project mysql_perf_analyzer by yahoo.

the class MetricsDbBase method SearchDbInfo.

/**
	   * Retrieve all DB entries based on keyword. 
	   * To retrieve all entries, use blank keyword (null string,
	   * empty string, or %)
	   * @param keyword
	   * @return
	   */
public java.util.List<DBInstanceInfo> SearchDbInfo(String keyword) {
    String sql = null;
    if (keyword == null || keyword.isEmpty() || "%".equals(keyword.trim()))
        sql = "select * from " + DBINFO_TABLENAME + " order by dbtype, dbgroupname, hostname";
    else {
        keyword = keyword.trim();
        sql = "select * from " + DBINFO_TABLENAME + " where hostname like '%" + keyword.toLowerCase() + "%' or lower(database_name) like '%" + keyword.toLowerCase() + "%' or lower(dbgroupname) like '%" + keyword.toLowerCase() + "%' order by dbtype, dbgroupname, hostname";
    }
    logger.info("Excute: " + sql);
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    java.util.ArrayList<DBInstanceInfo> dbList = new java.util.ArrayList<DBInstanceInfo>();
    try {
        conn = this.createConnection(true);
        stmt = conn.createStatement();
        rs = stmt.executeQuery(sql);
        while (rs != null && rs.next()) {
            DBInstanceInfo dbinfo = new DBInstanceInfo();
            dbinfo.setDbid(rs.getInt("DBID"));
            dbinfo.setDbType(rs.getString("DBTYPE"));
            dbinfo.setDbGroupName(rs.getString("DBGROUPNAME"));
            dbinfo.setInstance(rs.getShort("INSTANCE"));
            dbinfo.setHostName(rs.getString("HOSTNAME"));
            dbinfo.setPort(rs.getString("PORT"));
            dbinfo.setDatabaseName(rs.getString("DATABASE_NAME"));
            dbinfo.setUseTunneling(rs.getShort("USE_SSHTUNNEL") == 1 ? true : false);
            dbinfo.setLocalHostName(rs.getString("LOCAL_HOSTNAME"));
            dbinfo.setLocalPort(rs.getString("LOCAL_PORT"));
            dbinfo.setConnectionVerified(rs.getInt("CONNECTION_VERIFIED") == 1);
            dbinfo.setVirtualHost(rs.getInt("VIRTUAL_HOST") == 1);
            dbinfo.setSnmpEnabled(rs.getShort("SNMP_ENABLED") == 1 ? true : false);
            dbinfo.setMetricsEnabled(rs.getShort("METRICS_ENABLED") == 1 ? true : false);
            dbinfo.setAlertEnabled(rs.getShort("ALERT_ENABLED") == 1 ? true : false);
            dbList.add(dbinfo);
        }
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "Failed to retrieve db info", ex);
    } finally {
        DBUtils.close(rs);
        DBUtils.close(stmt);
        DBUtils.close(conn);
    }
    return dbList;
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) DBInstanceInfo(com.yahoo.dba.perf.myperf.common.DBInstanceInfo) SQLException(java.sql.SQLException)

Example 7 with DBInstanceInfo

use of com.yahoo.dba.perf.myperf.common.DBInstanceInfo in project mysql_perf_analyzer by yahoo.

the class StatusController method handleRequestImpl.

@Override
protected ModelAndView handleRequestImpl(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    AppUser appUser = retrieveAppUser(req);
    HashSet<String> filteredGroups = new HashSet<String>();
    boolean useFilter = false;
    if (appUser != null && appUser.isRestrictedUser()) {
        useFilter = true;
        Set<String> mydbs = this.frameworkContext.getDbInfoManager().getMyDatabases(appUser.getName(), appUser.isRestrictedUser()).getMyDbList();
        if (mydbs != null) {
            for (String s : mydbs) filteredGroups.add(s);
        }
    }
    String dbgroup = req.getParameter("group");
    if (dbgroup == null || dbgroup.trim().length() == 0)
        dbgroup = "all";
    String dbhost = req.getParameter("host");
    java.util.Date now = new java.util.Date();
    //the first scan yet to complete
    long oneinterval = (this.frameworkContext.getMyperfConfig().getScannerIntervalSeconds() + 1) * 1000;
    boolean withinOneScan = (now.getTime() - this.frameworkContext.getStartTime().getTime()) <= oneinterval;
    long twointerval = (2 * this.frameworkContext.getMyperfConfig().getScannerIntervalSeconds() + 1) * 1000;
    ResultList rList = new ResultList();
    //if("all".equalsIgnoreCase(dbgroup))
    if (//we should have id either as all or a dbgroup
    dbhost == null || dbhost.isEmpty()) {
        ColumnDescriptor desc = new ColumnDescriptor();
        desc.addColumn("DBGROUP", false, 1);
        desc.addColumn("HOST", false, 2);
        desc.addColumn("QUERIES /SEC", true, 3);
        desc.addColumn("SYS CPU%", true, 4);
        desc.addColumn("USER CPU%", true, 5);
        desc.addColumn("IOWAIT%", true, 6);
        desc.addColumn("LOAD AVG", true, 7);
        desc.addColumn("REPL LAG", true, 8);
        desc.addColumn("FREE MEM (MB)", true, 9);
        desc.addColumn("SLOW QUERY /MIN", true, 10);
        desc.addColumn("THREADS RUNNING", true, 11);
        desc.addColumn("THREADS", true, 12);
        desc.addColumn("CONNECTIONS /SEC", true, 13);
        desc.addColumn("ABORTED CC /SEC", true, 14);
        desc.addColumn("DEADLOCKS", false, 15);
        desc.addColumn("STATUS", false, 16);
        desc.addColumn("LAST CHECK TIME", false, 17);
        desc.addColumn("LAST ALERT", false, 18);
        desc.addColumn("SCAN TIME", true, 19);
        rList.setColumnDescriptor(desc);
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        java.text.DecimalFormat df = new java.text.DecimalFormat("#.###");
        for (Map.Entry<String, DBGroupInfo> e : this.frameworkContext.getDbInfoManager().getClusters().entrySet()) {
            for (DBInstanceInfo dbinfo : e.getValue().getInstances()) {
                String dbgroupName = dbinfo.getDbGroupName();
                String dbHostName = dbinfo.getHostName();
                if (!"all".equals(dbgroup) && !dbgroup.equalsIgnoreCase(dbgroupName))
                    continue;
                if (useFilter && !filteredGroups.contains(dbgroupName))
                    continue;
                InstanceStates stateSnaps = this.frameworkContext.getInstanceStatesManager().getStates(dbinfo.getDbid());
                if (stateSnaps == null)
                    continue;
                //java.util.Date lastConnectTime = this.frameworkContext.getDbInfoManager().getLastAccessDate(dbgroupName, dbHostName);
                java.util.Date lastConnectTime = stateSnaps.getLastAccessTime();
                String dbstatus = "Green";
                if (lastConnectTime == null && withinOneScan)
                    dbstatus = "Unknown";
                else if (lastConnectTime == null)
                    dbstatus = "Red";
                else if (now.getTime() - lastConnectTime.getTime() > twointerval)
                    dbstatus = "Red";
                else if (now.getTime() - lastConnectTime.getTime() > oneinterval)
                    dbstatus = "Yellow";
                ResultRow row = new ResultRow();
                row.setColumnDescriptor(desc);
                List<String> vals = new ArrayList<String>();
                vals.add(dbgroupName);
                vals.add(dbHostName);
                StateSnapshot[] snaps = stateSnaps.copySnapshots();
                boolean isSnapValid = snaps[0].getTimestamp() >= 0L && snaps[1].getTimestamp() > snaps[0].getTimestamp();
                long interval = snaps[1].getTimestamp() - snaps[0].getTimestamp();
                if (isSnapValid) {
                    //sys CPU
                    double syscpu = 0.0f;
                    double usercpu = 0.0f;
                    double iowaits = 0.0f;
                    double loadAvg = 0.0f;
                    int activeThreads = 0;
                    if (snaps[0].getQueries() >= 0L && snaps[1].getQueries() >= 0L) {
                        //double val = ((double)(snaps[1].getQueries() - snaps[0].getQueries())*1000)/(double)interval;
                        double val = diffAvgNoNegative(snaps[1].getQueries(), snaps[0].getQueries(), snaps[1].getTimestamp(), snaps[0].getTimestamp(), snaps[1].getUptime(), true, 1000);
                        vals.add(df.format(val));
                    } else {
                        vals.add("0.00");
                    }
                    if (snaps[0].getSyscputime() >= 0L && snaps[1].getSyscputime() >= 0L) {
                        if (snaps[0].getSyscputime() == snaps[1].getSyscputime()) {
                            syscpu = 0.0;
                            vals.add("0.00");
                        } else {
                            double val = ((double) (snaps[1].getSyscputime() - snaps[0].getSyscputime()) * 100) / (double) (snaps[1].getTotalcputime() - snaps[0].getTotalcputime());
                            vals.add(df.format(val));
                            syscpu = val;
                        }
                    } else {
                        vals.add("0.00");
                    }
                    if (snaps[0].getUsercputime() >= 0L && snaps[1].getUsercputime() >= 0L) {
                        if (snaps[0].getUsercputime() == snaps[1].getUsercputime()) {
                            usercpu = 0.0;
                            vals.add("0.00");
                        } else {
                            double val = ((double) (snaps[1].getUsercputime() - snaps[0].getUsercputime()) * 100) / (double) (snaps[1].getTotalcputime() - snaps[0].getTotalcputime());
                            vals.add(df.format(val));
                            usercpu = val;
                        }
                    } else {
                        vals.add("0.00");
                    }
                    if (snaps[0].getIotime() >= 0L && snaps[1].getIotime() >= 0L) {
                        if (snaps[0].getIotime() == snaps[1].getIotime()) {
                            iowaits = 0.0;
                            vals.add("0.00");
                        } else {
                            double val = ((double) (snaps[1].getIotime() - snaps[0].getIotime()) * 100) / (double) (snaps[1].getTotalcputime() - snaps[0].getTotalcputime());
                            vals.add(df.format(val));
                            iowaits = val;
                        }
                    } else {
                        vals.add("0.00");
                    }
                    if (snaps[1].getLoadAverage() >= 0.0f) {
                        vals.add(df.format(snaps[1].getLoadAverage()));
                        loadAvg = snaps[1].getLoadAverage();
                    } else
                        vals.add("0.00");
                    vals.add(String.valueOf(snaps[1].getReplLag()));
                    if (snaps[1].getAvailableMem() >= 0L) {
                        long val = snaps[1].getAvailableMem() / 1024;
                        vals.add(String.valueOf(val));
                    } else
                        vals.add("0.00");
                    if (snaps[0].getSlowQueryCount() >= 0L && snaps[1].getSlowQueryCount() >= 0L) {
                        double val = diffAvgNoNegative(snaps[1].getSlowQueryCount(), snaps[0].getSlowQueryCount(), snaps[1].getTimestamp(), snaps[0].getTimestamp(), snaps[1].getUptime(), true, 60000);
                        vals.add(df.format(val));
                    } else
                        vals.add("0.00");
                    vals.add(String.valueOf(snaps[1].getActiveThreads()));
                    activeThreads = snaps[1].getActiveThreads();
                    vals.add(String.valueOf(snaps[1].getThreads()));
                    if (snaps[0].getConnections() >= 0L && snaps[1].getConnections() >= 0L) {
                        //double val = ((double)(snaps[1].getConnections() - snaps[0].getConnections())*1000)/(double)interval;
                        double val = diffAvgNoNegative(snaps[1].getConnections(), snaps[0].getConnections(), snaps[1].getTimestamp(), snaps[0].getTimestamp(), snaps[1].getUptime(), true, 1000);
                        vals.add(df.format(val));
                    } else {
                        vals.add("0.00");
                    }
                    if (snaps[0].getAbortedConnectsClients() >= 0L && snaps[1].getAbortedConnectsClients() >= 0L) {
                        //double val = ((double)(snaps[1].getAbortedConnectsClients() - snaps[0].getAbortedConnectsClients())*1000)/(double)interval;
                        double val = diffAvgNoNegative(snaps[1].getAbortedConnectsClients(), snaps[0].getAbortedConnectsClients(), snaps[1].getTimestamp(), snaps[0].getTimestamp(), snaps[1].getUptime(), true, 1000);
                        vals.add(df.format(val));
                    } else {
                        vals.add("0.00");
                    }
                    if (snaps[0].getDeadlocks() >= 0L && snaps[1].getDeadlocks() >= 0L) {
                        double val = diffAvgNoNegative(snaps[1].getDeadlocks(), snaps[0].getDeadlocks(), snaps[1].getTimestamp(), snaps[0].getTimestamp(), snaps[1].getUptime(), false, 1);
                        vals.add((long) val + "(" + snaps[0].getDeadlocks() + ")");
                    } else {
                        vals.add(String.valueOf(snaps[1].getDeadlocks()));
                    }
                    //hardcoded threshold now
                    if (iowaits > this.frameworkContext.getAlertSettings().getAlertThreshold(dbinfo, "IO") || (syscpu + usercpu > this.frameworkContext.getAlertSettings().getAlertThreshold(dbinfo, "CPU")) || loadAvg > this.frameworkContext.getAlertSettings().getAlertThreshold(dbinfo, "LOADAVG") || activeThreads > this.frameworkContext.getAlertSettings().getAlertThreshold(dbinfo, "THREAD"))
                        if (!"Red".equals(dbstatus))
                            dbstatus = "Yellow";
                } else {
                    vals.add("0.00");
                    vals.add("0.00");
                    vals.add("0.00");
                    vals.add("0.00");
                    if (snaps[1].getTimestamp() > 0L)
                        vals.add(String.valueOf(snaps[1].getReplLag()));
                    else
                        vals.add("0");
                    vals.add("0.00");
                    vals.add("0.00");
                    vals.add("0");
                    vals.add("0");
                    vals.add("0.00");
                    vals.add("0.00");
                    vals.add("0.00");
                    vals.add("0");
                }
                vals.add(dbstatus);
                if (lastConnectTime != null)
                    vals.add(sdf.format(lastConnectTime));
                else
                    vals.add("");
                Date altDt = stateSnaps.getLastAlertTime();
                if (altDt != null) {
                    String val = stateSnaps.getLastAlertValue();
                    if (val == null)
                        val = "";
                    //else if(val.indexOf('.')>0 && val.length()>=val.indexOf('.')+4)
                    //  val = val.substring(0, val.indexOf('.')+4);
                    String end_dt = stateSnaps.getLastAlertEndTime() != null ? sdf.format(stateSnaps.getLastAlertEndTime()) : "";
                    vals.add(sdf.format(altDt) + "-" + end_dt + "/" + stateSnaps.getLastAlertType() + "/" + val);
                } else
                    vals.add("");
                vals.add(String.valueOf(stateSnaps.getLastScanTime()));
                row.setColumns(vals);
                rList.addRow(row);
            }
        }
    } else {
        Date d1 = null;
        Date d2 = null;
        Calendar c = Calendar.getInstance();
        //7 days
        c.add(Calendar.DATE, -7);
        d1 = c.getTime();
        d2 = new Date();
        java.text.SimpleDateFormat sdf2 = new java.text.SimpleDateFormat("yyyyMMddHHmmss");
        sdf2.setTimeZone(TimeZone.getTimeZone("UTC"));
        String startDate = sdf2.format(d1);
        String endDate = sdf2.format(d2);
        try {
            //TODO validate DB
            rList = this.frameworkContext.getAutoScanner().getMetricDb().retrieveMetricsStatus("mysql_globalstatus".toUpperCase(), this.frameworkContext.getDbInfoManager().findDB(dbgroup, dbhost).getDbid(), Long.parseLong(startDate), Long.parseLong(endDate));
            HashMap<String, Integer> connectCounter = new HashMap<String, Integer>(24 * 8);
            HashMap<String, Integer> slowCounter = new HashMap<String, Integer>(24 * 8);
            if (rList != null && rList.getRows().size() > 0) {
                for (ResultRow row : rList.getRows()) {
                    String ts = row.getColumns().get(1);
                    String h = ts.substring(0, ts.length());
                    if (connectCounter.containsKey(h))
                        connectCounter.put(h, connectCounter.get(h) + 1);
                    else
                        connectCounter.put(h, 1);
                    try {
                        if (slowCounter.containsKey(h))
                            slowCounter.put(h, connectCounter.get(h) + Integer.parseInt(row.getColumns().get(3)));
                        else
                            slowCounter.put(h, Integer.parseInt(row.getColumns().get(3)));
                    } catch (Exception ex) {
                    }
                }
            }
        } catch (Exception ex) {
        //logger.log(Level.WARNING, "Failed to retrieve metrics data", ex);
        }
    }
    ModelAndView mv = new ModelAndView(jsonView);
    String msg = "OK";
    mv.addObject("json_result", ResultListUtil.toJSONString(rList, null, 0, msg));
    return mv;
}
Also used : ResultList(com.yahoo.dba.perf.myperf.common.ResultList) HashMap(java.util.HashMap) Date(java.util.Date) InstanceStates(com.yahoo.dba.perf.myperf.common.InstanceStates) ArrayList(java.util.ArrayList) ModelAndView(org.springframework.web.servlet.ModelAndView) AppUser(com.yahoo.dba.perf.myperf.common.AppUser) DBGroupInfo(com.yahoo.dba.perf.myperf.common.DBGroupInfo) HashSet(java.util.HashSet) ResultRow(com.yahoo.dba.perf.myperf.common.ResultRow) ColumnDescriptor(com.yahoo.dba.perf.myperf.common.ColumnDescriptor) Calendar(java.util.Calendar) StateSnapshot(com.yahoo.dba.perf.myperf.common.StateSnapshot) Date(java.util.Date) HashMap(java.util.HashMap) Map(java.util.Map) DBInstanceInfo(com.yahoo.dba.perf.myperf.common.DBInstanceInfo)

Example 8 with DBInstanceInfo

use of com.yahoo.dba.perf.myperf.common.DBInstanceInfo in project mysql_perf_analyzer by yahoo.

the class UdmController method testAndValidateAlert.

private void testAndValidateAlert(HttpServletRequest req, AlertDefinition def, String db) throws Exception {
    if (def == null)
        throw new Exception("please provide valid alert definition for test");
    if (AlertDefinition.SOURCE_SQL.equals(def.getSource())) {
        if (def.getSqlText() == null || def.getSqlText().isEmpty())
            throw new Exception("please provide valid SQL text for test");
        if (db == null || db.isEmpty())
            throw new Exception("please provide valid database for test");
        String[] dbs = db.split("\\|");
        if (dbs == null || dbs.length < 2)
            throw new Exception("please provide valid database for test");
        DBInstanceInfo dbinfo = this.frameworkContext.getDbInfoManager().findDB(dbs[0], dbs[1]);
        if (dbinfo == null)
            throw new Exception("please provide valid database for test");
        DBConnectionWrapper connWrapper = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            connWrapper = WebAppUtil.getDBConnection(req, this.frameworkContext, dbinfo);
            if (connWrapper == null) {
                throw new Exception("failed to connect to target db (" + dbinfo + ")");
            }
            String sql = def.getSqlText();
            if (def.getParams() != null && def.getParams().size() > 0) {
                for (Map.Entry<String, String> e : def.getParams().entrySet()) {
                    sql = sql.replace("&" + e.getKey(), e.getValue());
                }
            }
            stmt = connWrapper.getConnection().createStatement();
            rs = stmt.executeQuery(sql);
        } finally {
            DBUtils.close(rs);
            DBUtils.close(stmt);
            //close it anyway
            WebAppUtil.closeDBConnection(req, connWrapper, true, false);
        }
    } else if (AlertDefinition.SOURCE_METRICS.equals(def.getSource())) {
        String metricsName = def.getMetricName();
        if (metricsName == null || metricsName.isEmpty())
            throw new Exception("please provide valid metrics name in the format of group[.subgroup].metric_name");
        String[] ms = metricsName.split("\\.");
        if (ms.length < 2)
            throw new Exception("please provide valid metrics name in the format of group[.subgroup].metric_name. " + metricsName + " is not valid.");
        MetricsGroup mg = this.frameworkContext.getMetricsDef().getGroupByName(ms[0]);
        if (mg == null) {
            //try UDM
            UserDefinedMetrics udm = this.frameworkContext.getMetricsDef().getUdmManager().getUDMByName(ms[0]);
            if (udm == null)
                throw new Exception("please provide valid metrics name in the format of group[.subgroup].metric_name. " + ms[0] + " is not valid metrics group name.");
            else
                mg = udm.getMetricsGroup();
        }
        if (mg == null)
            throw new Exception("please provide valid metrics name in the format of group[.subgroup].metric_name. " + ms[0] + " is not valid metrics group name.");
        if (mg.getSubGroups().size() > 0) {
            if (ms.length < 3)
                throw new Exception("please provide valid metrics name in the format of group[.subgroup].metric_name. " + ms[0] + " has sub groups.");
            else
                mg = mg.getSubGroupByName(ms[1]);
            if (mg == null)
                throw new Exception("please provide valid metrics name in the format of group[.subgroup].metric_name. " + ms[1] + " is not a valid sub group in group " + ms[0]);
        }
        boolean findOne = false;
        for (Metric m : mg.getMetrics()) {
            if (m.getName().equals(ms[ms.length - 1])) {
                findOne = true;
                break;
            }
        }
        if (!findOne)
            throw new Exception("please provide valid metrics name in the format of group[.subgroup].metric_name. " + metricsName + " is not valid.");
    }
}
Also used : UserDefinedMetrics(com.yahoo.dba.perf.myperf.common.UserDefinedMetrics) MetricsGroup(com.yahoo.dba.perf.myperf.common.MetricsGroup) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) Metric(com.yahoo.dba.perf.myperf.common.Metric) DBConnectionWrapper(com.yahoo.dba.perf.myperf.db.DBConnectionWrapper) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SQLException(java.sql.SQLException) DBInstanceInfo(com.yahoo.dba.perf.myperf.common.DBInstanceInfo)

Example 9 with DBInstanceInfo

use of com.yahoo.dba.perf.myperf.common.DBInstanceInfo in project mysql_perf_analyzer by yahoo.

the class UdmController method testUDM.

/**
	 * 
	 * Test UDM, use exception to send out message
	 * @param req
	 * @param udm
	 * @param db
	 * @throws SQLException
	 */
private void testUDM(HttpServletRequest req, UserDefinedMetrics udm, String db) throws Exception {
    if (db == null || db.isEmpty())
        throw new Exception("please provide valid database for test");
    String[] dbs = db.split("\\|");
    if (dbs == null || dbs.length < 2)
        throw new Exception("please provide valid database for test");
    DBInstanceInfo dbinfo = this.frameworkContext.getDbInfoManager().findDB(dbs[0], dbs[1]);
    if (dbinfo == null)
        throw new Exception("please provide valid database for test");
    HashSet<String> metricsNameSet = new HashSet<String>();
    for (Metric m : udm.getMetrics()) {
        metricsNameSet.add(m.getSourceName());
    }
    DBConnectionWrapper connWrapper = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
        String sql = udm.getSql();
        MetricsGroup mg = udm.getMetricsGroup();
        String udmType = udm.getUdmType();
        String nameCol = udm.getNameCol();
        String valCol = udm.getValueCol();
        String keyCol = udm.getKeyCol();
        boolean isBuiltin = false;
        if (!"SQL".equals(udm.getSource())) {
            sql = this.frameworkContext.getSqlTextForMetricsGroup(udm.getSource());
            mg = this.frameworkContext.getMetricsDef().getGroupByName(udm.getSource());
            if (mg != null) {
                if (mg.getKeyColumn() != null)
                    udmType = "key";
                else if (mg.isMultipleMetricsPerRow())
                    udmType = "column";
                else
                    udmType = "row";
                nameCol = mg.getMetricNameColumn();
                valCol = mg.getMetricValueColumn();
                keyCol = mg.getKeyColumn();
            }
            isBuiltin = true;
        }
        if (sql == null || sql.isEmpty()) {
            throw new Exception("please provide valid SQL");
        }
        connWrapper = WebAppUtil.getDBConnection(req, this.frameworkContext, dbinfo);
        if (connWrapper == null) {
            throw new Exception("failed to connect to target db (" + dbinfo + ")");
        }
        stmt = connWrapper.getConnection().createStatement();
        rs = stmt.executeQuery(sql);
        if (rs != null) {
            ResultSetMetaData meta = rs.getMetaData();
            //verify columns
            int cols = meta.getColumnCount();
            Map<String, Integer> colMap = new HashMap<String, Integer>(cols);
            for (int i = 1; i <= cols; i++) colMap.put(meta.getColumnName(i).toUpperCase(), meta.getColumnType(i));
            if ("row".equals(udmType)) {
                if (!colMap.containsKey(udm.getNameCol().toUpperCase()))
                    throw new Exception("Failed to find name column from SQL result: " + udm.getNameCol() + ", returned: " + colMap);
                if (!colMap.containsKey(udm.getValueCol().toUpperCase()))
                    throw new Exception("Failed to find value column from SQL result: " + udm.getValueCol() + ", returned: " + colMap);
            } else //check metrics column 
            {
                if ("key".equals(udmType)) {
                    if (!colMap.containsKey(keyCol.toUpperCase()))
                        throw new Exception("Failed to find key column from SQL result: " + udm.getKeyCol());
                }
                for (Metric m : udm.getMetrics()) {
                    if (!colMap.containsKey(m.getSourceName().toUpperCase()))
                        throw new Exception("Failed to find metric column from SQL result: " + m.getSourceName());
                }
            }
        } else {
            throw new Exception("Failed to test SQL.");
        }
        while (rs != null && rs.next()) {
            if ("row".equals(udmType)) {
                String name = rs.getString(nameCol);
                if (!metricsNameSet.contains(name))
                    continue;
                String val = rs.getString(valCol);
                try {
                    BigDecimal d = new BigDecimal(val == null ? "0" : val);
                } catch (Exception ex) {
                    throw new Exception("Expect numeric value for metric from SQL result, got " + val);
                }
            } else {
                for (Metric m : udm.getMetrics()) {
                    String val = rs.getString(m.getSourceName());
                    try {
                        BigDecimal d = new BigDecimal(val == null ? "0" : val);
                    } catch (Exception ex) {
                        throw new Exception("Expect numeric value metric value from SQL result for column " + m.getShortName() + ", got " + val);
                    }
                }
            }
        }
    } finally {
        DBUtils.close(rs);
        DBUtils.close(stmt);
        //close it anyway
        WebAppUtil.closeDBConnection(req, connWrapper, true, false);
    }
}
Also used : HashMap(java.util.HashMap) Statement(java.sql.Statement) SQLException(java.sql.SQLException) BigDecimal(java.math.BigDecimal) ResultSetMetaData(java.sql.ResultSetMetaData) MetricsGroup(com.yahoo.dba.perf.myperf.common.MetricsGroup) ResultSet(java.sql.ResultSet) Metric(com.yahoo.dba.perf.myperf.common.Metric) DBConnectionWrapper(com.yahoo.dba.perf.myperf.db.DBConnectionWrapper) DBInstanceInfo(com.yahoo.dba.perf.myperf.common.DBInstanceInfo) HashSet(java.util.HashSet)

Example 10 with DBInstanceInfo

use of com.yahoo.dba.perf.myperf.common.DBInstanceInfo in project mysql_perf_analyzer by yahoo.

the class GlobalVariableChangeScanTask method run.

@Override
public void run() {
    Thread.currentThread().setName("GlobalVariableChangeScanTask");
    conns = new UserDBConnections();
    conns.setAppUser(appUser.getName());
    conns.setFrameworkContext(context);
    File root = new File(new File(this.context.getFileReposirtoryPath()), STORAGE_DIR);
    //get all dbids
    List<DBInstanceInfo> dbs = new ArrayList<DBInstanceInfo>();
    for (Map.Entry<String, DBGroupInfo> e : context.getDbInfoManager().getClusters().entrySet()) {
        DBGroupInfo g = e.getValue();
        for (DBInstanceInfo i : g.getInstances()) {
            dbs.add(i);
        }
    }
    //now get current timestamp
    sdf = new java.text.SimpleDateFormat("yyyyMMddHHmmss");
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    for (//now we need scan for each db
    DBInstanceInfo db : //now we need scan for each db
    dbs) {
        ConfigBlock cb = scanHost(db);
        if (cb != null) {
            updateConfigBlock(db, cb);
        }
    }
}
Also used : ConfigBlock(com.yahoo.dba.perf.myperf.common.ConfigBlock) ArrayList(java.util.ArrayList) DBGroupInfo(com.yahoo.dba.perf.myperf.common.DBGroupInfo) MyPerfContext(com.yahoo.dba.perf.myperf.common.MyPerfContext) UserDBConnections(com.yahoo.dba.perf.myperf.db.UserDBConnections) File(java.io.File) Map(java.util.Map) DBInstanceInfo(com.yahoo.dba.perf.myperf.common.DBInstanceInfo)

Aggregations

DBInstanceInfo (com.yahoo.dba.perf.myperf.common.DBInstanceInfo)14 SQLException (java.sql.SQLException)10 ResultSet (java.sql.ResultSet)8 Map (java.util.Map)8 Statement (java.sql.Statement)7 HashMap (java.util.HashMap)7 ResultList (com.yahoo.dba.perf.myperf.common.ResultList)6 ArrayList (java.util.ArrayList)6 ResultRow (com.yahoo.dba.perf.myperf.common.ResultRow)5 ColumnDescriptor (com.yahoo.dba.perf.myperf.common.ColumnDescriptor)4 QueryParameters (com.yahoo.dba.perf.myperf.common.QueryParameters)4 DBConnectionWrapper (com.yahoo.dba.perf.myperf.db.DBConnectionWrapper)4 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 ModelAndView (org.springframework.web.servlet.ModelAndView)4 DBGroupInfo (com.yahoo.dba.perf.myperf.common.DBGroupInfo)3 MetricsGroup (com.yahoo.dba.perf.myperf.common.MetricsGroup)3 HashSet (java.util.HashSet)3 ConfigBlock (com.yahoo.dba.perf.myperf.common.ConfigBlock)2 Metric (com.yahoo.dba.perf.myperf.common.Metric)2