216216import org .w3c .dom .Node ;
217217import org .w3c .dom .NodeList ;
218218import org .xml .sax .InputSource ;
219+ import org .xml .sax .SAXException ;
219220
220221import javax .ejb .Local ;
221222import javax .naming .ConfigurationException ;
222223import javax .xml .parsers .DocumentBuilderFactory ;
224+ import javax .xml .parsers .ParserConfigurationException ;
223225import java .io .BufferedReader ;
224226import java .io .File ;
225227import java .io .FileInputStream ;
226228import java .io .IOException ;
227229import java .io .InputStream ;
228230import java .io .InputStreamReader ;
229- import java .io . StringReader ;
231+ import java .net . MalformedURLException ;
230232import java .net .URI ;
231233import java .net .URISyntaxException ;
232234import java .net .URL ;
@@ -2529,46 +2531,65 @@ protected GetVmDiskStatsAnswer execute(GetVmDiskStatsCommand cmd) {
25292531 return new GetVmDiskStatsAnswer (cmd , null , null , null );
25302532 }
25312533
2532- protected Object [] getRRDData (Connection conn , int flag ) {
2533-
2534- /*
2535- * Note: 1 => called from host, hence host stats 2 => called from vm, hence vm stats
2536- */
2537- String stats = "" ;
25382534
2535+ protected Document getStatsRawXML (Connection conn , boolean host ) {
2536+ Date currentDate = new Date ();
2537+ String urlStr = "http://" + _host .ip + "/rrd_updates?" ;
2538+ urlStr += "session_id=" + conn .getSessionReference ();
2539+ urlStr += "&host=" + (host ? "true" : "false" );
2540+ urlStr += "&cf=" + _consolidationFunction ;
2541+ urlStr += "&interval=" + _pollingIntervalInSeconds ;
2542+ urlStr += "&start=" + (currentDate .getTime () / 1000 - 1000 - 100 );
2543+
2544+ URL url ;
2545+ BufferedReader in = null ;
25392546 try {
2540- if (flag == 1 ) {
2541- stats = getHostStatsRawXML (conn );
2542- }
2543- if (flag == 2 ) {
2544- stats = getVmStatsRawXML (conn );
2545- }
2546- } catch (Exception e1 ) {
2547- s_logger .warn ("Error whilst collecting raw stats from plugin: " , e1 );
2547+ url = new URL (urlStr );
2548+ url .openConnection ();
2549+ URLConnection uc = url .openConnection ();
2550+ in = new BufferedReader (new InputStreamReader (uc .getInputStream ()));
2551+ InputSource statsSource = new InputSource (in );
2552+ return DocumentBuilderFactory .newInstance ().newDocumentBuilder ().parse (statsSource );
2553+ } catch (MalformedURLException e ) {
2554+ s_logger .warn ("Malformed URL? come on...." + urlStr );
2555+ return null ;
2556+ } catch (IOException e ) {
2557+ s_logger .warn ("Problems getting stats using " + urlStr , e );
2558+ return null ;
2559+ } catch (SAXException e ) {
2560+ s_logger .warn ("Problems getting stats using " + urlStr , e );
25482561 return null ;
2562+ } catch (ParserConfigurationException e ) {
2563+ s_logger .warn ("Problems getting stats using " + urlStr , e );
2564+ return null ;
2565+ } finally {
2566+ if (in != null ) {
2567+ try {
2568+ in .close ();
2569+ } catch (IOException e ) {
2570+ s_logger .warn ("Unable to close the buffer " , e );
2571+ }
2572+ }
25492573 }
2574+ }
25502575
2551- // s_logger.debug("The raw xml stream is:"+stats);
2552- // s_logger.debug("Length of raw xml is:"+stats.length());
25532576
2554- //stats are null when the host plugin call fails (host down state)
2555- if (stats == null ) {
2556- return null ;
2557- }
25582577
2559- StringReader statsReader = new StringReader (stats );
2560- InputSource statsSource = new InputSource (statsReader );
2578+ protected Object [] getRRDData (Connection conn , int flag ) {
25612579
2580+ /*
2581+ * Note: 1 => called from host, hence host stats 2 => called from vm, hence vm stats
2582+ */
25622583 Document doc = null ;
2584+
25632585 try {
2564- doc = DocumentBuilderFactory . newInstance (). newDocumentBuilder (). parse ( statsSource );
2565- } catch (Exception e ) {
2566- s_logger .warn ("Exception caught whilst processing the document via document factory: " , e );
2586+ doc = getStatsRawXML ( conn , flag == 1 ? true : false );
2587+ } catch (Exception e1 ) {
2588+ s_logger .warn ("Error whilst collecting raw stats from plugin: " , e1 );
25672589 return null ;
25682590 }
25692591
2570- if (doc == null ) {
2571- s_logger .warn ("Null document found after tryinh to parse the stats source" );
2592+ if (doc == null ) { //stats are null when the host plugin call fails (host down state)
25722593 return null ;
25732594 }
25742595
@@ -2592,7 +2613,7 @@ protected Object[] getRRDData(Connection conn, int flag) {
25922613 }
25932614 }
25942615
2595- return new Object [] {numRows , numColumns , legend , dataNode };
2616+ return new Object [] { numRows , numColumns , legend , dataNode };
25962617 }
25972618
25982619 protected String getXMLNodeValue (Node n ) {
@@ -2630,22 +2651,6 @@ protected double getDataAverage(Node dataNode, int col, int numRows) {
26302651
26312652 }
26322653
2633- protected String getHostStatsRawXML (Connection conn ) {
2634- Date currentDate = new Date ();
2635- String startTime = String .valueOf (currentDate .getTime () / 1000 - 1000 );
2636-
2637- return callHostPlugin (conn , "vmops" , "gethostvmstats" , "collectHostStats" , String .valueOf ("true" ), "consolidationFunction" , _consolidationFunction , "interval" ,
2638- String .valueOf (_pollingIntervalInSeconds ), "startTime" , startTime );
2639- }
2640-
2641- protected String getVmStatsRawXML (Connection conn ) {
2642- Date currentDate = new Date ();
2643- String startTime = String .valueOf (currentDate .getTime () / 1000 - 1000 );
2644-
2645- return callHostPlugin (conn , "vmops" , "gethostvmstats" , "collectHostStats" , String .valueOf ("false" ), "consolidationFunction" , _consolidationFunction , "interval" ,
2646- String .valueOf (_pollingIntervalInSeconds ), "startTime" , startTime );
2647- }
2648-
26492654 protected State convertToState (Types .VmPowerState ps ) {
26502655 final State state = s_statesTable .get (ps );
26512656 return state == null ? State .Unknown : state ;
0 commit comments