Skip to content

Commit

Permalink
Add check website sh script
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvainb committed Dec 11, 2012
1 parent 1259688 commit 48bc50f
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 4 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@ REAME.html
htmlcov
man
.project
.pydevproject
.pydevproject
src/supervision/website/config.py
src/supervision/website/reports
src/supervision/website/utils/check_hosts.sh
23 changes: 23 additions & 0 deletions src/supervision/website/TODO.txt
Original file line number Diff line number Diff line change
@@ -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 ?
18 changes: 18 additions & 0 deletions src/supervision/website/config.py.sample
Original file line number Diff line number Diff line change
@@ -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"
1 change: 0 additions & 1 deletion src/supervision/website/static/css/README.txt

This file was deleted.

1 change: 0 additions & 1 deletion src/supervision/website/static/images/README.txt

This file was deleted.

1 change: 0 additions & 1 deletion src/supervision/website/static/js/README.txt

This file was deleted.

19 changes: 19 additions & 0 deletions src/supervision/website/utils/check_host_generate_script.py
Original file line number Diff line number Diff line change
@@ -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()
42 changes: 42 additions & 0 deletions src/supervision/website/utils/check_hosts.sh.tmpl
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 48bc50f

Please sign in to comment.