Thursday, February 18, 2021

UNIX: How to print column nicely using printf

[user@hostfwnms1-oam tmp]# cat b.sh

printf "%-26s %-19s %-8s %-8s %-s %-s\n" HOSTNAME IP PING SNMPWALK 0-ok 1-fail

for i in `cat newhost2021 | awk '{print $1}'`

do

HOSTNAME=`cat newhost2021 | grep $i | awk '{print $2}'`

HOST=$i

ping -c 3 -W 2 $i > /dev/null

PINGSTATUS=$?

snmpwalk -v 2c -c public $i &> /dev/null

SNMPWLKSTATUS=$?

#echo "$HOSTNAME $HOST $PINGSTATUS $SNMPWLKSTATUS"| awk '{ print $1"\t\t" $2"\t\t" $3"\t\t" $4}'

echo "$HOSTNAME $HOST $PINGSTATUS $SNMPWLKSTATUS"| awk '{ printf "%-26s %-19s %-8s %-8s\n", $1, $2, $3, $4}'

done


[user@hostfwnms1-oam tmp]# sh b.sh

HOSTNAME                   IP                  PING     SNMPWALK 0-ok 1-fail

hostfwhostlab2-ilo          101.123.125.18       0        1       

hostfwsanctr05ctr1          101.123.125.22       0        1       

hostfwsanctr05ctr2          101.123.125.23       1        1       

hostcdrhost1-ilo            101.123.125.24       0        1       

hostcdrhost1-oam            101.123.126.28       0        1       

hostcdrapp1-oam             101.123.126.38       0        0       

hostfwmgmtsw                101.123.125.27       0        0       

[user@hostfwnms1-oam tmp]#

Tuesday, January 26, 2021

Unix Scripting : extract value at Nth line after string

 To extract the Nth line after a matching pattern you want:

awk 'c&&!--c;/pattern/{c=N}' file

sample use in script
#! /bin/sh
# check pid for OrderManagement 
PID=`ps -ef | grep Order | grep OrderManagement| awk '{print $2}'`
if [ -z "$PID" ]
then
# return 0 if PID does not exist
echo 0
else
# If PID exist ,test if jmap okay
if jmap -heap $PID > /dev/null 2>&1
then
echo pid is $PID
#run jmap -heap PID
/usr/bin/jmap -heap $PID | awk 'c&&!--c;/G1 Heap/{c=5}' | awk -F% '{print $1}' | cut -d. -f1 | sed 's/ //g'
else 
# return 0 if jmap command fails
echo 0
fi
fi


Tuesday, November 24, 2020

Unix/Linux Script how to print time 1 hour 2 hours ago

 $ cat testtime.sh

#!/bin/sh

###################### sample ###########################

#getdatenow=`date +%Y/%m/%d" "%H`

#getdate1hago=`date --date="1 hour ago" +%Y/%m/%d" "%H`

#getdate2hago=`date --date="2 hour ago" +%Y/%m/%d" "%H`

#getdate3hago=`date --date="3 hour ago" +%Y/%m/%d" "%H`

#########################################################


#########################################################

# for 0 hour, 1 hour, 2 hours .. ago

#########################################################

for i in 0 1 2 3 4 5

do

echo $i

datehour=`date --date="$i hour ago" +%Y/%m/%d" "%H`

echo $datehour

done


$ vi testtime.sh

$ sh testtime.sh

0

2020/11/24 11

1

2020/11/24 10

2

2020/11/24 09

3

2020/11/24 08

4

2020/11/24 07

5

2020/11/24 06

$ date

Tue Nov 24 11:56:11 WIB 2020

$

Unix/Linux Scripts to check daily logfile status

 # This is to check dump files are completed for last cronjob

DATE=`date --date="1 days ago" +"%d%m%Y"`

cd /var/DUMPDir

dump1date1=`ls -l --time-style='+%d%m%Y' dump_file1.csv| awk '{print $6}'`

dump1date2=`ls -l --time-style='+%d%m%Y' dump_file2.csv| awk '{print $6}'`

dump1date3=`ls -l --time-style='+%d%m%Y' dump_file3.csv| awk '{print $6}'`

dump1date4=`ls -l --time-style='+%d%m%Y' dump_file4.csv| awk '{print $6}'`

dump1date5=`ls -l --time-style='+%d%m%Y' dump_file5.csv| awk '{print $6}'`

dump1date6=`ls -l --time-style='+%d%m%Y' dump_file6.csv| awk '{print $6}'`

dump1date7=`ls -l --time-style='+%d%m%Y' dump_file7.csv| awk '{print $6}'`

dump1date8=`ls -l --time-style='+%d%m%Y' dump_file8.csv| awk '{print $6}'`

dump1date9=`ls -l --time-style='+%d%m%Y' dump_file9.csv| awk '{print $6}'`



echo "=============================dump files========================"

echo "Checking DUMP files for $DATE"

if [ "$dump1date1" == "$DATE" -a "$dump1date2" == "$DATE" -a "$dump1date3" == "$DATE" -a "$dump1date4" == "$DATE" -a "$dump1date5" == "$DATE" -a "$dump1date6" == "$DATE" -a "$dump1date7" == "$DATE" -a "$dump1date8" == "$DATE" -a "$dump1date9" == "$DATE" ]

