Créer un fichier PID pour Funambol sous GNU/Linux
Lorsqu'il est lancé, le serveur Funambol ne crée pas de fichier PID rendant difficile son monitoring à l'aide d'outils comme Monit qui, à l'aide d'un PID, monitorent les ressources consommées par un process donné.
À l'aide d'un script bash, nous allons permettre cette création de fichier PID.
Fonctionnement du script
Rien de bien sorcier, juste une recherche des PID des différents composants du serveur Funambol à l'aide d'un bon vieux ps couplé à une paire de grep associés à un petit awk suivi d'un head... autrement dit "on recherche les processus des composants de funambol dans la liste des processus, et on récupère leurs PID".
Le script
Il y a certainement plus élégant mais l'essentiel c'est qu'il fasse ce qu'on lui demande et qu'il remplisse sa fonction didactique.
#!/bin/bash # Check the script is being run by root, or die. ROOT_UID=0 if [ "$UID" -ne "$ROOT_UID" ] then echo "Run script as "root"." echo exit 1 fi DS_SERVER=`ps -ef | grep ds-server | grep -v grep` HYPERSONIC=`ps -ef | grep hypersonic | grep -v grep` CTP_SERVER=`ps -ef | grep com.funambol.ctp.server.CTPServer | grep -v grep` INBOX_LISTENER=`ps -ef | grep inbox-listener | grep -v grep` PIM_LISTENER=`ps -ef | grep pimlistener | grep -v grep` if [ -z "$DS_SERVER" ] then if [ -e /opt/funambol/ds-server.pid ]; then rm /opt/Funambol/ds-server.pid fi else ps -ef | grep ds-server | grep -v grep | awk '{print $2}' | head -n 1 > /opt/Funambol/ds-server.pid fi if [ -z "$HYPERSONIC" ] then if [ -e /opt/funambol/hypersonic.pid ]; then rm /opt/Funambol/hypersonic.pid fi else ps -ef | grep hypersonic | grep -v grep | awk '{print $2}' | head -n 1 > /opt/Funambol/hypersonic.pid fi if [ -z "$CTP_SERVER" ] then if [ -e /opt/funambol/ctp-server.pid ]; then rm /opt/Funambol/ctp-server.pid fi else ps -ef | grep com.funambol.ctp.server.CTPServer | grep -v grep | awk '{print $2}' | head -n 1 > /opt/Funambol/ctp-server.pid fi if [ -z "$INBOX_LISTENER" ] then if [ -e /opt/funambol/inbox-listener.pid ]; then rm /opt/Funambol/inbox-listener.pid fi else ps -ef | grep inbox-listener | grep -v grep | awk '{print $2}' | head -n 1 > /opt/Funambol/inbox-listener.pid fi if [ -z "$PIM_LISTENER" ] then if [ -e /opt/funambol/pim-listener.pid ]; then rm /opt/Funambol/pim-listener.pid fi else ps -ef | grep pimlistener | grep -v grep | awk '{print $2}' | head -n 1 > /opt/Funambol/pim-listener.pid fi
Utilisation
En appelant ce script via une tache cron, par exemple, vous permettrez aux système de monitoring d'avoir un pid à jour en cas de redémarrage.
Il est aussi possible de créer un script de commande pour (re)lancer/arrêter Funambol afin de créer/détruire ces fichiers PID au démarrage/arrêt de Funambol. Voici un exemple d'un tel script, où le script précédent (celui qui crée les PID) est dans /opt/controls/mk-funambol-pid-file :
#!/bin/bash # Check the script is being run by root, or die. ROOT_UID=0 if [ "$UID" -ne "$ROOT_UID" ] then echo "Run script as "root"." echo exit 1 fi FUNAMBOL=`ps -ef | grep /opt/Funambol | grep -v grep` if [ -z "$FUNAMBOL" ] then FUNAMBOL_ON=0 else FUNAMBOL_ON=1 fi case $1 in start) if [ $FUNAMBOL_ON -eq 1 ] then echo "Funambol is already started : use $0 restart option to restart." echo exit 1 else echo "Starting Funambol..." /opt/Funambol/bin/funambol start echo "done." echo echo "Creating PID files..." /opt/controls/mk-funambol-pid-file echo "done." echo fi ;; stop) if [ $FUNAMBOL_ON -eq 0 ] then echo "Funambol is already stopped." echo exit 1 else echo "Stopping Funambol..." /opt/Funambol/bin/funambol stop echo "done." echo echo "Deleting PID files..." /opt/controls/mk-funambol-pid-file echo "done." echo fi ;; restart) if [ $FUNAMBOL_ON -eq 0 ] then echo "Funambol was already stopped !" echo echo "Starting Funambol..." /opt/Funambol/bin/funambol start echo "done." echo echo "Creating PID files..." /opt/controls/mk-funambol-pid-file echo "done." echo else echo "Stopping Funambol..." /opt/Funambol/bin/funambol stop echo "done." echo echo "Deleting PID files..." /opt/controls/mk-funambol-pid-file echo "done." echo echo echo "Starting Funambol..." /opt/Funambol/bin/funambol start echo "done." echo echo "Creating PID files..." /opt/controls/mk-funambol-pid-file echo "done." echo fi ;; status) if [ $FUNAMBOL_ON -eq 0 ] then echo "Funambol is curently stopped." echo else echo "Funambol is curently started." echo fi ;; *) echo "Usage $0 [start|stop|restart|status]" ;; esac
Ce script permet, en outre, de permettre un redémarrage direct de Funambol.
Exemple d'application : configuration de Monit pour Funambol
Considérant que nous avons les 2 scripts précédent, l'un dans /opt/controls/mk-funambol-pid-file et l'autre dans /opt/controls/funambol, on pourra utiliser, par exemple, la configuration suivante pour Monit :
# Funambol
check process ds-server with pidfile /opt/Funambol/ds-server.pid
group funambol
start program "/opt/controls/funambol start"
stop program "/opt/controls/funambol stop"
if failed port 8443 for 3 times within 3 cycles then restart
if 2 restart within 10 cycles then timeout
check process hypersonic with pidfile /opt/Funambol/hypersonic.pid
group funambol
check process ctp-server with pidfile /opt/Funambol/ctp-server.pid
group funambol
check process inbox-listener with pidfile /opt/Funambol/inbox-listener.pid
group funambol
check process pim-listener with pidfile /opt/Funambol/pim-listener.pid
group funambol
Ici funambol utilise une connexion sécurisée sur le port 8443 comme indiqué dans le tutoriel d'installation de Funambol, nous monitorons donc ce port.
Grâce aux pid, nous pourrons voir sur l'interface web de Monit les ressources consommées par les composants de Funambol, et éventuellement créer des règles dans le cas où cette consommation deviendrait excessive.

