def _getXmlImpl(self, element, filterList=None, queryargs=None): """ This method can be called recursively to traverse the data store and produce XML for specific nodes. It also respects the filter and query args when generating the XML.""" # Add the XML tag rbuf = "<%s" % element.tag # If this is a grid tag, then get the local time since a time stamp was never provided by gmond. if "GRID" == element.id: element.setAttr("localtime", int(time.time())) # Add each attribute that is contained in the. By pass some specific attributes. for k, v in element.getAttrs().items(): rbuf += ' %s="%s"' % (k.upper(), v) if queryargs is not None: if ("GRID" == element.id or "CLUSTER" == element.id) and (filterList is None or not len(filterList)): try: # If the filter specifies that this is a summary rather than a regular XML dump, generate the # summary XML. if queryargs["filter"].lower().strip() == "summary": # A summary XML dump will contain a grid summary as well as each cluster summary. Each will # be added during recusive calls to this method. if "GRID" == element.id: rbuf += ">\n%s</GRID>\n" % self._getGridSummary(element, filterList, queryargs) return rbuf elif "CLUSTER" == element.id: rbuf += ">\n%s</CLUSTER>\n" % self._getClusterSummary(element, filterList, queryargs) return rbuf except ValueError: pass # If there aren't any children, then no reason to continue. if 0 < len(element.children): # Close the last tag rbuf += ">\n" showAllChildren = True # If there was a specific filter specified, then only include the appropriate children. Otherwise # show all of the children. if filterList is not None and len(filterList): try: # Get the key and call this method recusively for the child key = Element.generateKey([self._pcid_map[element.id], filterList[0]]) rbuf += self._getXmlImpl(element.children[key], filterList[1:], queryargs) showAllChildren = False except KeyError: pass if showAllChildren: # For each child, call this method recusively. This will produce a complete dump of all children for c in element.children.values(): rbuf += self._getXmlImpl(c, filterList, queryargs) rbuf += "</%s>\n" % element.tag else: rbuf += " />\n" return rbuf
def run(self): ds = DataStore() while not self._shuttingDown: connected = False # Create a socket and connect to the cluster data source. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: sock.connect( self._getEndpoint( self.dataSource.hosts[self.lastKnownGoodHost])) connected = True except socket.error: # Keep track of the last good data source within the cluster. If we can't reconnect to the # same data source, try the next one in the list. curidx = self.lastKnownGoodHost while True: curidx += 1 if curidx >= len(self.dataSource.hosts): curidx = 0 if curidx == self.lastKnownGoodHost: break try: sock.connect( self._getEndpoint(self.dataSource.hosts[curidx])) self.lastKnownGoodHost = curidx connected = True break except socket.error: pass if connected: logging.info('Querying data source %s via host %s' % (self.dataSource.name, self.dataSource.hosts[self.lastKnownGoodHost])) xmlbuf = '' while True: # Read all of the XML data from the data source. buf = sock.recv(8192) if not buf: break xmlbuf += buf sock.close() # Create an XML parser and parse the buffer gch = GmondContentHandler() xml.sax.parseString(xmlbuf, gch) # Notify the data store that all updates for the cluster are finished. clusterNode = ds.getNode(gch.getClusterAncestry()) if clusterNode is not None: clusterNode.setAttr('status', 'up') else: logging.error( 'Could not connect to any host for data source %s' % self.dataSource.name) ds = DataStore() cfg = getConfig() gridKey = Element.generateKey( ['GRID', cfg[GmetadConfig.GRIDNAME]]) clusterKey = Element.generateKey( ['CLUSTER', self.dataSource.name]) gridNode = ds.getNode([str(ds.rootElement), gridKey]) clusterNode = None if gridNode is not None and str(gridNode) == gridKey: try: clusterNode = gridNode[clusterKey] except KeyError: clusterNode = Element( 'CLUSTER', { 'NAME': self.dataSource.name, 'LOCALTIME': '%d' % time.time() }) ds.setNode(clusterNode, gridNode) if clusterNode is not None: clusterNode.setAttr('status', 'down') #clusterNode.localtime = time.time() ds.updateFinished(clusterNode) if self._shuttingDown: break # Go to sleep for a while. self._cond.acquire() self._cond.wait(getRandomInterval(self.dataSource.interval)) self._cond.release()
def run(self): ds = DataStore() while not self._shuttingDown: connected = False # Create a socket and connect to the cluster data source. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: sock.connect( self._getEndpoint(self.dataSource.hosts[self.lastKnownGoodHost]) ) connected = True except socket.error: # Keep track of the last good data source within the cluster. If we can't reconnect to the # same data source, try the next one in the list. curidx = self.lastKnownGoodHost while True: curidx += 1 if curidx >= len(self.dataSource.hosts): curidx = 0 if curidx == self.lastKnownGoodHost: break try: sock.connect( self._getEndpoint(self.dataSource.hosts[curidx]) ) self.lastKnownGoodHost = curidx connected=True break except socket.error: pass if connected: logging.info('Querying data source %s via host %s' % (self.dataSource.name, self.dataSource.hosts[self.lastKnownGoodHost])) xmlbuf = '' while True: # Read all of the XML data from the data source. buf = sock.recv(8192) if not buf: break xmlbuf += buf sock.close() # Create an XML parser and parse the buffer gch = GmondContentHandler() xml.sax.parseString(xmlbuf, gch) # Notify the data store that all updates for the cluster are finished. clusterNode = ds.getNode(gch.getClusterAncestry()) if clusterNode is not None: clusterNode.setAttr('status', 'up') else: logging.error('Could not connect to any host for data source %s' % self.dataSource.name) ds = DataStore() cfg = getConfig() gridKey = Element.generateKey(['GRID',cfg[GmetadConfig.GRIDNAME]]) clusterKey = Element.generateKey(['CLUSTER', self.dataSource.name]) gridNode = ds.getNode([str(ds.rootElement), gridKey]) clusterNode = None if gridNode is not None and str(gridNode) == gridKey: try: clusterNode = gridNode[clusterKey] except KeyError: clusterNode = Element('CLUSTER', {'NAME':self.dataSource.name, 'LOCALTIME':'%d' % time.time()}) ds.setNode(clusterNode, gridNode) if clusterNode is not None: clusterNode.setAttr('status', 'down') #clusterNode.localtime = time.time() ds.updateFinished(clusterNode) if self._shuttingDown: break # Go to sleep for a while. self._cond.acquire() self._cond.wait(getRandomInterval(self.dataSource.interval)) self._cond.release()
def _getXmlImpl(self, element, filterList=None, queryargs=None): ''' This method can be called recursively to traverse the data store and produce XML for specific nodes. It also respects the filter and query args when generating the XML.''' skipTag = None rbuf = '' if element.id in ['CLUSTER', 'HOST', 'EXTRA_DATA', 'EXTRA_ELEMENT'] and self.gridDepth > 0: skipTag = True # If this is a grid tag, then get the local time since a time stamp was never provided by gmond. if 'GRID' == element.id: element.setAttr('localtime', int(time.time())) self.gridDepth += 1 logging.info('Found <GRID> depth is now: %d' %self.gridDepth) if not skipTag: # Add the XML tag rbuf = '<%s' % element.tag # Add each attribute that is contained in the. By pass some specific attributes. for k,v in element.getAttrs().items(): rbuf += ' %s="%s"' % (k.upper(), v) if queryargs is not None or ('GRID' == element.id and self.gridDepth > 0): if (('GRID' == element.id or 'CLUSTER' == element.id) and (filterList is None or not len(filterList))) or ('GRID' == element.id and self.gridDepth > 0): try: # If the filter specifies that this is a summary rather than a regular XML dump, generate the # summary XML. if (queryargs is not None and queryargs['filter'].lower().strip() == 'summary') or ('GRID' == element.id and self.gridDepth > 0): # A summary XML dump will contain a grid summary as well as each cluster summary. Each will # be added during recusive calls to this method. if 'GRID' == element.id: rbuf += '>\n%s</GRID>\n' % self._getGridSummary(element, filterList, queryargs) self.gridDepth -= 1 logging.info('Found </GRID> depth is now %d' %self.gridDepth) return rbuf elif 'CLUSTER' == element.id: if not skipTag: rbuf += '>\n%s</CLUSTER>\n' % self._getClusterSummary(element, filterList, queryargs) else: rbuf += '%s' % self._getClusterSummary(element, filterList, queryargs) return rbuf except ValueError: pass # If there aren't any children, then no reason to continue. if 0 < len(element.children): if not skipTag: # Close the last tag rbuf += '>\n' showAllChildren = True # If there was a specific filter specified, then only include the appropriate children. Otherwise # show all of the children. if filterList is not None and len(filterList): try: # Get the key and call this method recusively for the child key = Element.generateKey([self._pcid_map[element.id], filterList[0]]) rbuf += self._getXmlImpl(element.children[key], filterList[1:], queryargs) showAllChildren = False except KeyError: pass if showAllChildren: # For each child, call this method recusively. This will produce a complete dump of all children for c in element.children.values(): rbuf += self._getXmlImpl(c, filterList, queryargs) if 'GRID' == element.tag: self.gridDepth -= 1 logging.info('Found </GRID> depth is now: %d' %self.gridDepth) if not skipTag: rbuf += '</%s>\n' % element.tag else: if not skipTag: rbuf += ' />\n' return rbuf
def _getXmlImpl(self, element, filterList=None, queryargs=None): ''' This method can be called recursively to traverse the data store and produce XML for specific nodes. It also respects the filter and query args when generating the XML.''' skipTag = None rbuf = '' if element.id in ['CLUSTER', 'HOST', 'EXTRA_DATA', 'EXTRA_ELEMENT' ] and self.gridDepth > 0: skipTag = True # If this is a grid tag, then get the local time since a time stamp was never provided by gmond. if 'GRID' == element.id: element.setAttr('localtime', int(time.time())) self.gridDepth += 1 logging.info('Found <GRID> depth is now: %d' % self.gridDepth) if not skipTag: # Add the XML tag rbuf = '<%s' % element.tag # Add each attribute that is contained in the. By pass some specific attributes. for k, v in element.getAttrs().items(): rbuf += ' %s="%s"' % (k.upper(), v) if queryargs is not None or ('GRID' == element.id and self.gridDepth > 0): if (('GRID' == element.id or 'CLUSTER' == element.id) and (filterList is None or not len(filterList))) or ( 'GRID' == element.id and self.gridDepth > 0): try: # If the filter specifies that this is a summary rather than a regular XML dump, generate the # summary XML. if (queryargs is not None and queryargs['filter'].lower().strip() == 'summary') or ('GRID' == element.id and self.gridDepth > 0): # A summary XML dump will contain a grid summary as well as each cluster summary. Each will # be added during recusive calls to this method. if 'GRID' == element.id: rbuf += '>\n%s</GRID>\n' % self._getGridSummary( element, filterList, queryargs) self.gridDepth -= 1 logging.info('Found </GRID> depth is now %d' % self.gridDepth) return rbuf elif 'CLUSTER' == element.id: if not skipTag: rbuf += '>\n%s</CLUSTER>\n' % self._getClusterSummary( element, filterList, queryargs) else: rbuf += '%s' % self._getClusterSummary( element, filterList, queryargs) return rbuf except ValueError: pass # If there aren't any children, then no reason to continue. if 0 < len(element.children): if not skipTag: # Close the last tag rbuf += '>\n' showAllChildren = True # If there was a specific filter specified, then only include the appropriate children. Otherwise # show all of the children. if filterList is not None and len(filterList): try: # Get the key and call this method recusively for the child key = Element.generateKey( [self._pcid_map[element.id], filterList[0]]) rbuf += self._getXmlImpl(element.children[key], filterList[1:], queryargs) showAllChildren = False except KeyError: pass if showAllChildren: # For each child, call this method recusively. This will produce a complete dump of all children for c in element.children.values(): rbuf += self._getXmlImpl(c, filterList, queryargs) if 'GRID' == element.tag: self.gridDepth -= 1 logging.info('Found </GRID> depth is now: %d' % self.gridDepth) if not skipTag: rbuf += '</%s>\n' % element.tag else: if not skipTag: rbuf += ' />\n' return rbuf