#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-
# $HeadURL$
# $Id$

"""This script uses a log file created by atop and adds the content to a database.
The database has to exist.

memlog -c -wdatabase.db # create database
atop2memlog.py atop.log* database.db
"""

import os
import sys
from optparse import OptionParser
import datetime
import sqlite3
import subprocess


def parse_commandline():
	"Parse command line options"
	parser = OptionParser("Usage: %prog atopfile ... database.db", version="%prog $Id$")
	options, args = parser.parse_args()
	if len(args) < 2: parser.error("The program takes at least two arguments (one or more atop.log file names and the database file name)")
	options.atop_filenames = args[0:-1]
	options.db_filename = args[-1]
	return options


def open_database(db_filename):
	if not os.path.exists(db_filename):
		raise Exception('The database file %s does not exist. Use memlog -c -w%s to create it.' % (db_filename, db_filename))
	conn = sqlite3.connect(db_filename) # Open database
	return conn



# Main program
# ------------
options = parse_commandline()
conn = open_database(options.db_filename) # Open database
c = conn.cursor()

cmdline = 'for f in %s; do atop -PPRM -r $f | cut -f4,5,7,8,11,12 -d" " | egrep -v "0$" | grep -v SEP; done | sort' % ' '.join(['"%s"' % f for f in options.atop_filenames])
atop_result = subprocess.Popen(cmdline, stdout=subprocess.PIPE, shell=True)

lastdate = datetime.datetime(1900, 1, 1)
for line in atop_result.stdout:
	# Parse date E.g. line = '2009/08/25 00:57:09 1304 (bash) 680 7076'
	date, time, procid, procname, vsize, rss = line.split(' ')
	date = datetime.datetime.strptime(date + " " + time, '%Y/%m/%d %H:%M:%S')
	procid = int(procid)
	procname = procname[1:-1]
	vsize = int(vsize) * 1024
	rss = int(rss) * 1024
	# Store data
	if date != lastdate:
		c.execute('insert into log_system (time) values (?)', (date,))
		lastdate = date
		lastrowid = c.lastrowid
	c.execute('insert into log_process (logid, pid, comm, vsize, rss) values (?, ?, ?, ?, ?)', (lastrowid, procid, procname, vsize, rss))
conn.commit()
conn.close()
		
exit(0)

