Watchdog (Oscam): Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
(→Script) |
|||
Zeile 39: | Zeile 39: | ||
<pre> | <pre> | ||
sudo chmod +x /usr/local/bin/oscam_watchdog.sh | sudo chmod +x /usr/local/bin/oscam_watchdog.sh | ||
</pre> | |||
<pre> | |||
#!/bin/bash | |||
# Oscam Watchdog & Nagios Script | |||
# ------------------------------- | |||
# you can share the script where and as much as you want | |||
# but leave the copyright & contact header untouched! | |||
# if you modify the script you can place your name below | |||
# ------------------------------- | |||
# Requires: curl (apt-get install curl) | |||
# wget is not supported cause a failure with authentication encryption | |||
# ------------------------------- | |||
# Changelog: | |||
# 15.10.12 - Checken von Entitlements hinzugefügt. Bug im ReaderRestart behoben. | |||
# 15.10.12 - Commandline Howto angepasst. Version auf 0.4 erhöht. | |||
# | |||
# | |||
# ------------------------------- | |||
filename="check_oscam" | |||
version="0.4" | |||
# Start | |||
# Konfiguration: | |||
# | |||
# Wert welcher per Commandozeile übergeben werden muss, damit der automatische Restart des Readers erfolgt. | |||
codeRestart="autorestart" | |||
# Anzahl wie oft hintereinander der Reader neugestartet wird bevor abgebrochen wird (falls es danach nicht OK ist) | |||
anticount="3" | |||
# Log 2 Syslog | |||
# "1" Aktiviert | |||
# "0" Deaktiviert | |||
logsyslog="0" | |||
# Debug | |||
# "1" Aktiviert | |||
# "0" Deaktiviert | |||
debug="0" | |||
# Temporärer Pfad | |||
# für html Seiten & Antiloop Dateien | |||
# OHNE abschließendem Slash (/) am Ende | |||
temp="/tmp" | |||
# Der User welcher das Skript ausführt muss Besitzer der Dateien (status.html) und (readers.html) sein. | |||
# (Wenn man das Skript zum Testen mit root ausführt, und später den Cron mit einem anderem User - wird das Skript fehlschlagen, da die Dateien nicht überschrieben werden können.) | |||
# Ende | |||
# Konfiguration | |||
# ------------------------------- | |||
howto='\nWarning: Wrong command line arguments. \n | |||
Usage: ./check_oscam <hostname> <port> <username> <password> <part> <autorestart> <reader0> <reader1> ... <reader4> <entitlementsReader0> <entitlementsReader1> ... <entitlementsReader4>\n | |||
Parts are: statuscheck, restart, entitlement\n | |||
Example: ./check_oscam 127.0.0.1 1403 admin "password" statuscheck autorestart hdplus 1702 | |||
- Checks the Readers with label hdplus and 1702. If Status != CARDOK the reader will be restarted automatic.\n | |||
Example: ./check_oscam 127.0.0.1 1403 admin "password" statuscheck norestart hdplus 1702 | |||
- Checks the Readers with label hdplus and 1702. But with no reader restart.\n | |||
Example: ./check_oscam 127.0.0.1 1403 admin "password" entitlement norestart hdplus 1702 "" "" "" "1" "10" | |||
- Refresh the Entitlements & Checks the Count of Entitlements of reader with label hdplus & 1702 where hdplus should have 1 entitlement and 1702 should have 10 entitlements. | |||
- You can specify a maximum of 5 readers. If you specify not 5 you have to place a "" for each missing before starting with the count of entitlements. | |||
- In the Example above 2 readers are specified so there are 3 placeholders ("" "" "") required until starting with the count of entitlements. | |||
Note: All values are case sensitive! | |||
' | |||
if [ "$#" -lt "6" ]; then | |||
echo -e "$howto" && exit "3" | |||
fi | |||
# General | |||
strHostname=${1} | |||
strPort=${2} | |||
strUser=${3} | |||
strPass=${4} | |||
strpart=${5} | |||
strRestart=${6} | |||
# Reader | |||
strReader0=${7} | |||
strReader1=${8} | |||
strReader2=${9} | |||
strReader3=${10} | |||
strReader4=${11} | |||
# Entitlements | |||
strEnt0=${12} | |||
strEnt1=${13} | |||
strEnt2=${14} | |||
strEnt3=${15} | |||
strEnt4=${16} | |||
if [ "$strpart" == "entitlement" ] && [ "$strEnt0" == "" ]; then | |||
echo -e "$howto" && exit "3" | |||
fi | |||
# Debugging | |||
if [ $debug -eq 1 ] ;then echo -e "Hostname:$strHostname\nPort:$strPort\nUser:$strUser\nPass:******\nPart:$strpart\nRestart:$strRestart\nReader0:$strReader0\nReader1:$strReader1\nReader2:$strReader2\nReader3:$strReader3\nReader4:$strReader4\n---------------------------------\nEntReader0:$strEnt0\nEntReader1:$strEnt1\nEntReader2:$strEnt2\nEntReader3:$strEnt3\nEntReader4:$strEnt4" ; fi | |||
# Remove old File | |||
rm -f ""$temp"/status.html" | |||
statuscheck() { | |||
#echo "$strHostname:$strPort" | |||
rm -f "$temp"/status.html | |||
# File Download | |||
a=$(curl --connect-timeout 2 -vu ""$strUser":"$strPass"" --anyauth -o ""$temp"/status.html" "http://$strHostname:$strPort/status.html" >/dev/null 2>/dev/null) | |||
if [ $? -eq 0 ] | |||
then | |||
{ lines=$(wc -l ""$temp"/status.html" | awk '{print $1}') | |||
if [ $lines -lt 2 ] ; then | |||
t=$(tail -n 1 ""$temp"/status.html") | |||
echo "CRITICAL: " $t | |||
# Logging | |||
if [ "$logsyslog" == "1" ] ; then logger "$filename $version - CRITICAL: $t"; fi | |||
if [ $debug -eq 1 ] ;then echo -e "Fehler - Status.html hat >2 Zeilen und sieht somit nicht korrekt aus." ; fi | |||
exit 2; | |||
else | |||
{ | |||
# Datei hat mehr als 1 Zeile - also suche nach Karten | |||
c=0 | |||
z=0 | |||
readerName="strReader" | |||
if [ $debug -eq 1 ] ;then echo -e "Beginne Schleife um nach Readern zu suchen.\nc=$c\nz=$z\nreaderName=$readerName" ; fi | |||
while [ $z == 0 ] | |||
do | |||
{ | |||
# Zähle ReaderNamen hoch | |||
readerNameComp="$readerName$c" | |||
if [ $debug -eq 1 ] ;then echo -e "Readername = readerNameComp=$readerNameComp\nReadernameREAL = ${!readerNameComp}" ; fi | |||
# Suche nach Karte | |||
b=$(grep '<TD class="statuscol16">CARDOK <A HREF="entitlements.html?label='${!readerNameComp}'&hideexpired=1"' "$temp"/status.html) | |||
if [ $debug -eq 1 ] ;then echo -e "Suchstring="$b"" ; fi | |||
if [ $? -eq 0 ] | |||
then | |||
#Grep erfolgreich - aber noch nicht klar ob string gefunden | |||
if [ ! "$b" == "" ] | |||
then | |||
#echo "Karte - OK" | |||
if [ ! "$y" == "NOK" ] ; then y="OK" ; fi | |||
e=""$e"OK: ${!readerNameComp} - " | |||
if [ $debug -eq 1 ] ;then echo -e "Sieht gut aus - String wurde gefunden. y=$y\ne=$e" ; fi | |||
# pattern found | |||
# Alles gut - lösche falls vorhanden die Antiloop Datei | |||
rm -f ""$temp"/${!readerNameComp}" | |||
else | |||
# String nicht gefunden, also Karte nicht OK | |||
#echo "Karte - Not OK" | |||
y=("NOK") | |||
e=""$e"CRITICAL: ${!readerNameComp} - " | |||
if [ "$codeRestart" == "$strRestart" ] ; then readerRestart ${!readerNameComp} ; fi | |||
if [ $debug -eq 1 ] ;then echo -e "Sieht schlecht aus - String nicht gefunden. (1. Abfrage)\ny=$y\ne=$e" ; fi | |||
fi | |||
else | |||
#echo "Karte - Not OK" | |||
y=("NOK") | |||
e=""$e"CRITICAL: ${!readerNameComp} - " | |||
if [ "$codeRestart" == "$strRestart" ] ; then readerRestart ${!readerNameComp} ; fi | |||
if [ $debug -eq 1 ] ;then echo -e "Sieht schlecht aus - String nicht gefunden.(2. Abfrage)\ny=$y\ne=$e" ; fi | |||
fi | |||
# Ende If | |||
tempCount=$[$c + 1] | |||
if [ $debug -eq 1 ] ;then echo -e "Zähle hoch - c=$c & tempCount=$tempCount" ; fi | |||
readerNameComp="$readerName$tempCount" | |||
if [ $debug -eq 1 ] ;then echo -e "Demnach ist der nächste Reader: $readerNameComp\nDas sollte sein:${!readerNameComp}" ; fi | |||
if [ "${!readerNameComp}" == "" ] ; then | |||
z=$[$z + 1] | |||
if [ $debug -eq 1 ] ;then echo -e "So der nächste Reader ist leer also Abbruch der Schleife. z=$z" ; fi | |||
else | |||
# Zähle Reader hoch | |||
c=$[$c + 1] | |||
if [ $debug -eq 1 ] ;then echo -e "Ende Schleifendurchlauf. Zähle Reader hoch. c=$c" ; fi | |||
fi | |||
} | |||
done | |||
if [ "$y" == 'OK' ] ; then | |||
echo "$e" | |||
# Logging | |||
if [ "$logsyslog" == "1" ] ; then logger "$filename $version - $e"; fi | |||
if [ $debug -eq 1 ] ;then echo -e "Fertig - Y = OK - also Exit 0." ; fi | |||
exit 0 | |||
fi | |||
if [ "$y" == "NOK" ] ; then | |||
echo "$e" | |||
# Logging | |||
if [ "$logsyslog" == "1" ] ; then logger "$filename $version - $e"; fi | |||
if [ $debug -eq 1 ] ;then echo -e "Fertig - Y = NOK - also Exit 2." ; fi | |||
exit 2 | |||
fi | |||
} | |||
fi | |||
} | |||
else | |||
{ | |||
echo "CRITICAL: Could not download status.html (StatusCheck)" | |||
# Logging | |||
if [ "$logsyslog" == "1" ] ; then logger "$filename $version - CRITICAL: Could not download status.html (StatusCheck)"; fi | |||
exit 2; | |||
} | |||
fi | |||
} | |||
readerRestart() { | |||
if [ $debug -eq 1 ] ;then echo -e "Beginne Restart Funktion - Übergabewert (Readername) ist: $1" ; fi | |||
# echo "$strHostname:$strPort" | |||
rm -f ""$temp"/readers.html" | |||
# Check das Loop nicht Größer X ist - damit die Reader nicht dauernd neu gestartet werden | |||
r=$(cat "$temp/$1" 2>/dev/null) | |||
if [ ""$r"" == "" ] ; then r=0 ;if [ $debug -eq 1 ] ;then echo -e "Temp. Restartdatei existiert noch nicht. r=$r" ; fi; fi | |||
if [ "$r" -ge "$anticount" ] ; then | |||
echo "CRITICAL: Nach $r Versuchen kein Reader restart mehr." | |||
# Logging | |||
if [ "$logsyslog" == "1" ] ; then logger "$filename $version - CRITICAL: Nach $r Versuchen kein Reader restart mehr."; fi | |||
# if [ $debug -eq 1 ] ;then echo -e "$filename $version - CRITICAL: Nach $r Versuchen kein Reader restart mehr." ; fi | |||
# Deaktiviert, da sonst das Skript komplett beendet wird | |||
# exit 2; | |||
else | |||
# File Download | |||
if [ $debug -eq 1 ] ;then echo -e "readers.html wird runtergeladen - bzw. befehl disable befehl ausgeführt" ; fi | |||
g=$(curl --connect-timeout 2 -vu ""$strUser":"$strPass"" --anyauth -o ""$temp"/readers.html" "http://$strHostname:$strPort/readers.html?label=$1&action=disable" >/dev/null 2>/dev/null) | |||
if [ $? -eq 0 ] | |||
then | |||
# Antiloop hochzählen | |||
echo $[$r+1] > "$temp/$1" | |||
# Zähle Zeilen um Fehler fest zu stellen ( Datei muss <1 Zeile sein ) | |||
lines=$(wc -l ""$temp"/readers.html" | awk '{print $1}') | |||
if [ $lines -lt 2 ] ; then | |||
s=$(tail -n 1 ""$temp"/readers.html") | |||
echo "CRITICAL: " $s | |||
# Logging | |||
if [ "$logsyslog" == "1" ] ; then logger "$filename $version - CRITICAL: $s"; fi | |||
# Deaktiviert, da sonst das Skript komplett beendet wird | |||
# exit 2; | |||
else | |||
# Datei hat mehr als 1 Zeile - also suche nach Karten | |||
echo "WARNING: Reader $1 disabled" | |||
# Aufräumen vor dem aktivieren | |||
rm -f ""$temp"/readers.html" | |||
lines="" | |||
s="" | |||
f=$(curl --connect-timeout 2 -vu ""$strUser":"$strPass"" --anyauth -o ""$temp"/readers.html" "http://$strHostname:$strPort/readers.html?label=$1&action=enable" >/dev/null 2>/dev/null) | |||
if [ $? -eq 0 ] | |||
then | |||
lines=$(wc -l ""$temp"/readers.html" | awk '{print $1}') | |||
if [ $lines -lt 2 ] ; then | |||
s=$(tail -n 1 ""$temp"/readers.html") | |||
echo "CRITICAL: " $s | |||
# Logging | |||
if [ "$logsyslog" == "1" ] ; then logger "$filename $version - CRITICAL: $s"; fi | |||
# Deaktiviert, da sonst das Skript komplett beendet wird | |||
# exit 2; | |||
else | |||
# Datei hat mehr als 1 Zeile - also suche nach Karten | |||
echo "WARNING: Reader $1 enabled" | |||
#sleep 10 | |||
#statuscheck | |||
fi | |||
fi | |||
fi | |||
else | |||
echo "CRITICAL: Could not download readers.html (ReaderRestart)" | |||
# Logging | |||
if [ "$logsyslog" == "1" ] ; then logger "$filename $version - CRITICAL: Could not download readers.html (ReaderRestart)"; fi | |||
# Deaktiviert, da sonst das Skript komplett beendet wird | |||
# exit 2; | |||
fi | |||
fi | |||
# ------------------------------------------------------------------- | |||
} | |||
entitlement() { | |||
#echo "$strHostname:$strPort" | |||
y="" | |||
e="" | |||
rm -f "$temp"/status.html | |||
# Aktualisiere Entitlements | |||
if [ ! "$strReader0" == "" ] ; then refreshEntitlement $strReader0 ; fi | |||
if [ ! "$strReader1" == "" ] ; then refreshEntitlement $strReader1 ; fi | |||
if [ ! "$strReader2" == "" ] ; then refreshEntitlement $strReader2 ; fi | |||
if [ ! "$strReader3" == "" ] ; then refreshEntitlement $strReader3 ; fi | |||
if [ ! "$strReader4" == "" ] ; then refreshEntitlement $strReader4 ; fi | |||
# File Download | |||
a=$(curl --connect-timeout 2 -vu ""$strUser":"$strPass"" --anyauth -o ""$temp"/status.html" "http://$strHostname:$strPort/status.html" >/dev/null 2>/dev/null) | |||
if [ $? -eq 0 ] | |||
then | |||
{ lines=$(wc -l ""$temp"/status.html" | awk '{print $1}') | |||
if [ $lines -lt 2 ] ; then | |||
t=$(tail -n 1 ""$temp"/status.html") | |||
echo "CRITICAL: " $t | |||
# Logging | |||
if [ "$logsyslog" == "1" ] ; then logger "$filename $version - CRITICAL: $t"; fi | |||
if [ $debug -eq 1 ] ;then echo -e "Fehler - Status.html hat >2 Zeilen und sieht somit nicht korrekt aus." ; fi | |||
exit 2; | |||
else | |||
{ | |||
# Datei hat mehr als 1 Zeile - also suche nach Entitlements | |||
c=0 | |||
z=0 | |||
readerName="strReader" | |||
entName="strEnt" | |||
if [ $debug -eq 1 ] ;then echo -e "Beginne Schleife um nach Entitlements zu suchen.\nc=$c\nz=$z\nreaderName=$readerName\nEntitleName=$entName" ; fi | |||
while [ $z == 0 ] | |||
do | |||
{ | |||
# Zähle Reader/Entitlement hoch | |||
readerNameComp="$readerName$c" | |||
entNameComp="$entName$c" | |||
if [ $debug -eq 1 ] ;then echo -e "Readername = readerNameComp="$readerNameComp"\nReadernameREAL="${!readerNameComp}"\nEntitleName="$entNameComp"\nEntitlementREAL="${!entNameComp}"" ; fi | |||
# Checke ob 1 oder mehr Entitlements | |||
if [ "${!entNameComp}" -eq "1" ] ;then | |||
entitleCount="entitlement" | |||
else | |||
entitleCount="entitlements" | |||
fi | |||
# Suche nach Entitlement | |||
b=$(grep '<A HREF="entitlements.html?label='${!readerNameComp}'&hideexpired=1" class="tooltip">('${!entNameComp}' '$entitleCount')<SPAN>' "$temp"/status.html) | |||
if [ $debug -eq 1 ] ;then echo -e "Suchstring="$b"" ; fi | |||
if [ $? -eq 0 ] | |||
then | |||
#Grep erfolgreich - aber noch nicht klar ob string gefunden | |||
if [ ! "$b" == "" ] | |||
then | |||
#echo "Entitlement - OK" | |||
if [ ! "$y" == "NOK" ] ; then y="OK" ; fi | |||
e=""$e"OK: ${!readerNameComp} ("${!entNameComp}" $entitleCount) - " | |||
if [ $debug -eq 1 ] ;then echo -e "Sieht gut aus - String wurde gefunden. y=$y\ne=$e" ; fi | |||
else | |||
# String nicht gefunden, also Entitlements nicht OK | |||
y=("NOK") | |||
e=""$e"CRITICAL: ${!readerNameComp} (${!entNameComp} $entitleCount) - " | |||
if [ $debug -eq 1 ] ;then echo -e "Sieht schlecht aus - String nicht gefunden. (1. Abfrage)\ny=$y\ne=$e" ; fi | |||
fi | |||
else | |||
#echo "Karte - Not OK" | |||
y=("NOK") | |||
e=""$e"CRITICAL: ${!readerNameComp} (${!entNameComp} $entitleCount) - " | |||
if [ $debug -eq 1 ] ;then echo -e "Sieht schlecht aus - String nicht gefunden.(2. Abfrage)\ny=$y\ne=$e" ; fi | |||
fi | |||
# Ende If | |||
tempCount=$[$c + 1] | |||
if [ $debug -eq 1 ] ;then echo -e "Zähle hoch - c=$c & tempCount=$tempCount" ; fi | |||
readerNameComp="$readerName$tempCount" | |||
entNameComp="$entName$tempCount" | |||
if [ $debug -eq 1 ] ;then echo -e "Demnach ist der nächste Reader: $readerNameComp\nDas sollte sein:${!readerNameComp}" ; fi | |||
if [ "${!readerNameComp}" == "" -o "${!entNameComp}" == "" ] ; then | |||
z=$[$z + 1] | |||
if [ $debug -eq 1 ] ;then echo -e "So der nächste Reader ist leer also Abbruch der Schleife. z=$z" ; fi | |||
else | |||
# Zähle Reader hoch | |||
c=$[$c + 1] | |||
if [ $debug -eq 1 ] ;then echo -e "Ende Schleifendurchlauf. Zähle Reader hoch. c=$c" ; fi | |||
fi | |||
} | |||
done | |||
if [ "$y" == 'OK' ] ; then | |||
echo "$e" | |||
# Logging | |||
if [ "$logsyslog" == "1" ] ; then logger "$filename $version - $e"; fi | |||
if [ $debug -eq 1 ] ;then echo -e "Fertig - Y = OK - also Exit 0." ; fi | |||
exit 0 | |||
fi | |||
if [ "$y" == "NOK" ] ; then | |||
echo "$e" | |||
# Logging | |||
if [ "$logsyslog" == "1" ] ; then logger "$filename $version - $e"; fi | |||
if [ $debug -eq 1 ] ;then echo -e "Fertig - Y = NOK - also Exit 2." ; fi | |||
exit 2 | |||
fi | |||
} | |||
fi | |||
} | |||
else | |||
{ | |||
echo "CRITICAL: Could not download status.html (StatusCheck)" | |||
# Logging | |||
if [ "$logsyslog" == "1" ] ; then logger "$filename $version - CRITICAL: Could not download status.html (StatusCheck)"; fi | |||
exit 2; | |||
} | |||
fi | |||
} | |||
refreshEntitlement() { | |||
if [ $debug -eq 1 ] ;then echo -e "Beginne Restart Entitlement Funktion" ; fi | |||
# echo "$strHostname:$strPort" | |||
rm -f ""$temp"/readers.html" | |||
# File Download | |||
if [ $debug -eq 1 ] ;then echo -e "readers.html wird runtergeladen - bzw. refresh befehl ausgeführt" ; fi | |||
g=$(curl --connect-timeout 2 -vu ""$strUser":"$strPass"" --anyauth -o ""$temp"/readers.html" "http://$strHostname:$strPort/readers.html?label=$1&action=reread" >/dev/null 2>/dev/null) | |||
if [ $? -eq 0 ] | |||
then | |||
# Antiloop hochzählen | |||
echo $[$r+1] > "$temp/$1" | |||
# Zähle Zeilen um Fehler fest zu stellen ( Datei muss <1 Zeile sein ) | |||
lines=$(wc -l ""$temp"/readers.html" | awk '{print $1}') | |||
if [ $lines -lt 2 ] ; then | |||
s=$(tail -n 1 ""$temp"/readers.html") | |||
echo "CRITICAL: " $s | |||
# Logging | |||
if [ "$logsyslog" == "1" ] ; then logger "$filename $version - CRITICAL: $s"; fi | |||
# Deaktiviert, da sonst das Skript komplett beendet wird | |||
# exit 2; | |||
else | |||
if [ $debug -eq 1 ] ;then echo "OK: Entitlement Refresh" ; fi | |||
if [ "$logsyslog" == "1" ] ; then logger "$filename $version - OK: Entitlement Refresh von $1"; fi | |||
fi | |||
else | |||
echo "CRITICAL: Could not download readers.html (ReaderRefresh)" | |||
# Logging | |||
if [ "$logsyslog" == "1" ] ; then logger "$filename $version - CRITICAL: Could not download readers.html (ReaderRefresh)"; fi | |||
# Deaktiviert, da sonst das Skript komplett beendet wird | |||
# exit 2; | |||
fi | |||
# ------------------------------------------------------------------- | |||
} | |||
if [ "$strpart" == "statuscheck" ]; then | |||
statuscheck | |||
elif [ "$strpart" == "entitlement" ]; then | |||
entitlement | |||
elif [ "$strpart" == "restart" ]; then | |||
c=0 | |||
z=0 | |||
readerName="strReader" | |||
if [ $debug -eq 1 ] ;then echo -e "Beginne Schleife um nach Readern zu suchen.\nc=$c\nz=$z\nreaderName=$readerName" ; fi | |||
while [ $z == 0 ] | |||
do | |||
{ | |||
# Zähle ReaderNamen hoch | |||
readerNameComp="$readerName$c" | |||
if [ $debug -eq 1 ] ;then echo -e "Readername = readerNameComp=$readerNameComp\nReadernameREAL = ${!readerNameComp}" ; fi | |||
# Code | |||
readerRestart ${!readerNameComp} | |||
# Count up | |||
tempCount=$[$c + 1] | |||
if [ $debug -eq 1 ] ;then echo -e "Zähle hoch - c=$c & tempCount=$tempCount" ; fi | |||
readerNameComp="$readerName$tempCount" | |||
if [ $debug -eq 1 ] ;then echo -e "Demnach ist der nächste Reader: $readerNameComp\nDas sollte sein:${!readerNameComp}" ; fi | |||
if [ "${!readerNameComp}" == "" ] ; then | |||
z=$[$z + 1] | |||
if [ $debug -eq 1 ] ;then echo -e "So der nächste Reader ist leer also Abbruch der Schleife. z=$z" ; fi | |||
else | |||
# Zähle Reader hoch | |||
c=$[$c + 1] | |||
if [ $debug -eq 1 ] ;then echo -e "Ende Schleifendurchlauf. Zähle Reader hoch. c=$c" ; fi | |||
fi | |||
} | |||
done | |||
fi | |||
# Ende Skript | |||
# Oscam Watchdog & Nagios Script | |||
# ------------------------------- | |||
</pre> | </pre> | ||
Version vom 17. September 2023, 23:41 Uhr
Noch in Bearbeitung
verwandte Themen
Siehe auch: Startup-Scripte, Mails versenden (Enigma2)
Raspberry
Noch in Bearbeitung
PID ermitteln
pidof /usr/local/bin/oscam
Script
sudo vi /usr/local/bin/oscam_watchdog.sh
#!/bin/bash PIDFILE=/var/run/oscam.pid if (kill -0 `cat $PIDFILE`) then echo "it's alive!" > /dev/null else rm $PIDFILE /usr/local/bin/oscam start > /dev/null echo "OSCAM restarted at `date`" >> /var/log/oscam-crash.log fi
sudo chmod +x /usr/local/bin/oscam_watchdog.sh
#!/bin/bash # Oscam Watchdog & Nagios Script # ------------------------------- # you can share the script where and as much as you want # but leave the copyright & contact header untouched! # if you modify the script you can place your name below # ------------------------------- # Requires: curl (apt-get install curl) # wget is not supported cause a failure with authentication encryption # ------------------------------- # Changelog: # 15.10.12 - Checken von Entitlements hinzugefügt. Bug im ReaderRestart behoben. # 15.10.12 - Commandline Howto angepasst. Version auf 0.4 erhöht. # # # ------------------------------- filename="check_oscam" version="0.4" # Start # Konfiguration: # # Wert welcher per Commandozeile übergeben werden muss, damit der automatische Restart des Readers erfolgt. codeRestart="autorestart" # Anzahl wie oft hintereinander der Reader neugestartet wird bevor abgebrochen wird (falls es danach nicht OK ist) anticount="3" # Log 2 Syslog # "1" Aktiviert # "0" Deaktiviert logsyslog="0" # Debug # "1" Aktiviert # "0" Deaktiviert debug="0" # Temporärer Pfad # für html Seiten & Antiloop Dateien # OHNE abschließendem Slash (/) am Ende temp="/tmp" # Der User welcher das Skript ausführt muss Besitzer der Dateien (status.html) und (readers.html) sein. # (Wenn man das Skript zum Testen mit root ausführt, und später den Cron mit einem anderem User - wird das Skript fehlschlagen, da die Dateien nicht überschrieben werden können.) # Ende # Konfiguration # ------------------------------- howto='\nWarning: Wrong command line arguments. \n Usage: ./check_oscam <hostname> <port> <username> <password> <part> <autorestart> <reader0> <reader1> ... <reader4> <entitlementsReader0> <entitlementsReader1> ... <entitlementsReader4>\n Parts are: statuscheck, restart, entitlement\n Example: ./check_oscam 127.0.0.1 1403 admin "password" statuscheck autorestart hdplus 1702 - Checks the Readers with label hdplus and 1702. If Status != CARDOK the reader will be restarted automatic.\n Example: ./check_oscam 127.0.0.1 1403 admin "password" statuscheck norestart hdplus 1702 - Checks the Readers with label hdplus and 1702. But with no reader restart.\n Example: ./check_oscam 127.0.0.1 1403 admin "password" entitlement norestart hdplus 1702 "" "" "" "1" "10" - Refresh the Entitlements & Checks the Count of Entitlements of reader with label hdplus & 1702 where hdplus should have 1 entitlement and 1702 should have 10 entitlements. - You can specify a maximum of 5 readers. If you specify not 5 you have to place a "" for each missing before starting with the count of entitlements. - In the Example above 2 readers are specified so there are 3 placeholders ("" "" "") required until starting with the count of entitlements. Note: All values are case sensitive! ' if [ "$#" -lt "6" ]; then echo -e "$howto" && exit "3" fi # General strHostname=${1} strPort=${2} strUser=${3} strPass=${4} strpart=${5} strRestart=${6} # Reader strReader0=${7} strReader1=${8} strReader2=${9} strReader3=${10} strReader4=${11} # Entitlements strEnt0=${12} strEnt1=${13} strEnt2=${14} strEnt3=${15} strEnt4=${16} if [ "$strpart" == "entitlement" ] && [ "$strEnt0" == "" ]; then echo -e "$howto" && exit "3" fi # Debugging if [ $debug -eq 1 ] ;then echo -e "Hostname:$strHostname\nPort:$strPort\nUser:$strUser\nPass:******\nPart:$strpart\nRestart:$strRestart\nReader0:$strReader0\nReader1:$strReader1\nReader2:$strReader2\nReader3:$strReader3\nReader4:$strReader4\n---------------------------------\nEntReader0:$strEnt0\nEntReader1:$strEnt1\nEntReader2:$strEnt2\nEntReader3:$strEnt3\nEntReader4:$strEnt4" ; fi # Remove old File rm -f ""$temp"/status.html" statuscheck() { #echo "$strHostname:$strPort" rm -f "$temp"/status.html # File Download a=$(curl --connect-timeout 2 -vu ""$strUser":"$strPass"" --anyauth -o ""$temp"/status.html" "http://$strHostname:$strPort/status.html" >/dev/null 2>/dev/null) if [ $? -eq 0 ] then { lines=$(wc -l ""$temp"/status.html" | awk '{print $1}') if [ $lines -lt 2 ] ; then t=$(tail -n 1 ""$temp"/status.html") echo "CRITICAL: " $t # Logging if [ "$logsyslog" == "1" ] ; then logger "$filename $version - CRITICAL: $t"; fi if [ $debug -eq 1 ] ;then echo -e "Fehler - Status.html hat >2 Zeilen und sieht somit nicht korrekt aus." ; fi exit 2; else { # Datei hat mehr als 1 Zeile - also suche nach Karten c=0 z=0 readerName="strReader" if [ $debug -eq 1 ] ;then echo -e "Beginne Schleife um nach Readern zu suchen.\nc=$c\nz=$z\nreaderName=$readerName" ; fi while [ $z == 0 ] do { # Zähle ReaderNamen hoch readerNameComp="$readerName$c" if [ $debug -eq 1 ] ;then echo -e "Readername = readerNameComp=$readerNameComp\nReadernameREAL = ${!readerNameComp}" ; fi # Suche nach Karte b=$(grep '<TD class="statuscol16">CARDOK <A HREF="entitlements.html?label='${!readerNameComp}'&hideexpired=1"' "$temp"/status.html) if [ $debug -eq 1 ] ;then echo -e "Suchstring="$b"" ; fi if [ $? -eq 0 ] then #Grep erfolgreich - aber noch nicht klar ob string gefunden if [ ! "$b" == "" ] then #echo "Karte - OK" if [ ! "$y" == "NOK" ] ; then y="OK" ; fi e=""$e"OK: ${!readerNameComp} - " if [ $debug -eq 1 ] ;then echo -e "Sieht gut aus - String wurde gefunden. y=$y\ne=$e" ; fi # pattern found # Alles gut - lösche falls vorhanden die Antiloop Datei rm -f ""$temp"/${!readerNameComp}" else # String nicht gefunden, also Karte nicht OK #echo "Karte - Not OK" y=("NOK") e=""$e"CRITICAL: ${!readerNameComp} - " if [ "$codeRestart" == "$strRestart" ] ; then readerRestart ${!readerNameComp} ; fi if [ $debug -eq 1 ] ;then echo -e "Sieht schlecht aus - String nicht gefunden. (1. Abfrage)\ny=$y\ne=$e" ; fi fi else #echo "Karte - Not OK" y=("NOK") e=""$e"CRITICAL: ${!readerNameComp} - " if [ "$codeRestart" == "$strRestart" ] ; then readerRestart ${!readerNameComp} ; fi if [ $debug -eq 1 ] ;then echo -e "Sieht schlecht aus - String nicht gefunden.(2. Abfrage)\ny=$y\ne=$e" ; fi fi # Ende If tempCount=$[$c + 1] if [ $debug -eq 1 ] ;then echo -e "Zähle hoch - c=$c & tempCount=$tempCount" ; fi readerNameComp="$readerName$tempCount" if [ $debug -eq 1 ] ;then echo -e "Demnach ist der nächste Reader: $readerNameComp\nDas sollte sein:${!readerNameComp}" ; fi if [ "${!readerNameComp}" == "" ] ; then z=$[$z + 1] if [ $debug -eq 1 ] ;then echo -e "So der nächste Reader ist leer also Abbruch der Schleife. z=$z" ; fi else # Zähle Reader hoch c=$[$c + 1] if [ $debug -eq 1 ] ;then echo -e "Ende Schleifendurchlauf. Zähle Reader hoch. c=$c" ; fi fi } done if [ "$y" == 'OK' ] ; then echo "$e" # Logging if [ "$logsyslog" == "1" ] ; then logger "$filename $version - $e"; fi if [ $debug -eq 1 ] ;then echo -e "Fertig - Y = OK - also Exit 0." ; fi exit 0 fi if [ "$y" == "NOK" ] ; then echo "$e" # Logging if [ "$logsyslog" == "1" ] ; then logger "$filename $version - $e"; fi if [ $debug -eq 1 ] ;then echo -e "Fertig - Y = NOK - also Exit 2." ; fi exit 2 fi } fi } else { echo "CRITICAL: Could not download status.html (StatusCheck)" # Logging if [ "$logsyslog" == "1" ] ; then logger "$filename $version - CRITICAL: Could not download status.html (StatusCheck)"; fi exit 2; } fi } readerRestart() { if [ $debug -eq 1 ] ;then echo -e "Beginne Restart Funktion - Übergabewert (Readername) ist: $1" ; fi # echo "$strHostname:$strPort" rm -f ""$temp"/readers.html" # Check das Loop nicht Größer X ist - damit die Reader nicht dauernd neu gestartet werden r=$(cat "$temp/$1" 2>/dev/null) if [ ""$r"" == "" ] ; then r=0 ;if [ $debug -eq 1 ] ;then echo -e "Temp. Restartdatei existiert noch nicht. r=$r" ; fi; fi if [ "$r" -ge "$anticount" ] ; then echo "CRITICAL: Nach $r Versuchen kein Reader restart mehr." # Logging if [ "$logsyslog" == "1" ] ; then logger "$filename $version - CRITICAL: Nach $r Versuchen kein Reader restart mehr."; fi # if [ $debug -eq 1 ] ;then echo -e "$filename $version - CRITICAL: Nach $r Versuchen kein Reader restart mehr." ; fi # Deaktiviert, da sonst das Skript komplett beendet wird # exit 2; else # File Download if [ $debug -eq 1 ] ;then echo -e "readers.html wird runtergeladen - bzw. befehl disable befehl ausgeführt" ; fi g=$(curl --connect-timeout 2 -vu ""$strUser":"$strPass"" --anyauth -o ""$temp"/readers.html" "http://$strHostname:$strPort/readers.html?label=$1&action=disable" >/dev/null 2>/dev/null) if [ $? -eq 0 ] then # Antiloop hochzählen echo $[$r+1] > "$temp/$1" # Zähle Zeilen um Fehler fest zu stellen ( Datei muss <1 Zeile sein ) lines=$(wc -l ""$temp"/readers.html" | awk '{print $1}') if [ $lines -lt 2 ] ; then s=$(tail -n 1 ""$temp"/readers.html") echo "CRITICAL: " $s # Logging if [ "$logsyslog" == "1" ] ; then logger "$filename $version - CRITICAL: $s"; fi # Deaktiviert, da sonst das Skript komplett beendet wird # exit 2; else # Datei hat mehr als 1 Zeile - also suche nach Karten echo "WARNING: Reader $1 disabled" # Aufräumen vor dem aktivieren rm -f ""$temp"/readers.html" lines="" s="" f=$(curl --connect-timeout 2 -vu ""$strUser":"$strPass"" --anyauth -o ""$temp"/readers.html" "http://$strHostname:$strPort/readers.html?label=$1&action=enable" >/dev/null 2>/dev/null) if [ $? -eq 0 ] then lines=$(wc -l ""$temp"/readers.html" | awk '{print $1}') if [ $lines -lt 2 ] ; then s=$(tail -n 1 ""$temp"/readers.html") echo "CRITICAL: " $s # Logging if [ "$logsyslog" == "1" ] ; then logger "$filename $version - CRITICAL: $s"; fi # Deaktiviert, da sonst das Skript komplett beendet wird # exit 2; else # Datei hat mehr als 1 Zeile - also suche nach Karten echo "WARNING: Reader $1 enabled" #sleep 10 #statuscheck fi fi fi else echo "CRITICAL: Could not download readers.html (ReaderRestart)" # Logging if [ "$logsyslog" == "1" ] ; then logger "$filename $version - CRITICAL: Could not download readers.html (ReaderRestart)"; fi # Deaktiviert, da sonst das Skript komplett beendet wird # exit 2; fi fi # ------------------------------------------------------------------- } entitlement() { #echo "$strHostname:$strPort" y="" e="" rm -f "$temp"/status.html # Aktualisiere Entitlements if [ ! "$strReader0" == "" ] ; then refreshEntitlement $strReader0 ; fi if [ ! "$strReader1" == "" ] ; then refreshEntitlement $strReader1 ; fi if [ ! "$strReader2" == "" ] ; then refreshEntitlement $strReader2 ; fi if [ ! "$strReader3" == "" ] ; then refreshEntitlement $strReader3 ; fi if [ ! "$strReader4" == "" ] ; then refreshEntitlement $strReader4 ; fi # File Download a=$(curl --connect-timeout 2 -vu ""$strUser":"$strPass"" --anyauth -o ""$temp"/status.html" "http://$strHostname:$strPort/status.html" >/dev/null 2>/dev/null) if [ $? -eq 0 ] then { lines=$(wc -l ""$temp"/status.html" | awk '{print $1}') if [ $lines -lt 2 ] ; then t=$(tail -n 1 ""$temp"/status.html") echo "CRITICAL: " $t # Logging if [ "$logsyslog" == "1" ] ; then logger "$filename $version - CRITICAL: $t"; fi if [ $debug -eq 1 ] ;then echo -e "Fehler - Status.html hat >2 Zeilen und sieht somit nicht korrekt aus." ; fi exit 2; else { # Datei hat mehr als 1 Zeile - also suche nach Entitlements c=0 z=0 readerName="strReader" entName="strEnt" if [ $debug -eq 1 ] ;then echo -e "Beginne Schleife um nach Entitlements zu suchen.\nc=$c\nz=$z\nreaderName=$readerName\nEntitleName=$entName" ; fi while [ $z == 0 ] do { # Zähle Reader/Entitlement hoch readerNameComp="$readerName$c" entNameComp="$entName$c" if [ $debug -eq 1 ] ;then echo -e "Readername = readerNameComp="$readerNameComp"\nReadernameREAL="${!readerNameComp}"\nEntitleName="$entNameComp"\nEntitlementREAL="${!entNameComp}"" ; fi # Checke ob 1 oder mehr Entitlements if [ "${!entNameComp}" -eq "1" ] ;then entitleCount="entitlement" else entitleCount="entitlements" fi # Suche nach Entitlement b=$(grep '<A HREF="entitlements.html?label='${!readerNameComp}'&hideexpired=1" class="tooltip">('${!entNameComp}' '$entitleCount')<SPAN>' "$temp"/status.html) if [ $debug -eq 1 ] ;then echo -e "Suchstring="$b"" ; fi if [ $? -eq 0 ] then #Grep erfolgreich - aber noch nicht klar ob string gefunden if [ ! "$b" == "" ] then #echo "Entitlement - OK" if [ ! "$y" == "NOK" ] ; then y="OK" ; fi e=""$e"OK: ${!readerNameComp} ("${!entNameComp}" $entitleCount) - " if [ $debug -eq 1 ] ;then echo -e "Sieht gut aus - String wurde gefunden. y=$y\ne=$e" ; fi else # String nicht gefunden, also Entitlements nicht OK y=("NOK") e=""$e"CRITICAL: ${!readerNameComp} (${!entNameComp} $entitleCount) - " if [ $debug -eq 1 ] ;then echo -e "Sieht schlecht aus - String nicht gefunden. (1. Abfrage)\ny=$y\ne=$e" ; fi fi else #echo "Karte - Not OK" y=("NOK") e=""$e"CRITICAL: ${!readerNameComp} (${!entNameComp} $entitleCount) - " if [ $debug -eq 1 ] ;then echo -e "Sieht schlecht aus - String nicht gefunden.(2. Abfrage)\ny=$y\ne=$e" ; fi fi # Ende If tempCount=$[$c + 1] if [ $debug -eq 1 ] ;then echo -e "Zähle hoch - c=$c & tempCount=$tempCount" ; fi readerNameComp="$readerName$tempCount" entNameComp="$entName$tempCount" if [ $debug -eq 1 ] ;then echo -e "Demnach ist der nächste Reader: $readerNameComp\nDas sollte sein:${!readerNameComp}" ; fi if [ "${!readerNameComp}" == "" -o "${!entNameComp}" == "" ] ; then z=$[$z + 1] if [ $debug -eq 1 ] ;then echo -e "So der nächste Reader ist leer also Abbruch der Schleife. z=$z" ; fi else # Zähle Reader hoch c=$[$c + 1] if [ $debug -eq 1 ] ;then echo -e "Ende Schleifendurchlauf. Zähle Reader hoch. c=$c" ; fi fi } done if [ "$y" == 'OK' ] ; then echo "$e" # Logging if [ "$logsyslog" == "1" ] ; then logger "$filename $version - $e"; fi if [ $debug -eq 1 ] ;then echo -e "Fertig - Y = OK - also Exit 0." ; fi exit 0 fi if [ "$y" == "NOK" ] ; then echo "$e" # Logging if [ "$logsyslog" == "1" ] ; then logger "$filename $version - $e"; fi if [ $debug -eq 1 ] ;then echo -e "Fertig - Y = NOK - also Exit 2." ; fi exit 2 fi } fi } else { echo "CRITICAL: Could not download status.html (StatusCheck)" # Logging if [ "$logsyslog" == "1" ] ; then logger "$filename $version - CRITICAL: Could not download status.html (StatusCheck)"; fi exit 2; } fi } refreshEntitlement() { if [ $debug -eq 1 ] ;then echo -e "Beginne Restart Entitlement Funktion" ; fi # echo "$strHostname:$strPort" rm -f ""$temp"/readers.html" # File Download if [ $debug -eq 1 ] ;then echo -e "readers.html wird runtergeladen - bzw. refresh befehl ausgeführt" ; fi g=$(curl --connect-timeout 2 -vu ""$strUser":"$strPass"" --anyauth -o ""$temp"/readers.html" "http://$strHostname:$strPort/readers.html?label=$1&action=reread" >/dev/null 2>/dev/null) if [ $? -eq 0 ] then # Antiloop hochzählen echo $[$r+1] > "$temp/$1" # Zähle Zeilen um Fehler fest zu stellen ( Datei muss <1 Zeile sein ) lines=$(wc -l ""$temp"/readers.html" | awk '{print $1}') if [ $lines -lt 2 ] ; then s=$(tail -n 1 ""$temp"/readers.html") echo "CRITICAL: " $s # Logging if [ "$logsyslog" == "1" ] ; then logger "$filename $version - CRITICAL: $s"; fi # Deaktiviert, da sonst das Skript komplett beendet wird # exit 2; else if [ $debug -eq 1 ] ;then echo "OK: Entitlement Refresh" ; fi if [ "$logsyslog" == "1" ] ; then logger "$filename $version - OK: Entitlement Refresh von $1"; fi fi else echo "CRITICAL: Could not download readers.html (ReaderRefresh)" # Logging if [ "$logsyslog" == "1" ] ; then logger "$filename $version - CRITICAL: Could not download readers.html (ReaderRefresh)"; fi # Deaktiviert, da sonst das Skript komplett beendet wird # exit 2; fi # ------------------------------------------------------------------- } if [ "$strpart" == "statuscheck" ]; then statuscheck elif [ "$strpart" == "entitlement" ]; then entitlement elif [ "$strpart" == "restart" ]; then c=0 z=0 readerName="strReader" if [ $debug -eq 1 ] ;then echo -e "Beginne Schleife um nach Readern zu suchen.\nc=$c\nz=$z\nreaderName=$readerName" ; fi while [ $z == 0 ] do { # Zähle ReaderNamen hoch readerNameComp="$readerName$c" if [ $debug -eq 1 ] ;then echo -e "Readername = readerNameComp=$readerNameComp\nReadernameREAL = ${!readerNameComp}" ; fi # Code readerRestart ${!readerNameComp} # Count up tempCount=$[$c + 1] if [ $debug -eq 1 ] ;then echo -e "Zähle hoch - c=$c & tempCount=$tempCount" ; fi readerNameComp="$readerName$tempCount" if [ $debug -eq 1 ] ;then echo -e "Demnach ist der nächste Reader: $readerNameComp\nDas sollte sein:${!readerNameComp}" ; fi if [ "${!readerNameComp}" == "" ] ; then z=$[$z + 1] if [ $debug -eq 1 ] ;then echo -e "So der nächste Reader ist leer also Abbruch der Schleife. z=$z" ; fi else # Zähle Reader hoch c=$[$c + 1] if [ $debug -eq 1 ] ;then echo -e "Ende Schleifendurchlauf. Zähle Reader hoch. c=$c" ; fi fi } done fi # Ende Skript # Oscam Watchdog & Nagios Script # -------------------------------
Links
https://www.digital-eliteboard.com/threads/oscam-reader-ueberwachen.240969/
Receiver
Startscript ermitteln
Der Befehl
ls -la /etc/init.d/
sollte einen Eintrag wie folgenden ausgeben:
current_cam.sh -> /usr/script/oscam_1.20-unstable-11682_cam.sh
PID ermitteln
pidof oscam_1.20-unstable-11682
Script
vi /usr/script/oscam_watchdog.sh
VTi Team Image (Receiver Hofstetten):
#!/bin/bash PID=$(pidof oscam_1.20-unstable-11682) if (kill -0 $PID 2>/dev/null) then echo "it's alive!" > /dev/null else /usr/script/oscam_1.20-unstable-11682_cam.sh restart > /dev/null echo "OSCAM restarted at `date +"'%d.%m.%Y %H:%M'"`" >> /tmp/crash-log-oscam.log echo -e "OSCAM of receiver-hofstetten restarted at `date +"'%d.%m.%Y %H:%M'"`" | /usr/bin/mailx -A kirner.or.at -s "OSCAM of receiver-hofstetten restarted" admin@kirner.or.at fi
OpenATV (Receiver Seestadt):
#!/bin/bash PID=$(pidof /usr/bin/oscam-trunk) if (kill -0 $PID 2>/dev/null) then echo "it's alive!" > /dev/null else /etc/init.d/softcam.oscam-trunk restart > /dev/null echo "OSCAM restarted at `date +"'%d.%m.%Y %H:%M'"`" >> /tmp/crash-log-oscam.log echo -e "OSCAM of receiver-seestadt restarted at `date +"'%d.%m.%Y %H:%M'"`" | /usr/bin/mailx -A kirner.or.at -s "OSCAM of receiver-seestadt restarted" admin@kirner.or.at fi
chmod +x /usr/script/oscam_watchdog.sh
Init-Script erstellen
ln -sf /usr/script/oscam_watchdog.sh /etc/init.d/oscam_watchdog.sh
cd /etc/rc3.d/ ln -sf ../init.d/oscam_watchdog.sh /etc/rc3.d/S99oscam_watchdog.sh
Links
http://raspberry.tips/raspberrypi-tutorials/oscam-watchdog-fuer-den-raspberry-pi/
Zurück zu OSCam_(Raspberry), OSCam_(Satreceiver)