summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiguel A. Cortizo <macortizo@gmail.com>2014-03-28 15:15:22 +0100
committerMiguel A. Cortizo <macortizo@gmail.com>2014-03-28 15:15:22 +0100
commit13cc9df657c01ce66b30e21fcbb5e1de9ea73d75 (patch)
treefedd753d4b1a582946fbfc21030c2ce326791e7a
parent4137c7da57e74e624a865e357479e7ec0f699b23 (diff)
Added option for database dump
-rw-r--r--README.rst1
-rwxr-xr-xngxtop/ngxtop.py26
2 files changed, 27 insertions, 0 deletions
diff --git a/README.rst b/README.rst
index d1750b4..199c055 100644
--- a/README.rst
+++ b/README.rst
@@ -53,6 +53,7 @@ Usage
-i <filter-expression>, --filter <filter-expression> filter in, records satisfied given expression are processed.
-p <filter-expression>, --pre-filter <filter-expression> in-filter expression to check in pre-parsing phase.
-s, --from-stdin read lines from stdin.
+ -b, --db-dump dump database to disk when finished
Samples
-------
diff --git a/ngxtop/ngxtop.py b/ngxtop/ngxtop.py
index 12a84c3..679f44c 100755
--- a/ngxtop/ngxtop.py
+++ b/ngxtop/ngxtop.py
@@ -30,6 +30,7 @@ Options:
-i <filter-expression>, --filter <filter-expression> filter in, records satisfied given expression are processed.
-p <filter-expression>, --pre-filter <filter-expression> in-filter expression to check in pre-parsing phase.
-s, --from-stdin read lines from stdin.
+ -b, --db-dump dump database to disk
Examples:
All examples read nginx config file for access log location and format.
@@ -62,6 +63,7 @@ import sqlite3
import subprocess
import threading
import time
+from datetime import date
import sys
try:
@@ -111,6 +113,12 @@ DEFAULT_QUERIES = [
DEFAULT_FIELDS = set(['status_type', 'bytes_sent'])
+# =============================
+# Global variable for dbdump
+# =============================
+processor = None
+
+
# ====================
# Nginx utilities
# ====================
@@ -403,6 +411,7 @@ def build_reporter(processor, arguments):
def process(arguments):
+ global processor
access_log = arguments['--access-log']
log_format = arguments['--log-format']
if access_log is None or log_format is None:
@@ -437,6 +446,21 @@ def process(arguments):
logging.info('Processed %d lines in %.3f seconds, %.2f lines/sec.', total, duration, total / duration)
+# ================
+# Database dump
+# ================
+def dbdump():
+ """
+ *experimental* if requested, database is dumped to a file when script is interrupted from keyboard
+ Filename is composed from current date and process id
+ """
+ dbfile = "{}_{}.sql".format(date.today().strftime("%Y%m%d"), os.getpid())
+ logging.info("Database dump: %s", dbfile)
+ with open(dbfile, 'w') as f:
+ for line in processor.conn.iterdump():
+ f.write('%s\n' % line)
+
+
def main():
args = docopt(__doc__, version='xstat 0.1')
@@ -451,6 +475,8 @@ def main():
try:
process(args)
except KeyboardInterrupt:
+ if args['--db-dump']:
+ dbdump()
sys.exit(0)