DF_CMD = ['/usr/bin/df', '--no-sync', '--portability']
-class FilesystemStats:
+class FilesystemFillState:
""" representation of 1 line of the 'df' command
has fields filesystem, size, used, available, capacity, mount_point
capacity = None
mount_point = None
+ def __str__(self):
+ return '[Filesystem {0} mounted at {1}: {2}% used]' \
+ .format(self.name, self.mount_point, self.capacity)
-def get_disc_stats():
- """ get stats on all filesystems
+def get_filesystem_fill_states():
+ """ get fill state on all filesystems
- parses the output of cmd 'df', returns list of FilesystemStats
+ parses the output of cmd 'df', returns list of :py:class:`FilesystemFillState`
"""
# call
# create result
result = []
for line in out[1:]:
- stats = FilesystemStats()
+ stats = FilesystemFillState()
stats.name = line[ : separator_cols[0]].strip()
stats.size = int(line[separator_cols[0] : separator_cols[1]].strip())
stats.used = int(line[separator_cols[1] : separator_cols[2]].strip())
def test_disc_stats(self):
- """ tests get_disc_stats """
+ """ tests get_filesystem_fill_states """
- stats = file_helpers.get_disc_stats()
+ stats = file_helpers.get_filesystem_fill_states()
# check number
code, out, err = call_and_capture(file_helpers.DF_CMD)
import contextlib
from threading import Thread
from time import sleep
-from file_helpers import get_disc_stats
+from file_helpers import get_filesystem_fill_states
from datetime import datetime as dt
from buffers import LogarithmicBuffer
from itertools import tee
self.decision_function = decision_function
# remember relevant disc stats at start
- bufs = dict((stat.name, LogarithmicBuffer(5)) for stat in stats)
- now = dt.now()
- for stat in stats:
- bufs[stat.name].add((now, stat.available))
+ self._bufs = {}
+ for fs_state in get_filesystem_fill_states():
+ self._internal_state_buffer(fs_state)
+
+ def _internal_state_buffer(self, fs_state):
+ """ update internal stats buffer, returns all estims
+
+ internal helper called from __init__ and run
+ """
+ buf = None
+ try:
+ buf = self._bufs[fs_state.name]
+ except KeyError:
+ # new file system -- need to create a new buffer
+ buf = LogarithmicBuffer(5)
+ self._bufs[fs_state.name] = buf
+
+ buf.add((dt.now(), float(fs_state.available)))
+ return buf.get_all()
def run(self):
while True:
sleep(self.interval)
- stats = get_disc_stats()
- now = dt.now()
- for stat in stats:
- buf = None
- try:
- buf = bufs[stat.name]
- except KeyError:
- # new file system -- need to create a new buffer
- buf = LogarithmicBuffer(5)
-
- buf.add((now, float(stat.available)))
- fill_states = buf.get_all()
+ fs_states = get_filesystem_fill_states()
+ for fs_state in fs_states:
+ fill_states = self._internal_state_buffer(fs_state)
# estimate time until full (i.e. when available == 0)
times_until_empty = \