How to select log levels to the output of a bash script - linux

In the script i have written, the output is sent to a log file mySysMonitor.log. I want the user of this script to be able to choose what log levels(debug or info) to be sent to the log file. How do i do this. Take the script below as an example.
#!/bin/bash
processKD(){
printf "\nCURRENT PROCESSES \r"
ps -eo user,pid,pcpu,pmem,start | sort -rk 3,4 | head -n 11
echo "TOTAL NUMBER OF RUNNING PROCESSES: $(ps aux | wc -l)"
}
loggedInKD(){
printf "\nUSERS CURRENTLY LOGGED IN\r"
echo "Number of users logged in: $(w | head -n 1 | cut -d"," -f 3)"
who
}
processKd >> mySysMonitor.log
loggedInKd >> mySysMonitor.log

you can use something like this;
#!/bin/bash
processKD(){
printf "\nCURRENT PROCESSES \r"
ps -eo user,pid,pcpu,pmem,start | sort -rk 3,4 | head -n 11
echo "TOTAL NUMBER OF RUNNING PROCESSES: $(ps aux | wc -l)"
}
loggedInKD(){
printf "\nUSERS CURRENTLY LOGGED IN\r"
echo "Number of users logged in: $(w | head -n 1 | cut -d"," -f 3)"
who
}
LOG_LEVEL="info"
#LOG_LEVEL=$1 #with parameter ./script info
#LOG_LEVEL="debug"
if [ "$LOG_LEVEL" == "info" ]; then
loggedInKd >> mySysMonitor.log
fi
if [ "$LOG_LEVEL" == "debug" ]; then
processKd >> mySysMonitor.log
loggedInKd >> mySysMonitor.log
fi

Related

Linux CLI, using var into echo with pipes

I'm trying to use the result of a pipe into a "var" and then use it on a echo like this way:
ls -al --time-style=+%D |
grep `date +%D` |
wc -l |
HERE I DON'T KNOW HOW TO ASSING THE VALUE OF THE PREVIOUS PIPE INTO var |
echo "the number is %var"
The idea is to print "the number is 3" (because ls -al --time-style=+%D | date +%D | wc -l is 3)
How can I do it?
One way, using command substitution:
$ var=$(ls -al --time-style=+%D | grep `date +%D` | wc -l)
$ echo "the number is $var"
the number is 3
$

Problem with putting value in array in bash

I would like to make array which put users in a time using for loop. For example:
y[1]="user1"
y[2]="user2"
...
y[n]="usern"
I tried to do it like this
#!/bin/bash
x=$(who | cut -d " " -f1 | sort | uniq | wc -l)
for (( i=1; i<=$x; i++ )); do
y[$i]=$(who | cut -d " " -f1 | sort | uniq | sed -n '$ip')
p[$i]=$(lsof -u ${y[$i]} | wc -l)
echo "Users:"
echo ${y[$i]}
echo -e "Number of launched files:\n" ${p[$i]}
done
Most likely I'm using command "sed" wrong.
Can you help me?
Indeed your sed command seems to be a bit off. I can't really guess what you're trying to do there. Besides that, I'm wondering why you're executing who twice. You can make use of the data first obtained in the following manner.
#!/bin/bash
# define two arrays
y=()
p=()
#x=0
while read -r username; do
y+=("$username")
p+=($(lsof -u $(id -u "$username") | wc -l))
echo -e "User:\n${y[-1]}"
echo -e "Open files:\n${p[-1]}"
# The -1 index is the last index in the array, but you
# could uncomment the x=0 variable and the line below:
#((x++))
done <<< $(who | cut -d " " -f1 | sort | uniq)
echo "Amount of users: $x"
exit 0

variable not holding output of command

