Example #1
0
    def __init__(self):
        # Replace the objects attributes with the original
        #  attributes and data.
        self.__dict__ = DataStore._shared_state

        # Make sure that the DataStore object is only initialized once.
        if not DataStore._initialized:
            # Allocate a lock that will be used by all threads
            #  that are reading or writing to the data store.
            self.lock = thread.allocate_lock()
            self.lock.acquire()
            self.rootElement = None
            # Initialize the data store with the GANGLIA_XML and GRID tags.
            # Data store should never be completely empty.  Even if there are
            # no reporting data sources the web front end depends on having
            # at least a GANGLIA_XML tag and a nested GRID tag.
            cfg = getConfig()
            self.setNode(Element('GANGLIA_XML', {'VERSION':cfg.VERSION, 'SOURCE':'gmetad'}))
            self.setNode(Element('GRID', {'NAME':cfg[GmetadConfig.GRIDNAME], 'AUTHORITY':cfg[GmetadConfig.AUTHORITY], 'LOCALTIME':'%d' % time.time()}), self.rootElement)
            self.lock.release()

            # Start up the grid summary thread. 
            self.gridSummary = DataStoreGridSummary()
            self.gridSummary.start()

            # Start up the plugin notifier.
            self.notifier = GmetadNotifier()
            self.notifier.start()
            DataStore._initialized = True
 def startElement(self, tag, attrs):
     ''' This methods creates an element based on XML tags and inserts it into the data store. '''
     ds = DataStore()
     # Create a new node based on the XML attributes.
     e = Element(tag, attrs)
     # If this is the head tag for the XML dump, initialize the data store
     if 'GANGLIA_XML' == tag:
         ds.acquireLock(self)
         self._elemStack.append(ds.getNode(
         ))  # Fetch the root node.  It has already been set into the tree.
         self._elemStackLen += 1
         cfg = getConfig()
         # We'll go ahead and update any existing GRID tag with a new one (new time) even if one already exists.
         e = Element(
             'GRID', {
                 'NAME': cfg[GmetadConfig.GRIDNAME],
                 'AUTHORITY': cfg[GmetadConfig.AUTHORITY],
                 'LOCALTIME': '%d' % time.time()
             })
         self._ancestry.append('GANGLIA_XML')
     # Insert the new node into the data store at the appropriate location
     self._elemStack.append(
         ds.setNode(e, self._elemStack[self._elemStackLen - 1]))
     # If this is a cluster or nested grid node, then keep track of the data store path to this node.
     if (len(self._ancestry) < 2
             or (len(self._ancestry) == 2 and e.id in ['GRID', 'CLUSTER'])):
         self._ancestry.append('%s:%s' % (e.id, e.getAttr('name')))
     self._elemStackLen += 1
Example #3
0
    def __init__(self):
        # Replace the objects attributes with the original
        #  attributes and data.
        self.__dict__ = DataStore._shared_state

        # Make sure that the DataStore object is only initialized once.
        if not DataStore._initialized:
            # Allocate a lock that will be used by all threads
            #  that are reading or writing to the data store.
            self.lock = thread.allocate_lock()
            self.lock.acquire()
            self.rootElement = None
            # Initialize the data store with the GANGLIA_XML and GRID tags.
            # Data store should never be completely empty.  Even if there are
            # no reporting data sources the web front end depends on having
            # at least a GANGLIA_XML tag and a nested GRID tag.
            cfg = getConfig()
            self.setNode(Element('GANGLIA_XML', {'VERSION':cfg.VERSION, 'SOURCE':'gmetad'}))
            self.setNode(Element('GRID', {'NAME':cfg[GmetadConfig.GRIDNAME], 'AUTHORITY':cfg[GmetadConfig.AUTHORITY], 'LOCALTIME':'%d' % time.time()}), self.rootElement)
            self.lock.release()

            # Start up the grid summary thread. 
            self.gridSummary = DataStoreGridSummary()
            self.gridSummary.start()

            # Start up the plugin notifier.
            self.notifier = GmetadNotifier()
            self.notifier.start()
            DataStore._initialized = True
 def startElement(self, tag, attrs):
     ''' This methods creates an element based on XML tags and inserts it into the data store. '''
     ds = DataStore()
     # Create a new node based on the XML attributes.
     e = Element(tag, attrs)
     # If this is the head tag for the XML dump, initialize the data store 
     if 'GANGLIA_XML' == tag:
         ds.acquireLock(self)
         self._elemStack.append(ds.getNode()) # Fetch the root node.  It has already been set into the tree.
         self._elemStackLen += 1
         cfg = getConfig()
         # We'll go ahead and update any existing GRID tag with a new one (new time) even if one already exists.
         e = Element('GRID', {'NAME':cfg[GmetadConfig.GRIDNAME], 'AUTHORITY':cfg[GmetadConfig.AUTHORITY], 'LOCALTIME':'%d' % time.time()})
         self._ancestry.append('GANGLIA_XML')
         
     # Insert the cpu_usage when cpu_idle is exsists
     if 'METRIC' == tag and 'cpu_idle' == e.attrs['name']:
         self.insert_cpu_usage_metric(e, ds)
     
     # Insert the mem_usage when mem_free and mem_total are exsists(mem_usage is exsist in vmware instances)
     temp_metric = self._elemStack[self._elemStackLen-1]
     if isinstance(temp_metric.children, dict) and not temp_metric.children.has_key('METRIC:mem_usage') and temp_metric.children.has_key('METRIC:mem_free') and temp_metric.children.has_key('METRIC:mem_total'):   
         self.insert_mem_usage_metric(temp_metric, ds)
     
     # Insert the new node into the data store at the appropriate location
     self._elemStack.append(ds.setNode(e, self._elemStack[self._elemStackLen-1]))
     # If this is a cluster or nested grid node, then keep track of the data store path to this node.
     if (len(self._ancestry) < 2 or (len(self._ancestry) == 2 and e.id in ['GRID', 'CLUSTER'])):
         self._ancestry.append('%s:%s'%(e.id,e.getAttr('name')))
     self._elemStackLen += 1
