コード例 #1
0
ファイル: collect.py プロジェクト: NBSW/slapos.core
    def test_compress_log_directory(self):
        log_directory = "%s/test_compress" % self.instance_root
        dump_folder = "%s/1990-01-01" % log_directory

        if not os.path.exists(log_directory):
            os.mkdir(log_directory)

        if os.path.exists(dump_folder):
            shutil.rmtree(dump_folder)

        os.mkdir("%s/1990-01-01" % log_directory)
        with open("%s/test.txt" % dump_folder, "w") as dump_file:
            dump_file.write("hi")
            dump_file.close()

        reporter.compressLogFolder(log_directory)

        self.assertFalse(os.path.exists(dump_folder))

        self.assertTrue(os.path.exists("%s.tar.gz" % dump_folder))

        with tarfile.open("%s.tar.gz" % dump_folder) as tf:
            self.assertEquals(tf.getmembers()[0].name, "1990-01-01")
            self.assertEquals(tf.getmembers()[1].name, "1990-01-01/test.txt")
            self.assertEquals(tf.extractfile(tf.getmembers()[1]).read(), 'hi')
コード例 #2
0
ファイル: __init__.py プロジェクト: SlapOS/slapos.core
def do_collect(conf):
  """
  Main function
  The idea here is to poll system every so many seconds
  For each poll, we get a list of Snapshots, holding informations about
  processes. We iterate over that list to store datas on a per user basis:
    Each user object is a dict, indexed on timestamp. We add every snapshot
    matching the user so that we get informations for each users
  """
  try:
    collected_date, collected_time = _get_time()
    user_dict = get_user_list(conf)
    try:
      for snapshot in current_state(user_dict):
        if snapshot:
          user_dict[snapshot.username].append(snapshot)
    except (KeyboardInterrupt, SystemExit, NoSuchProcess):
      raise
      
    log_directory = "%s/var/data-log" % conf.get("slapos", "instance_root")
    mkdir_p(log_directory, 0o755)
    
    consumption_report_directory = "%s/var/consumption-report" % \
                                        conf.get("slapos", "instance_root") 
    mkdir_p(consumption_report_directory, 0o755)

    xml_report_directory = "%s/var/xml_report/%s" % \
                    (conf.get("slapos", "instance_root"), 
                     conf.get("slapos", "computer_id"))
    mkdir_p(xml_report_directory, 0o755)

    if stat.S_IMODE(os.stat(log_directory).st_mode) != 0o755:
      os.chmod(log_directory, 0o755)    

    database = Database(log_directory)

    if conf.has_option("slapformat", "computer_model_id"):
      computer_model_id = conf.get("slapformat", 
                                  "computer_model_id")
 
    else:
      computer_model_id = "no_model"

    uptime = _get_uptime()
    if conf.has_option("slapformat", "heating_sensor_id"):
      heating_sensor_id = conf.get("slapformat", 
                                  "heating_sensor_id")
      database.connect()
      test_heating = uptime is not None and \
                     uptime > datetime.timedelta(seconds=86400) and \
                     database.getLastHeatingTestTime() > uptime
      database.close()

    else:
      heating_sensor_id = "no_sensor"
      test_heating = False

    computer = Computer(ComputerSnapshot(model_id=computer_model_id, 
                                     sensor_id = heating_sensor_id,
                                     test_heating=test_heating))

    computer.save(database, collected_date, collected_time)

    for user in user_dict.values():
      user.save(database, collected_date, collected_time)
    
    SystemCSVReporterDumper(database).dump(log_directory)
    RawCSVDumper(database).dump(log_directory)
    consumption_report = ConsumptionReport(
                      computer_id=conf.get("slapos", "computer_id"), 
                      user_list=get_user_list(conf), 
                      database=database,
                      location=consumption_report_directory)
    
    base = datetime.datetime.today()
    for x in range(1, 3):
      report_file = consumption_report.buildXMLReport(
          (base - datetime.timedelta(days=x)).strftime("%Y-%m-%d"))

      if report_file is not None:
        shutil.copy(report_file, xml_report_directory)

    compressLogFolder(log_directory)

    # Drop older entries already reported
    database.garbageCollect()

  except AccessDenied:
    print("You HAVE TO execute this script with root permission.")