From ffe2a47a7c817a9520c909015c4423e3b855d0cd Mon Sep 17 00:00:00 2001 From: ThomasC Date: Tue, 23 Mar 2021 22:13:15 +0100 Subject: [PATCH] mise au point --- eval.sh | 15 ----- eval.yml | 37 ----------- functions.sh | 184 +-------------------------------------------------- localeval.sh | 26 ++++++++ 4 files changed, 27 insertions(+), 235 deletions(-) delete mode 100644 eval.sh delete mode 100644 eval.yml mode change 100644 => 120000 functions.sh create mode 100644 localeval.sh diff --git a/eval.sh b/eval.sh deleted file mode 100644 index c5a59cf..0000000 --- a/eval.sh +++ /dev/null @@ -1,15 +0,0 @@ -#! /bin/bash -source functions.sh - - - -pkgNotInstalled '^php-[57]' - -fileMustExist /etc/yum.repos.d/remi.repo - -if ( systemctl status php-fpm &>/dev/null || systemctl status php73-php-fpm &> /dev/null ) ; then e 1 ; else e 0 ; fi - -fileMustExist /ar/www/html/version.php - -if ( curl localhost/version.php 2>/dev/null |grep -q 7.3 ) ; then e 1 ; else e 0 ; fi -echo diff --git a/eval.yml b/eval.yml deleted file mode 100644 index 61a8b4f..0000000 --- a/eval.yml +++ /dev/null @@ -1,37 +0,0 @@ ---- -- hosts: - - all - user: root - gather_facts: false - vars: - result_file: "tp_php-fpm" - - pre_tasks: - - name: insert header - lineinfile: - path: "{{ result_file }}" - line: "machine\tuser\tmod_php not installed\tremi repo\tphp-fpm running\tversoin.php exists\tphp correct install" - state: present - create: true - delegate_to: localhost - - tasks: - - name: copy script - copy: - src: "{{ item }}" - dest: /root - mode: 0700 - loop: - - functions.sh - - eval.sh - - - name: exec script - command: /root/eval.sh - register: result - - - name: get result - lineinfile: - path: "{{ result_file }}" - line: "{{ result.stdout }}" - create: true - delegate_to: localhost \ No newline at end of file diff --git a/functions.sh b/functions.sh deleted file mode 100644 index 9d96991..0000000 --- a/functions.sh +++ /dev/null @@ -1,183 +0,0 @@ -#! /bin/bash - -function e { - echo -ne "${1}\t" -} - -#ok if given program returns given code -# arg1: program to run -# arg2: expected return code (default 0) -function exitCodeOk { - program=$1 - code=${2:=0} - $program &> /dev/null - if [ $? -eq $code ] ; then echo -ne "1\t" ; else echo -ne "0\t" ; fi -} - -#Ok if file size > X -# arg1: file -# arg2: min size -function fileBiggerThan { - if [ ! -f $1 ] ; then echo -ne "0\t" ; return 0 ; fi - filesize=$(stat --format "%s" $1) - if [ $filesize -lt $2 ] ; then echo -ne "0\t" ; else echo -ne "1\t" ; fi -} - -#Ok if file exists -# arg1: file to check -function fileMustExist { -if [ -f ${1} ] ; then echo -ne "1\t" - else echo -ne "0\t" -fi -} - -#Ok if file DOESNOT exists -# arg1: file to check -function fileMustNOTExist { -if [ -f ${1} ] ; then echo -ne "0\t" - else echo -ne "1\t" -fi -} - -#Ok if dir exists -# arg1: dir to check -function dirMustExist { -if [ -d ${1} ] ; then echo -ne "1\t" - else echo -ne "0\t" -fi -} -# Ok if given dir DOESNOT exists -# arg1: dir -function dirMustNOTExist { -if [ -d ${1} ] ; then echo -ne "0\t" - else echo -ne "1\t" -fi -} - -# Ok if file contains at least one occurence of pattern -# arg1: file -# arg2: pattern -function fileMustContain { - file=$1 - shift - string=$@ - if [ ! -f $file ] ; then echo -ne "0\t" ; return 0 ;fi - if ( grep -i -qE "${string}" $file ) ; - then echo -ne "1\t" - else - echo -ne "0\t" - fi -} - -# Return num of occurences of pattern -# arg1: file -# arg2: pattern -function fileCountPattern { - file=$1 - shift - string=$@ - if [ ! -f $file ] ; then echo -ne "0\t" ; return 0 ;fi - num=$(grep -ciE "${string}" $file ) - echo -ne "$num\t" -} - -# Ok if file contains N pattern -# arg1: file -# arg2: num of expected item -# arg3: pattern -function fileMustContainNItem { - file=$1 - n=$2 - shift - string=$@ - if [ ! -f $file ] ; then echo -ne "0\t" ; return 0 ;fi - num=$(grep -ciE "${string}" $file ) - if [ $num -eq $1 ] ; - then echo -ne "1\t" - else - echo -ne "0\t" - fi -} - -# Ok if file DOESNOT contain pattern -# Arg1: file -# ArgN: pattern -function fileMustNOTContain { - file=$1 - shift - string=$@ - if [ ! -f $file ] ; then echo -ne "0\t" ; return 0 ; fi - if ( grep -i -qE "${string}" $file ) ; - then echo -ne "0\t" - else - echo -ne "1\t" - fi -} - -# insert hostname at beginning of result line -function addHeader { - test -f /etc/motd && user="$(cat /etc/motd | sed 's/\n//')\t" - echo -ne "${user}$(hostname -s)\t" -} - -# Ok if given package is installed -# arg1: pkg to check -function pkgInstalled { - pkglist=/tmp/pkg.list - test -f $pkglist || rpm -qa > $pkglist - if ( grep -qi $1 $pkglist ) ; then echo -ne "1\t" ; else echo -ne "0\t" ; fi -} - -# Ok if given package is NOT installed -# arg1: pkg to check -function pkgNotInstalled { - pkglist=/tmp/pkg.list - test -f $pkglist || rpm -qa > $pkglist - if ( grep -qiE $1 $pkglist ) ; then echo -ne "0\t" ; else echo -ne "1\t" ; fi -} - -# ok if given process is running -# arg1: process to check -function processIsRunning { - if ( pgrep -f $1 &> /dev/null ) ; then echo -ne "1\t" ; else echo -ne "0\t" ; fi -} - -#Ok if given command returns 0 -# arg1: command to be run -function commandIsWorking { - command=$1 - if ( $command &> /dev/null ) ; then echo -ne "1\t" ; else echo -ne "0\t" ; fi -} - -# ok if given pattern is detected on command's stdout or stderr -# arg1: command -# arg2: pattern -function commandStdoutPattern { - command=$1 - shift - pattern=$@ - if ( $command 2>&1 | grep -qiE "${pattern}" ) ; then echo -ne "1\t" ; else echo -ne "0\t" ; fi -} - -# ok if given service is enabled -# arg1: service -function serviceIsEnabled { - service=$1 - if ( systemctl is-enabled $1 &> /dev/null ) ; then echo -ne "1\t" ; else echo -ne "0\t" ; fi -} - -# ok if given service is active -# arg1: service -function serviceIsActive { - service=$1 - if ( systemctl is-active $1 &> /dev/null ) ; then echo -ne "1\t" ; else echo -ne "0\t" ; fi -} - -# ok if output of given url contains given stuff -# arg1: url -# arg2: string -function okIfCurl { - url=$1 - shift - if ( curl -q "${url}" | grep -q "$@" ) ; then echo -ne "1\t" ; else echo -ne "0\t" ; fi -} diff --git a/functions.sh b/functions.sh new file mode 120000 index 0000000..a5626c8 --- /dev/null +++ b/functions.sh @@ -0,0 +1 @@ +../eval_functions/functions.sh \ No newline at end of file diff --git a/localeval.sh b/localeval.sh new file mode 100644 index 0000000..354770b --- /dev/null +++ b/localeval.sh @@ -0,0 +1,26 @@ +#! /bin/bash +set -eu +IFS=$'\n\t' +source functions.sh +echo -ne "host;name;php installed 1;version.php;php-fpm enabled\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)" + test -z "$user" && continue + echo -ne "${hostname};${user};" + pkgInstalled "${root}" php-fpm-7 + #pkgInstalled "${root}" php7[34]-php-fpm + fileMustContains ${root}/srv/cours/www/version.php + serviceIsEnabled ${root} php-fpm + + echo + +done \ No newline at end of file