Commit 829b4a61 authored by Julien Muchembled's avatar Julien Muchembled Committed by Alain Takoudjou

monitor: more code simplification

parent fb9db1c7
...@@ -53,7 +53,7 @@ mode = 0644 ...@@ -53,7 +53,7 @@ mode = 0644
recipe = hexagonit.recipe.download recipe = hexagonit.recipe.download
url = ${:_profile_base_location_}/${:filename} url = ${:_profile_base_location_}/${:filename}
download-only = true download-only = true
md5sum = 49587347fd82d93cca27715f787f8bec md5sum = 5b12e864f1762d7984f7d4863d0b795d
destination = ${buildout:parts-directory}/monitor-template-monitor-bin destination = ${buildout:parts-directory}/monitor-template-monitor-bin
filename = monitor.py.in filename = monitor.py.in
mode = 0644 mode = 0644
......
#!{{ python_executable }} #!{{ python_executable }}
import datetime
import json import json
import os import os
import subprocess import subprocess
...@@ -60,10 +59,8 @@ class Popen(subprocess.Popen): ...@@ -60,10 +59,8 @@ class Popen(subprocess.Popen):
return event.__killed return event.__killed
return killed return killed
def init_db(): def init_db(db):
db = sqlite3.connect(db_path) db.executescript("""
c = db.cursor()
c.executescript("""
CREATE TABLE IF NOT EXISTS status ( CREATE TABLE IF NOT EXISTS status (
timestamp INTEGER UNIQUE, timestamp INTEGER UNIQUE,
status VARCHAR(255)); status VARCHAR(255));
...@@ -73,8 +70,6 @@ CREATE TABLE IF NOT EXISTS individual_status ( ...@@ -73,8 +70,6 @@ CREATE TABLE IF NOT EXISTS individual_status (
element VARCHAR(255), element VARCHAR(255),
output TEXT); output TEXT);
""") """)
db.commit()
db.close()
def getListOfScripts(directory): def getListOfScripts(directory):
""" """
...@@ -136,25 +131,20 @@ def runScripts(directory): ...@@ -136,25 +131,20 @@ def runScripts(directory):
def writeFiles(monitors): def writeFiles(monitors):
timestamp = int(time.time()) timestamp = int(time.time())
date = datetime.datetime.now().ctime()
init_db()
db = sqlite3.connect(db_path) db = sqlite3.connect(db_path)
fail = False init_db(db)
status = SUCCESS
for key, value in monitors.iteritems(): for key, value in monitors.iteritems():
if value:
element_status = status = FAILURE
else:
element_status = SUCCESS element_status = SUCCESS
if value != "" :
fail = True
element_status = FAILURE
db.execute("insert into individual_status(timestamp, element, output, status) values (?, ?, ?, ?)", (timestamp, key, value, element_status)) db.execute("insert into individual_status(timestamp, element, output, status) values (?, ?, ?, ?)", (timestamp, key, value, element_status))
db.commit()
status = SUCCESS
if fail:
status = FAILURE
db.execute("insert into status(timestamp, status) values (?, ?)", (timestamp, status)) db.execute("insert into status(timestamp, status) values (?, ?)", (timestamp, status))
db.commit() db.commit()
db.close() db.close()
monitors['datetime'] = date monitors['datetime'] = time.ctime(timestamp)
open(monitoring_file_json, "w+").write(json.dumps(monitors)) json.dump(monitors, open(monitoring_file_json, "w+"))
def main(): def main():
parser = OptionParser(option_list=option_list) parser = OptionParser(option_list=option_list)
......
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