@Override public void start() { metricList = new ArrayList<Metric>(); if (mode.equals("tower4clouds")) { for (String metric : getProvidedMetrics()) { try { InternalComponent resource = new InternalComponent( Config.getInstance().getInternalComponentType(), Config.getInstance().getInternalComponentId()); if (dcAgent.shouldMonitor(resource, metric)) { Map<String, String> parameters = dcAgent.getParameters(resource, metric); JDBC_URL = parameters.get("databaseAddress"); JDBC_NAME = parameters.get("databaseUser"); JDBC_PASSWORD = parameters.get("databasePassword"); } } catch (ConfigurationException e) { e.printStackTrace(); } } } else { try { String folder = new File(".").getCanonicalPath(); File file = new File(folder + "/config/configuration_MySQL.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder; dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(file); doc.getDocumentElement().normalize(); NodeList nList = doc.getElementsByTagName("metricName"); for (int temp = 0; temp < nList.getLength(); temp++) { Node nNode = nList.item(temp); Metric temp_metric = new Metric(); temp_metric.setMetricName(nNode.getTextContent()); temp_metric.setSamplingProb(1); metricList.add(temp_metric); } NodeList nList_jdbc = doc.getElementsByTagName("mysql-metric"); for (int i = 0; i < nList_jdbc.getLength(); i++) { Node nNode = nList_jdbc.item(i); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; JDBC_URL = eElement.getElementsByTagName("databaseAddress").item(0).getTextContent(); JDBC_NAME = eElement.getElementsByTagName("databaseUser").item(0).getTextContent(); JDBC_PASSWORD = eElement.getElementsByTagName("databasePassword").item(0).getTextContent(); monitoredTarget = eElement.getElementsByTagName("monitoredTarget").item(0).getTextContent(); period = Integer.valueOf( eElement.getElementsByTagName("monitorPeriod").item(0).getTextContent()); } } } catch (ParserConfigurationException e1) { e1.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } try { Class.forName(JDBC_DRIVER); } catch (Exception ex) { System.out.println("Error Class.forName" + ex); ex.printStackTrace(); System.exit(0); } try { conDb = DriverManager.getConnection(JDBC_URL, JDBC_NAME, JDBC_PASSWORD); } catch (Exception ex) { System.out.println("Error getConnection" + ex); ex.printStackTrace(); System.exit(0); } sqlt = new Thread(this, "mysql-mon"); }
@Override public void run() { long startTime = 0; while (!sqlt.isInterrupted()) { if (mode.equals("tower4clouds")) { if (System.currentTimeMillis() - startTime > 10000) { metricList = new ArrayList<Metric>(); List<Integer> periodList = new ArrayList<Integer>(); for (String metric : getProvidedMetrics()) { try { InternalComponent resource = new InternalComponent( Config.getInstance().getInternalComponentType(), Config.getInstance().getInternalComponentId()); if (dcAgent.shouldMonitor(resource, metric)) { Map<String, String> parameters = dcAgent.getParameters(resource, metric); Metric temp = new Metric(); temp.setMetricName(metric); periodList.add(Integer.valueOf(parameters.get("samplingTime")) * 1000); temp.setSamplingProb(Double.valueOf(parameters.get("samplingProbability"))); metricList.add(temp); } } catch (NumberFormatException | ConfigurationException e) { e.printStackTrace(); } } period = Collections.min(periodList); startTime = System.currentTimeMillis(); } } String query = "SHOW GLOBAL STATUS where "; int numMetrics = 0; for (Metric metric : metricList) { if (numMetrics == 0) { query = query + "Variable_name like '" + metric.getMetricName() + "'"; } else { query = query + "or Variable_name like '" + metric.getMetricName() + "'"; } numMetrics++; } Long t0 = System.currentTimeMillis(); // TO FIX adding delay PreparedStatement ps = null; ResultSet rs; try { try { ps = conDb.prepareStatement(query); } catch (SQLException e1) { e1.printStackTrace(); } rs = ps.executeQuery(query); while (rs.next()) { String variableName = rs.getString("Variable_name"); String value = rs.getString("Value"); try { for (Metric metric : metricList) { if (metric.getMetricName().equals(variableName)) { if (Math.random() < metric.getSamplingProb()) { logger.info("Sending datum: {} {} {}", value, variableName, monitoredTarget); dcAgent.send( new InternalComponent( Config.getInstance().getInternalComponentType(), Config.getInstance().getInternalComponentId()), variableName, Double.valueOf(value)); } } } } catch (Exception e) { e.printStackTrace(); } // sendMonitoringDatum(Double.valueOf(value), ResourceFactory.createResource(MC.getURI() + // variableName), monitoredResourceURL, monitoredResource); // System.out.println(variableName + ": " + value); } } catch (SQLException ex) { System.out.println("Error execute query" + ex); ex.printStackTrace(); System.exit(0); } Long t1 = System.currentTimeMillis(); try { // correct sleep time to ensure periodic sampling Thread.sleep(Math.max(period * 1000 - (t1 - t0), 0)); } catch (InterruptedException e) { Thread.currentThread().interrupt(); break; } } }