get rrd through http directly instead of xapi plugins

This commit is contained in:
Anthony Xu 2014-03-26 18:02:52 -07:00
parent 15d798d882
commit b4d3aac74d
9 changed files with 52 additions and 124 deletions

View File

@ -216,17 +216,19 @@ import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.ejb.Local;
import javax.naming.ConfigurationException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
@ -2529,46 +2531,65 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
return new GetVmDiskStatsAnswer(cmd, null, null, null);
}
protected Document getStatsRawXML(Connection conn, boolean host) {
Date currentDate = new Date();
String urlStr = "http://" + _host.ip + "/rrd_updates?";
urlStr += "session_id=" + conn.getSessionReference();
urlStr += "&host=" + (host ? "true" : "false");
urlStr += "&cf=" + _consolidationFunction;
urlStr += "&interval=" + _pollingIntervalInSeconds;
urlStr += "&start=" + (currentDate.getTime() / 1000 - 1000 - 100);
URL url;
BufferedReader in = null;
try {
url = new URL(urlStr);
url.openConnection();
URLConnection uc = url.openConnection();
in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
InputSource statsSource = new InputSource(in);
return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(statsSource);
} catch (MalformedURLException e) {
s_logger.warn("Malformed URL? come on...." + urlStr);
return null;
} catch (IOException e) {
s_logger.warn("Problems getting stats using " + urlStr, e);
return null;
} catch (SAXException e) {
s_logger.warn("Problems getting stats using " + urlStr, e);
return null;
} catch (ParserConfigurationException e) {
s_logger.warn("Problems getting stats using " + urlStr, e);
return null;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
s_logger.warn("Unable to close the buffer ", e);
}
}
}
}
protected Object[] getRRDData(Connection conn, int flag) {
/*
* Note: 1 => called from host, hence host stats 2 => called from vm, hence vm stats
*/
String stats = "";
Document doc = null;
try {
if (flag == 1) {
stats = getHostStatsRawXML(conn);
}
if (flag == 2) {
stats = getVmStatsRawXML(conn);
}
doc = getStatsRawXML(conn, flag == 1 ? true : false);
} catch (Exception e1) {
s_logger.warn("Error whilst collecting raw stats from plugin: ", e1);
return null;
}
// s_logger.debug("The raw xml stream is:"+stats);
// s_logger.debug("Length of raw xml is:"+stats.length());
//stats are null when the host plugin call fails (host down state)
if (stats == null) {
return null;
}
StringReader statsReader = new StringReader(stats);
InputSource statsSource = new InputSource(statsReader);
Document doc = null;
try {
doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(statsSource);
} catch (Exception e) {
s_logger.warn("Exception caught whilst processing the document via document factory:", e);
return null;
}
if (doc == null) {
s_logger.warn("Null document found after tryinh to parse the stats source");
if (doc == null) { //stats are null when the host plugin call fails (host down state)
return null;
}
@ -2592,7 +2613,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
}
}
return new Object[] {numRows, numColumns, legend, dataNode};
return new Object[] { numRows, numColumns, legend, dataNode };
}
protected String getXMLNodeValue(Node n) {
@ -2630,22 +2651,6 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
}
protected String getHostStatsRawXML(Connection conn) {
Date currentDate = new Date();
String startTime = String.valueOf(currentDate.getTime() / 1000 - 1000);
return callHostPlugin(conn, "vmops", "gethostvmstats", "collectHostStats", String.valueOf("true"), "consolidationFunction", _consolidationFunction, "interval",
String.valueOf(_pollingIntervalInSeconds), "startTime", startTime);
}
protected String getVmStatsRawXML(Connection conn) {
Date currentDate = new Date();
String startTime = String.valueOf(currentDate.getTime() / 1000 - 1000);
return callHostPlugin(conn, "vmops", "gethostvmstats", "collectHostStats", String.valueOf("false"), "consolidationFunction", _consolidationFunction, "interval",
String.valueOf(_pollingIntervalInSeconds), "startTime", startTime);
}
protected State convertToState(Types.VmPowerState ps) {
final State state = s_statesTable.get(ps);
return state == null ? State.Unknown : state;

View File

@ -1,61 +0,0 @@
#!/usr/bin/python
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# $Id: hostvmstats.py 10054 2010-06-29 22:09:31Z abhishek $ $HeadURL: svn://svn.lab.vmops.com/repos/vmdev/java/scripts/vm/hypervisor/xenserver/hostvmstats.py $
import XenAPI
import urllib
import time
import logging
import logging.handlers
LOG_FILENAME = '/tmp/xapilog'
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG)
stats_logger = logging.getLogger('statsLogger')
stats_logger.setLevel(logging.DEBUG)
#handler with maxBytes=10MiB
handler = logging.handlers.RotatingFileHandler(LOG_FILENAME, maxBytes=10*1024*1024, backupCount=5)
stats_logger.addHandler(handler)
def get_stats(session, collect_host_stats, consolidation_function, interval, start_time):
try:
if collect_host_stats == "true" :
url = "http://localhost/rrd_updates?"
url += "session_id=" + session._session
url += "&host=" + collect_host_stats
url += "&cf=" + consolidation_function
url += "&interval=" + str(interval)
url += "&start=" + str(int(time.time())-100)
else :
url = "http://localhost/rrd_updates?"
url += "session_id=" + session._session
url += "&host=" + collect_host_stats
url += "&cf=" + consolidation_function
url += "&interval=" + str(interval)
url += "&start=" + str(int(time.time())-100)
stats_logger.debug("Calling URL: %s",url)
sock = urllib.URLopener().open(url)
xml = sock.read()
sock.close()
stats_logger.debug("Size of returned XML: %s",len(xml))
return xml
except Exception,e:
stats_logger.exception("get_stats() failed")
raise

