diff --git a/setup/start.sh b/setup/start.sh index 81059eb10..0e35ea271 100755 --- a/setup/start.sh +++ b/setup/start.sh @@ -170,7 +170,7 @@ cp "${script_dir}/start/sudoers" /etc/sudoers.d/${USER} echo "==> Configuring collectd" rm -rf /etc/collectd ln -sfF "${PLATFORM_DATA_DIR}/collectd" /etc/collectd -cp "${script_dir}/start/collectd.conf" "${PLATFORM_DATA_DIR}/collectd/collectd.conf" +cp "${script_dir}/start/collectd/collectd.conf" "${PLATFORM_DATA_DIR}/collectd/collectd.conf" systemctl restart collectd echo "==> Adding motd message for admins" diff --git a/setup/start/collectd.conf b/setup/start/collectd/collectd.conf similarity index 95% rename from setup/start/collectd.conf rename to setup/start/collectd/collectd.conf index 91b32e6b9..4990bc5ce 100644 --- a/setup/start/collectd.conf +++ b/setup/start/collectd/collectd.conf @@ -89,7 +89,7 @@ LoadPlugin cpu #LoadPlugin curl_json #LoadPlugin curl_xml #LoadPlugin dbi -LoadPlugin df +#LoadPlugin df #LoadPlugin disk #LoadPlugin dns #LoadPlugin email @@ -138,9 +138,9 @@ LoadPlugin nginx #LoadPlugin powerdns #LoadPlugin processes #LoadPlugin protocols -# -# Globals true -# + + Globals true + #LoadPlugin rrdcached #LoadPlugin rrdtool #LoadPlugin sensors @@ -192,17 +192,6 @@ LoadPlugin write_graphite - - FSType "ext4" - MountPoint "/" - - ReportByDevice true - IgnoreSelected false - - ValuesAbsolute true - ValuesPercentage true - - Interface "eth0" IgnoreSelected false @@ -244,6 +233,17 @@ LoadPlugin write_graphite + + # https://blog.dbrgn.ch/2017/3/10/write-a-collectd-python-plugin/ + ModulePath "/home/yellowtent/box/setup/start/collectd/" + LogTraces false # enable this to get traces in /var/log/collectd.log + Interactive false + + Import "df" + # + # + + Host "localhost" diff --git a/setup/start/collectd/df.py b/setup/start/collectd/df.py new file mode 100644 index 000000000..023bdf50c --- /dev/null +++ b/setup/start/collectd/df.py @@ -0,0 +1,36 @@ +import collectd,os + +# https://blog.dbrgn.ch/2017/3/10/write-a-collectd-python-plugin/ + +disks = [] + +def init(): + global disks + lines = [s.split() for s in os.popen("df --type=ext4 --output=source,target,size,used,avail").read().splitlines()] + disks = lines[1:] # strip header + collectd.info('custom df plugin initialized with %s' % disks) + +def read(): + for d in disks: + device = d[0] + if 'devicemapper' in d[1] or not device.startswith('/dev/'): continue + instance = device[len('/dev/'):].replace('/', '-') + + try: + st = os.statvfs(d[1]) # handle disk removal + except: + continue + + val = collectd.Values(type='df_complex', plugin='df', plugin_instance=instance) + + free = st.f_bavail * st.f_frsize # bavail is for non-root user. bfree is total + val.dispatch(values=[free], type_instance='free') + + reserved = (st.f_bfree - st.f_bavail) * st.f_frsize # root took these + val.dispatch(values=[reserved], type_instance='reserved') + + used = (st.f_blocks - st.f_bfree) * st.f_frsize + val.dispatch(values=[used], type_instance='used') + +collectd.register_init(init) +collectd.register_read(read) diff --git a/webadmin/src/views/graphs.js b/webadmin/src/views/graphs.js index 590c8e150..8bd198298 100644 --- a/webadmin/src/views/graphs.js +++ b/webadmin/src/views/graphs.js @@ -32,6 +32,7 @@ angular.module('Application').controller('GraphsController', ['$scope', '$locati } function renderDisk(type, free, reserved, used) { + // this will mismatch df output since df -H is SI units (1000) $scope.diskUsage[type] = { used: bytesToGigaBytes(used.datapoints[0][0] + reserved.datapoints[0][0]), free: bytesToGigaBytes(free.datapoints[0][0]),