apps / sms / bot.monitoring /
824f427 2 years ago
1 contributor
82 lines | 2.215kb
#!/bin/bash

if test -e /dev/shm/smsbot.envoi_en_cours; then
    rm -f /dev/shm/smsbot.envoi.ok /dev/shm/smsbot.reception.ok
    exit
fi

eval $(systemctl show smsbot --property=ActiveState)
test -n "$ActiveState" && test "$ActiveState" = 'active' || exit 1

NOW=$(date +%s)

/usr/local/bin/smsapi "supervision réception" || exit 2
        
function test_reception () {
    flag=0
    for i in $(seq ${2:-180}); do
        sleep ${3:-10}
        if test -e "$1"; then
            reception=$(stat -c %Y "$1")
            test $reception -gt $NOW && flag=1 && break
        fi
    done
    test $flag -ne 0
}

function hilink () {
    perl -e '
        use HiPi qw( :hilink );
        use Encode qw(encode decode);
        use HiPi::Huawei::E3531;
        my $hlink = HiPi::Huawei::E3531->new();
        my $reponse = $hlink->get_'$1'();
        print "$reponse->{Count}\n";
        for ($i = $reponse->{Count} - 1; $i >= 0; $i--) {
            my $msg = $reponse->{Messages}[$i];
            foreach my $k (keys(%$msg)) {
                if ($k == "Content") {
                    $msg->{$k} = encode("utf8", $msg->{$k});
                }
                print "$k: $msg->{$k}\n";
            }
            print "\n";
        }
    '
}

function device_reboot () {
    perl -e '
        use HiPi qw( :hilink );
        use Encode qw(encode decode);
        use HiPi::Huawei::E3531;
        my $hlink = HiPi::Huawei::E3531->new();
        my $reponse = $hlink->device_reboot();
    '
}

if ! test_reception /dev/shm/smsbot.reception.ok; then
    msg='problème de réception'
elif ! test_reception /dev/shm/smsbot.envoi.ok; then
    msg="problème d'envoi"
fi

if test -n "$msg"; then
    msmtp contact@paris12.pcf.fr << EOM
Subject: SMSBOT $msg

$(systemctl status smsbot)

INBOX:
$(hilink inbox)

OUTBOX:
$(hilink outbox)

JOURNAL:
$(sudo /bin/journalctl -u smsbot | tail)

$(if test "$msg" = "problème d'envoi"; then sudo /bin/systemctl restart smsbot && echo "smsbot relancé" || echo "erreur $?"; fi)
$(if test "$msg" = "problème de réception"; then sudo /bin/systemctl stop smsbot && device_reboot && sleep 30 && sudo /bin/systemctl start smsbot && echo "device rebooté" || echo "erreur $?"; fi)
EOM
fi