The output of $ip coming blank
#!/bin/bash
for i in illinrcmsg{002..003}
do
echo " ####################### $i #################### "
ssh -o StrictHostKeyChecking=no $i "
echo $i
cp /etc/hosts /etc/hosts_bak
ip=`ifconfig | head -n 2 | tail -n 1 | awk '{print $2}' | cut -d':' -f2`
echo $ip
echo "$ip $i.corp.amdocs.com $i" >> /etc/hosts
"
done
I am trying to fix /etc/hosts entry on multiple servers with a bash script, but it is dumping on hostname values on the file.
This worked:
#!/bin/bash
for i in illinrcmsg{002..003}
do
echo " ####################### $i #################### "
ssh -o StrictHostKeyChecking=no $i "
echo $i
cp /etc/hosts /etc/hosts_bak
#ip=`ifconfig | head -n 2 | tail -n 1 | awk '{print $2}' | cut -d':' -f2`
#echo $ip
echo "`nslookup illinrcmsg002 | tail -n 2 | head -n 1|awk '{print $2}'`
$i.corp.amdocs.com $i" >> /etc/hosts
"
done

Send message to group in Linux

I found the way to send message to currently logged in user by username:
who | grep username | cut -c1-20 | while read line; do printf "Message Text" | write $line ; done
However, is there anyway I can send message to currently logged in user by group?
Thanks for your help.
So... To send a message to every logged in user in a particular group, on every tty they're logged in to...
#!/bin/sh
usage() {
cat <<-EOT
Usage: writegroup groupname [message]
where [message] will be taken from stdin if not provided on the command line.
EOT
}
if [ $# -eq 0 ]; then
usage
exit 1
fi
groupname="$1"
shift
message="$*"
if [ -z "$message" ]; then
read message
fi
who | while read user tty junk; do
if groups "$user" | grep -wq "$groupname"; then
echo "$message" | write "$user" "$tty"
fi
done
Provided you set the variable group to the group you're interested in, you can try this:
for i in $(who -u | cut -d " " -f1 | sort | uniq); do if echo $(groups $i | cut -d " " -f3-) | grep $group >/dev/null; then echo "Message Text" | write $i; fi; done
$(who -u | cut -d " " -f1 | sort | uniq) gives the list of user logged in.
$(groups $i | cut -d " " -f3-) gives all groups a user belongs to.
The grep statement will reduce the list to the user belonging to the $group

Linux Script: How to check for any lines that starts with but not contain

I'm at the following directory...
/var/log/homes
and I would like a script to check for any lines starts with 'Error' and contains a word 'NotAuthorized' and not contains '13024' or '31071'.
Within /var/log/homes/, there are 700 files, but that doesn't matter...
Below is what I have...
#!/bin/bash
host=`hostname`
date=`date`
monitor='Error'
pattern='Error'
pattern2='NotAuthorized'
ignore=13024
ignore2=31071
logfile='/var/log/homes/*'
mailto='test#linux.com'
if [ -e $logfile ]
then
if [ `grep -i "$pattern" "$pattern2" "$logfile" | grep -v $ignore $ignore ]
then
echo "Errors found in $monitor at $date - see log $logfile on server $host for full details" |mail -s "ALERT - $errors in logs, please review" $mailto
elif [ `grep -i "$pattern2" "$logfile" |wc -l` -lt 1 ]
fi
else
echo "Logfile $logfile doesn't exist on server $host at $date, this is probably bad, please investigate" |mail -s "ALERT - $monitor monitor has an issue" $mailto
fi
This should work:
grep "^Error" $logfile | grep "NotAuthorized" | grep -v "13024" | grep -v "31071"
grep "^Error": get the lines starting ith "Error"
grep "NotAuthorized": get the lines containing "NotAuthorized"
grep -v "XXX": get the lines not containing "XXX"
It looks fine to me. Just cross check if you are using all of these conditions:
grep "^Error" $file | grep "NotAuthorized" | grep -Ev "31071|13024"
^^^^^^^ ^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^
lines starting with Error | |
containing NotAuthorized
exclude either lines containing 31071 or 13024
To know which file is it happening in, use -H option in grep:
grep -H "^Error" $file | grep "NotAuthorized" | grep -Ev "31071|13024"
So all together you can do:
grep -H "^Error" /var/log/homes/* | grep "NotAuthorized" | grep -Ev "31071|13024"
Print any lines that start with 'Error' and contain the word 'NotAuthorized' and does not contain '13024' or '31071':
sed -n '/^Error/{/NotAuthorized/{/13024/!{/31071/!p}}}'
This awk may do:
awk '/^Error/ && /NotAuthorized/ && !/31071|13024/' $logfile

Resources