コード例 #1
0
  def collectVMmetrics(self):

    domains = domainsVM()

    try:
      while not self._stopevent.isSet():
      #while True:
      #for i in range(1,6):
        #print "...getting Domains..."

        dom_list = domains.getDomains()
        #print "Number of VMs:", len(dom_list)

        for key in dom_list:
          if dom_list[key].firstTime:
            vm_name = dom_list[key].name

            if self.get_vm_metrics is True:
              # for each NEW VM start a thread for collecting and sending VM metrics
              logger.info("New VM detected: starting thread for %s", vm_name)
          
              thread_VMmetric = Thread(target = self.readVMmetrics, args=(vm_name, ))
              thread_VMmetric.daemon = True
              thread_VMmetric.start()

            if self.get_vm_counters or self.get_vm_raw_counters:
              # get pid of vm

              path = vm_pid_path_prefix + vm_name + '.pid'
              with open(path, 'r') as f:
                vm_pid = f.read()

            if self.get_vm_counters is True and self.counters_list:
              # for each NEW VM start a thread for collecting and sending counters metrics
              logger.info("New VM detected: starting collecting VM Counters - thread for %s", vm_name)
              counters_metric = CountersMetrics(self.counters_directory, self.counters_interval, self.counters_list, self.mconf, self.gconf, vm_pid, vm_name)
              thread_VMCountersMetric = Thread(target = counters_metric.collectCountersMetrics)
              thread_VMCountersMetric.daemon = True
              thread_VMCountersMetric.start()

              #send metrics

            if self.get_vm_raw_counters is True and  self.raw_counters_list:
              # for each NEW VM start a thread for collecting and sending counters metrics
              logger.info("New VM detected: starting collecting VM Raw Counters - thread for %s", vm_name)

              raw_counters_metric = RawCountersMetrics(self.counters_directory, self.counters_interval, self.raw_counters_list, self.mconf, self.gconf, vm_pid, vm_name)
              thread_VMRawCountersMetric = Thread(target = raw_counters_metric.collectCountersMetrics)
              thread_VMRawCountersMetric.daemon = True
              thread_VMRawCountersMetric.start()

        sleep(check_vm_interval)


      logger.info("All VM threads terminated")

    except (KeyboardInterrupt, SystemExit):
      self.stopThreads()
      self.exit = True
      sys.exit(0)
コード例 #2
0
  def readVMmetrics(self, vm_name): 
    gmetric_obj = gmetric.Gmetric(self.gconf.host, self.gconf.port, self.gconf.protocol)

    list_metrics = {}
    exec_time = 0

    while not self._stopevent.isSet():
      #check if vm is still alive
      domains = domainsVM()
      dom_list = domains.getDomains()
      if vm_name not in dom_list:
        logger.info("%s has been destroyed", vm_name)
        break

      try:

        start_time = time.time()
        g = guestfs.GuestFS ()
        # Attach the disk image read-only to libguestfs. 
        g.add_domain(vm_name, readonly=1)
        # Run the libguestfs back-end.
        
        g.launch()
        
        # Ask libguestfs to inspect for operating systems.
        roots = g.inspect_os ()
        if len (roots) == 0:
          logger.error("no operating systems found")
          break

        if len (roots) > 1:
          logger.error("dual/multi-boot images are not supported")
          break

        root = roots[0]
        # Mount up the disks, like guestfish -i.
        mps = g.inspect_get_mountpoints (root)
        for device in mps:
          try:
            g.mount(device[1], device[0])
          except RuntimeError as msg:
            logger.error("%s (ignored)",msg)

        try:    
          lines = g.read_lines(self.vm_file_path)
              
          for l in lines:
            if len(l.strip()) > 0:
              token = l.split('|')

              n = token[0].strip()
              v = token[1].strip()
                
              list_metrics[n] = v
                  
          #send metrics
          self.sendVMmetrics(list_metrics, gmetric_obj, vm_name)
        except RuntimeError, io:
          logger.warning("%s %s %s", threading.currentThread().name, vm_name, io)

        g.umount_all()
        g.close()
        exec_time = time.time() - start_time

      except (KeyboardInterrupt, SystemExit):
        self.stopThreads()
        sys.exit(0)