Commit 77f49bd2 authored by Alain Takoudjou's avatar Alain Takoudjou

monitor: prevent to run collect when another instance is running

parent c2e0df76
......@@ -45,6 +45,8 @@ def parseArguments():
parser = argparse.ArgumentParser()
parser.add_argument('--output_folder',
help='Path of the folder where output files should be written.')
parser.add_argument('--pid_file',
help='Path where should be written the pid of process.')
parser.add_argument('--partition_id',
help='ID of the computer partition to collect data from.')
parser.add_argument('--collector_db',
......@@ -291,6 +293,20 @@ def main():
if not os.path.exists(parser.output_folder) and os.path.isdir(parser.output_folder):
raise Exception("Invalid ouput folder: %s" % parser.output_folder)
if parser.pid_file:
# Check that this process is not running
if os.path.exists(parser.pid_file):
with open(parser.pid_file, "r") as pidfile:
try:
pid = int(pidfile.read(6))
except ValueError:
pid = None
if pid and os.path.exists("/proc/" + str(pid)):
print("A process is already running with pid " + str(pid))
exit(1)
with open(parser.pid_file, "w") as pidfile:
pidfile.write('%s' % os.getpid())
# Consumption global status
process_file = os.path.join(parser.output_folder, 'monitor_resource_process.data.json')
mem_file = os.path.join(parser.output_folder, 'monitor_resource_memory.data.json')
......@@ -354,3 +370,6 @@ def main():
if resource_process_status_list:
with open(resource_file, 'w') as rf:
rf.write(json.dumps(resource_process_status_list))
if os.path.exists(parser.pid_file):
os.unlink(parser.pid_file)
......@@ -522,8 +522,11 @@ class Monitoring(object):
if self.nice_command:
# run monitor collect with low priority
command += '%s ' % self.nice_command
command += "%s --output_folder %s --collector_db %s" % (
self.collect_script, self.data_folder, self.collector_db)
command += "%s --output_folder %s --collector_db %s --pid_file %s" % (
self.collect_script,
self.data_folder,
self.collector_db,
os.path.join(self.service_pid_folder, "monitor-collect.pid"))
self.addCronEntry('monitor_collect', '* * * * *', command)
# Write an empty file when monitor bootstrap went until the end
......
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