Skip to content

Commit e82e142

Browse files
author
Alex Huang
committed
Changed vm stats to be collected through the xs http connection
1 parent 5e44df6 commit e82e142

1 file changed

Lines changed: 25 additions & 47 deletions

File tree

‎plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java‎

Lines changed: 25 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.io.IOException;
2626
import java.io.InputStream;
2727
import java.io.InputStreamReader;
28-
import java.io.StringReader;
2928
import java.net.MalformedURLException;
3029
import java.net.URI;
3130
import java.net.URISyntaxException;
@@ -49,6 +48,7 @@
4948
import javax.ejb.Local;
5049
import javax.naming.ConfigurationException;
5150
import javax.xml.parsers.DocumentBuilderFactory;
51+
import javax.xml.parsers.ParserConfigurationException;
5252

5353
import org.apache.commons.codec.binary.Base64;
5454
import org.apache.log4j.Logger;
@@ -57,6 +57,7 @@
5757
import org.w3c.dom.Node;
5858
import org.w3c.dom.NodeList;
5959
import org.xml.sax.InputSource;
60+
import org.xml.sax.SAXException;
6061

6162
import com.google.gson.Gson;
6263
import com.trilead.ssh2.SCPClient;
@@ -2803,41 +2804,16 @@ protected Object[] getRRDData(Connection conn, int flag) {
28032804
/*
28042805
* Note: 1 => called from host, hence host stats 2 => called from vm, hence vm stats
28052806
*/
2806-
String stats = "";
2807+
Document doc = null;
28072808

28082809
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);
28152811
} catch (Exception e1) {
28162812
s_logger.warn("Error whilst collecting raw stats from plugin: ", e1);
28172813
return null;
28182814
}
28192815

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)
28412817
return null;
28422818
}
28432819

@@ -2908,45 +2884,47 @@ protected double getDataAverage(Node dataNode, int col, int numRows) {
29082884

29092885
}
29102886

2911-
protected String getHostStatsRawXML(Connection conn) {
2887+
protected Document getStatsRawXML(Connection conn, boolean host) {
29122888
Date currentDate = new Date();
29132889
String urlStr = "http://" + _host.ip + "/rrd_updates?";
29142890
urlStr += "session_id=" + conn.getSessionReference();
2915-
urlStr += "&host=" + "true";
2891+
urlStr += "&host=" + (host ? "true" : "false");
29162892
urlStr += "&cf=" + _consolidationFunction;
29172893
urlStr += "&interval=" + _pollingIntervalInSeconds;
29182894
urlStr += "&start=" + (currentDate.getTime() / 1000 - 1000 - 100);
29192895

29202896
URL url;
2897+
BufferedReader in = null;
29212898
try {
29222899
url = new URL(urlStr);
29232900
url.openConnection();
29242901
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);
29332905
} catch (MalformedURLException e) {
29342906
s_logger.warn("Malformed URL? come on...." + urlStr);
29352907
return null;
29362908
} catch (IOException e) {
29372909
s_logger.warn("Problems getting stats using " + urlStr, e);
29382910
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+
}
29392925
}
29402926
}
29412927

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-
29502928
protected State convertToState(Types.VmPowerState ps) {
29512929
final State state = s_statesTable.get(ps);
29522930
return state == null ? State.Unknown : state;

0 commit comments

Comments
 (0)