﻿#!/bin/sh

arg1=$1
arg2=$2
arg3=$3
arg4=$4

test_dir=/home/ibz/iptv.watchdog
test_filename=test_file
test_file=$test_dir/$test_filename
log_filename=vlc_watchdog.log
log_file=$test_dir/$log_filename
log_file2=$test_dir/watchdogger.log
syslog_ip=192.168.0.200  # ип сервера сислогов куда централизовано скидываются сообщения со всей сети
ip_for_syslog=192.168.0.1 # наш ип для сервера сислогов

rm $test_file
rm $log_file
vlc -d -vvv udp://@$arg1:1234 --file-logging --logfile $log_file --sout='#duplicate{dst=std{access=file,mux=raw,dst="'"$test_file"'"}}'
sleep 7
kill `ps axw|grep $test_filename|grep "access=file"|grep -v grep|gawk '{print $1}'`

if [ -f $test_file ]; then
  filesize=`du --bytes $test_file|gawk '{print $1}'`
  if [ $filesize -lt 10000 ]; then
    if cat $log_file | grep "ts warning: invalid header"
      then
        echo "`date`: Scrambled $arg4 -&gt; $arg2 ( $arg1:1234 )!!!" &gt;&gt;$log_file2
        mv -f $log_file "$test_dir/vlc.logs/$arg4" #на всякий случай копируем лог-файл от vlc отдельно
        /usr/bin/mysql -ptv_pass -utv_user tv_db -e "update chan_stat set last_state=1, last_check_date=Now() where ip='$arg1';"
        echo "`date`: SCRAMBLED" &gt;&gt; /home/ibz/iptv.watchdog/logs/$arg1.log
        echo "&lt;8&gt;`date '+%b %e %k:%M:%S'` $ip_for_syslog IPTV: Scrambled $arg4 -&gt; $arg2 ( $arg1:1234 )!!!" | nc -i 2 -w 2 -q 2 -u $syslog_ip 514
      else
        echo "`date`: Umer $arg4 -&gt; $arg2 ( $arg1:1234 )!!!" &gt;&gt;$log_file2
        mv -f $log_file "$test_dir/vlc.logs/$arg4"
        /usr/bin/mysql -ptv_pass -utv_user tv_db -e "update chan_stat set last_state=2, last_check_date=Now() where ip='$arg1';"
        echo "`date`: NO STREAM" &gt;&gt; /home/ibz/iptv.watchdog/logs/$arg1.log
        echo "&lt;8&gt;`date '+%b %e %k:%M:%S'` $ip_for_syslog IPTV: NO STREAM $arg4 -&gt; $arg2 ( $arg1:1234 )!!!" | nc -i 2 -w 2 -q 2 -u $syslog_ip 514
    fi
    exit 1
  else
    if [ $arg3 != "0" ]; then
      echo "test pids: $arg3"
      for pid in `echo "$arg3" | awk -F"," '{ print $1" "$2" "$3" "$4" "$5 }'`; do
        echo "ignore pid: $pid"
        cat $log_file | grep "ts warning: invalid header" | grep -v "(pid: $pid)" &gt; $log_file."_".$pid
        rm $log_file
        mv $log_file."_".$pid $log_file
      done
      if cat $log_file | grep "ts warning: invalid header"
        then
          echo "`date`: Scrambled $arg4 -&gt; $arg2 ( $arg1:1234 )!!!" &gt;&gt;$log_file2
          mv -f $log_file "$test_dir/vlc.logs/$arg4"
          /usr/bin/mysql -ptv_pass -utv_user tv_db -e "update chan_stat set last_state=1, last_check_date=Now() where ip='$arg1';"
          echo "`date`: SCRAMBLED" &gt;&gt; /home/ibz/iptv.watchdog/logs/$arg1.log
          echo "&lt;8&gt;`date '+%b %e %k:%M:%S'` $ip_for_syslog IPTV: Scrambled $arg4 -&gt; $arg2 ( $arg1:1234 )!!!" | nc -i 2 -w 2 -q 2 -u $syslog_ip 514
          exit 1
      fi
    else
      if cat $log_file | grep "ts warning: invalid header"
        then
          echo "`date`: Scrambled $arg4 -&gt; $arg2 ( $arg1:1234 )!!!" &gt;&gt;$log_file2
          mv -f $log_file "$test_dir/vlc.logs/$arg4"
          /usr/bin/mysql -ptv_pass -utv_user tv_db -e "update chan_stat set last_state=1, last_check_date=Now() where ip='$arg1';"
          echo "`date`: SCRAMBLED" &gt;&gt; /home/ibz/iptv.watchdog/logs/$arg1.log
          echo "&lt;8&gt;`date '+%b %e %k:%M:%S'` $ip_for_syslog IPTV: Scrambled $arg4 -&gt; $arg2 ( $arg1:1234 )!!!" | nc -i 2 -w 2 -q 2 -u $syslog_ip 514
          exit 1
      fi
    fi
  fi
fi

/usr/bin/mysql -ptv_pass -utv_user tv_db -e "update chan_stat set last_state=0, last_check_date=Now(), prev_state=0, sms_sended=0, first_failed=NULL where ip='$arg1';"
echo "`date`: clear" &gt;&gt; /home/ibz/iptv.watchdog/logs/$arg1.log

sleep 1
exit 0