Comment configurer le mode ‘live’ avec pywws

Introduction

There are two quite different modes of operation with pywws. Traditionally Hourly would be run at regular intervals (usually an hour) from cron. This is suitable for fairly static websites, but more frequent updates can be useful for sites such as Weather Underground (http://www.wunderground.com/). The newer LiveLog program runs continuously and can upload data every 48 seconds.

Mise en route

Avant tout, vous devez installer pywws et vous assurer qu’il reçoit bien les informations de votre station météo. Voir Comment démarrer avec pywws pour plus de détails.

Try running LiveLog from the command line, with a high level of verbosity so you can see what’s happening:

python -m pywws.LiveLog -vvv ~/weather/data

Within five minutes (assuming you have set a 5 minute logging interval) you should see a ‘live_data new ptr’ message, followed by fetching any new data from the weather station and processing it. Let LiveLog run for a minute or two longer, then kill the process by typing ‘<Ctrl>C’.

Configurer l’emplacement des fichiers

Ouvrez votre fichier weather.ini avec un éditeur de texte. Vous devriez avoir une section [paths] similaire à ce qui suit (où xxx est votre nom d’usager):

[paths]
work = /tmp/weather
templates = /home/xxx/weather/templates/
graph_templates = /home/xxx/weather/graph_templates/

Éditez pour correspondre à votre installation et à vos préférences. work est un dossier temporaire utilisé pour emmagasiner les fichiers intermédiaires, templates est le dossier où vous gardez vos fichiers de gabarit texte et graph_templates est le dossier où vous gardez vos fichiers de gabarit graphes. Ne pas utiliser les dossiers exemple de pywws pour celà, puisqu’ils seront écrasés lors de mise à jour de pywws.

Copy your text and graph templates to the appropriate directories. You may find some of the examples provided with pywws useful to get started. If you installed pywws with pip the examples should be in /usr/share/pywws or /usr/local/share/pywws or similar.

Configurer les tâches périodiques

Dans weather.ini vous devriez avoir une section [live] similaire à celle-ci:

[live]
services = []
twitter = []
plot = []
text = []

Cette section spécifie ce que devrait faire pywws à chaque fois qu’il reçoit une lecture de la station météo, ex. toutes les 48 secondes. Les entrées services sont des listes de services météo en ligne sur lesquels envoyer vos données météo, ex. ['underground']. Les entrées plot et text sont des listes de fichiers gabarits de graphe et de texte à téléverser sur votre site web, et l’entrée twitter est une liste de gabarits pour les messages à poster sur Twitter. Vous devriez probablement laisser toutes ces entrées vides, sauf pour services.

Si vous utilisez YoWindow (http://yowindow.com/) vous pouvez ajouter l’entrée à la section [live] pour spécifier votre fichier YoWindow, ex.:

[live]
yowindow = /home/jim/data/yowindow.xml
services = ['underground']
...

Si vous ne les avez pas déjà, créez quatre sections supplémentaires dans votre fichier weather.ini : [logged], [hourly], [12 hourly] et [daily]. Ces sections doivent avoir des entrées similaires à la section [live], et spécifiez ce qui doit être fait chaque fois qu’une donnée est enregistrée (5 à 30 minutes, dépendant de votre intervalle), chaque heure, deux fois par jour et chaque jour. Ajoutez les noms de vos fichiers de gabarit à l’entrée appropriée, par exemple:

[logged]
services = ['underground', 'metoffice']
twitter = []
plot = []
text = []

[hourly]
services = []
twitter = ['tweet.txt']
plot = ['7days.png.xml', '24hrs.png.xml', 'rose_24hrs.png.xml']
text = ['24hrs.txt', '6hrs.txt', '7days.txt']

[12 hourly]
services = []
twitter = []
plot = []
text = []

[daily]
services = []
twitter = ['forecast.txt']
plot = ['28days.png.xml']
text = ['allmonths.txt']

Using a utility script

The pywws installation includes a short script pywws-livelog.py that gets installed in /usr/bin or /usr/local/bin or similar. You should be able to use this script to run LiveLog:

pywws-livelog.py -v ~/weather/data

Exécuter en arrière-plan

In order to have LiveLog carry on running after you finish using your computer it needs to be run as a ‘background job’. On most Linux / UNIX systems you can do this by putting an ampersand (‘&’) at the end of the command line. For example:

pywws-livelog.py ~/weather/data &

However, it would be useful to know what went wrong if the program crashes for any reason. LiveLog can store its messages in a log file, specified with the -l option:

pywws-livelog.py -v -l ~/weather/data/pywws.log ~/weather/data &

Redémarrage automatique

There are various ways of configuring a Linux system to start a program when the machine boots up. Typically these involve putting a file in /etc/init.d/, which requires root privileges. A slightly harder problem is ensuring a program restarts if it crashes. My solution to both problems is to run the following script from cron, every hour.

#!/bin/sh

pidfile=/var/run/pywws.pid
datadir=/data/weather
logfile=$datadir/live_logger.log

# exit if process is running
[ -f $pidfile ] && kill -0 `cat $pidfile` && exit

# email last few lines of the logfile to see why it died
if [ -f $logfile ]; then
  log=/var/log/log-weather
  tail -40 $logfile >$log
  /home/jim/scripts/email-log.sh $log "weather log"
  rm $log
  fi

# restart process
pywws-livelog.py -v -l $logfile $datadir &
echo $! >$pidfile

This stores the process id of the running LiveLog in pidfile. If the process is running, the script does nothing. If the process has crashed, it emails the last 40 lines of the log file to me (using a script that creates a message and passes it to sendmail) and then restarts LiveLog. You’ll need to edit this quite a lot to suit your file locations and so on, but it gives some idea of what to do.

Commentaires ou questions? SVP, souscrivez à la liste d’envoi de pywws http://groups.google.com/group/pywws et laissez-le nous savoir.