Script for monitor porteus folder

For discussions about programming and projects not necessarily associated with Porteus.
luko
White ninja
White ninja
Posts: 16
Joined: 05 Oct 2015, 13:58
Distribution: Porteus, Lubuntu
Location: Slovakia

Script for monitor porteus folder

Post#1 by luko » 30 Jun 2017, 11:40

My script for monitor folders in porteus: base,modules,optional and /bin folder

I use it for my good feeling :D

Code: Select all

#!/bin/sh

# porteusGuard - porteus directory guard
#
# Copyright (c) 2017 Veselovsky <lukves at gmail.com>
# This software is licensed under the GPL v2 or later.

### BEGIN INIT INFO
# Provides:          porteusGuard
# Required-Start:
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: porteusGuard start/stop script
# Description:       Initialize porteusGuard
### END INIT INFO

MD=$(md5sum /etc/rc.d/rc.porteusGuard)

#
# all tests
#
# Try n^x and when files changed in base,modules,optional, 
# or /bin then do halt. disconnect wlan0 a eth0, too..
#
# this utility create a 128m ramdisk for store
# listing files
#
prepare_all_tests() {
declare -i TIME

TIME=1

i=0

mkdir /mnt/ramdisk
mount -t tmpfs -o size=128m tmpfs /mnt/ramdisk

ls -l /bin > /mnt/ramdisk/root.lst
ls -l /mnt/sda1/porteus/base > /mnt/ramdisk/base.lst
ls -l /mnt/sda1/porteus/modules > /mnt/ramdisk/modules.lst
ls -l /mnt/sda1/porteus/optional > /mnt/ramdisk/opt.lst

while [ 1 ]; do
	# selftest
	RD=$(md5sum /etc/rc.d/rc.porteusGuard)
	if [ "$MD" = "$RD" ]; then
		echo "porteusGuard OK - nothing changed"
	else
		echo "RC SCRIPT CHANGED!!!"
		ifconfig eth0 down
		ifconfig wlan0 down
		rm /mnt/ramdisk/*.lst
		umount /mnt/ramdisk
		rmdir /mnt/ramdisk
		shutdown -h now
	fi
	# test for root
	ls -l /bin > /mnt/ramdisk/root2.lst
	diff /mnt/ramdisk/root.lst /mnt/ramdisk/root2.lst
	if [ $? -ne 0 ]; then
		echo "FILES IN ROOT ARE CHANGED"
		echo "GOES SHUTDOWN!!!"
		ifconfig eth0 down
		ifconfig wlan0 down
		rm /mnt/ramdisk/*.*
		umount /mnt/ramdisk
		rmdir /mnt/ramdisk
		shutdown -h now
	else
		echo "porteusGuard root OK - nothing changed"
		let i=0
	fi
	# test in base folder
	ls -l /mnt/sda1/porteus/base > /mnt/ramdisk/base2.lst
	diff /mnt/ramdisk/base.lst /mnt/ramdisk/base2.lst
	if [ $? -ne 0 ]; then
		echo "FILES IN base ARE CHANGED"
		echo "GOES SHUTDOWN!!!"
		ifconfig eth0 down
		ifconfig wlan0 down
		rm /mnt/ramdisk/*.*
		umount /mnt/ramdisk
		rmdir /mnt/ramdisk
		shutdown -h now
	else
		echo "porteusGuard base OK - nothing changed"
		let i=0
	fi
	# test in modules folder
	ls -l /mnt/sda1/porteus/modules > /mnt/ramdisk/modules2.lst
	diff /mnt/ramdisk/modules.lst /mnt/ramdisk/modules2.lst
	if [ $? -ne 0 ]; then
		echo "FILES IN modules ARE CHANGED"
		echo "GOES SHUTDOWN!!!"
		ifconfig eth0 down
		ifconfig wlan0 down
		rm /mnt/ramdisk/*.*
		umount /mnt/ramdisk
		rmdir /mnt/ramdisk
		shutdown -h now
	else
		echo "porteusGuard modules OK - nothing changed"
		let i=0
	fi
	# test in base folder
	ls -l /mnt/sda1/porteus/optional > /mnt/ramdisk/opt2.lst
	diff /mnt/ramdisk/opt.lst /mnt/ramdisk/opt2.lst
	if [ $? -ne 0 ]; then
		echo "FILES IN optional ARE CHANGED"
		echo "GOES SHUTDOWN!!!"
		ifconfig eth0 down
		ifconfig wlan0 down
		rm /mnt/ramdisk/*.*
		umount /mnt/ramdisk
		rmdir /mnt/ramdisk
		shutdown -h now
	else
		echo "porteusGuard optional OK - nothing changed"
		let i=0
	fi
	sleep $TIME;
done

# echo $i
}


case "$1" in
    status)
		cat /etc/porteus-version
        ;;
    stop)
		rm /mnt/ramdisk/*.*
		umount /mnt/ramdisk
		rmdir /mnt/ramdisk
	;;
    start|\
    restart|\
    force-reload)
        prepare_all_tests &
        ;;

     *)
        echo "Usage: $0 start|stop|restart|force-reload|status" 1>&2
        exit 3
        ;;
esac

exit 0

User avatar
brokenman
Site Admin
Site Admin
Posts: 6105
Joined: 27 Dec 2010, 03:50
Distribution: Porteus v4 all desktops
Location: Brazil

Re: Script for monitor porteus folder

Post#2 by brokenman » 30 Jun 2017, 13:40

Nice. Similar to something I run here except for the shutdown.

One thing to think about. Each time your script runs it regenerates a new .lst file
This means if a file is changed early during boot or during shutdown then on the next time your script runs it will regenerate the *.lst files and all will appear normal.
How do i become super user?
Wear your underpants on the outside and put on a cape.

luko
White ninja
White ninja
Posts: 16
Joined: 05 Oct 2015, 13:58
Distribution: Porteus, Lubuntu
Location: Slovakia

Re: Script for monitor porteus folder

Post#3 by luko » 30 Jun 2017, 14:25

yes, and before a shutdown i delete all *.lst and remove ramdisk folder, when at first i have idea to write this script i find that hardisk was heavy load, after i create ramdisk for minimize this

luko
White ninja
White ninja
Posts: 16
Joined: 05 Oct 2015, 13:58
Distribution: Porteus, Lubuntu
Location: Slovakia

Re: Script for monitor porteus folder

Post#4 by luko » 30 Jun 2017, 17:00

Small Update:

* add function for Repair modules from USB thumb mounted as SDB1

Code: Select all

#!/bin/sh

# porteusGuard - porteus directory guard
#
# Copyright (c) 2017 Veselovsky <lukves at gmail.com>
# This software is licensed under the GPL v2 or later.

### BEGIN INIT INFO
# Provides:          porteusGuard
# Required-Start:
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: porteusGuard start/stop script
# Description:       Initialize porteusGuard
### END INIT INFO

MD=$(md5sum /etc/rc.d/rc.porteusGuard)


# if is pluged in to /dev/sdb1 USB thumb than
# this funcion make diff of base folders and repair modules
# with copying original not touched modules
# from USB to disk
# in the future i want mount ISO and repair modules from network
repair_base() {
    # test and repair
	for a in `find /mnt/sdb1/porteus/base -maxdepth 1 -type f | sed '1d'`; do
	b=${a##*/}
	echo "testing file $b"
	A=$(md5sum "/mnt/sda1/porteus/base/"${b} | cut -d ' ' -f 1)
	B=$(md5sum "/mnt/sdb1/porteus/base/"${b} | cut -d ' ' -f 1)
	if [ "$A" = "$B" ]; then
		echo "file ${b} OK"
	else
		printf "refresh file ${b}"
		cp /mnt/sdb1/porteus/base/${b} /mnt/sda1/porteus/base/${b}
		chmod 644 /mnt/sda1/porteus/base/${b}
		printf "OK \n"
	fi
	done
}

