Skip to content

Commit

Permalink
Merge pull request #1 from wildfoundry/disk-meta
Browse files Browse the repository at this point in the history
Poll disks once an hour
  • Loading branch information
stuartkmarsh authored Sep 1, 2016
2 parents 6b065f6 + 3ae9148 commit 18e0841
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
29 changes: 29 additions & 0 deletions dataplicity/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from . import jsonrpc
from . import settings
from . import tools
from .disk_tools import disk_usage
from .m2mmanager import M2MManager
from .portforward import PortForwardManager

Expand Down Expand Up @@ -47,6 +48,8 @@ def _init(self):
self.serial = tools.resolve_value(conf.get('device', 'serial'))
self.auth_token = tools.resolve_value(conf.get('device', 'auth'))
self.poll_rate_seconds = conf.get_float("daemon", "poll", 60.0)
self.disk_poll_rate_seconds = conf.get_integer("daemon", "disk_poll", 60 * 60)
self.next_disk_poll_time = time.time()

log.info('api=%s', self.rpc_url)
log.info('serial=%s', self.serial)
Expand Down Expand Up @@ -76,10 +79,36 @@ def run_forever(self):
self.close()
log.debug('goodbye')

def disk_poll(self):
now = time.time()

if now >= self.next_disk_poll_time:
self.next_disk_poll_time = now + self.disk_poll_rate_seconds
disk_space = disk_usage('/')

with self.remote.batch() as batch:
batch.call_with_id(
'authenticate_result',
'device.check_auth',
device_class='tuxtunnel',
serial=self.serial,
auth_token=self.auth_token
)
batch.call_with_id(
'set_disk_space_result',
'device.set_disk_space',
disk_capacity=disk_space.total,
disk_used=disk_space.used
)

def poll(self):
"""Called at regulat intervals."""
t = time.time()
log.debug('poll t=%.02fs', t)
try:
self.disk_poll()
except Exception as e:
log.error("disk poll failed %s", e)
self.sync()

def close(self):
Expand Down
66 changes: 66 additions & 0 deletions dataplicity/disk_tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from collections import namedtuple

import os
import sys


sdiskusage = namedtuple('sdiskusage', ['total', 'used', 'free', 'percent'])

def usage_percent(used, total, _round=None):
"""Calculate percentage usage of 'used' against 'total'."""
try:
ret = (used / total) * 100
except ZeroDivisionError:
ret = 0.0 if isinstance(used, float) or isinstance(total, float) else 0
if _round is not None:
return round(ret, _round)
else:
return ret


def disk_usage(path):
"""Return disk usage associated with path.
Note: UNIX usually reserves 5% disk space which is not accessible
by user. In this function "total" and "used" values reflect the
total and used disk space whereas "free" and "percent" represent
the "free" and "used percent" user disk space.
Code from https://github.com/giampaolo/psutil/blob/master/psutil/_psposix.py
"""
try:
st = os.statvfs(path)
except UnicodeEncodeError:
if isinstance(path, unicode):
# this is a bug with os.statvfs() and unicode on
# Python 2, see:
# - https://github.com/giampaolo/psutil/issues/416
# - http://bugs.python.org/issue18695
try:
path = path.encode(sys.getfilesystemencoding())
except UnicodeEncodeError:
pass
st = os.statvfs(path)
else:
raise

# Total space which is only available to root (unless changed
# at system level).
total = (st.f_blocks * st.f_frsize)
# Remaining free space usable by root.
avail_to_root = (st.f_bfree * st.f_frsize)
# Remaining free space usable by user.
avail_to_user = (st.f_bavail * st.f_frsize)
# Total space being used in general.
used = (total - avail_to_root)
# Total space which is available to user (same as 'total' but
# for the user).
total_user = used + avail_to_user
# User usage percent compared to the total amount of space
# the user can use. This number would be higher if compared
# to root's because the user has less space (usually -5%).
usage_percent_user = usage_percent(used, total_user, _round=1)

# NB: the percentage is -5% than what shown by df due to
# reserved blocks that we are currently not considering:
# https://github.com/giampaolo/psutil/issues/829#issuecomment-223750462
return sdiskusage(
total=total, used=used, free=avail_to_user, percent=usage_percent_user)
1 change: 1 addition & 0 deletions dataplicity/rpi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import unicode_literals


RPI_REVISIONS = {
'Beta': 'rpi_1b',
'0002': 'rpi_1b',
Expand Down

0 comments on commit 18e0841

Please sign in to comment.