LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-25-2017, 12:19 PM   #1
SeaPhor
LQ Newbie
 
Registered: Feb 2017
Distribution: SLES, RHEL, OpenSUSE, CentOS
Posts: 13

Rep: Reputation: Disabled
BASH script for Before and after Health checks of multiple systems for monthly maintenance


BASH script for Before and after Health checks of multiple systems for monthly maintenance
I have a bash script that outputs the [1] CPU Usage (system, IO-Wait, Idle) & [2] Load Average (last 15 m), [3] MEM (System usage), [4] SWAP (size, usage, free), [5] disk/partitions (mount-point - % in-use -- in multiple rows), [6] Critical-Applications-Status (core monitoring and management apps - status -- in multiple rows)
What I need is the output to the report to be in horizontal format for csv rather than verticle format.
I will include the current actual output after the details below, and the actual bash script code I wrote at the end of this post (all critism welcome, but, I do know how sloppy my code is- I don't claim to being proficient with this, I only had one day to put this together at the time of creation, and I doubt any could be any more harsh of my ability (or lack thereof) than I am of myself )
Existing-
This is done once before the patching in the following format:
Code:
sh scriptname serverlist pre
This is done once after the patching in the following format:
Code:
sh scriptname serverlist post
In the 'post' process all server's individual pre/post files are cat'd together, and then all those server's combined before/after files are cat'd together for one report of all servers.
the final report is as follows:
Code:
Report date patching title
"servername Before-stats"
[1]
[2]
[3]
[4]
[5]
"servername After-stats"
[1]
[2]
[3]
[4]
[5]
-
"next-servername Before-stats"
[1]
[2]
[3]
[4]
[5]
"next-servername After-stats"
[1]
[2]
[3]
[4]
[5]
-
[Repeats for each server in serverlist]
what I need is :
Code:
Report date patching title
Server 		Before \t\t\t\t\t 																After
Hostname 	| CPU-Usage \t\t 		| CPU-Load 	| MEM-Usage 	| SWAP \t\t			| Disk/Partition Usage	| Application status	| CPU-Usage \t\t                | CPU-Load      | MEM-Usage     | SWAP \t\t                     | Disk/Partition Usage  | Application status
servername	| %system \t %iowait \t %idle	| ldavg-15	| %memused	| Total \t in-use \t free	| Mount-Point \t in-use	| App-Name \t Status	| %system \t %iowait \t %idle   | ldavg-15      | %memused      | Total \t in-use \t free       | Mount-Point \t in-use | App-Name \t Status
		| Value   \t value   \t value	| value		| value		| value \t value  \t value	| value       \t value	| value    \t status	| value   \t value   \t value   | value         | value         | value \t value  \t value      | value       \t value  | value    \t status
														| value       \t value  | value    \t status	|                                                                                               | value       \t value  | value    \t status
														| value       \t value  | value    \t status	|                                                                                               | value       \t value  | value    \t status
														| value       \t value  | value    \t status	|                                                                                               | value       \t value  | value    \t status
														| value       \t value  | value    \t status	|                                                                                               | value       \t value  | value    \t status
														| value       \t value 				|                                                                                            | value       \t value
														| value       \t value				|                                                                                            | value       \t value
														| value       \t value				|                                                                                            | value       \t value
nextservername	| %system \t %iowait \t %idle	| ldavg-15	| %memused	| Total \t in-use \t free	| Mount-Point \t in-use	| App-Name \t Status	| %system \t %iowait \t %idle   | ldavg-15      | %memused      | Total \t in-use \t free       | Mount-Point \t in-use | App-Name \t Status
		| Value   \t value   \t value	| value		| value		| value \t value  \t value	| value       \t value	| value    \t status	| value   \t value   \t value   | value         | value         | value \t value  \t value      | value       \t value  | value    \t status
														| value       \t value  | value    \t status	|                                                                                               | value       \t value  | value    \t status
														| value       \t value  | value    \t status	|                                                                                               | value       \t value  | value    \t status
														| value       \t value  | value    \t status	|                                                                                               | value       \t value  | value    \t status
														| value       \t value  | value    \t status	|                                                                                               | value       \t value  | value    \t status
														| value       \t value 				|                                                                                            | value       \t value
														| value       \t value				|                                                                                            | value       \t value
														| value       \t value				|                                                                                            | value       \t value
nextservername	| %system \t %iowait \t %idle	| ldavg-15	| %memused	| Total \t in-use \t free	| Mount-Point \t in-use	| App-Name \t Status	| %system \t %iowait \t %idle   | ldavg-15      | %memused      | Total \t in-use \t free       | Mount-Point \t in-use | App-Name \t Status
		| Value   \t value   \t value	| value		| value		| value \t value  \t value	| value       \t value	| value    \t status	| value   \t value   \t value   | value         | value         | value \t value  \t value      | value       \t value  | value    \t status
														| value       \t value  | value    \t status	|                                                                                               | value       \t value  | value    \t status
														| value       \t value  | value    \t status	|                                                                                               | value       \t value  | value    \t status
														| value       \t value  | value    \t status	|                                                                                               | value       \t value  | value    \t status
														| value       \t value  | value    \t status	|                                                                                               | value       \t value  | value    \t status
														| value       \t value 				|                                                                                            | value       \t value
														| value       \t value				|                                                                                            | value       \t value
														| value       \t value				|                                                                                            | value       \t value
[Repeats for each server in serverlist]
Existing Final Report run for 3 servers-
Code:
	Statistics Comparison Before and After Patching on 2017-Feb-23-19:13


=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
plapnag02-Before-stats-2017-Feb-23-14:21
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Linux 3.0.101-91-pae (plapnag02) 	02/23/17 	_i686_

CPU Usage
%iowait %steal 
0.50	0.00	98.00

LOAD Average - last 15 min.
14:21:46	

Averaged Memory percentage in use
14:21:46	%memusage 
		90.72
Memory Size, SWAP space size and usage
Mem--
total used free 
 1982 1798 184
             total       used       free              
Swap:          517         25        492

Disk Partition percentage in use
Use%	Mounted
72%	/
58%	/boot
7%	/var
29%	/opt
10%	/usr/local

Application Status
	Application BigFix is running
	Application Eco-Tools is running
	Application ILMT is running
	Application SNMP is running
	Application Splunk is running
	Application TSM is running
	Application Nagios is running

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
plapnag02-After-stats-2017-Feb-23-19:13
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Linux 3.0.101-91-pae (plapnag02) 	02/23/17 	_i686_

CPU Usage
%iowait %steal 
0.00	0.00	98.50

LOAD Average - last 15 min.
19:13:11	

Averaged Memory percentage in use
19:13:11 	%memusage
		32.03
Memory Size, SWAP space size and usage
Mem--
total used free 
 1982 635 1347
             total       used       free              
Swap:          517          0        517

Disk Partition percentage in use
Use%	Mounted
72%	/
58%	/boot
7%	/var
29%	/opt
10%	/usr/local

Application Status
	Application BigFix is running
	Application Eco-Tools was Started
	Application ILMT is running
	Application SNMP is running
	Application Splunk is running
	Application TSM is running
	Application Nagios Was Started

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
plappup02e-Before-stats-2017-Feb-23-14:21
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Linux 3.0.101-91-default (plappup02e) 	02/23/17 	_x86_64_

CPU Usage
%iowait %steal 
0.00	0.00	96.00

LOAD Average - last 15 min.
14:22:11	

Averaged Memory percentage in use
14:22:11 	%memusage
		91.19
Memory Size, SWAP space size and usage
Mem--
total used free 
 996 908 87
             total       used       free              
Swap:          519        105        414

Disk Partition percentage in use
Use%	Mounted
64%	/
26%	/boot
4%	/opt
15%	/var
26%	/RearDRBU

Application Status
	Application BigFix is running
	Application Eco-Tools is running
	Application ILMT is running
	Application SNMP is running
	Application Splunk is running
	Application TSM is running
	Application Puppet Master is running

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
plappup02e-After-stats-2017-Feb-23-19:13
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Linux 3.0.101-91-default (plappup02e) 	02/23/17 	_x86_64_

CPU Usage
%iowait %steal 
1.00	0.00	98.00

LOAD Average - last 15 min.
19:13:32	

Averaged Memory percentage in use
19:13:32 	%memusage
		63.12
Memory Size, SWAP space size and usage
Mem--
total used free 
 996 629 367
             total       used       free              
Swap:          519          0        519

Disk Partition percentage in use
Use%	Mounted
64%	/
26%	/boot
4%	/opt
15%	/var
26%	/RearDRBU

Application Status
	Application BigFix is running
	Application Eco-Tools was Started
	Application ILMT is running
	Application SNMP is running
	Application Splunk is running
	Application TSM is running
	Application Puppet Master Was Started

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
plapsmg02e-Before-stats-2017-Feb-23-14:21
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Linux 3.12.67-60.64.24-default (plapsmg02e) 	02/23/17 	_x86_64_	(2 CPU)

CPU Usage
%iowait %steal 
0.00	0.00	100.00

LOAD Average - last 15 min.
14:22:23	0

Averaged Memory percentage in use
14:22:23 	%memusage
		37.71
Memory Size, SWAP space size and usage
Mem--
total used free 
 7828 2952 4875
             total       used       free              
Swap:         2038          0       2038

Disk Partition percentage in use
Use%	Mounted
16%	/
10%	/boot
7%	/var
1%	/var/log/audit
1%	/var/log/rhn
1%	/tmp
1%	/var/cache/rhn
1%	/var/spacewalk

Application Status
Application BigFix is NOT Installed
Application Eco-Tools is NOT Installed
Application ILMT is NOT Installed
	Application SNMP Was Started
Application Splunk is NOT Installed
Application TSM is NOT Installed

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
plapsmg02e-After-stats-2017-Feb-23-19:13
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Linux 3.12.69-60.64.32-default (plapsmg02e) 	02/23/17 	_x86_64_	(2 CPU)

CPU Usage
%iowait %steal 
0.00	0.00	97.98

LOAD Average - last 15 min.
19:13:52	0

Averaged Memory percentage in use
19:13:52 	%memusage
		16.26
Memory Size, SWAP space size and usage
Mem--
total used free 
 7828 1273 6555
             total       used       free              
Swap:         2038          0       2038

Disk Partition percentage in use
Use%	Mounted
16%	/
10%	/boot
1%	/tmp
6%	/var
1%	/var/spacewalk
1%	/var/log/rhn
1%	/var/log/audit
1%	/var/cache/rhn

Application Status
Application BigFix is NOT Installed
Application Eco-Tools is NOT Installed
Application ILMT is NOT Installed
	Application SNMP Was Started
Application Splunk is NOT Installed
Application TSM is NOT Installed
Code of existing bash script- (be kind )
Code:
#! /bin/bash
##	Usage <scriptname> <listname> <Option [pre|post|apps] >
#
	PWD=`pwd`
	PDATE=`date +%Y-%b`
	CDATE=`date +%Y-%b-%d`
	TDATE=`date +%Y-%b-%d-%H:%M`
	PROGNAME=$(basename $0)
case $2 in
"apps")
	APPCHK=true
	PRECHK=false
	PSTCHK=false
	;;
