Commit 18c82ba2 authored by Mohamadou Mbengue's avatar Mohamadou Mbengue

update script to support slapreport service

parent fcd87709
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2010 Vifib SARL and Contributors. All Rights Reserved.
......@@ -58,6 +59,7 @@ class GenerateXML(object):
self.element_tree = element_tree
self.path_database = path_database
self.path_xml = path_xml
#self.log_file = log_file
def dump_xml(self):
"""This func read data from database and through
......@@ -196,7 +198,7 @@ class GenerateXML(object):
def parse_opt():
usage="""usage: slapmonitor [options] PID_FILE_PATH DATABASE_PATH
Usage: slapreport [options] XML_PATH DATABASE_PATH """
Usage: slapreport [options] LOG_PATH DATABASE_PATH LOGBOX_IP LOGBOX_PORT LOGBOX_LOGIN LOGBOX_PASSWORD"""
parser = OptionParser(usage=usage)
parser.add_option('-t', '--update_time',type=int, dest='update_time', help='Specify the interval'\
......@@ -222,14 +224,14 @@ class SlapMonitor(object):
conn = sqlite3.connect(self.path_database)
cursor = conn.cursor()
cursor.execute("create table if not exists data (cpu real, cpu_time real, memory real, rss real," \
"date text, time text)")
"date text, time text, report integer NULL DEFAULT 0)")
try:
res_list = temporary_monitoring.get_cpu_and_memory_usage(self.proc)
date_catched = "-".join([str(res_list[4].year), str(res_list[4].month), str(res_list[4].day)])
time_catched = ":".join([str(res_list[4].hour), str(res_list[4].minute), str(res_list[4].second)])
res_list[4] = date_catched
res_list.append(time_catched)
cursor.execute("insert into data values (?,?,?,?,?,?)" , res_list)
cursor.execute("insert into data(cpu, cpu_time, memory, rss, date, time) values (?,?,?,?,?,?)" , res_list)
conn.commit()
cursor.close()
conn.close()
......@@ -243,6 +245,78 @@ class SlapMonitor(object):
logging.info("EXIT 0: Process terminated normally!")
sys.exit(0)
class SlapReport(object):
"""
reports usage of apache and mariadb logs to an existing log server
"""
def __init__(self, proc, update_time, consumption_log_path, database_path, ssh_parameters):
self.proc = proc
self.update_time = update_time
self.path_database = database_path
self.consumption_log_path = consumption_log_path
self.ssh_parameters = ssh_parameters
self.start_report()
def write_log(self):
"""This func read data from database and through
write_log_file write result on a log file"""
#write none reported log in the consumption log log file
fd = open(self.consumption_log_path, 'a')
conn = sqlite3.connect(self.path_database)
cursor = conn.cursor()
log_list = cursor.execute("SELECT * FROM data WHERE report=0 ORDER BY rowid ASC")
last_report_time = ""
for row in log_list:
cpu_consumption_info = "%s %s Instance %s CPU Consumption: CPU:%s CPU_TIME:%s\n" \
% (row[4],row[5], self.proc.name, row[0],row[1])
memory_consumption_info = "%s %s Instance %s Memory Consumption: %s\n" \
% (row[4], row[5], self.proc.name, row[2])
try:
cmd = "tail -n 2 %s | ssh %s:%s@%s -p %s " \
% (self.consumption_log_path, self.ssh_parameters['user'], \
self.ssh_parameters['passwd'], self.ssh_parameters['ip'], \
self.ssh_parameters['port'])
res = os.system(cmd)
if not res:
if last_report_time != row[5]:
fd.write("%s%s" % (cpu_consumption_info,memory_consumption_info))
last_report_time = "%s" % row[5]
cursor.execute("UPDATE data set report='1' WHERE time=?", (row[5],))
conn.commit()
except Exception, e:
if log_file:
logging.info("ERROR : Unable to connect to % at %s"
% (self.ssh_parameters['ip'], strftime("%Y-%m-%d at %H:%m")))
#sys.exit(1)
cursor.close()
conn.close()
def start_report(self):
"""
while the process is running, connect to the database
and report the non-reported line,
when line is reported put 1 to column report
"""
temporary_monitoring = MonitoringTool()
#check if the process is alive == None, instead zero value == proc is over
while self.proc.pid in psutil.get_pid_list():
try:
# send log to logbox
self.write_log()
time.sleep(self.update_time)
except IOError:
if log_file:
logging.info("ERROR : process with pid : %s watched by slap monitor exited too quickly at %s"
% (self.proc.pid, strftime("%Y-%m-%d at %H:%m")))
sys.exit(1)
if log_file:
logging.info("EXIT 0: Process terminated normally!")
sys.exit(0)
def run_slapmonitor():
#This function require the pid file and the database path
parser = parse_opt()
......@@ -261,8 +335,7 @@ def run_slapmonitor():
proc = psutil.Process(int(pid_read))
SlapMonitor(proc, opts.update_time, args[1])
def run_slapreport():
def run_slapreport_():
#This function require the xml_path and database_path
parser = parse_opt()
opts, args = parser.parse_args()
......@@ -277,3 +350,39 @@ def run_slapreport():
get_xml_hand = GenerateXML(ElementTree, args[1], args[0])
get_xml_hand.dump_xml()
def run_slapreport():
#This function require the consumption_log_path and database_path ssh_parameters
parser = parse_opt()
ssh_parameters ={}
opts, args = parser.parse_args()
if len(args) != 7:
parser.error("Incorrect number of arguments, 7 required but "+str(len(args))+" detected" )
if opts.path_log_file:
logging.basicConfig(filename=opts.path_log_file,level=logging.DEBUG)
global log_file
log_file = True
pid_file_path = "%s" % args[0]
#set ssh parameters
ssh_parameters['ip']=args[3]
ssh_parameters['port']=args[4]
ssh_parameters['user']=args[5]
ssh_parameters['passwd']=args[6]
try:
fed = open(pid_file_path, 'r')
pid_read = fed.read()
fed.close()
proc = psutil.Process(int(pid_read))
SlapReport(proc, opts.update_time, args[1], args[2], ssh_parameters)
except IOError:
if log_file:
logging.info("ERROR : process with pid : %s watched by slap monitor exited too quickly at %s"
% (self.proc.pid, strftime("%Y-%m-%d at %H:%m")))
sys.exit(1)
if log_file:
logging.info("EXIT 0: Process terminated normally!")
#sys.exit(0)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment