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