From 13cc9df657c01ce66b30e21fcbb5e1de9ea73d75 Mon Sep 17 00:00:00 2001 From: "Miguel A. Cortizo" Date: Fri, 28 Mar 2014 15:15:22 +0100 Subject: Added option for database dump --- README.rst | 1 + ngxtop/ngxtop.py | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) 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 filter in, records satisfied given expression are processed. -p , --pre-filter 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 filter in, records satisfied given expression are processed. -p , --pre-filter 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) -- cgit v1.2.3