From 733b5eb7535bf456a16ff6622a1bb8601917a743 Mon Sep 17 00:00:00 2001 From: Jan Kaluza Date: Wed, 29 Feb 2012 11:17:54 +0100 Subject: [PATCH] Added storage class for persistent data storage --- tuned/plugins/plugin_sysctl.py | 6 ++++ tuned/utils/storage.py | 57 ++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 tuned/utils/storage.py diff --git a/tuned/plugins/plugin_sysctl.py b/tuned/plugins/plugin_sysctl.py index a9943e1..e6e4c8b 100644 --- a/tuned/plugins/plugin_sysctl.py +++ b/tuned/plugins/plugin_sysctl.py @@ -1,6 +1,7 @@ import tuned.plugins import tuned.logs import tuned.monitors +import tuned.utils.storage import os import struct import glob @@ -53,6 +54,11 @@ class SysctlPlugin(tuned.plugins.Plugin): self._sysctl_original[k] = v self._exec_sysctl(key + "=" + value, True) + + storage = tuned.utils.storage.Storage.get_instance() + storage.data = {"sysctl" : self._sysctl_original} + storage.save() + return True def _revert_sysctl(self): diff --git a/tuned/utils/storage.py b/tuned/utils/storage.py new file mode 100644 index 0000000..3d0d86d --- /dev/null +++ b/tuned/utils/storage.py @@ -0,0 +1,57 @@ +# Copyright (C) 2008-2012 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# + +import tuned.patterns +import tuned.logs +import tuned.utils +import pickle + +log = tuned.logs.get() + +DEFAULT_STORAGE_FILE = "./save.pickle" + +class Storage(tuned.patterns.Singleton): + def __init__(self): + super(self.__class__, self).__init__() + self._data = {} + self.load() + + @property + def data(self): + return self._data + + @data.setter + def data(self, data): + self._data.update(data) + + def save(self): + try: + with open(DEFAULT_STORAGE_FILE, "w") as f: + pickle.dump(self._data, f) + except (OSError,IOError) as e: + log.error("Error saving storage file %s: %s" % (DEFAULT_STORAGE_FILE, e)) + + def load(self): + try: + with open(DEFAULT_STORAGE_FILE, "r") as f: + self._data = pickle.load(f) + except (OSError,IOError) as e: + log.error("Error loading storage file %s: %s" % (DEFAULT_STORAGE_FILE, e)) + self._data = {} + except EOFError: + self._data = {} +