"pre")
	APPCHK=false
	PRECHK=true
	PSTCHK=false
	;;
"post")
	APPCHK=false
	PRECHK=false
	PSTCHK=true
	;;
*)
	APPCHK=false
	PRECHK=false
	PSTCHK=false
	echo -e "\n\n\tUsage -- $PROGNAME <listname> <Option [pre|post|apps] >\n\n"
	exit $?
        ;;
esac
#
if [ "`echo hostname`" != "plapsmg02e" ]; then
	OWNER=seaphor
else
	OWNER=smgadmin
fi
if ! $APPCHK; then
	WRKDIR="$PWD/$PDATE-patching"
	WRKFIL="$WRKDIR/IT-$i-stats.txt"
else
	WRKDIR="$PWD"
	WRKFIL="$WRKDIR/IT-apps-stats.txt"
fi
#################
if ! $APPCHK; then
#################
#
if [ "`echo $2`" == "pre" ]; then
	if [ ! -d $WRKDIR ]; then
		mkdir $WRKDIR
	else
		rm -rf $WRKDIR/*
	fi
fi
if [ "`echo $2`" == "post" ]; then
	if [ ! -d $WRKDIR ]; then
		echo -e "\n\n\tUsage $PROGNAME <listname> <Option [pre|post|apps] >\nRun the pre first\n"
		exit $?
	fi
fi
	TSAR="$WRKDIR/tmpsar.txt"
chown -R $OWNER: $WRKDIR
#
##	Functions
#
function chk_stats
{
	ssh $i sar -u 1 1 | sed -e '/^$/d' | head -n1 >> $WRKDIR/IT-$i-stats.txt
	echo -e "\nCPU Usage" >> $WRKDIR/IT-$i-stats.txt
	ssh $i sar -u 1 1 | sed -e '/^$/d' | head -n 2 | tail -n 1 | awk '{print $6,$7,$9}' >> $WRKDIR/IT-$i-stats.txt
	ssh $i sar -u 1 1 | sed -e '/^$/d' | tail -n 1 | awk '{print $5"\t"$6"\t"$8}' >> $WRKDIR/IT-$i-stats.txt
	echo -e "\nLOAD Average - last 15 min." >> $WRKDIR/IT-$i-stats.txt
	ssh $i sar -q 1 1 | tail -n 2 | head -n 1 | awk '{print $1"\t"$7}' >> $WRKDIR/IT-$i-stats.txt
	echo -e "\nAveraged Memory percentage in use" >> $WRKDIR/IT-$i-stats.txt
	ssh $i sar -r 1 1 | sed -e '/^$/d' | head -n 2 | tail -n 1 | awk '{print $1,$2,$5}' >> $WRKDIR/IT-$i-stats.txt
	ssh $i sar -r 1 1 | sed -e '/^$/d' | tail -n 1 | awk '{print "\t\t"$4}' >> $WRKDIR/IT-$i-stats.txt
	echo -e "Memory Size, SWAP space size and usage\nMem--" >> $WRKDIR/IT-$i-stats.txt
	if [ "`echo $UID`" == 0 ]; then
		ssh $i free -m | head -n2 | awk '{print $1,$2,$3,$4}' | sed -e 's/Mem://g' | sed -e 's/shared//g' >> $WRKDIR/IT-$i-stats.txt
	else
		sudo ssh $i free -m | head -n2 | awk '{print $1,$2,$3,$4}' | sed -e 's/Mem://g' | sed -e 's/shared//g' >> $WRKDIR/IT-$i-stats.txt
	fi
	ssh $i free -m | head -n 1 | sed -e 's/cached//g' | sed -e 's/shared//g' | sed -e 's/buffers//g' >> $WRKDIR/IT-$i-stats.txt
	ssh $i free -m | tail -n 1 >> $WRKDIR/IT-$i-stats.txt
#
	echo -e "\nDisk Partition percentage in use" >> $WRKDIR/IT-$i-stats.txt
	ssh $i df -hl | grep -v tmpfs | grep -v devtmpfs | grep -v udev | awk '{print $5"\t"$6}' >> $WRKDIR/IT-$i-stats.txt
#
	apps_chk
}
#
#################
fi
#################
function apps_chk
{
##	Apps
#
if ! $APPCHK; then
	WRKFIL="$WRKDIR/IT-$i-stats.txt"
fi
if $APPCHK; then
	WRKFIL="$WRKDIR/IT-apps-stats.txt"
fi
echo -e "\nApplication Status" >> $WRKFIL
if [ "`ssh $i pgrep BESClient`" != "" ]; then
	echo -e "\tApplication BigFix is running" >> $WRKFIL
else
	if [ "`sh $i ls /etc/init.d/rc2.d/ | grep K01besclient`" == "" ]; then 
			echo "Application BigFix is NOT Installed" >> $WRKFIL
	else
		ssh $i /etc/init.d/rc2.d/K01besclient restart
		if [ "`ssh $i pgrep BESClient`" != "" ]; then
		        echo -e "\tApplication BigFix was Started" >> $WRKFIL
		else
			echo "Application BigFix is NOT running" >> $WRKFIL
		fi
	fi
fi
#
if [ "`ssh $i pgrep ecoagt`" != "" ]; then
	echo -e "\tApplication Eco-Tools is running" >> $WRKFIL
else
	if [ "`ssh $i ls /usr/ | grep ecotools`" == "" ]; then
		echo "Application Eco-Tools is NOT Installed" >> $WRKFIL
	else
		ssh $i /usr/ecotools/bin/restart_ecoagt
		if [ "`ssh $i pgrep ecoagt`" != "" ]; then
			echo -e "\tApplication Eco-Tools was Started" >> $WRKFIL
		else
			echo "Application Eco-Tools is NOT running" >> $WRKFIL
		fi
	fi
fi
#
if [ "`ssh $i pgrep tlmagent.bin`" != "" ]; then
	echo -e "\tApplication ILMT is running" >> $WRKFIL
else
	if [ "`ssh $i ls /var/ | grep itlm`" == "" ]; then
		echo "Application ILMT is NOT Installed" >> $WRKFIL
	else
		ssh $i /var/itlm/tlmagent.bin start
		if [ "`ssh $i pgrep tlmagent.bin`" != "" ]; then
			echo -e "\tApplication ILMT was Started" >> $WRKFIL
		else
			echo "Application ILMT is NOT running" >> $WRKFIL
		fi
	fi
fi
#
if [ "`ssh $i pgrep snmpd`" != "" ]; then
	echo -e "\tApplication SNMP is running" >> $WRKFIL
else
	ssh $i /etc/init.d/snmpd start
	if [ "`ssh $i pgrep snmpd`" != "" ]; then
		echo -e "\tApplication SNMP Was Started" >> $WRKFIL
	else
		echo "Application SNMP is NOT running" >> $WRKFIL
	fi
fi
#
if [ "`ssh $i pgrep splunkd`" != "" ]; then
	echo -e "\tApplication Splunk is running" >> $WRKFIL
else
	if [ "`ssh $i ls /etc/init.d/ | grep splunk`" == "" ]; then
		echo "Application Splunk is NOT Installed" >> $WRKFIL
	else
		ssh $i /etc/init.d/splunk start
		if [ "`ssh $i pgrep splunkd`" != "" ]; then
			echo -e "\tApplication Splunk Was Started" >> $WRKFIL
		else
			echo "Application Splunk is NOT running" >> $WRKFIL
		fi
	fi
fi
if [ "`ssh $i pgrep dsmc`" != "" ]; then
	echo -e "\tApplication TSM is running" >> $WRKFIL
else
	if [ "`ssh $i ls /opt/tivoli/ | grep tsm`" == "" ]; then
		echo "Application TSM is NOT Installed" >> $WRKFIL
	else
		ssh $i /opt/tivoli/tsm/client/ba/bin/dsmc sched #>/dev/null 2>&1
		ssh $i /opt/tivoli/tsm/client/ba/bin/dsmcad
		if [ "`ssh $i pgrep dsmc`" != "" ]; then
			echo -e "\tApplication TSM Was Started" >> $WRKFIL
		else
			echo "Application TSM is NOT running" >> $WRKFIL
		fi
	fi
fi
#
##	Special apps - SUMA, Puppet, Nagios, etc.
#
if [ "`ssh $i hostname | grep lapnag`" != "" ]; then
	if [ "`ssh $i pgrep nagios`" != "" ]; then
        	echo -e "\tApplication Nagios is running" >> $WRKFIL
	else
		ssh $i /etc/init.d/nagios restart
		if [ "`ssh $i pgrep nagios`" != "" ]; then
			echo -e "\tApplication Nagios Was Started" >> $WRKFIL
		else
			echo "Application Nagios is NOT running" >> $WRKFIL
		fi
	fi
fi
#
if [ "`ssh $i hostname | grep lappup`" != "" ]; then
	if [ "`ssh $i pgrep puppetmasterd`" != "" ]; then
        	echo -e "\tApplication Puppet Master is running" >> $WRKFIL
	else
		ssh $i /etc/init.d/puppetmasterd restart
		if [ "`ssh $i pgrep puppetmasterd`" != "" ]; then
			echo -e "\tApplication Puppet Master Was Started" >> $WRKFIL
		else
			echo "Application Puppet Master is NOT running" >> $WRKFIL
		fi
	fi
fi
#
#
##################################
##	End of Apps Check
##################################
#
}
#
if ! $APPCHK; then
	WRKFIL="$WRKDIR/IT-$i-stats.txt"
fi
if $APPCHK; then
	WRKFIL="$WRKDIR/IT-apps-stats.txt"
fi
#################
if $APPCHK; then
#################
	FILE=`cat $1`
	for i in $FILE
        do
                echo -e "\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n$i-Applications-Status-$TDATE\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" >> app-status.txt
echo -e "\nApplication Status" >> app-status.txt
if [ "`ssh $i pgrep BESClient`" != "" ]; then
	echo -e "\tApplication BigFix is running" >> app-status.txt
else
	if [ "`sh $i ls /etc/init.d/rc2.d/ | grep K01besclient`" == "" ]; then 
			echo "Application BigFix is NOT Installed" >> app-status.txt
	else
		ssh $i /etc/init.d/rc2.d/K01besclient restart
		if [ "`ssh $i pgrep BESClient`" != "" ]; then
		        echo -e "\tApplication BigFix was Started" >> app-status.txt
		else
			echo "Application BigFix is NOT running" >> app-status.txt
		fi
	fi
fi
#
if [ "`ssh $i pgrep ecoagt`" != "" ]; then
	echo -e "\tApplication Eco-Tools is running" >> app-status.txt
else
	if [ "`ssh $i ls /usr/ | grep ecotools`" == "" ]; then
		echo "Application Eco-Tools is NOT Installed" >> app-status.txt
	else
		ssh $i /usr/ecotools/bin/restart_ecoagt
		if [ "`ssh $i pgrep ecoagt`" != "" ]; then
			echo -e "\tApplication Eco-Tools was Started" >> app-status.txt
		else
			echo "Application Eco-Tools is NOT running" >> app-status.txt
		fi
	fi
fi
#
if [ "`ssh $i pgrep tlmagent.bin`" != "" ]; then
	echo -e "\tApplication ILMT is running" >> app-status.txt
else
	if [ "`ssh $i ls /var/ | grep itlm`" == "" ]; then
		echo "Application ILMT is NOT Installed" >> app-status.txt
	else
		ssh $i /var/itlm/tlmagent.bin start
		if [ "`ssh $i pgrep tlmagent.bin`" != "" ]; then
			echo -e "\tApplication ILMT was Started" >> app-status.txt
		else
			echo "Application ILMT is NOT running" >> app-status.txt
		fi
	fi
fi
#
if [ "`ssh $i pgrep snmpd`" != "" ]; then
	echo -e "\tApplication SNMP is running" >> app-status.txt
else
	ssh $i /etc/init.d/snmpd start
	if [ "`ssh $i pgrep snmpd`" != "" ]; then
		echo -e "\tApplication SNMP Was Started" >> app-status.txt
	else
		echo "Application SNMP is NOT running" >> app-status.txt
	fi
fi
#
if [ "`ssh $i pgrep splunkd`" != "" ]; then
	echo -e "\tApplication Splunk is running" >> app-status.txt
else
	if [ "`ssh $i ls /etc/init.d/ | grep splunk`" == "" ]; then
		echo "Application Splunk is NOT Installed" >> app-status.txt
	else
		ssh $i /etc/init.d/splunk start
		if [ "`ssh $i pgrep splunkd`" != "" ]; then
			echo -e "\tApplication Splunk Was Started" >> app-status.txt
		else
			echo "Application Splunk is NOT running" >> app-status.txt
		fi
	fi
fi
if [ "`ssh $i pgrep dsmc`" != "" ]; then
	echo -e "\tApplication TSM is running" >> app-status.txt
else
	if [ "`ssh $i ls /opt/tivoli/ | grep tsm`" == "" ]; then
		echo "Application TSM is NOT Installed" >> app-status.txt
	else
		ssh $i /opt/tivoli/tsm/client/ba/bin/dsmc sched #>/dev/null 2>&1
		ssh $i /opt/tivoli/tsm/client/ba/bin/dsmcad
		if [ "`ssh $i pgrep dsmc`" != "" ]; then
			echo -e "\tApplication TSM Was Started" >> app-status.txt
		else
			echo "Application TSM is NOT running" >> app-status.txt
		fi
	fi
fi
#
##	Special apps - SUMA, Puppet, Nagios, etc.
#
if [ "`ssh $i hostname | grep lapnag`" != "" ]; then
	if [ "`ssh $i pgrep nagios`" != "" ]; then
        	echo -e "\tApplication Nagios is running" >> app-status.txt
	else
		ssh $i /etc/init.d/nagios restart
		if [ "`ssh $i pgrep nagios`" != "" ]; then
			echo -e "\tApplication Nagios Was Started" >> app-status.txt
		else
			echo "Application Nagios is NOT running" >> app-status.txt
		fi
	fi
fi
#
if [ "`ssh $i hostname | grep lappup`" != "" ]; then
	if [ "`ssh $i pgrep puppetmasterd`" != "" ]; then
        	echo -e "\tApplication Puppet Master is running" >> app-status.txt
	else
		ssh $i /etc/init.d/puppetmasterd restart
		if [ "`ssh $i pgrep puppetmasterd`" != "" ]; then
			echo -e "\tApplication Puppet Master Was Started" >> app-status.txt
		else
			echo "Application Puppet Master is NOT running" >> app-status.txt
		fi
	fi
fi
#
#		apps_chk
	done
	less app-status.txt
	exit $?
else
	FILE=`cat $1`
fi
#################
if ! $APPCHK; then
#################
if [ "`echo $2`" == "pre" ]; then
	for i in $FILE
	do
		if [ "`ssh $i rpm -qa | grep sysstat`" == "" ]; then
			ssh $i cat /etc/*release > tmp.txt
			if [ "`cat tmp.txt | grep -i suse`" != "" ]; then
				if [ "`echo $UID`" == 0 ]; then
					INSTAL="zypper in -y sysstat"
				else
					INSTAL="sudo zypper in -y sysstat"
				fi
				rm tmp.txt
			else
				if [ "`cat tmp.txt | grep -i redhat`" != "" ]; then
					if [ "`echo $UID`" == 0 ]; then
						INSTAL="yum install -y sysstat"
					else
						INSTAL="sudo yum install -y sysstat"
					fi
					rm tmp.txt
				fi
			fi
			ssh $i $INSTAL
		fi
		echo -e "\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n$i-Before-stats-$TDATE\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" >> $WRKDIR/IT-$i-stats.txt
		chk_stats
	done
	chown $OWNER: $WRKDIR/*.txt
fi
#################
##	POST
#################
if [ "`echo $2`" == "post" ]; then
	for i in $FILE
	do
		echo -e "\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n$i-After-stats-$TDATE\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" >> $WRKDIR/IT-$i-stats.txt
		chk_stats
	done
	chown $OWNER: $WRKDIR/*.txt
#
	echo -e "\tStatistics Comparison Before and After Patching on $TDATE\n" > $WRKDIR/$CDATE-patching-Comp.txt
#
	cat $WRKDIR/IT*stats.txt >> $WRKDIR/$CDATE-patching-Comp.txt

	cp $WRKDIR/$CDATE-patching-Comp.txt $WRKDIR/$CDATE-patching-Comp.odt
	chown -R $OWNER: $WRKDIR/*
	rm $TSAR
	if [ -f $WRKDIR/tmp.txt ]; then
		rm $WRKDIR/tmp.txt
	fi
	mv $WRKDIR $PWD/$CDATE-patching
	chown -R $OWNER: *
	tar -czvf $PWD/$CDATE-patching.tar.gz $PWD/$CDATE-patching
	chown $OWNER: *
function snd_mail
{
	SUBJECT="testing $CDATE"
	FROMA=seaphor@myemail.com
        EMAILMSGZ=$PWD/tmp.txt
	EMAILG=seaphor@myemail.com
	ATTACHA="$PWD/$CDATE-patching/$CDATE-patching-Comp.odt"
touch $EMAILMSGZ
echo $CDATE > $EMAILMSGZ
	/usr/bin/mailx -a $ATTACHA -s "$SUBJECT" "$EMAILG" -f $FROMA < $EMAILMSGZ
	rm $EMAILMSGZ
}
	snd_mail
	exit $?
fi
##############
fi
exit $?
##############
###
 
Old 02-25-2017, 01:10 PM   #2
nodir
Member
 
Registered: May 2016
Posts: 222

Rep: Reputation: Disabled
Not sure if you are asking for obvious pitfalls in your script, if not: just ignore.

Look at the output of http://shellcheck.net

Quote all variables when extended ( "$1", "$var", etc ... ). Really ! Quote them !

As you use bash you should use [[ instead of [ for tests.
You don't use "echo command" but
Code:
if [[ $(hostname) == "string" ]]; then ...
And you don't use "echo $var", but
Code:
if [[ "$var" == "string" ]]; then ...
Also for commands test ( or [ or [[ ) isn't needed at all:
Code:
if grep file.txt; then
I am not sure if the same is valid when ssh is used, but would assume so (Your example: [ "`ssh $i pgrep ecoagt`" != "" ] )



Lots of people highly recommend to not use uppercase, but lowercase, for non-environment variables.

last but not least: pretty cool for one day. respect.

Last edited by nodir; 02-25-2017 at 01:14 PM.
 
1 members found this post helpful.
Old 02-25-2017, 01:37 PM   #3
SeaPhor
LQ Newbie
 
Registered: Feb 2017
Distribution: SLES, RHEL, OpenSUSE, CentOS
Posts: 13

Original Poster
Rep: Reputation: Disabled
Thank You Very much for your Reply! I will take your advice and begin to put it into practice. (I PROMISE!)
But, no, I am trying to figure out how to get the output into horizontal (columns) on one line (row), and outputs with multiple lines (like the apps and partitioning) be output in a way that when the report is opened with LibreOffice (or excel, by my boss), it will be as such:
ROW-1=
Title with Date
ROW-2=
Header-Row with descriptions of what is represented by the values in that 'column'
ROW-3=
servername and All values, in tab'd format with the value corresponding to description in the column header above, and values with multiple lines (rows, like partitioning and apps) be in 1 (one) 'cell' in that column
ROW-4
next-servername--- etc...

This is to make a report that can be parsed for particular values in certain columns

Thanks again for the VERY valuable info and the link you provided!
C4
 
Old 02-25-2017, 04:09 PM   #4
SeaPhor
LQ Newbie
 
Registered: Feb 2017
Distribution: SLES, RHEL, OpenSUSE, CentOS
Posts: 13

Original Poster
Rep: Reputation: Disabled
I guess what I'm needing is to make a hvar and a vvar for each .... so it would look like-
Code:
echo -e "$hvar1\t$hvar2\t\t\t$havar3\t$hvar4" # etc... and 
echo -e "$vvar1\t$vvar2a\t$vvar2b\t$vvar2c\t$vvar3\t$vvar4"
but what I can't seem to get is how to make the output like-
Code:
Use%	Mounted
Code:
72%	/
58%	/boot
7%	/var
29%	/opt
10%	/usr/local
be ONE (actually 2 variables tab'd...) variable vertical in ONE cell in the final output when opened in LibreOffice Calc,,, so that, each server and all its values are on ONE row...
for the example here would be -
Code:
# for the partitioning output
$var1\t$var2  # contains the 5 sets of values above- still verticle

Last edited by SeaPhor; 02-25-2017 at 04:14 PM. Reason: addition of explanation
 
Old 02-26-2017, 08:23 AM   #5
nodir
Member
 
Registered: May 2016
Posts: 222

Rep: Reputation: Disabled
again not sure if i understand you correct. :-)
I think for formated output one would rather use printf than echo. Only to give an example i use mount with awk {printf}:
Code:
user$ mount | awk ' { printf("%-10s %s\n",$1,$5) }' 
/dev/sda2  ext4
proc       proc
sysfs      sysfs
tmpfs      tmpfs
user$
(the trick to do the formatting being to use %-10s instead of simply %s (s for string, duh, d for digit, etc), but i really can't explain the details. The web or the man page sure can explain it quick and better than i could).
I am pretty sure for formatted output you want printf though (not echo).

Assuming you are under time pressure, you might wanna give the forum http://unix.com a try (it is more focused on scripting than this forum, i think, hence the chances to get a quick answer are slightly higher).

Last edited by nodir; 02-26-2017 at 08:25 AM.
 
1 members found this post helpful.
Old 02-26-2017, 11:18 AM   #6
SeaPhor
LQ Newbie
 
Registered: Feb 2017
Distribution: SLES, RHEL, OpenSUSE, CentOS
Posts: 13

Original Poster
Rep: Reputation: Disabled
Thanks again for valuable info- I will begin implementing these suggestions and try to achieve my expected result now that my boss is... 'ok' with what is there now as long as it is a 'work-in-progress'
I took the '.odt' output and manually cut/paste that into a '.ods' in the format that I am trying to achieve, I will attempt to attach that here as I see there is an "Attachments" icon here-
Click image for larger version

Name:	Screenshot from 2017-02-26 11-16-19.png
Views:	32
Size:	162.7 KB
ID:	24336

Anyways- Thanks for the excellent help and suggestions!
C4

Last edited by SeaPhor; 02-26-2017 at 11:19 AM. Reason: adding Thanks
 
Old 02-26-2017, 12:22 PM   #7
SeaPhor
LQ Newbie
 
Registered: Feb 2017
Distribution: SLES, RHEL, OpenSUSE, CentOS
Posts: 13

Original Poster
Rep: Reputation: Disabled
While trying to re-write the script according to the info received here, the realization hit me...
I can NOT do it according to the "Expected Output" from the attached pic above...
Because=> It is a "Before & After", meaning the script is run once ('pre'), and then run again ('post'), so the output will be appended to the bottom of the file.
However, If I simply send all the 'pre' output to a file-
Code:
# pre
<command1> >> $(prevar1)
echo "prevar1=$prevar1" >> pre-file.sh
<command2> >> $(prevar2)
echo "prevar1=$prevar" >> pre-file.sh
# all commands follow
# post
<command1> >> $(postvar1)
echo "postvar1=$postvar1" >> post-file.sh
<command2> >> $(postvar2)
echo "postvar1=$postvar" >> post-file.sh
# all commands follow
source pre-file.sh
source post-file.sh
echo -e "$prevar1\t$prevar2\t$postvar1\t$postvar2" >> output-report.csv
Interesting,,, I will have to consider this option
 
Old 03-05-2017, 09:28 AM   #8
SeaPhor
LQ Newbie
 
Registered: Feb 2017
Distribution: SLES, RHEL, OpenSUSE, CentOS
Posts: 13

Original Poster
Rep: Reputation: Disabled
Well, I have made adjustments to some of the syntax/code-structure as suggested, and I have it "usable", but I need it to better for the output to CSV for creating a report for Libre-Calc or Excel (for my boss). Is there a way to have the output maintain it's position in the column? Below is example of the csv/odt for Libre-Calc

What I have
Code:
        | A     | B     | C     | D                     |
 1      | value | value | value | value                 |
 2      | value | value | value | value value value     |
 3      | value | value | value | value                 |
What I need
Code:
        | A     | B     | C     | D     |
 1      | value | value | value | value |       
 2      | value | value | value | value |
                                | value |
                                | value |
 3      | value | value | value | value |
Or even better-
Code:
        | A     | B     | C     | D     |
 1      | value | value | value | value |
 2      | value | value | value | value |
 3                              | value |
 4                              | value |
 5      | value | value | value | value |
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script that checks network connection between servers rug.roya LinuxQuestions.org Member Success Stories 1 06-03-2014 01:24 PM
Bash Script that that checks CPU load average and triggers other script petruha1983 Programming 1 12-08-2013 05:22 AM
[Bash] MD5 Checks with Script. BiFo Programming 5 08-17-2010 06:08 AM
bash script that checks authentication failures and sends mail panoskan Programming 4 06-06-2009 05:24 PM
Bash script thats checks for faild login attempts. k1piee Programming 4 02-11-2009 09:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration