def test_active_pids(): set_cwd(tempfile.mkdtemp()) cuckoo_create() Pidfile("test6").create() assert Pidfile.get_active_pids() == { "test6": os.getpid(), }
def cuckoo_status(): # In order to keep track of the diskspace statistics of the temporary # directory we create a temporary file so we can statvfs() on that. temp_file = Files.temp_put("") paths = dict( binaries=cwd("storage", "binaries"), analyses=cwd("storage", "analyses"), temporary=temp_file, ) diskspace = {} for key, path in paths.items(): if hasattr(os, "statvfs") and os.path.isdir(path): stats = os.statvfs(path) diskspace[key] = dict( free=stats.f_bavail * stats.f_frsize, total=stats.f_blocks * stats.f_frsize, used=(stats.f_blocks - stats.f_bavail) * stats.f_frsize, ) # Now we remove the temporary file and its parent directory. os.unlink(temp_file) # Get the CPU load. if hasattr(os, "getloadavg"): cpuload = os.getloadavg() else: cpuload = [] if os.path.isfile("/proc/meminfo"): values = {} for line in open("/proc/meminfo"): key, value = line.split(":", 1) values[key.strip()] = value.replace("kB", "").strip() if "MemAvailable" in values and "MemTotal" in values: memavail = int(values["MemAvailable"]) memtotal = int(values["MemTotal"]) memory = 100 - 100.0 * memavail / memtotal else: memory = memavail = memtotal = None else: memory = memavail = memtotal = None try: cpu_core_count = multiprocessing.cpu_count() except NotImplementedError: cpu_core_count = None response = dict(version=version, hostname=socket.gethostname(), machines=dict(total=len(db.list_machines()), available=db.count_machines_available()), tasks=dict(total=db.count_tasks(), pending=db.count_tasks("pending"), running=db.count_tasks("running"), completed=db.count_tasks("completed"), reported=db.count_tasks("reported")), diskspace=diskspace, cpuload=cpuload, cpu_count=cpu_core_count, memory=memory, memavail=memavail, memtotal=memtotal, processes=Pidfile.get_active_pids()) return jsonify(response)
def cuckoo_status(): # In order to keep track of the diskspace statistics of the temporary # directory we create a temporary file so we can statvfs() on that. temp_file = Files.temp_put("") paths = dict( binaries=cwd("storage", "binaries"), analyses=cwd("storage", "analyses"), temporary=temp_file, ) diskspace = {} for key, path in paths.items(): if hasattr(os, "statvfs") and os.path.isdir(path): stats = os.statvfs(path) diskspace[key] = dict( free=stats.f_bavail * stats.f_frsize, total=stats.f_blocks * stats.f_frsize, used=(stats.f_blocks - stats.f_bavail) * stats.f_frsize, ) # Now we remove the temporary file and its parent directory. os.unlink(temp_file) # Get the CPU load. if hasattr(os, "getloadavg"): cpuload = os.getloadavg() else: cpuload = [] if os.path.isfile("/proc/meminfo"): values = {} for line in open("/proc/meminfo"): key, value = line.split(":", 1) values[key.strip()] = value.replace("kB", "").strip() if "MemAvailable" in values and "MemTotal" in values: memavail = int(values["MemAvailable"]) memtotal = int(values["MemTotal"]) memory = 100 - 100.0 * memavail / memtotal else: memory = memavail = memtotal = None else: memory = memavail = memtotal = None try: cpu_core_count = multiprocessing.cpu_count() except NotImplementedError: cpu_core_count = None response = dict( version=version, hostname=socket.gethostname(), machines=dict( total=len(db.list_machines()), available=db.count_machines_available() ), tasks=dict( total=db.count_tasks(), pending=db.count_tasks("pending"), running=db.count_tasks("running"), completed=db.count_tasks("completed"), reported=db.count_tasks("reported") ), diskspace=diskspace, cpuload=cpuload, cpu_count=cpu_core_count, memory=memory, memavail=memavail, memtotal=memtotal, processes=Pidfile.get_active_pids() ) return jsonify(response)