Commit 848a10f0 authored by Marco Mariani's avatar Marco Mariani

Merge branch 'slapmonitor'

parents fd078caf 8ab1ea86
...@@ -65,6 +65,7 @@ setup(name=name, ...@@ -65,6 +65,7 @@ setup(name=name,
'shacache = slapos.shacache:main', 'shacache = slapos.shacache:main',
'slapbuilder = slapos.builder:main', 'slapbuilder = slapos.builder:main',
'slapmonitor = slapos.monitor:run_slapmonitor', 'slapmonitor = slapos.monitor:run_slapmonitor',
'slapmonitor-xml = slapos.monitor:run_slapmonitor_xml',
'slapreport = slapos.monitor:run_slapreport', 'slapreport = slapos.monitor:run_slapreport',
'slaprunner = slapos.runner:run', 'slaprunner = slapos.runner:run',
'killpidfromfile = slapos.systool:killpidfromfile', 'killpidfromfile = slapos.systool:killpidfromfile',
......
...@@ -25,20 +25,28 @@ ...@@ -25,20 +25,28 @@
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# #
############################################################################## ##############################################################################
import datetime import datetime
import os
import time
from time import strftime
import logging import logging
import optparse
import os
import sys import sys
import time
from lxml import etree as ElementTree from lxml import etree as ElementTree
import psutil
from optparse import OptionParser
import sqlite3.dbapi2 as sqlite3 import sqlite3.dbapi2 as sqlite3
import psutil
#define global variable for log file #define global variable for log file
log_file = False log_file = False
def read_pid(path):
with open(path, 'r') as fin:
# pid file might contain other stuff we don't care about (ie. postgres)
pid = fin.readline()
return int(pid)
class MonitoringTool(object): class MonitoringTool(object):
"""Provide functions to monitor CPU and Memory""" """Provide functions to monitor CPU and Memory"""
def __init__(self): def __init__(self):
...@@ -196,7 +204,7 @@ def parse_opt(): ...@@ -196,7 +204,7 @@ def parse_opt():
usage="""usage: slapmonitor [options] PID_FILE_PATH DATABASE_PATH usage="""usage: slapmonitor [options] PID_FILE_PATH DATABASE_PATH
Usage: slapreport [options] LOG_PATH DATABASE_PATH LOGBOX_IP LOGBOX_PORT LOGBOX_LOGIN LOGBOX_PASSWORD""" Usage: slapreport [options] LOG_PATH DATABASE_PATH LOGBOX_IP LOGBOX_PORT LOGBOX_LOGIN LOGBOX_PASSWORD"""
parser = OptionParser(usage=usage) parser = optparse.OptionParser(usage=usage)
parser.add_option('-t', '--update_time',type=int, dest='update_time', help='Specify the interval'\ parser.add_option('-t', '--update_time',type=int, dest='update_time', help='Specify the interval'\
'(in seconds) to check resources consumption [default 30 seconds]', default=3) '(in seconds) to check resources consumption [default 30 seconds]', default=3)
parser.add_option('-l', '--log_file', dest='path_log_file',help='Specify the logfile destination path', parser.add_option('-l', '--log_file', dest='path_log_file',help='Specify the logfile destination path',
...@@ -235,7 +243,7 @@ class SlapMonitor(object): ...@@ -235,7 +243,7 @@ class SlapMonitor(object):
except IOError: except IOError:
if log_file: if log_file:
logging.info("ERROR : process with pid : %s watched by slap monitor exited too quickly at %s" 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"))) % (self.proc.pid, time.strftime("%Y-%m-%d at %H:%m")))
sys.exit(1) sys.exit(1)
if log_file: if log_file:
logging.info("EXIT 0: Process terminated normally!") logging.info("EXIT 0: Process terminated normally!")
...@@ -285,7 +293,7 @@ class SlapReport(object): ...@@ -285,7 +293,7 @@ class SlapReport(object):
except Exception: except Exception:
if log_file: if log_file:
logging.info("ERROR : Unable to connect to % at %s" logging.info("ERROR : Unable to connect to % at %s"
% (self.ssh_parameters['ip'], strftime("%Y-%m-%d at %H:%m"))) % (self.ssh_parameters['ip'], time.strftime("%Y-%m-%d at %H:%m")))
#sys.exit(1) #sys.exit(1)
cursor.close() cursor.close()
...@@ -307,7 +315,7 @@ class SlapReport(object): ...@@ -307,7 +315,7 @@ class SlapReport(object):
except IOError: except IOError:
if log_file: if log_file:
logging.info("ERROR : process with pid : %s watched by slap monitor exited too quickly at %s" 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"))) % (self.proc.pid, time.strftime("%Y-%m-%d at %H:%m")))
sys.exit(1) sys.exit(1)
if log_file: if log_file:
logging.info("EXIT 0: Process terminated normally!") logging.info("EXIT 0: Process terminated normally!")
...@@ -325,12 +333,29 @@ def run_slapmonitor(): ...@@ -325,12 +333,29 @@ def run_slapmonitor():
global log_file global log_file
log_file = True log_file = True
fed = open(args[0], 'r') proc = psutil.Process(read_pid(args[0]))
pid_read = fed.read() # XXX FIXME: THE PID IS ONLY READ ONCE.
fed.close() # process death and pid reuse are not detected.
proc = psutil.Process(int(pid_read))
SlapMonitor(proc, opts.update_time, args[1]) SlapMonitor(proc, opts.update_time, args[1])
def run_slapmonitor_xml():
#This function require the database path and XML path
parser = parse_opt()
opts, args = parser.parse_args()
if len(args) != 2:
parser.error("Incorrect number of arguments, 2 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
get_xml_hand = GenerateXML(ElementTree, args[0], args[1])
get_xml_hand.dump_xml()
def run_slapreport_(): def run_slapreport_():
#This function require the xml_path and database_path #This function require the xml_path and database_path
parser = parse_opt() parser = parse_opt()
...@@ -371,15 +396,12 @@ def run_slapreport(): ...@@ -371,15 +396,12 @@ def run_slapreport():
ssh_parameters['passwd']=args[6] ssh_parameters['passwd']=args[6]
try: try:
fed = open(pid_file_path, 'r') proc = psutil.Process(read_pid(pid_file_path))
pid_read = fed.read()
fed.close()
proc = psutil.Process(int(pid_read))
SlapReport(proc, opts.update_time, args[1], args[2], ssh_parameters) SlapReport(proc, opts.update_time, args[1], args[2], ssh_parameters)
except IOError: except IOError:
if log_file: if log_file:
logging.info("ERROR : process with pid : %s watched by slap monitor exited too quickly at %s" logging.info("ERROR : process with pid : %s watched by slap monitor exited too quickly at %s"
% (proc.pid, strftime("%Y-%m-%d at %H:%m"))) % (proc.pid, time.strftime("%Y-%m-%d at %H:%m")))
sys.exit(1) sys.exit(1)
if log_file: if log_file:
......
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