Compare commits

...

12 Commits

Author SHA1 Message Date
203d20bb11 user variable deleted at each iteration 2021-06-02 17:14:54 +02:00
4f2d8f9bac modif function serviceIsEnabled 2021-06-02 17:12:27 +02:00
451752901c localize serviceIsEnabled, remove serviceIsActive 2021-03-23 22:03:55 +01:00
347a9362ad ajout possibilité tester 1 seule machine 2021-03-22 15:36:50 +01:00
1a311bc1cf correction fonction numlines 2021-03-22 15:33:10 +01:00
9538b1f0d6 rajout numline 2021-03-19 12:01:00 +01:00
60f8fb1782 fix some typo
localize isinstalled function
2021-02-23 12:18:00 +01:00
fb03f6318c Merge branch 'master' of ssh://infra.opendoor.fr:2222/srv/git/repos/tom/eval_functions 2021-02-23 12:11:50 +01:00
3bc7f94ac2 functions
some moar local functions
2021-02-23 12:11:37 +01:00
5b642806b1 amelioration hostname et csv 2021-02-23 10:58:09 +01:00
9921222131 functions: renommage fonction
localeval: more robust hostname handling
2021-02-22 11:02:14 +01:00
bb26868607 check mtime of bash history file 2021-02-08 18:29:47 +01:00
2 changed files with 72 additions and 34 deletions

View File

