#!/usr/bin/env python # -*- coding: utf-8 -*- # An example script for collectd's exec plugin, reading drive temperatures from UDisks2 # # Notes about the script: # # - there are other properties like SmartNumAttributesFailing or SmartNumBadSectors etc # (see http://udisks.freedesktop.org/docs/latest/gdbus-org.freedesktop.UDisks2.Drive.Ata.html) # which can easily be monitored too, but I've been only interested in temperatures # # - drives that have SMART disabled are simply skipped, as calling SmartSetEnabled() would # probably require admin privileges, authentication, or changing polkit rules # (see http://udisks.freedesktop.org/docs/latest/udisks-polkit-actions.html) # # - same with SmartUpdate(), so we're limited to default UDisks2 update interval, whatever it is # (seems pretty responsive on my system though) import dbus import re import time import os import sys host = os.environ.get('COLLECTD_HOSTNAME', 'localhost') interval = float(os.environ.get('COLLECTD_INTERVAL', '60')) bus = dbus.SystemBus() manager = dbus.Interface( bus.get_object('org.freedesktop.UDisks2', '/org/freedesktop/UDisks2'), 'org.freedesktop.DBus.ObjectManager') re_drive = re.compile('(?P.*?/drives/(?P.*))') while 1: objects = manager.GetManagedObjects() drives = [m.groupdict() for m in [re_drive.match(path) for path in objects.keys()] if m] for drive in drives: try: ata = objects[drive['path']]['org.freedesktop.UDisks2.Drive.Ata'] if ata['SmartSupported'] and ata['SmartEnabled'] \ and ata['SmartTemperature'] and ata['SmartUpdated']: print 'PUTVAL %s/exec-udisks/temperature-%s interval=%s N:%s' \ % (host, drive['id'], interval, ata['SmartTemperature'] - 273.15) except KeyError: pass sys.stdout.flush() time.sleep(interval)