Example #5
0
def setuid():
    cfg = getConfig()
    setuid_user = None
    if cfg[GmetadConfig.SETUID]:
        setuid_user = cfg[GmetadConfig.SETUID_USERNAME]
    if setuid_user is not None:
        try:
            os.setuid(pwd.getpwnam(setuid_user)[2])
        except Exception:
            print 'Unable to setuid to user "%s", exiting' % setuid_user
            sys.exit()
Example #6
0
def setuid():
    cfg = getConfig()
    setuid_user = None
    if cfg[GmetadConfig.SETUID]:
        setuid_user = cfg[GmetadConfig.SETUID_USERNAME]
    if setuid_user is not None:
        try:
            os.setuid(pwd.getpwnam(setuid_user)[2])
        except Exception:
            print 'Unable to setuid to user "%s", exiting' % setuid_user
            sys.exit()
Example #7
0
    def __init__(self, impid, cfgid=None):
        # All method instances are initialized with a module id that should match a section id
        # found in the configuration file.
        self.cfgid = impid
        if cfgid is not None:
            self.cfgid += '.' + cfgid

        # Initialize the default values of method configs
        self.cfg = None
        self.initConfDefaults()

        # Initialize the config handlers
        self.cfgHandlers = None
        self.initConfHandlers()

        # Parse the config of alarm method
        self._parseConfig(getConfig().getSection(impid))

        if cfgid is not None:
            self._parseConfig(getConfig().getSection(impid + '.' + cfgid))
    def __init__(self):
        threading.Thread.__init__(self)

        # Load and start all of the plugins that are found in the plugin directory
        gmetadConfig = getConfig()
        load_plugins(gmetadConfig[GmetadConfig.PLUGINS_DIR])
        start_plugins()
    
        # Intialize the thread
        self._cond = threading.Condition()
        self._running = False
        self._shuttingDown = False
        self._transQueue = []
Example #9
0
    def __init__(self):
        threading.Thread.__init__(self)

        # Load and start all of the plugins that are found in the plugin directory
        gmetadConfig = getConfig()
        load_plugins(gmetadConfig[GmetadConfig.PLUGINS_DIR])
        start_plugins()

        # Intialize the thread
        self._cond = threading.Condition()
        self._running = False
        self._shuttingDown = False
        self._transQueue = []
 def startElement(self, tag, attrs):
     ''' This methods creates an element based on XML tags and inserts it into the data store. '''
     ds = DataStore()
     # Create a new node based on the XML attributes.
     e = Element(tag, attrs)
     # If this is the head tag for the XML dump, initialize the data store 
     if 'GANGLIA_XML' == tag:
         ds.acquireLock(self)
         self._elemStack.append(ds.getNode()) # Fetch the root node.  It has already been set into the tree.
         self._elemStackLen += 1
         cfg = getConfig()
         # We'll go ahead and update any existing GRID tag with a new one (new time) even if one already exists.
         e = Element('GRID', {'NAME':cfg[GmetadConfig.GRIDNAME], 'AUTHORITY':cfg[GmetadConfig.AUTHORITY], 'LOCALTIME':'%d' % time.time()})
         self._ancestry.append('GANGLIA_XML')
     # Insert the new node into the data store at the appropriate location
     self._elemStack.append(ds.setNode(e, self._elemStack[self._elemStackLen-1]))
     # If this is a cluster node, then keep track of the data store path to this node.
     if (self._ancestry[len(self._ancestry)-1].startswith('CLUSTER') == False):
         self._ancestry.append('%s:%s'%(e.id,e.getAttr('name')))
     self._elemStackLen += 1
Example #11
0
    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()
Example #12
0
 def __init__(self, cfgid):
     # All modules are intialized with a module id that should match a section id
     #  found in the configuration file.
     self.cfgid = cfgid
     self._parseConfig(getConfig().getSection(cfgid))
 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()