repair_modules() {
    # test and repair
	for a in `find /mnt/sdb1/porteus/modules -maxdepth 1 -type f | sed '1d'`; do
	b=${a##*/}
	echo "testing file $b"
	A=$(md5sum "/mnt/sda1/porteus/modules/"${b} | cut -d ' ' -f 1)
	B=$(md5sum "/mnt/sdb1/porteus/modules/"${b} | cut -d ' ' -f 1)
	if [ "$A" = "$B" ]; then
		echo "file ${b} OK"
	else
		printf "refresh file ${b}"
		cp /mnt/sdb1/porteus/modules/${b} /mnt/sda1/porteus/modules/${b}
		chmod 644 /mnt/sda1/porteus/modules/${b}
		printf "OK \n"
	fi
	done
}

repair_optional() {
    # test and repair
	for a in `find /mnt/sdb1/porteus/optional -maxdepth 1 -type f | sed '1d'`; do
	b=${a##*/}
	echo "testing file $b"
	A=$(md5sum "/mnt/sda1/porteus/optional/"${b} | cut -d ' ' -f 1)
	B=$(md5sum "/mnt/sdb1/porteus/optional/"${b} | cut -d ' ' -f 1)
	if [ "$A" = "$B" ]; then
		echo "file ${b} OK"
	else
		printf "refresh file ${b}"
		cp /mnt/sdb1/porteus/optional/${b} /mnt/sda1/porteus/optional/${b}
		chmod 644 /mnt/sda1/porteus/optional/${b}
		printf "OK \n"
	fi
	done
}

#
# all tests
#
# Try n^x and when files changed in base,modules,optional, 
# or /bin then do halt. disconnect wlan0 a eth0, too..
#
# this utility create a 128m ramdisk for store
# listing files
#
prepare_all_tests() {
declare -i TIME

TIME=1

i=0

mkdir /mnt/ramdisk
mount -t tmpfs -o size=128m tmpfs /mnt/ramdisk

ls -l /bin > /mnt/ramdisk/root.lst
ls -l /mnt/sda1/porteus/base > /mnt/ramdisk/base.lst
ls -l /mnt/sda1/porteus/modules > /mnt/ramdisk/modules.lst
ls -l /mnt/sda1/porteus/optional > /mnt/ramdisk/opt.lst

while [ 1 ]; do
	# selftest
	RD=$(md5sum /etc/rc.d/rc.porteusGuard)
	if [ "$MD" = "$RD" ]; then
		echo "porteusGuard OK - nothing changed"
	else
		echo "RC SCRIPT CHANGED!!!"
		ifconfig eth0 down
		ifconfig wlan0 down
		rm /mnt/ramdisk/*.lst
		umount /mnt/ramdisk
		rmdir /mnt/ramdisk
		echo "RC SCRIPT" > /var/log/porteusGuard-$i
		shutdown -h now
	fi
	# test for root
	ls -l /bin > /mnt/ramdisk/root2.lst
	diff /mnt/ramdisk/root.lst /mnt/ramdisk/root2.lst
	if [ $? -ne 0 ]; then
		echo "FILES IN ROOT ARE CHANGED"
		echo "GOES SHUTDOWN!!!"
		ifconfig eth0 down
		ifconfig wlan0 down
		rm /mnt/ramdisk/*.*
		umount /mnt/ramdisk
		rmdir /mnt/ramdisk
		echo "Files in Root" > /var/log/porteusGuard-$i
		shutdown -h now
	else
		echo "porteusGuard root OK - nothing changed"
		let i=0
	fi
	# test in base folder
	ls -l /mnt/sda1/porteus/base > /mnt/ramdisk/base2.lst
	diff /mnt/ramdisk/base.lst /mnt/ramdisk/base2.lst
	if [ $? -ne 0 ]; then
		echo "FILES IN base ARE CHANGED"
		echo "GOES SHUTDOWN!!!"
		ifconfig eth0 down
		ifconfig wlan0 down
		rm /mnt/ramdisk/*.*
		umount /mnt/ramdisk
		rmdir /mnt/ramdisk
		echo "Files in base" > /var/log/porteusGuard-$i
		shutdown -h now
	else
		echo "porteusGuard base OK - nothing changed"
		let i=0
	fi
	# test in modules folder
	ls -l /mnt/sda1/porteus/modules > /mnt/ramdisk/modules2.lst
	diff /mnt/ramdisk/modules.lst /mnt/ramdisk/modules2.lst
	if [ $? -ne 0 ]; then
		echo "FILES IN modules ARE CHANGED"
		echo "GOES SHUTDOWN!!!"
		ifconfig eth0 down
		ifconfig wlan0 down
		rm /mnt/ramdisk/*.*
		umount /mnt/ramdisk
		rmdir /mnt/ramdisk
		echo "Files in modules" > /var/log/porteusGuard-$i
		shutdown -h now
	else
		echo "porteusGuard modules OK - nothing changed"
		let i=0
	fi
	# test in base folder
	ls -l /mnt/sda1/porteus/optional > /mnt/ramdisk/opt2.lst
	diff /mnt/ramdisk/opt.lst /mnt/ramdisk/opt2.lst
	if [ $? -ne 0 ]; then
		echo "FILES IN base ARE CHANGED"
		echo "GOES SHUTDOWN!!!"
		ifconfig eth0 down
		ifconfig wlan0 down
		rm /mnt/ramdisk/*.*
		umount /mnt/ramdisk
		rmdir /mnt/ramdisk
		echo "Files in optional" > /var/log/porteusGuard-$i
		shutdown -h now
	else
		echo "porteusGuard optional OK - nothing changed"
		let i=0
	fi
	sleep $TIME;
done

# echo $i
}


case "$1" in
    status)
		cat /etc/porteus-version
		;;
    repairbase)
		repair_base
		;;
    repairmod)
		repair_modules
		;;
    repairopt)
		repair_optional
		;;
    repair)
		repair_base
		repair_modules
		repair_optional
		;;
    stop)
		rm /mnt/ramdisk/*.*
		umount /mnt/ramdisk
		rmdir /mnt/ramdisk
	;;
    start|\
    restart|\
    force-reload)
        prepare_all_tests &
        ;;

     *)
        echo "Usage: $0 start|repair|repairbase|repairmod|repairopt|stop|restart|force-reload|status" 1>&2
        exit 3
        ;;
esac

exit 0

Post Reply