@@ -4,6 +4,16 @@ function e {
echo -ne "${1};" echo -ne "${1};"
} }
# file age
# return 0 if given file mtime is older than given age (in day)
# arg1: file
# arg2: age to compare
function _fileOlderThan {
if [ ! -f $1 ] ; then echo -ne "0;" ; return 0 ; fi
test $(find test -mtime +$2)
return $?
}
# file mtime # file mtime
# arg1: file # arg1: file
function fileMTime { function fileMTime {
@@ -11,6 +21,14 @@ function fileMTime {
stat --printf "%y;" $1 stat --printf "%y;" $1
} }
# file numline
# arg1: file
function fileNumLines {
if [ ! -f $1 ] ; then echo -ne "0;" ; return 0 ; fi
n=$(wc -l $1 | awk '{print $1}')
echo -ne $n;
}
#ok if given program returns given code #ok if given program returns given code
# arg1: program to run # arg1: program to run
@@ -33,7 +51,7 @@ function fileBiggerThan {
#Ok if file exists #Ok if file exists
# arg1: file to check # arg1: file to check
function fileMustExist { function fileMustExists {
if [ -f ${1} ] ; then echo -ne "1;" if [ -f ${1} ] ; then echo -ne "1;"
else echo -ne "0;" else echo -ne "0;"
fi fi
@@ -41,7 +59,7 @@ fi
#Ok if file DOESNOT exists #Ok if file DOESNOT exists
# arg1: file to check # arg1: file to check
function fileMustNOTExist { function fileMustNOTExists {
if [ -f ${1} ] ; then echo -ne "0;" if [ -f ${1} ] ; then echo -ne "0;"
else echo -ne "1;" else echo -ne "1;"
fi fi
@@ -49,14 +67,14 @@ fi
#Ok if dir exists #Ok if dir exists
# arg1: dir to check # arg1: dir to check
function dirMustExist { function dirMustExists {
if [ -d ${1} ] ; then echo -ne "1;" if [ -d ${1} ] ; then echo -ne "1;"
else echo -ne "0;" else echo -ne "0;"
fi fi
} }
# Ok if given dir DOESNOT exists # Ok if given dir DOESNOT exists
# arg1: dir # arg1: dir
function dirMustNOTExist { function dirMustNOTExists {
if [ -d ${1} ] ; then echo -ne "0;" if [ -d ${1} ] ; then echo -ne "0;"
else echo -ne "1;" else echo -ne "1;"
fi fi
@@ -65,7 +83,7 @@ fi
# Ok if file contains at least one occurence of pattern # Ok if file contains at least one occurence of pattern
# arg1: file # arg1: file
# arg2: pattern # arg2: pattern
function fileMustContain { function fileMustContains {
file=$1 file=$1
shift shift
string=$@ string=$@
@@ -84,16 +102,18 @@ function fileCountPattern {
file=$1 file=$1
shift shift
string=$@ string=$@
if [ ! -f $file ] ; then echo -ne "0;" ; return 0 ;fi if [ ! -f $file ] ; then e 0 ; return 0 ;fi
local size=$(stat --printf '%s' $file)
if [ $size -eq 0 ] ; then e 0 ; return 0 ; fi
num=$(grep -ciE "${string}" $file ) num=$(grep -ciE "${string}" $file )
echo -ne "$num;" e "$num"
} }
# Ok if file contains N pattern # Ok if file contains N pattern
# arg1: file # arg1: file
# arg2: num of expected item # arg2: num of expected item
# arg3: pattern # arg3: pattern
function fileMustContainNItem { function fileMustContainsNItem {
file=$1 file=$1
n=$2 n=$2
shift shift
@@ -110,7 +130,7 @@ function fileMustContainNItem {
# Ok if file DOESNOT contain pattern # Ok if file DOESNOT contain pattern
# Arg1: file # Arg1: file
# ArgN: pattern # ArgN: pattern
function fileMustNOTContain { function fileMustNOTContains {
file=$1 file=$1
shift shift
string=$@ string=$@
@@ -140,20 +160,33 @@ function addHeader {
echo -ne "${user}$(hostname -s);" echo -ne "${user}$(hostname -s);"
} }
# get file md5
# arg1: file to checksum
function md5 {
test -f "$1" && local am=$(md5sum "$1" | awk '{print $1}')
echo -ne "${am};"
}
# init pkg list
function _initPkgList {
root="$1"
test -f "${1}/tmp/pkg.list" || chroot "$1" sh -c "rpm -qa > /tmp/pkg.list"
}
# Ok if given package is installed # Ok if given package is installed
# arg1: pkg to check # arg1: rootfs
# arg2: pkg to check
function pkgInstalled { function pkgInstalled {
pkglist=/tmp/pkg.list _initPkgList $1
test -f $pkglist || rpm -qa > $pkglist if (grep -qi "$2" "${1}/tmp/pkg.list" ) ; then e 1 ; else e 0 ; fi
if ( grep -qi $1 $pkglist ) ; then echo -ne "1;" ; else echo -ne "0;" ; fi
} }
# Ok if given package is NOT installed # Ok if given package is NOT installed
# arg1: pkg to check # arg1: pkg to check
function pkgNotInstalled { function pkgNotInstalled {
pkglist=/tmp/pkg.list _initPkgList $1
test -f $pkglist || rpm -qa > $pkglist if (grep -qi "$2" "${1}/tmp/pkg.list" ) ; then e 0 ; else e 1 ; fi
if ( grep -qiE $1 $pkglist ) ; then echo -ne "0;" ; else echo -ne "1;" ; fi echo -ne "${?};"
} }
# ok if given process is running # ok if given process is running
@@ -182,15 +215,10 @@ function commandStdoutPattern {
# ok if given service is enabled # ok if given service is enabled
# arg1: service # arg1: service
function serviceIsEnabled { function serviceIsEnabled {
service=$1 local root=$1
if ( systemctl is-enabled $1 &> /dev/null ) ; then echo -ne "1;" ; else echo -ne "0;" ; fi local service=${2}.service
} c=$(ls -l ${root}/etc/systemd/system/multi-user.target.wants/ | grep -i ${service} | wc -l)
if [ $c -gt 0 ] ; then e 1 ; else e 0 ; fi
# ok if given service is active
# arg1: service
function serviceIsActive {
service=$1
if ( systemctl is-active $1 &> /dev/null ) ; then echo -ne "1;" ; else echo -ne "0;" ; fi
} }
# ok if output of given url contains given stuff # ok if output of given url contains given stuff

View File

@@ -1,14 +1,24 @@
#! /bin/bash #! /bin/bash
set -e
IFS=$'\n\t'
source functions.sh source functions.sh
# check that alias has been tried echo -ne "host;name;root history;bash history;<+CHANGEME+>\n"
echo -ne "host\tname\t<+CHANGEME+>\n" if [ $# -ge 1 ] ; then
for host in /srv/lxc/epsi/b1/* ; do hostlist=/srv/lxc/epsi/b2/$1
else
hostlist=/srv/lxc/epsi/b2/*
fi
for host in $hostlist ; do
root=${host}/rootfs root=${host}/rootfs
host=$(basename $host) host=$(basename $host)
hostname=$(grep HOSTNAME ${root}/etc/sysconfig/network | cut -f2 -d=) hostname=$(grep HOSTNAME ${root}/etc/sysconfig/network 2>/dev/null| cut -f2 -d=)
test -f ${root}/etc/motd && user="$(cat ${root}/etc/motd | sed 's/\n//')\t" test -z "${hostname}" && hostname=$(cat ${root}/etc/hostname)
echo -ne "${hostname}\t${user}" test -f ${root}/etc/motd && user="$(cat ${root}/etc/motd | xargs)"
<+CHANGEME+> echo -ne "${hostname};${user};"
fileMTime ${root}/root/.bash_history
fileMTime ${root}/home/formation/.bash_history
unset user
echo echo
done done