From: "André Erdmann" <dywi@mailerd.de>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/R_overlay:master commit in: roverlay/db/
Date: Tue, 13 Aug 2013 08:56:36 +0000 (UTC) [thread overview]
Message-ID: <1376383739.52fb4a9c2e71de6a02c75194c405037c33ba0a3c.dywi@gentoo> (raw)
commit: 52fb4a9c2e71de6a02c75194c405037c33ba0a3c
Author: André Erdmann <dywi <AT> mailerd <DOT> de>
AuthorDate: Tue Aug 13 08:45:08 2013 +0000
Commit: André Erdmann <dywi <AT> mailerd <DOT> de>
CommitDate: Tue Aug 13 08:48:59 2013 +0000
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/R_overlay.git;a=commit;h=52fb4a9c
roverlay/db/rrdtool: rudimentary db reading
Read stats from the database. Using "rrdtool dump" (-> xml dump) as that's
easier to extend (read/parser RRA etc.; compared to calling rrdtool multiple
times).
Additionally, this commit adds some read-only checks to the RRD database object.
---
roverlay/db/rrdtool.py | 106 +++++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 99 insertions(+), 7 deletions(-)
diff --git a/roverlay/db/rrdtool.py b/roverlay/db/rrdtool.py
index 6d450fe..e21f592 100644
--- a/roverlay/db/rrdtool.py
+++ b/roverlay/db/rrdtool.py
@@ -12,11 +12,12 @@ import logging
import os
import time
+import xml.etree.ElementTree
+
import roverlay.tools.runcmd
-from roverlay.tools.runcmd import run_command
+from roverlay.tools.runcmd import run_command, run_command_get_output
import roverlay.util
-
import roverlay.util.objects
class RRDVariable ( object ):
@@ -109,23 +110,65 @@ class RRD ( object ):
LOGGER = logging.getLogger ( 'RRD' )
- def __init__ ( self, filepath ):
+ def __init__ ( self, filepath, readonly=False ):
+ super ( RRD, self ).__init__()
+ self.readonly = bool ( readonly )
self.filepath = filepath
self._commit_buffer = []
self._dbfile_exists = False
self.logger = self.__class__.LOGGER
self.INIT_TIME = self.time_now() - 10
+ self.cache = None
# --- end of __init__ (...) ---
def time_now ( self ):
return int ( time.time() )
# --- end of time_now (...) ---
- def _call_rrdtool ( self, args, return_success=True ):
- return run_command (
- self.RRDTOOL_CMDV_HEAD + args, None, self.logger,
- return_success=return_success
+ def get_xml_dump ( self ):
+ return self._get_rrdtool_output ( "dump" )
+ # --- end of get_xml_dump (...) ---
+
+ def _get_rrdtool_output ( self, command, *args ):
+ retcode, output = self._call_rrdtool_command (
+ command, args, return_success=True, get_output=True
)
+ if retcode == os.EX_OK:
+ return output[0]
+ else:
+ return None
+ # --- end of _get_rrdtool_output (...) ---
+
+ def _call_rrdtool_command (
+ self, command, args, return_success=True, get_output=False
+ ):
+ """Creates an arg tuple ( command, <stats db file>, *args ) and
+ calls _call_rrdtool() afterwards.
+ """
+ return self._call_rrdtool (
+ ( command, self.filepath, ) + args,
+ return_success=return_success, get_output=get_output
+ )
+ # --- end of _call_rrdtool_command (...) ---
+
+ def _call_rrdtool ( self, args, return_success=True, get_output=False ):
+ if get_output:
+ cmd_call, output = run_command_get_output (
+ self.RRDTOOL_CMDV_HEAD + args, None
+ )
+ if output[1]:
+ logger = self.logger.getChild ( 'rrdtool_call' )
+ for line in output[1]:
+ logger.warning ( line )
+ return (
+ cmd_call.returncode if return_success else cmd_call, output
+ )
+
+ else:
+ return run_command (
+ self.RRDTOOL_CMDV_HEAD + args, None, self.logger,
+ return_success=return_success
+ )
# --- end of _call_rrdtool (...) ---
@roverlay.util.objects.abstractmethod
@@ -133,12 +176,39 @@ class RRD ( object ):
pass
# --- end of _do_create (...) ---
+ def check_readonly ( self,
+ raise_exception=True, error_msg=None, error_msg_append=True
+ ):
+ """Verifies that database writing is allowed.
+
+ Returns True if writing is allowed, else False.
+
+ Raises an Exception if the database is readonly and raise_exception
+ evaluates to True.
+ """
+ if self.readonly:
+ if raise_exception:
+ msg = error_msg
+ if error_msg_append:
+ if msg:
+ msg += " - database is read-only"
+ else:
+ msg = "database is read-only"
+
+ raise Exception ( msg )
+ else:
+ return False
+ else:
+ return True
+ # --- end of check_readonly (...) ---
+
def create_if_missing ( self ):
if self._dbfile_exists or not os.access ( self.filepath, os.F_OK ):
return self.create()
# --- end of create_if_missing (...) ---
def create ( self ):
+ self.check_readonly()
roverlay.util.dodir_for_file ( self.filepath )
self._do_create ( self.filepath )
if os.access ( self.filepath, os.F_OK ):
@@ -165,6 +235,7 @@ class RRD ( object ):
# --- end of clear (...) ---
def commit ( self ):
+ self.check_readonly()
if self._commit_buffer:
self.create_if_missing()
self._call_rrdtool (
@@ -173,4 +244,25 @@ class RRD ( object ):
self.clear()
# --- end of commit (...) ---
+ def make_cache ( self, mask=-1, clear_cache=False ):
+ if clear_cache or not self.cache:
+ self.cache = dict()
+
+ xml_dump = self.get_xml_dump()
+ if not xml_dump:
+ return False
+
+ eroot = xml.etree.ElementTree.fromstring ( '\n'.join ( xml_dump ) )
+
+ self.cache ['lastupdate'] = eroot.find ( "./lastupdate" ).text.strip()
+ self.cache ['values'] = {
+ node.find ( "name" ).text.strip():
+ node.find ( "last_ds" ).text.strip()
+ for node in eroot.findall ( "./ds" )
+ }
+
+
+ return True
+ # --- end of make_cache (...) ---
+
# --- end of RRD ---
next reply other threads:[~2013-08-13 8:56 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-08-13 8:56 André Erdmann [this message]
-- strict thread matches above, loose matches on Subject: below --
2016-07-07 4:19 [gentoo-commits] proj/R_overlay:master commit in: roverlay/db/ Benda XU
2014-07-16 15:14 André Erdmann
2013-09-05 14:43 André Erdmann
2013-09-05 14:43 André Erdmann
2013-09-05 14:43 André Erdmann
2013-09-05 10:24 André Erdmann
2013-09-05 9:25 André Erdmann
2013-09-05 9:25 André Erdmann
2013-09-05 9:25 André Erdmann
2013-09-03 15:37 André Erdmann
2013-09-03 13:15 André Erdmann
2013-09-03 12:21 André Erdmann
2013-09-03 12:21 André Erdmann
2013-09-02 8:44 André Erdmann
2013-08-30 15:25 André Erdmann
2013-08-30 15:23 André Erdmann
2013-08-30 14:49 André Erdmann
2013-08-30 14:49 André Erdmann
2013-08-22 9:01 André Erdmann
2013-08-16 14:26 André Erdmann
2013-08-16 12:42 André Erdmann
2013-08-15 9:18 André Erdmann
2013-08-14 14:56 André Erdmann
2013-08-14 14:56 André Erdmann
2013-07-30 18:40 André Erdmann
2013-07-10 15:10 [gentoo-commits] proj/R_overlay:gsoc13/next " André Erdmann
2013-07-10 16:16 ` [gentoo-commits] proj/R_overlay:master " André Erdmann
2013-06-22 15:14 [gentoo-commits] proj/R_overlay:gsoc13/next " André Erdmann
2013-06-22 15:24 ` [gentoo-commits] proj/R_overlay:master " André Erdmann
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1376383739.52fb4a9c2e71de6a02c75194c405037c33ba0a3c.dywi@gentoo \
--to=dywi@mailerd.de \
--cc=gentoo-commits@lists.gentoo.org \
--cc=gentoo-dev@lists.gentoo.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox