Commit 8f9101c2 authored by Cédric Le Ninivin's avatar Cédric Le Ninivin

monitor: RSS inform of status change and store all data in a sqlite database

monitor: status value are stored in sqlite3 database

monitor: now store all ran scripts
parent be34e4c6
#!${dash-output:dash}
STATUS={{ directory['monitor-result-bool'] }}
STATUS_DB={{ monitor_parameters['db-path'] }}
RSS_FILE={{ monitor_parameters['rss-path'] }}
PYTHON=${buildout:directory}/bin/${extra-eggs:interpreter}
STATUS2RSS=${rss-bin:location}/${rss-bin:filename}
NAME=`basename $STATUS`
cat $STATUS/* | $PYTHON $STATUS2RSS "Monitoring RSS feed" "{{ monitor_parameters['url'] }}/{{ monitor_parameters['index-filename'] }}" > $RSS_FILE
$PYTHON $STATUS2RSS "Monitoring RSS feed" "{{ monitor_parameters['url'] }}/{{ monitor_parameters['index-filename'] }}" $STATUS_DB > $RSS_FILE
......@@ -17,6 +17,7 @@ htaccess-file = $${monitor-directory:etc}/.htaccess-monitor
url = https://[$${slap-parameters:ipv6-random}]:$${:port}
index-filename = index.cgi
index-path = $${monitor-directory:www}/$${:index-filename}
db-path = $${monitor-directory:etc}/monitor.db
[monitor-directory]
recipe = slapos.cookbook:mkdirectory
......@@ -47,7 +48,7 @@ public-cgi = $${:cgi-bin}/public
monitor-custom-scripts = $${:etc}/monitor
monitor-result = $${:var}/monitor
monitor-result-bool = $${:monitor-result}/bool
private-directory = $${:srv}/monitor-private
[public-symlink]
......@@ -145,6 +146,7 @@ rendered = $${monitor-parameters:executable}
mode = 0744
context =
section directory monitor-directory
section monitor_parameter monitor-parameters
key monitoring_file_json monitor-parameters:json-path
raw python_executable ${buildout:executable}
......
......@@ -5,16 +5,21 @@ import json
import os
import subprocess
import sys
import sqlite3
import time
from optparse import OptionParser, make_option
FAILURE = "FAILURE"
SUCCESS = "SUCCESS"
db_path = "{{ monitor_parameter['db-path'] }}"
instance_path = "{{ directory['home'] }}"
monitor_dir = "{{ directory['monitor-custom-scripts'] }}"
pid_dir = "{{ directory['run'] }}"
promise_dir = "{{ directory['promise'] }}"
monitoring_file_json = "{{ monitoring_file_json }}"
monitoring_folder_bool = "{{ directory['monitor-result-bool'] }}"
option_list = [
make_option("-a", "--all", action="store_true", dest="all",
......@@ -30,6 +35,22 @@ option_list = [
]
def init_db():
db = sqlite3.connect(db_path)
c = db.cursor()
c.executescript("""
CREATE TABLE IF NOT EXISTS status (
timestamp INTEGER UNIQUE,
status VARCHAR(255));
CREATE TABLE IF NOT EXISTS individual_status (
timestamp INTEGER,
status VARCHAR(255),
element VARCHAR(255),
output TEXT);
""")
db.commit()
db.close()
def getListOfScripts(directory):
"""
Get the list of script inside of a directory (not recursive)
......@@ -98,22 +119,28 @@ def runScripts(directory):
def writeFiles(monitors):
timestamp = int(time.time())
date = datetime.datetime.now().ctime()
init_db()
db = sqlite3.connect(db_path)
fail = False
for i in monitors.values():
if i != "" :
for key, value in monitors.iteritems():
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.commit()
status = SUCCESS
if fail:
message = "FAILURE : something went wrong\n"
else:
message = "SUCCESS : everything is ok\n"
date = datetime.datetime.now().ctime()
status = FAILURE
db.execute("insert into status(timestamp, status) values (?, ?)", (timestamp, status))
db.commit()
db.close()
monitors['datetime'] = date
file_bool = os.path.join(monitoring_folder_bool, str(time.time()))
open(file_bool, "w+").write(date + "," + message)
open(monitoring_file_json, "w+").write(json.dumps(monitors))
if __name__ == "__main__":
def main():
parser = OptionParser(option_list=option_list)
monitors = {}
(options, args) = parser.parse_args()
......@@ -138,3 +165,6 @@ if __name__ == "__main__":
else:
exit(1)
if __name__ == "__main__":
main()
import datetime
import PyRSS2Gen
import sys
from email.utils import parsedate_tz, mktime_tz
import sqlite3
import time
import base64
# Based on http://thehelpfulhacker.net/2011/03/27/a-rss-feed-for-your-crontabs/
......@@ -9,27 +10,36 @@ import base64
# ### Defaults
TITLE = sys.argv[1]
LINK = sys.argv[2]
db_path = sys.argv[3]
DESCRIPTION = TITLE
SUCCESS = "SUCCESS"
FAILURE = "FAILURE"
items = []
status = ""
while 1:
try:
line = sys.stdin.readline()
except KeyboardInterrupt:
break
current_timestamp = int(time.time())
# We only build the RSS for the last ten days
period = 3600 * 24 * 10
db = sqlite3.connect(db_path)
rows = db.execute("select timestamp, status from status where timestamp>? order by timestamp", (current_timestamp - period,))
for row in rows:
line_timestamp, line_status = row
line_status = line_status.encode()
if not line:
break
if line_status == status:
continue
time, desc = line.split(',', 1)
status = line_status
event_time = datetime.datetime.fromtimestamp(line_timestamp).strftime('%Y-%m-%d %H:%M:%S')
rss_item = PyRSS2Gen.RSSItem(
title = desc,
description = "%s, %s" % (time, desc),
title = status,
description = "%s: %s" % (event_time, status),
link = LINK,
pubDate = datetime.datetime.fromtimestamp(mktime_tz(parsedate_tz(time))),
guid = PyRSS2Gen.Guid(base64.b64encode("%s, %s" % (time, desc)))
pubDate = event_time,
guid = PyRSS2Gen.Guid(base64.b64encode("%s, %s" % (event_time, status)))
)
items.append(rss_item)
......
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