def _cleanup_spool(self):
        """Periodically clean up old things in the spool dir.

        This shouldn't normally be required but if things get screwed
        up we don't want the directory to get so huge that it keeps
        things slow after nagios is handling results again.
        """

        # Note: It is entirely possible that the command to submit
        # this file is still in the writer queue, if that's the case
        # nagios will also log an error when it gets around to
        # reading from the queue.

        # Set the threshold to 5 minutes ago, if nagios hasn't been
        # able to keep up for the past 5 minutes we have problems.
        threshold = time.time() - 300
        count = 0
        for item in os.listdir(self.spool_dir):
            path = "%s/%s" % (self.spool_dir, item)
            try:
               info = os.stat(path)
            except:
                continue
            if info.st_mtime < threshold:
                try:
                    os.unlink(path)
                except __HOLE__, ex:
                    log.error("Failed to remove %s: %s" % (path, ex))
                else:
                    count += 1

        if count:
            log.warn("Removed %d stale nagios command files" % count)

        # Schedule the next cleanup to run from a thread in 1 minute
        reactor.callLater(60, reactor.callInThread, self._cleanup_spool)
dataset/ETHPy150Open marineam/nagcat/python/nagcat/nagios_api.py/NagiosCommander._cleanup_spool