#!/bin/bash
#
# update_proxy_clients
#
# ULS-Clients in der DMZ aktualisieren
#
###############################################################################
#
ULSPWDB64="to be defined"
HTTPGETCMD="httpget -S -s -a $ULSPWDB64"
MYSQL="/usr/bin/mysql -B -N -u logginglesen -plesenlogging logging"
#
PATH=/usr/bin:/usr/local/bin:/srv/uls/bin:$PATH
export PATH
#
if [[ -z "$1" ]]
 then
  echo "usage: `basename $0` <proxy>"
  exit 1
fi
#
PROXY="$0"
#
if [ -f /etc/ulsserver.conf ]
 then
  . /etc/ulsserver.conf
fi
if [ -z "$ULSNAME" ]
 then
  ULSNAME=`hostname`
fi
#
TEEDATA=/dev/shm/clientlist.$$
#
$MYSQL -e "
select lower(servername), lower(fqdn), inet_ntoa(server.ip), inet_ntoa(transferip), uls_werte_ips, sin, verfahren.name, seriennummer,
  replace(server.contact, '\\n', ' '), replace(server.location, '\\n', ' '), replace(server.beschreibung, '\\n', ' '),
  replace(server.maintenancewindow, '\\n', ' '),
  cast(coalesce(max(if(zeitvon != '00:00:00' OR zeitbis != '23:59:59',
                       unix_timestamp(least('2038-01-18 00:00:00',datumbis, concat(curdate(), ' ', zeitbis))),
                       unix_timestamp(least('2038-01-18 00:00:00',datumbis)))), 0) as unsigned) as suspendend
from server
left join verfahren on verfahren.ID = server.verfahrenID
left join serverclasses on serverclasses.ID = server.serverclassesID
left join ueberwachungspausen as up
  on up.verfahrenID = server.verfahrenID
    and testreg(coalesce(serverclasses.name, ''), classes, classes_v)
    and testreg(server.servername, server, server_v)
    and (section = '.*' OR section = '^.*$') and section_v = ''
    and (teststep = '.*' OR teststep = '^.*$') and teststep_v = ''
    and (details = '.*' OR details = '^.*$') and details_v = ''
    and (einheit = '.*' OR einheit = '^.*$') and einheit_v = ''
    and datumvon <= now() and now() <= datumbis
    and if(mdaystart >= 0, mdaystart <= day(now()),
           month(curdate()) < month(curdate() - interval mdaystart day))
    and if(mdayend >= 0, day(curdate()) <= mdayend,
           month(curdate()) = month(curdate() - interval 1+mdayend day))
    and find_in_set(weekday(curdate()), weekdays)
    and zeitvon <= curtime() and curtime() <= zeitbis
where state != 'inactive'
group by server.ID
order by servername
" | tee $TEEDATA | $HTTPGETCMD -mn CLIENTDATA -m- $1 "update_proxy_clients.s2w"
ls -l $TEEDATA
rm $TEEDATA
