From 48bc50ff2d95c8b6897050035852ffd72d94c904 Mon Sep 17 00:00:00 2001 From: Sylvain Boureliou Date: Tue, 11 Dec 2012 10:27:08 +0100 Subject: [PATCH] Add check website sh script --- .gitignore | 5 ++- src/supervision/website/TODO.txt | 23 ++++++++++ src/supervision/website/config.py.sample | 18 ++++++++ src/supervision/website/static/css/README.txt | 1 - .../website/static/images/README.txt | 1 - src/supervision/website/static/js/README.txt | 1 - .../utils/check_host_generate_script.py | 19 +++++++++ .../website/utils/check_hosts.sh.tmpl | 42 +++++++++++++++++++ 8 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 src/supervision/website/TODO.txt create mode 100644 src/supervision/website/config.py.sample delete mode 100644 src/supervision/website/static/css/README.txt delete mode 100644 src/supervision/website/static/images/README.txt delete mode 100644 src/supervision/website/static/js/README.txt create mode 100644 src/supervision/website/utils/check_host_generate_script.py create mode 100644 src/supervision/website/utils/check_hosts.sh.tmpl diff --git a/.gitignore b/.gitignore index bffc6c1..4141896 100644 --- a/.gitignore +++ b/.gitignore @@ -36,4 +36,7 @@ REAME.html htmlcov man .project -.pydevproject \ No newline at end of file +.pydevproject +src/supervision/website/config.py +src/supervision/website/reports +src/supervision/website/utils/check_hosts.sh diff --git a/src/supervision/website/TODO.txt b/src/supervision/website/TODO.txt new file mode 100644 index 0000000..db9c8dc --- /dev/null +++ b/src/supervision/website/TODO.txt @@ -0,0 +1,23 @@ + +a) Un fichier de configuration avec les HOSTS + +b) Un script qui génère le script SH avec HOSTS et TIMEOUT only (utiliser la syntaxe python simple avec %(var_name)s), + monitoring du temps, wget status, archive anciens fichier pour étudier le changement de status + wget http://www.asilax.fr --no-check-certificate --timeout=5 -S -q -O - 2>&1 | awk '/^ HTTP/{print $2}' + http://www.sectorfej.net/2012/03/24/simple-web-server-availability-monitoring-with-cron-bash-and-wget/ + + TODO : sauvegarde ancien status si nécessaire + écrire nouveau status avec "$HOST;$RESPONSE;$START;$END" + +c) Un script python qui étudie les fichiers de log : + - vérifie le code de retour + - détermine le status (cf lien bash) avec le SLOW_THRESHOLD + - génère un rapport texte, un rapport HTML, envoi des mails + +d) Documentation du README + +e) Post du rapport dans un Plone, via une configuration dans le fichier de configuration + +f) Support i18n pour les rapports, via une entrée dans le fichier de configuration + +g) Tests ? \ No newline at end of file diff --git a/src/supervision/website/config.py.sample b/src/supervision/website/config.py.sample new file mode 100644 index 0000000..a2fb6f8 --- /dev/null +++ b/src/supervision/website/config.py.sample @@ -0,0 +1,18 @@ +# List of websites to check +WEBSITE_URIS = [ + 'http://www.yahoo.com', + 'https://www.google.com', + 'http://www.france.fr/nous-contacter' +] + +# Requests timeout in second (used by wget calls) +TIMEOUT = 10 + +# If response time, in seconds, is greater than SLOW_THRESHOLD, then the website status will be SLOW +SLOW_THRESHOLD = 7 + +# Accepted HTTP status codes +OK_STATUSES = ['200'] + +# Reports folder, where status files and reports will be added +REPORTS_PATH = "/tmp/reports" \ No newline at end of file diff --git a/src/supervision/website/static/css/README.txt b/src/supervision/website/static/css/README.txt deleted file mode 100644 index b52dc79..0000000 --- a/src/supervision/website/static/css/README.txt +++ /dev/null @@ -1 +0,0 @@ -Project specific CSS files. \ No newline at end of file diff --git a/src/supervision/website/static/images/README.txt b/src/supervision/website/static/images/README.txt deleted file mode 100644 index 49f6c1c..0000000 --- a/src/supervision/website/static/images/README.txt +++ /dev/null @@ -1 +0,0 @@ -Project specific images files. \ No newline at end of file diff --git a/src/supervision/website/static/js/README.txt b/src/supervision/website/static/js/README.txt deleted file mode 100644 index daf8c65..0000000 --- a/src/supervision/website/static/js/README.txt +++ /dev/null @@ -1 +0,0 @@ -Project specific JavaScript files. \ No newline at end of file diff --git a/src/supervision/website/utils/check_host_generate_script.py b/src/supervision/website/utils/check_host_generate_script.py new file mode 100644 index 0000000..6a589f2 --- /dev/null +++ b/src/supervision/website/utils/check_host_generate_script.py @@ -0,0 +1,19 @@ +import sys +from supervision.website.config import * + +def generate_sh_script(): + # Generate the sh script using our custom configuration + + with open('check_hosts.sh.tmpl', 'r') as f: + script = f.read() + + script = script.replace('TO_BE_REPLACE_TIMEOUT', str(TIMEOUT)) + script = script.replace('TO_BE_REPLACE_HOSTS', '\n'.join((['"%s" \\' % uri for uri in WEBSITE_URIS]))) + script = script.replace('TO_BE_REPLACE_REPORTS_PATH', REPORTS_PATH) + + with open('check_hosts.sh', 'w') as f: + f.write(script) + + +if __name__ == '__main__': + generate_sh_script() \ No newline at end of file diff --git a/src/supervision/website/utils/check_hosts.sh.tmpl b/src/supervision/website/utils/check_hosts.sh.tmpl new file mode 100644 index 0000000..d189bb2 --- /dev/null +++ b/src/supervision/website/utils/check_hosts.sh.tmpl @@ -0,0 +1,42 @@ +#!/bin/bash +# Thanks to http://www.sectorfej.net/2012/03/24/simple-web-server-availability-monitoring-with-cron-bash-and-wget/ + +HOSTS=( \ + TO_BE_REPLACE_HOSTS + ) + +TIMEOUT=TO_BE_REPLACE_TIMEOUT + +REPORT_DIRECTORY="TO_BE_REPLACE_REPORTS_PATH" +CURRENT_STATUS_FILE="$REPORT_DIRECTORY/sitemonitor.current.status" +PREVIOUS_STATUS_FILE="$REPORT_DIRECTORY/sitemonitor.previous.status" + +# Create files structure if needed +mkdir -p $REPORT_DIRECTORY +if [ ! -e $CURRENT_STATUS_FILE ]; then + touch $CURRENT_STATUS_FILE +fi +if [ ! -e $PREVIOUS_STATUS_FILE ]; then + touch $PREVIOUS_STATUS_FILE +fi + +# Backup current status file and remove his content +mv -f $CURRENT_STATUS_FILE $PREVIOUS_STATUS_FILE +touch $CURRENT_STATUS_FILE + +for HOST in "${HOSTS[@]}" +do + START=$(date +%s) + RESPONSE=`wget $HOST --no-check-certificate --timeout=$TIMEOUT -S -q -O - 2>&1 | \ + awk '/^ HTTP/{print \$2}'` + END=$(date +%s) + #DIFF=$(( $END - $START )) + + if [ -z "$RESPONSE" ]; then + # Zero-length ("null") string variable + RESPONSE="0" + fi + + echo $HOST";"$RESPONSE";"$START";"$END >> $CURRENT_STATUS_FILE + +done \ No newline at end of file