#!/bin/bash

if [ $(CreoleGet activer_nut) = "oui" ]; then
    if [ $(CreoleGet nut_ups_daemon) = "oui" ]; then
        for nut_ups_name in $(CreoleGet nut_ups_name); do
            monitors="$monitors$nut_ups_name@localhost "
        done
    else
        monitors="$(CreoleGet nut_monitor_foreign_name)@$(CreoleGet nut_monitor_foreign_host)"
    fi
    . /usr/lib/eole/diagnose.sh
    EchoGras "*** Onduleur"
    for monitor in $monitors; do
        printf ".  %${len_pf}s => " $monitor
        status=$(/bin/upsc $monitor ups.status 2> /dev/null)
        if [ $? -ne 0 ];then
            status=$(/bin/upsc $monitor ups.status 2>&1 | grep -v 'Init SSL without certificate database')
            EchoRouge "$status"
        else
            charge=`/bin/upsc $monitor battery.charge 2> /dev/null| cut -d'.' -f1`
            #si OL dans le status
            for a in $status; do
                [ "$a" = "OL" ] && ok=true && break
            done

            if [ "$ok" = "true" ]; then
                EchoVert "OK ($charge%)"
            elif [ "$charge" -lt "20" ]; then
                EchoRouge "Charge batterie faible ($charge%)"
            else
                EchoOrange "Fonctionnement sur batterie ($charge%)"
            fi
        fi
    done
    echo
fi