then

echo $DATE > /home/user/DUMP_date.txt

echo "dump files date is current $DATE"

echo "Dump : 1 / 1"

else

LAST_SUC_RUN=`cat /home/user/DUMP_date.txt`

echo "dump files date is not current, last successful run: $LAST_SUC_RUN"

echo "Dump : 0 / 1"


fi


Monday, November 23, 2020

Unix/linux how to configure SSH proxy

Let say, server A B C

Server A cannot SSH to C directly

but B can SSH to C(192.168.66.29)

A can SSH to B(192.168.68.90)

Below are the setting

A .ssh]$ ls

config  id_rsa  id_rsa.pub  known_hosts

A .ssh]$ cat id_rsa.pub

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDTGmoWURAB7OGw7T+r8stLraf5j8FdHNgE4SdVM0L8Zop6AIKcFhMa2suOeKzx+B4VXH54DT5vmV2X9iUrWQRqTODz5XaXX3Ck4VPnKawyOra8aLmqEfTS2Q4auKkb0rzXoEjNCu7YDttYjkiUpn/dZ2yw5TfXaKJrnIBqrFuxBh6sLHaYcRI+jzPPik1HIBnSv2rLVBtI6yF7mXom7stRt7dVe1CWvMCLLCalJMtgy+bx4wLbr61moAmSD0j/GTQWuek7/2fqpMEIMl4Y57B4T6sTSSsCJRs5SFxVQu/l908UjWTOhpjToJzltkmJNA2IovyoIBAwSFLabwb9+6PH useid@A

A .ssh]$


1- Update .ssh/authorized_keys on user1@192.168.68.80 with id_rsa.pub per above on hostA n B

2- Update .ssh/authorized_keys on user2@192.168.66.29 

3- Update per below on A

A ~]$ ls -l /home/openet/.ssh/config

-rwx------ 1 user2 user2 80 Sep 26 13:46 /home/user2/.ssh/config

A ]$ cat /home/openet/.ssh/config

Host 192.168.66.29

     ProxyCommand ssh -l user1 192.168.68.90 exec nc %h %p

A }$




Unix/Linux mailx send email via relay

 

192.168.1.1 is the ip of mail relay (example)

$ mailx -v -s "testing" -S smtp=smtp://192.168.1.1 -S from="test@yourdomain.com" 

OPENNMS How to test polling manually

 OPENNMS how to test snmp port ok


# ssh -p 8101 admin@localhost

Password authentication

Password: 


   ____                   _   _ __  __  _____  

  / __ \                 | \ | |  \/  |/ ____| 

 | |  | |_ __   ___ _ __ |  \| | \  / | (___   

 | |  | | '_ \ / _ \ '_ \| . ` | |\/| |\___ \  

 | |__| | |_) |  __/ | | | |\  | |  | |____) | 

  \____/| .__/ \___|_| |_|_| \_|_|  |_|_____/  

        | |                                    

        |_|                                    


  OpenNMS (21.0.3) on Apache Karaf (4.1.2)


Hit '<tab>' for a list of available commands

and '[cmd] --help' for help on a specific command.

Hit '<ctrl-d>' to exit this console.


admin@opennms()> poll org.opennms.netmgt.poller.monitors.SnmpMonitor 192.168.70.10

...

Service is Down on 192.168.70.10 using org.opennms.netmgt.poller.monitors.SnmpMonitor

Reason: SNMP poll failed, addr=192.168.70.10 oid=.1.3.6.1.2.1.1.2.0

admin@opennms()> poll org.opennms.netmgt.poller.monitors.SnmpMonitor 192.168.70.10                                                                                                          


Service is Up on 192.168.70.10 using org.opennms.netmgt.poller.monitors.SnmpMonitor:

response-time: 0.9282

admin@opennms()> poll org.opennms.netmgt.poller.monitors.SnmpMonitor 192.168.70.14


Service is Up on 192.168.70.14 using org.opennms.netmgt.poller.monitors.SnmpMonitor:

response-time: 1.1582

admin@opennms()> poll org.opennms.netmgt.poller.monitors.SnmpMonitor 192.168.70.15


Service is Up on 192.168.70.15 using org.opennms.netmgt.poller.monitors.SnmpMonitor:

response-time: 1.3335

admin@opennms()> poll org.opennms.netmgt.poller.monitors.SnmpMonitor 192.168.70.15                                                                                                          


Service is Up on 192.168.70.15 using org.opennms.netmgt.poller.monitors.SnmpMonitor:

response-time: 1.0122

admin@opennms()> Connection to localhost closed by remote host.                                                                                                                             

Connection to localhost closed.

# Connection closing...Socket close.

UNIX: How to print column nicely using printf

[user@hostfwnms1-oam tmp]# cat b.sh printf "%-26s %-19s %-8s %-8s %-s %-s\n" HOSTNAME IP PING SNMPWALK 0-ok 1-fail for i in `cat n...