Compare commits

...

11 Commits

2 changed files with 177 additions and 42 deletions

173
functions.sh Normal file → Executable file
View File

@@ -4,6 +4,25 @@ function e {
echo -ne "${1};"
}
# get machine number from name
# arg1: name of machine
function getMachineNumber {
latest=${1: -2}
left=${latest:0:1}
match='[0-9]'
if [[ "$left" =~ $match ]] ; then
echo $latest
else
echo ${1: -1}
fi
}
# get first line of file
function fisrstLine {
if [ ! -f $1 ] ; then echo -ne "0;" ; return 0 ; fi
head -n 1 $1
}
# file age
# return 0 if given file mtime is older than given age (in day)
# arg1: file
@@ -26,17 +45,19 @@ function fileMTime {
function fileNumLines {
if [ ! -f $1 ] ; then echo -ne "0;" ; return 0 ; fi
n=$(wc -l $1 | awk '{print $1}')
echo -ne $n;
echo -ne "$n;"
}
#ok if given program returns given code
# arg1: program to run
# arg2: expected return code (default 0)
# arg1: chroot
# arg2: program to run
# arg2: expected return code
function exitCodeOk {
program=$1
code=${2:=0}
$program &> /dev/null
chroot=$1
program=$2
code=${3}
chroot ${chroot} bash -c "$program" &> /dev/null
if [ $? -eq $code ] ; then echo -ne "1;" ; else echo -ne "0;" ; fi
}
@@ -49,10 +70,11 @@ function fileBiggerThan {
if [ $filesize -lt $2 ] ; then echo -ne "0;" ; else echo -ne "1;" ; fi
}
#Ok if file exists
# arg1: file to check
function fileMustExists {
if [ -f ${1} ] ; then echo -ne "1;"
if [ -f "${1}" ] ; then echo -ne "1;"
else echo -ne "0;"
fi
}
@@ -103,7 +125,7 @@ function fileMustContains {
shift
string=$@
if [ ! -f $file ] ; then echo -ne "0;" ; return 0 ;fi
if ( grep -i -qE "${string}" $file ) ;
if ( grep -i -qE -- "${string}" $file ) ;
then echo -ne "1;"
else
echo -ne "0;"
@@ -124,6 +146,25 @@ function fileCountPattern {
e "$num"
}
# Ok if file contains at least N pattern
# arg1: file
# arg2: num of expected item
# arg3: pattern
function fileMustContainsMoreNItem {
file=$1
n=$2
shift
shift
string=$@
if [ ! -f $file ] ; then echo -ne "0;" ; return 0 ;fi
num=$(grep -ciE "${string}" $file )
if [ $num -ge $n ] ;
then echo -ne "1;"
else
echo -ne "0;"
fi
}
# Ok if file contains N pattern
# arg1: file
# arg2: num of expected item
@@ -158,15 +199,17 @@ function fileMustNOTContains {
}
# Ok if given user exists
# Arg1: user
# Arg1: chroot dir
# Arg2: user
function userExists {
if ( grep -iq $1 /etc/passwd ) ; then e 1 ; else e 0 ; fi
if ( grep -iq $2 ${1}/etc/passwd ) ; then e 1 ; else e 0 ; fi
}
# Ok if given group exists
# Arg1: group
# Arg1: chroot
# Arg2: group
function groupExists {
if ( grep -iq $1 /etc/group ) ; then e 1 ; else e 0 ; fi
if ( grep -iq $2 ${root}/etc/group ) ; then e 1 ; else e 0 ; fi
}
# insert hostname at beginning of result line
@@ -175,10 +218,17 @@ function addHeader {
echo -ne "${user}$(hostname -s);"
}
# get file content
# arg1: file to read
function getFileContent {
test -f $1 && content="$(cat $1 | sed 's/\n//');" || content="NULL;"
echo -ne "${content}"
}
# get file md5
# arg1: file to checksum
function md5 {
test -f "$1" && local am=$(md5sum "$1" | awk '{print $1}')
test -f "$2" && local am=$(md5sum "$1" | awk '{print $1}')
echo -ne "${am};"
}
@@ -212,10 +262,30 @@ function pkgNotInstalled {
echo -ne "${?};"
}
# WIP: dont work
# ok if given process is listening on given port
# arg1: chroot
# arg2: process to check
# arg3: port
function processIsListening {
chroot=$1
process=$2
port=$3
chroot $chroot bash -c "ss -taupen|grep -q \"LISTEN.*:${port}.*${process}\""
test $? -eq 0 && e 1 || e 0
}
# WIP: dont work
# ok if given process is running
# arg1: process to check
# arg1: chroot
# arg2: process to check
function processIsRunning {
if ( pgrep -f $1 &> /dev/null ) ; then echo -ne "1;" ; else echo -ne "0;" ; fi
chroot=$1
process=$2
chroot $chroot bash -c "ps -ef | grep -q $process"
test $? -ne 0 && e 1 || e 0
}
#Ok if given command returns 0
@@ -231,14 +301,71 @@ function commandIsWorking {
}
# ok if given pattern is detected on command's stdout or stderr
# arg1: command
# arg2: pattern
function commandStdoutPattern {
command=$1
# return latest line of commande output
# arg1: chroot dir
# arg2: command to be run
function commandOutput {
chroot=$1
shift
pattern=$@
if ( $command 2>&1 | grep -qiE "${pattern}" ) ; then echo -ne "1;" ; else echo -ne "0;" ; fi
command="$@"
result=$(chroot ${chroot} bash -c "$command" 2>&1)
e "$result"
}
# get num of files in given dir
# arg1: dir
function numFilesInDir {
local dir=$1
test -d $dir || { e 0 ; return 0 ; }
local n=$(ls -l $dir | wc -l)
e $n
}
# check wether file mode is correct
# arg1: file
# arg2: mode
function modeIsCorrect {
file=$1
mode=$2
if [ ! -e $1 ] ; then e 0 ; return 0 ; fi
current_mode=$(stat --format "%a" $1)
if [ "$mode" = "$current_mode" ] ; then e 1 ; else e 0 ; fi
}
# check wether file owner is correct
# arg1: file
# arg2: owner
function ownerIsCorrect {
file=$1
owner=$2
if [ ! -e $1 ] ; then e 0 ; return 0 ; fi
current_owner=$(stat --format "%u" $1)
if [ "$owner" = "$current_owner" ] ; then e 1 ; else e 0 ; fi
}
# check wether file group is correct
# arg1: file
# arg2: group
function groupIsCorrect {
file=$1
group=$2
if [ ! -e $1 ] ; then e 0 ; return 0 ; fi
current_group=$(stat --format "%g" $1)
if [ "$group" = "$current_group" ] ; then e 1 ; else e 0 ; fi
}
# ok if given pattern is detected on command's stdout or stderr
# arg1: chroot
# arg2: command
# arg3: pattern
function commandStdoutPattern {
local chroot=$1
local command=$2
local pattern="$3"
result=$(chroot ${chroot} bash -c "$command" 2> /dev/null)
if ( echo -n "$result" | grep -qiE "${pattern}" ) ; then echo -ne "1;" ; else echo -ne "0;" ; fi
}
# ok if given service is enabled
@@ -256,5 +383,5 @@ function serviceIsEnabled {
function okIfCurl {
url=$1
shift
if ( curl -q "${url}" | grep -q "$@" ) ; then echo -ne "1;" ; else echo -ne "0;" ; fi
if ( curl -q --silent "${url}" | grep -qE "$@" ) ; then echo -ne "1;" ; else echo -ne "0;" ; fi
}

View File

@@ -1,24 +1,32 @@
#! /bin/bash
set -e
#set -e
set -u
IFS=$'\n\t'
source functions.sh
echo -ne "host;name;root history;bash history;<+CHANGEME+>\n"
if [ $# -ge 1 ] ; then
hostlist=/srv/lxc/epsi/b2/$1
else
hostlist=/srv/lxc/epsi/b2/*
fi
for host in $hostlist ; do
root=${host}/rootfs
host=$(basename $host)
hostname=$(grep HOSTNAME ${root}/etc/sysconfig/network 2>/dev/null| cut -f2 -d=)
test -z "${hostname}" && hostname=$(cat ${root}/etc/hostname)
test -f ${root}/etc/motd && user="$(cat ${root}/etc/motd | xargs)"
echo -ne "${hostname};${user};"
fileMTime ${root}/root/.bash_history
fileMTime ${root}/home/formation/.bash_history
unset user
echo -ne "host;name;<+CHANGE+>\n"
function _process {
fileMustExists ${root}/etc/postfix/main.cf
<+ADD SOME FUNCTIONS+>
echo
}
if [ $# -gt 0 ] ; then
root=/
host=$(hostname)
test -f ${root}/etc/motd && user="$(cat ${root}/etc/motd | xargs -0)"
echo -ne "${host};${user};"
_process
else
hostlist=/srv/lxc/<+CHANGEME+>
for host in $hostlist ; do
root=${host}/rootfs
host=$(basename $host)
hostname=$(grep HOSTNAME ${root}/etc/sysconfig/network 2>/dev/null| cut -f2 -d=)
test -z "${hostname}" && hostname=$(cat ${root}/etc/hostname)
test -f ${root}/etc/motd && user="$(cat ${root}/etc/motd | xargs -0)"
test -z $user && continue
echo -ne "${hostname};${user};"
_process
unset user
done
fi