View File

@ -27,7 +27,6 @@ if os.path.exists("/opt/xensource/sm"):
if os.path.exists("/usr/lib/xcp/sm"):
sys.path.extend(["/usr/lib/xcp/sm/", "/usr/local/sbin/", "/sbin/"])
import base64
import hostvmstats
import socket
import stat
import tempfile
@ -61,15 +60,6 @@ def add_to_VCPUs_params_live(session, args):
return 'false'
return 'true'
@echo
def gethostvmstats(session, args):
collect_host_stats = args['collectHostStats']
consolidation_function = args['consolidationFunction']
interval = args['interval']
start_time = args['startTime']
result = hostvmstats.get_stats(session, collect_host_stats, consolidation_function, interval, start_time)
return result
@echo
def setup_iscsi(session, args):
uuid=args['uuid']
@ -1502,7 +1492,7 @@ def network_rules(session, args):
logging.debug("Failed to network rule !")
if __name__ == "__main__":
XenAPIPlugin.dispatch({"pingtest": pingtest, "setup_iscsi":setup_iscsi, "gethostvmstats": gethostvmstats,
XenAPIPlugin.dispatch({"pingtest": pingtest, "setup_iscsi":setup_iscsi,
"getgateway": getgateway, "preparemigration": preparemigration,
"setIptables": setIptables, "pingdomr": pingdomr, "pingxenserver": pingxenserver,
"routerProxy": routerProxy,

View File

@ -31,7 +31,6 @@ vmops=..,0755,/usr/lib/xcp/plugins
ovsgre=..,0755,/usr/lib/xcp/plugins
ovstunnel=..,0755,/usr/lib/xcp/plugins
vmopsSnapshot=..,0755,/usr/lib/xcp/plugins
hostvmstats.py=..,0755,/usr/lib/xcp/sm
systemvm.iso=../../../../../vms,0644,/usr/share/xcp/packages/iso/
id_rsa.cloud=../../../systemvm,0600,/root/.ssh
network_info.sh=..,0755,/opt/cloud/bin

View File

@ -31,7 +31,6 @@ NFSSR.py=/opt/xensource/sm
vmops=..,0755,/etc/xapi.d/plugins
ovstunnel=..,0755,/etc/xapi.d/plugins
vmopsSnapshot=..,0755,/etc/xapi.d/plugins
hostvmstats.py=..,0755,/opt/xensource/sm
systemvm.iso=../../../../../vms,0644,/opt/xensource/packages/iso
id_rsa.cloud=../../../systemvm,0600,/root/.ssh
network_info.sh=..,0755,/opt/cloud/bin

View File

@ -30,7 +30,6 @@ NFSSR.py=/opt/xensource/sm
vmops=..,0755,/etc/xapi.d/plugins
vmopsSnapshot=..,0755,/etc/xapi.d/plugins
cloudstack_pluginlib.py=..,0755,/etc/xapi.d/plugins
hostvmstats.py=..,0755,/opt/xensource/sm
systemvm.iso=../../../../../vms,0644,/opt/xensource/packages/iso
id_rsa.cloud=../../../systemvm,0600,/root/.ssh
network_info.sh=..,0755,/opt/cloud/bin

View File

@ -30,7 +30,6 @@ NFSSR.py=/opt/xensource/sm
vmops=..,0755,/etc/xapi.d/plugins
vmopsSnapshot=..,0755,/etc/xapi.d/plugins
cloudstack_pluginlib.py=..,0755,/etc/xapi.d/plugins
hostvmstats.py=..,0755,/opt/xensource/sm
systemvm.iso=../../../../../vms,0644,/opt/xensource/packages/iso
id_rsa.cloud=../../../systemvm,0600,/root/.ssh
network_info.sh=..,0755,/opt/cloud/bin

View File

@ -34,7 +34,6 @@ cloudstack_plugins.conf=..,0644,/etc/xensource
cloudstack_pluginlib.py=..,0755,/etc/xapi.d/plugins
ovstunnel=..,0755,/etc/xapi.d/plugins
vmopsSnapshot=..,0755,/etc/xapi.d/plugins
hostvmstats.py=..,0755,/opt/xensource/sm
systemvm.iso=../../../../../vms,0644,/opt/xensource/packages/iso
id_rsa.cloud=../../../systemvm,0600,/root/.ssh
network_info.sh=..,0755,/opt/cloud/bin

View File

@ -34,7 +34,6 @@ cloudstack_plugins.conf=..,0644,/etc/xensource
cloudstack_pluginlib.py=..,0755,/etc/xapi.d/plugins
ovstunnel=..,0755,/etc/xapi.d/plugins
cloud-plugin-storage=,0755,/etc/xapi.d/plugins
hostvmstats.py=..,0755,/opt/xensource/sm
systemvm.iso=../../../../../vms,0644,/opt/xensource/packages/iso
id_rsa.cloud=../../../systemvm,0600,/root/.ssh
network_info.sh=..,0755,/opt/cloud/bin