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.

OPENNMS psql how to get host and catagories

 

#psql opennms -h localhost -d opennms


Paste sql command below

 SELECT

    n.nodesysname,

    COALESCE(s_cat.categoryname, 'no category') AS categoryname

  FROM

    node n

  LEFT JOIN

    category_node cn

  ON

    n.nodeid = cn.nodeid

  LEFT JOIN

    categories s_cat

  ON

    cn.categoryid = s_cat.categoryid;






opennms=>   SELECT

opennms->     n.nodesysname,

opennms->     COALESCE(s_cat.categoryname, 'no category') AS categoryname

opennms->   FROM

opennms->     node n

opennms->   LEFT JOIN

opennms->     category_node cn

opennms->   ON

opennms->     n.nodeid = cn.nodeid

opennms->   LEFT JOIN

opennms->     categories s_cat

opennms->   ON

opennms->     cn.categoryid = s_cat.categoryid order by nodesysname;

OPENNMS : How to get OID for custom scripts

add your script on target nodes.

edit snmpd.conf accordingly

restart snmpd

Run below command

# for i in `snmpwalk -v 2c -c TselSA 127.0.0.1 NET-SNMP-EXTEND-MIB::nsExtendObjects| grep "NET-SNMP-EXTEND-MIB::nsExtendOutLine." | cut -d "=" -f1`; do echo $i; echo "snmptranslate -On '$i'" >> a.sh ; done

NET-SNMP-EXTEND-MIB::nsExtendOutLine."OM_LATEST".1

NET-SNMP-EXTEND-MIB::nsExtendOutLine."OM_LATEST".2

NET-SNMP-EXTEND-MIB::nsExtendOutLine."OM_LATEST".3

NET-SNMP-EXTEND-MIB::nsExtendOutLine."OM_LATEST".4

NET-SNMP-EXTEND-MIB::nsExtendOutLine."billingApi".1

NET-SNMP-EXTEND-MIB::nsExtendOutLine."billingApi".2

NET-SNMP-EXTEND-MIB::nsExtendOutLine."billingApi".3

NET-SNMP-EXTEND-MIB::nsExtendOutLine."Erecharge_Session_JWT".1

# cat a.sh

snmptranslate -On 'NET-SNMP-EXTEND-MIB::nsExtendOutLine."OM_LATEST".1'

snmptranslate -On 'NET-SNMP-EXTEND-MIB::nsExtendOutLine."OM_LATEST".2'

snmptranslate -On 'NET-SNMP-EXTEND-MIB::nsExtendOutLine."OM_LATEST".3'

snmptranslate -On 'NET-SNMP-EXTEND-MIB::nsExtendOutLine."OM_LATEST".4'

snmptranslate -On 'NET-SNMP-EXTEND-MIB::nsExtendOutLine."billingApi".1'

snmptranslate -On 'NET-SNMP-EXTEND-MIB::nsExtendOutLine."billingApi".2'

snmptranslate -On 'NET-SNMP-EXTEND-MIB::nsExtendOutLine."billingApi".3'

snmptranslate -On 'NET-SNMP-EXTEND-MIB::nsExtendOutLine."Erecharge_Session_JWT".1'

# sh a.sh

.1.3.6.1.4.1.8072.1.3.2.4.1.2.9.79.77.95.76.65.84.69.83.84.1

.1.3.6.1.4.1.8072.1.3.2.4.1.2.9.79.77.95.76.65.84.69.83.84.2

.1.3.6.1.4.1.8072.1.3.2.4.1.2.9.79.77.95.76.65.84.69.83.84.3

.1.3.6.1.4.1.8072.1.3.2.4.1.2.9.79.77.95.76.65.84.69.83.84.4

.1.3.6.1.4.1.8072.1.3.2.4.1.2.10.98.105.108.108.105.110.103.65.112.105.1

.1.3.6.1.4.1.8072.1.3.2.4.1.2.10.98.105.108.108.105.110.103.65.112.105.2

.1.3.6.1.4.1.8072.1.3.2.4.1.2.10.98.105.108.108.105.110.103.65.112.105.3

.1.3.6.1.4.1.8072.1.3.2.4.1.2.21.69.114.101.99.104.97.114.103.101.95.83.101.115.115.105.111.110.95.74.87.84.1

Add above IOD to polling setting to monitor

Unix/linux: parameter now, 1 hour ago, 2 hours ago


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` 

Unix/linux: how to arithmetic in /bin/sh and grep log based on last hours event happened

 

# calculator

y0=0

y1=0

y2=0

y3=0

t0=0

t1=0

t2=0

t3=0


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`


# processing today loga

y0=`grep "unable to send message to peer," /logs/UL_$yesterday\_*| grep "$getdatenow" | wc -l`

y1=`grep "unable to send message to peer," /logs/UL_$yesterday\_*| grep "$getdate1hago" | wc -l`

y2=`grep "unable to send message to peer," /logs/UL_$yesterday\_*| grep "$getdate2hago" | wc -l`

y3=`grep "unable to send message to peer," /logs/UL_$yesterday\_*| grep "$getdate3hago" | wc -l`


# processing today loga


t0=`grep "unable to send message to peer," /logs/UL_$getdate\_*| grep "$getdatenow" | wc -l`

t1=`grep "unable to send message to peer," /logs/UL_$getdate\_*| grep "$getdate1hago" | wc -l`

t2=`grep "unable to send message to peer," /logs/UL_$getdate\_*| grep "$getdate2hago" | wc -l`

t3=`grep "unable to send message to peer," /logs/UL_$getdate\_*| grep "$getdate3hago" | wc -l`

NUMBER_ERROR_DETECTED=`expr "$y0 + $y1 + $y2 + $y3 + $t0 + $t1 + $t2 + $t3" | bc`



echo $NUMBER_ERROR_DETECTED

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...