|
25 | 25 | import java.io.IOException; |
26 | 26 | import java.io.InputStream; |
27 | 27 | import java.io.InputStreamReader; |
28 | | -import java.io.StringReader; |
29 | 28 | import java.net.MalformedURLException; |
30 | 29 | import java.net.URI; |
31 | 30 | import java.net.URISyntaxException; |
|
49 | 48 | import javax.ejb.Local; |
50 | 49 | import javax.naming.ConfigurationException; |
51 | 50 | import javax.xml.parsers.DocumentBuilderFactory; |
| 51 | +import javax.xml.parsers.ParserConfigurationException; |
52 | 52 |
|
53 | 53 | import org.apache.commons.codec.binary.Base64; |
54 | 54 | import org.apache.log4j.Logger; |
|
57 | 57 | import org.w3c.dom.Node; |
58 | 58 | import org.w3c.dom.NodeList; |
59 | 59 | import org.xml.sax.InputSource; |
| 60 | +import org.xml.sax.SAXException; |
60 | 61 |
|
61 | 62 | import com.google.gson.Gson; |
62 | 63 | import com.trilead.ssh2.SCPClient; |
@@ -2803,41 +2804,16 @@ protected Object[] getRRDData(Connection conn, int flag) { |
2803 | 2804 | /* |
2804 | 2805 | * Note: 1 => called from host, hence host stats 2 => called from vm, hence vm stats |
2805 | 2806 | */ |
2806 | | - String stats = ""; |
| 2807 | + Document doc = null; |
2807 | 2808 |
|
2808 | 2809 | try { |
2809 | | - if (flag == 1) { |
2810 | | - stats = getHostStatsRawXML(conn); |
2811 | | - } |
2812 | | - if (flag == 2) { |
2813 | | - stats = getVmStatsRawXML(conn); |
2814 | | - } |
| 2810 | + doc = getStatsRawXML(conn, flag == 1 ? true : false); |
2815 | 2811 | } catch (Exception e1) { |
2816 | 2812 | s_logger.warn("Error whilst collecting raw stats from plugin: ", e1); |
2817 | 2813 | return null; |
2818 | 2814 | } |
2819 | 2815 |
|
2820 | | - // s_logger.debug("The raw xml stream is:"+stats); |
2821 | | - // s_logger.debug("Length of raw xml is:"+stats.length()); |
2822 | | - |
2823 | | - //stats are null when the host plugin call fails (host down state) |
2824 | | - if(stats == null) { |
2825 | | - return null; |
2826 | | - } |
2827 | | - |
2828 | | - StringReader statsReader = new StringReader(stats); |
2829 | | - InputSource statsSource = new InputSource(statsReader); |
2830 | | - |
2831 | | - Document doc = null; |
2832 | | - try { |
2833 | | - doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(statsSource); |
2834 | | - } catch (Exception e) { |
2835 | | - s_logger.warn("Exception caught whilst processing the document via document factory:", e); |
2836 | | - return null; |
2837 | | - } |
2838 | | - |
2839 | | - if(doc==null){ |
2840 | | - s_logger.warn("Null document found after tryinh to parse the stats source"); |
| 2816 | + if (doc == null) { //stats are null when the host plugin call fails (host down state) |
2841 | 2817 | return null; |
2842 | 2818 | } |
2843 | 2819 |
|
@@ -2908,45 +2884,47 @@ protected double getDataAverage(Node dataNode, int col, int numRows) { |
2908 | 2884 |
|
2909 | 2885 | } |
2910 | 2886 |
|
2911 | | - protected String getHostStatsRawXML(Connection conn) { |
| 2887 | + protected Document getStatsRawXML(Connection conn, boolean host) { |
2912 | 2888 | Date currentDate = new Date(); |
2913 | 2889 | String urlStr = "http://" + _host.ip + "/rrd_updates?"; |
2914 | 2890 | urlStr += "session_id=" + conn.getSessionReference(); |
2915 | | - urlStr += "&host=" + "true"; |
| 2891 | + urlStr += "&host=" + (host ? "true" : "false"); |
2916 | 2892 | urlStr += "&cf=" + _consolidationFunction; |
2917 | 2893 | urlStr += "&interval=" + _pollingIntervalInSeconds; |
2918 | 2894 | urlStr += "&start=" + (currentDate.getTime() / 1000 - 1000 - 100); |
2919 | 2895 |
|
2920 | 2896 | URL url; |
| 2897 | + BufferedReader in = null; |
2921 | 2898 | try { |
2922 | 2899 | url = new URL(urlStr); |
2923 | 2900 | url.openConnection(); |
2924 | 2901 | URLConnection uc = url.openConnection(); |
2925 | | - BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream())); |
2926 | | - StringBuilder buff = new StringBuilder(); |
2927 | | - String inputLine; |
2928 | | - while ((inputLine = in.readLine()) != null) { |
2929 | | - buff.append(inputLine); |
2930 | | - } |
2931 | | - in.close(); |
2932 | | - return buff.toString(); |
| 2902 | + in = new BufferedReader(new InputStreamReader(uc.getInputStream())); |
| 2903 | + InputSource statsSource = new InputSource(in); |
| 2904 | + return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(statsSource); |
2933 | 2905 | } catch (MalformedURLException e) { |
2934 | 2906 | s_logger.warn("Malformed URL? come on...." + urlStr); |
2935 | 2907 | return null; |
2936 | 2908 | } catch (IOException e) { |
2937 | 2909 | s_logger.warn("Problems getting stats using " + urlStr, e); |
2938 | 2910 | return null; |
| 2911 | + } catch (SAXException e) { |
| 2912 | + s_logger.warn("Problems getting stats using " + urlStr, e); |
| 2913 | + return null; |
| 2914 | + } catch (ParserConfigurationException e) { |
| 2915 | + s_logger.warn("Problems getting stats using " + urlStr, e); |
| 2916 | + return null; |
| 2917 | + } finally { |
| 2918 | + if (in != null) { |
| 2919 | + try { |
| 2920 | + in.close(); |
| 2921 | + } catch (IOException e) { |
| 2922 | + s_logger.warn("Unable to close the buffer ", e); |
| 2923 | + } |
| 2924 | + } |
2939 | 2925 | } |
2940 | 2926 | } |
2941 | 2927 |
|
2942 | | - protected String getVmStatsRawXML(Connection conn) { |
2943 | | - Date currentDate = new Date(); |
2944 | | - String startTime = String.valueOf(currentDate.getTime() / 1000 - 1000); |
2945 | | - |
2946 | | - return callHostPlugin(conn, "vmops", "gethostvmstats", "collectHostStats", String.valueOf("false"), "consolidationFunction", _consolidationFunction, "interval", String |
2947 | | - .valueOf(_pollingIntervalInSeconds), "startTime", startTime); |
2948 | | - } |
2949 | | - |
2950 | 2928 | protected State convertToState(Types.VmPowerState ps) { |
2951 | 2929 | final State state = s_statesTable.get(ps); |
2952 | 2930 | return state == null ? State.Unknown : state; |
|
0 commit comments