]> git.hoellein.online Git - skripte/commitdiff
noch ein paar mehr skripte
authorroot <root@hoellein.at>
Thu, 26 May 2016 12:45:12 +0000 (14:45 +0200)
committerroot <root@hoellein.at>
Thu, 26 May 2016 12:45:12 +0000 (14:45 +0200)
CPU-Temperatur/sensor_action.sh [new file with mode: 0755]
backup-ccu2.bash.lastrun [new file with mode: 0644]
checkInternet/checkInternet.sh [new file with mode: 0755]
homematic/backup-ccu/backup-ccu2.bash [new file with mode: 0755]
homematic/create_klima.sh [new file with mode: 0755]
homematic/homematic.awk [new file with mode: 0755]
homematic/homematic.sh [new file with mode: 0755]
memory.sh [new file with mode: 0755]
mysql/backup_mysqldb.sh [new file with mode: 0755]
pingservice.sh [new file with mode: 0755]
rsnapshot/rsnapshot.sh [new file with mode: 0755]

diff --git a/CPU-Temperatur/sensor_action.sh b/CPU-Temperatur/sensor_action.sh
new file mode 100755 (executable)
index 0000000..919ec0e
--- /dev/null
@@ -0,0 +1,34 @@
+#!/bin/sh
+export LANG=C
+
+### Temperature Monitor
+
+## Define threshold for CPU Temperature
+# AMD Opteron(tm) Processor 4376 HE
+# http://products.amd.com/en-us/OpteronCPUDetail.aspx?id=828
+# Max Temps (C) 68°C
+threshold=90
+
+## Get CPU Temperatur using lm-sensor
+t1=$(/usr/bin/sensors | awk '/temp1:/ {print substr($2,2,2)}' | head -1)
+
+if test $t1 -gt $threshold 
+then
+    cat <<- MAILTEXTEND | mail -s "$(hostname): CPU Temperatur zu hoch" -c mhoellein@moellein.homeip.net 
+
+    Der $(hostname) hat Hitzeprobleme und wird in 5min heruntergefahren!
+
+    Aktuell hat
+    Temp1: $t1
+
+    Das kann durch ein "ssh root@$(hostname) '/sbin/shutdown -c'" verhindert werden.
+
+    Diese Nachricht wurde automatisch generiert.
+MAILTEXTEND
+/sbin/shutdown -h +5 "CPU Temperature is exceeding threshold - Shutting down" &
+fi
+
+echo "$(date) $t1 $t2 $t3 $t4" >> /var/log/sensors.dat
+# altes Script:
+#echo -n "DATE:" >> sensors.dat && date "+%Y%m%d%H%M%S" >> sensors.dat && uptime >> sensors.dat && sensors >> sensors.dat
+
diff --git a/backup-ccu2.bash.lastrun b/backup-ccu2.bash.lastrun
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/checkInternet/checkInternet.sh b/checkInternet/checkInternet.sh
new file mode 100755 (executable)
index 0000000..4305288
--- /dev/null
@@ -0,0 +1,65 @@
+#/bin/bash
+DEBUG=0
+ADRESS_TO_PING="www.google.de"
+PING='ping -c 1 -q'
+DATE=`date +%Y%m%d_%H:%M:%S`
+MYLOG='/var/log/checkInternet.log'
+
+PINGOK=0
+
+DEBUG() {
+       if [ $DEBUG -eq 1 ]; then
+               echo $1
+       fi
+}
+doOnePing() {
+       adress=${1-${ADRESS_TO_PING}}
+       DEBUG "${PING} ${adress}"
+       ${PING} $adress 2>&1 > /dev/null
+       CONNECTION_STATUS=$?
+}
+
+doPings() {
+       for ping in 1 2 3 4 5 6
+       do
+               doOnePing
+               if [ $CONNECTION_STATUS -ne 0 ]; then
+                       echo "ping not ok"
+               else
+                       PINGOK=$((PINGOK+1))            
+               fi
+       done
+}
+
+doPingsWithWaits() {
+       for ping in 1 2 3 4 5 6
+       do
+               sleep 5
+               doOnePing www.heise.de
+               if [ $CONNECTION_STATUS -ne 0 ]; then
+                       echo "ping not ok"
+               else
+                       PINGOK=$((PINGOK+1))            
+               fi
+       done
+}
+
+doPings
+
+#zweiter Versuch, wenn wieder nicht, dann modem aus und wieder an
+if [ $PINGOK -lt 4 ]
+then
+       echo "${DATE}: erster Versuch ging schief, nochmaliger Versuch" >> $MYLOG
+       sleep 5
+       PINGOK=0
+       doPingsWithWaits
+fi
+
+if [ $PINGOK -lt 4 ]
+then
+       echo "${DATE}: schalte Kabelmodem aus und wieder ein" >> $MYLOG
+       ssh -p 1194 root@fritz.box sispmctl -f1  
+       sleep 5
+       ssh -p 1194 root@fritz.box sispmctl -o1
+fi
+doPingsWithWaits
diff --git a/homematic/backup-ccu/backup-ccu2.bash b/homematic/backup-ccu/backup-ccu2.bash
new file mode 100755 (executable)
index 0000000..1fe8db3
--- /dev/null
@@ -0,0 +1,48 @@
+#!/bin/bash
+# http://www.lxccu.com/manuals:manual-ccu2-backup
+# Homematic CCU Backup Script fuer crontab
+# Erstellt am 13.03.11 von Danny B.
+# Angepasst am 05.08.14 von PaulG4H
+
+# Parameter
+backupdir="/home/backup/ccu2"
+host="192.168.178.20"
+user="backup"
+password="XXXYYYZZZ"
+
+# Voraussetzung: Nutzer hinzufügen, Passwort setzen, Typ Administrator
+#
+# crontab Zeile alle 2 Stunden prüfen ob das letzte Backup älter als 2 Tage
+# ist, dann Backup; es werden max 5 Backups aufgehoben:
+# 0 0-23/2 * * * mario [ -z "$(find /home/backup/ -name homematic-ccu2\* -mtime -2)" ] && ping -q -n -c 1 -W 1 192.168.178.20 > /dev/null 2>&1 && bin/backup-ccu2.bash && find /home/backup/ -name homematic-ccu2\* | sort -r | sed -e "1,5d" | xargs -r rm -f ; true
+
+############### Es sind keine weiteren Anpassung ab hier Notwendig
+
+SCRIPTNAME=${0##*/}
+
+[ -d run ] && cd run
+
+run=$SCRIPTNAME.lastrun
+
+# Homematic Login
+wget --post-data '{"method":"Session.login","params":{"username":"'$user'","password":"'$password'"}}' http://$host/api/homematic.cgi -O hm.login.response -q >$run 2>&1
+
+# Login-Pruefung
+loginerror=`cat hm.login.response|cut -d "," -f3|awk '{print $2}'`
+if [ "$loginerror" != "null}" ]; then
+   echo "Fehler beim Homematic-Login !"|tee -a $run
+   cat hm.login.response|grep message|cut -d '"' -f4|tee -a $run
+   exit 1
+fi
+sessionid=`cat hm.login.response|cut -d "," -f2|awk '{print $2}'|cut -d '"' -f2`
+
+# Backupdatei herunterladen
+wget "http://$host/config/cp_security.cgi?sid=@$sessionid@&action=create_backup" -O $backupdir/homematic-ccu2-$host-$(date +%Y%m%d).tar.sbk -q >>$run 2>&1
+
+# Homematic Logout
+wget --post-data '{"method":"Session.logout","params":{"_session_id_":"'$sessionid'"}}' http://$host/api/homematic.cgi -O hm.logout.response -q >>$run 2>&1
+
+# temp. Dateien loeschen
+
+rm hm.login.response hm.logout.response >>$run 2>&1
+
diff --git a/homematic/create_klima.sh b/homematic/create_klima.sh
new file mode 100755 (executable)
index 0000000..d87f22a
--- /dev/null
@@ -0,0 +1,114 @@
+#!/bin/bash
+
+DEBUG=0
+credentials='--user=homematic --password=AdN:homematic --database=homematic --silent'
+MYSQL="/usr/bin/mysql"
+ID=$$
+TMPDIR="/tmp"
+TMPFILE=/tmp/`basename $0`
+
+GNUPLOT="/usr/bin/gnuplot"
+PLOT_TEMP_FULL=${TMPDIR}/temp.plt
+DATE=$(date +%Y-%m)
+TODAY=$(date +%Y-%m-%d)
+getData () {
+       echo "use homematic;SELECT time, temperature, humidity from klima where position='${1}' order by time" | $MYSQL $credentials > $TMPDIR/klima_daten
+       echo "use homematic;SELECT time, temperature, humidity from klima where position='${1}' and time >= \"${DATE}-01\" and time <= \"${DATE}-31\" order by time" | $MYSQL $credentials > $TMPDIR/klima_daten_month
+       echo "use homematic;SELECT time, temperature, humidity from klima where position='${1}' and time >= \"${TODAY}\" order by time" | $MYSQL $credentials > $TMPDIR/klima_daten_day
+       if [ $DEBUG -eq 1 ]; then
+               echo "use homematic;SELECT time, temperature, humidity from klima where position='${1}' and time >= \"${TODAY}\" order by time"
+       fi
+}
+if [ $DEBUG -eq 1 ]; then
+       getData Terrasse
+       exit 0
+fi
+createPLT () {
+       cat > ${PLOT_TEMP_FULL} <<EOF
+reset
+set term png size 1280,1024
+set output "${1}.png"
+set datafile separator "\t"
+set timefmt "%Y-%m-%d %H:%M:%S"
+set xdata time
+set ylabel "Temperatur °C"
+set xlabel "Datum"
+plot "klima_daten" using 1:2 notitle with line
+EOF
+}
+
+createPLTMonth () {
+       cat > ${PLOT_TEMP_FULL} <<EOF
+reset
+set term png size 1280,1024
+set output "${1}.png"
+set datafile separator "\t"
+set timefmt "%Y-%m-%d %H:%M:%S"
+set xdata time
+set ylabel "Temperatur °C"
+set xlabel "Datum"
+plot "klima_daten_month" using 1:2 notitle with line
+EOF
+}
+
+createPLTDay () {
+       cat > ${PLOT_TEMP_FULL} <<EOF
+reset
+set term png size 1280,1024
+set output "${1}.png"
+set datafile separator "\t"
+set timefmt "%Y-%m-%d %H:%M:%S"
+set xdata time
+set ylabel "Temperatur °C"
+set xlabel "Zeit"
+plot "klima_daten_day" using 1:2 notitle with line
+EOF
+}
+
+createDiag () {
+       pushd $TMPDIR
+       $GNUPLOT ${PLOT_TEMP_FULL} 
+       popd
+}
+
+move () {
+       pushd $TMPDIR
+       mv -f ${1}.png /var/www/ && chown www-data.www-data /var/www/${1}.png
+       popd
+}
+
+getData Terrasse
+#createPLT Terrasse
+#createDiag Terrasse
+createPLTMonth TerrasseMonth
+createDiag TerrasseMonth
+createPLTDay TerrasseDay
+createDiag TerrasseDay
+#move Terrasse
+move TerrasseMonth
+move TerrasseDay
+
+getData Wohnzimmer
+createPLT Wohnzimmer
+createDiag Wohnzimmer
+move Wohnzimmer
+
+getData Bad
+createPLT Bad
+createDiag Bad
+move Bad
+
+getData Flur_OG
+createPLT Flur_OG
+createDiag Flur_OG
+move Flur_OG
+
+getData Kueche
+createPLT Kueche
+createDiag Kueche
+move Kueche
+
+getData Arbeitszimmer
+createPLT Arbeitszimmer
+createDiag Arbeitszimmer
+move Arbeitszimmer
diff --git a/homematic/homematic.awk b/homematic/homematic.awk
new file mode 100755 (executable)
index 0000000..69b8e8b
--- /dev/null
@@ -0,0 +1,12 @@
+#!/usr/bin/awk -f
+
+BEGIN {
+       FS=""
+       RS=">"
+}
+
+{
+if (match ($0, "name='([^']*)'.*variable='([^']*)'", result) > 0) {
+       print (result[1] "\t" result[2])
+}
+}
diff --git a/homematic/homematic.sh b/homematic/homematic.sh
new file mode 100755 (executable)
index 0000000..2ac84e2
--- /dev/null
@@ -0,0 +1,43 @@
+#!/bin/bash
+
+WRITETODB=1;
+ECHOVALUES=0;
+# Parameter
+credentials='--user=homematic --password=AdN:homematic --database=homematic --silent'
+filename=/tmp/output
+tablename=/tmp/table
+homematic=homematic
+#Arbeitszimmer
+TEMP_AZ=`wget http://homematic/config/xmlapi/state.cgi?datapoint_id=3844 -qO-|xmlstarlet sel -t -m "//datapoint" -v "@value"`
+FEU_AZ=`wget http://homematic/config/xmlapi/state.cgi?datapoint_id=3843 -qO-|xmlstarlet sel -t -m "//datapoint" -v "@value"`
+#Flur_OG
+TEMP_FOG=`wget http://homematic/config/xmlapi/state.cgi?datapoint_id=3815 -qO-|xmlstarlet sel -t -m "//datapoint" -v "@value"`
+FEU_FOG=`wget http://homematic/config/xmlapi/state.cgi?datapoint_id=3814 -qO-|xmlstarlet sel -t -m "//datapoint" -v "@value"`
+#Wohnzimmer
+TEMP_WZ=`wget http://homematic/config/xmlapi/state.cgi?datapoint_id=1429 -qO-|xmlstarlet sel -t -m "//datapoint" -v "@value"`
+FEU_WZ=`wget http://homematic/config/xmlapi/state.cgi?datapoint_id=1428 -qO-|xmlstarlet sel -t -m "//datapoint" -v "@value"`
+#Bad
+TEMP_BAD=`wget http://homematic/config/xmlapi/state.cgi?datapoint_id=1461 -qO-|xmlstarlet sel -t -m "//datapoint" -v "@value"`
+FEU_BAD=`wget http://homematic/config/xmlapi/state.cgi?datapoint_id=1460 -qO-|xmlstarlet sel -t -m "//datapoint" -v "@value"`
+#Kueche
+TEMP_KUECHE=`wget http://homematic/config/xmlapi/state.cgi?datapoint_id=3728 -qO-|xmlstarlet sel -t -m "//datapoint" -v "@value"`
+FEU_KUECHE=`wget http://homematic/config/xmlapi/state.cgi?datapoint_id=3727 -qO-|xmlstarlet sel -t -m "//datapoint" -v "@value"`
+#Temperatur/Feuchte Terrasse
+TEMP_TER=`wget http://homematic/config/xmlapi/state.cgi?datapoint_id=2886 -qO-|xmlstarlet sel -t -m "//datapoint" -v "@value"`
+FEU_TER=`wget http://homematic/config/xmlapi/state.cgi?datapoint_id=2885 -qO-|xmlstarlet sel -t -m "//datapoint" -v "@value"`
+if [ $ECHOVALUES -eq 1 ];then
+       echo -e "Temp. Arbeitszimmer=$TEMP_AZ°C\tLuftfeuchte=$FEU_AZ%"
+       echo -e "Temp. Flur_OG=$TEMP_FOG°C\tLuftfeuchte=$FEU_FOG%"
+       echo -e "Temp. Wohnzimmer=$TEMP_WZ°C\tLuftfeuchte=$FEU_WZ%"
+       echo -e "Temp. Bad=$TEMP_BAD°C\tLuftfeuchte=$FEU_BAD%"
+       echo -e "Temp. Kueche=$TEMP_KUECHE°C\tLuftfeuchte=$FEU_KUECHE%"
+       echo -e "Temp. Terrasse=$TEMP_TER°C\tLuftfeuchte=$FEU_TER%"
+fi
+if [ $WRITETODB -eq 1 ]; then
+       mysql $credentials --execute='INSERT INTO `klima` (`time`, position, temperature, humidity) values (now(), "Wohnzimmer", '"$TEMP_WZ"', '"$FEU_WZ"');'
+       mysql $credentials --execute='INSERT INTO `klima` (`time`, position, temperature, humidity) values (now(), "Arbeitszimmer", '"$TEMP_AZ"', '"$FEU_AZ"');'
+       mysql $credentials --execute='INSERT INTO `klima` (`time`, position, temperature, humidity) values (now(), "Flur_OG", '"$TEMP_FOG"', '"$FEU_FOG"');'
+       mysql $credentials --execute='INSERT INTO `klima` (`time`, position, temperature, humidity) values (now(), "Kueche", '"$TEMP_KUECHE"', '"$FEU_KUECHE"');'
+       mysql $credentials --execute='INSERT INTO `klima` (`time`, position, temperature, humidity) values (now(), "Bad", '"$TEMP_BAD"', '"$FEU_BAD"');'
+       mysql $credentials --execute='INSERT INTO `klima` (`time`, position, temperature, humidity) values (now(), "Terrasse", '"$TEMP_TER"', '"$FEU_TER"');'
+fi
diff --git a/memory.sh b/memory.sh
new file mode 100755 (executable)
index 0000000..670aa6c
--- /dev/null
+++ b/memory.sh
@@ -0,0 +1,12 @@
+#!/bin/bash
+#prints PID total MEMORY and the COMMAND in a table
+
+/usr/bin/printf "%-6s %-9s %s\n" "PID" "Total" "Command"
+/usr/bin/printf "%-6s %-9s %s\n" "---" "-----" "-------"
+
+for PID in `/bin/ps -e | /usr/bin/awk '$1 ~ /[0-9]+/ { print $1 }'`
+do
+   CMD=`/bin/ps -o comm -p ${PID} | /usr/bin/tail -1`
+   TOTAL=`/usr/bin/pmap ${PID} 2>/dev/null | /usr/bin/tail -1 | /usr/bin/awk '{ print $2 }'`
+   [ -n "${TOTAL}" ] && /usr/bin/printf "%-6s %-9s %s\n" "${PID}" "${TOTAL}"  "${CMD}"
+done | /usr/bin/sort -n -k2
diff --git a/mysql/backup_mysqldb.sh b/mysql/backup_mysqldb.sh
new file mode 100755 (executable)
index 0000000..23604fe
--- /dev/null
@@ -0,0 +1,5 @@
+#!/bin/bash
+/usr/bin/mysqldump --no-defaults -u backup vmail > mysqldump_vmail.sql
+/usr/bin/mysqldump --no-defaults -u backup tine20 > mysqldump_tine20.sql
+/usr/bin/mysqldump --no-defaults -u backup homematic > mysqldump_homematic.sql
+#/usr/bin/mysqldump --no-defaults --event -u backup --all-databases > mysqldump_all_databases.sql
diff --git a/pingservice.sh b/pingservice.sh
new file mode 100755 (executable)
index 0000000..84c1bfd
--- /dev/null
@@ -0,0 +1,33 @@
+#!/bin/bash
+LOGFILE=/var/log/ping.log
+PINGHOST=8.8.8.8
+i=0
+while true; do
+       if [ $((i%2)) -eq 0 ];then
+               PINGHOST=8.8.8.8
+       else
+               PINGHOST=www.google.de
+       fi
+       ((i++))
+        ping -c 1 -w 1 -W 1 $PINGHOST > /dev/null
+        if [[ $? -eq 0 ]]; then
+            echo $(date) $PINGHOST "1" | tee -a $LOGFILE
+        else
+            echo $(date) $PINGHOST "0" | tee -a $LOGFILE
+        fi
+       PINGHOST=langateway
+       ping -c 1 -w 1 -W 1  $PINGHOST > /dev/null
+        if [[ $? -eq 0 ]]; then
+            echo $(date) $PINGHOST "1" | tee -a $LOGFILE
+        else
+            echo $(date) $PINGHOST "0" | tee -a $LOGFILE
+        fi
+       PINGHOST=langateway2
+       ping -c 1 -w 1 -W 1  $PINGHOST > /dev/null
+        if [[ $? -eq 0 ]]; then
+            echo $(date) $PINGHOST "1" | tee -a $LOGFILE
+        else
+            echo $(date) $PINGHOST "0" | tee -a $LOGFILE
+        fi
+        sleep 5
+done
diff --git a/rsnapshot/rsnapshot.sh b/rsnapshot/rsnapshot.sh
new file mode 100755 (executable)
index 0000000..f6c770d
--- /dev/null
@@ -0,0 +1,32 @@
+#!/bin/bash
+
+
+if [ "$1" != "" ];then
+       backuplevel=$1
+else
+       backuplevel=hourly
+fi
+
+#0 - off
+#1 - on
+echo `date`
+state=`/usr/bin/sispmctl -ng4|tail -n1|sed -e "s/Status of outlet 4://" -e "s/\t//"`
+
+if [ $state -eq 0 ]; then
+       echo "switching outlet 4 on"
+       /usr/bin/sispmctl -o4
+       sleep 25
+fi
+
+#device is mounted or not
+if [ $(mount |grep -c 500GB) -ne 1 ]; then
+       echo "mounting /dev/disk/by-id/usb-ST350063_0AS_ST3500630A_9QG71JJF-0:0-part1 /media/500GB"
+       mount /dev/disk/by-id/usb-ST350063_0AS_ST3500630A_9QG71JJF-0:0-part1 /media/500GB
+       sleep 10
+fi
+
+echo "doing: /usr/bin/rsnapshot -c /etc/rsnapshot.conf.homeserver $backuplevel"
+/usr/bin/rsnapshot -c /etc/rsnapshot.conf.homeserver $backuplevel 
+
+echo "doing: umount /media/500GB && sispmctl -f4"
+umount /media/500GB && /usr/bin/sispmctl -f4