Wednesday, 19 August 2015

Random Stuff

This is the bash script

#!/bin/bash

NOW=$( tail -n1 log1 | awk -F'[][]' '{ gsub(/\//," ",$2); sub(/:/," ",$2); "date +%s -d \""$2"\""|getline d; print d;}' ) ## get last line timestamp
REF=$(( $NOW - 15*60 ))  ##previous 15 minute
NUM=$REF
LINE=1
COUNT=0
while [ $NUM -ge $REF ] ## Run until the condition is not met
do
NUM=$( tail -n$LINE log1 | head -1 | awk -F'[][]' '{ gsub(/\//," ",$2); sub(/:/," ",$2); "date +%s -d \""$2"\""|getline d; print d;}' ) ## pass value of each time stamp and compare with the while condition
tail -n$LINE log1 | head -1 | grep -i "ready" && let COUNT=$COUNT+1  ## count the number of ready occurrence
let LINE=$LINE+1 ## for backward iteration
done
echo "Total Error = "$COUNT" " ## display count

The log file used is "log1"

10.183.253.51 - - [18/Mar/2013:22:25:00 -0700] POST /adserver/getOpenRTBSmaatoInfo HTTP/1.1 204 - 1
10.183.253.51 - - [18/Mar/2013:22:26:00 -0700] POST /adserver/getOpenRTBSmaatoInfo HTTP/1.1 204 - 1
10.183.253.51 - - [18/Mar/2013:22:27:00 -0700] POST /adserver/getOpenRTBSmaatoInfo HTTP/1.1 204 - 1
10.183.253.51 - - [18/Mar/2013:22:28:00 -0700] GET /adserver/ready HTTP/1.1 200 1 0
10.183.253.51 - - [18/Mar/2013:22:29:00 -0700] POST /adserver/getOpenRTBSmaatoInfo HTTP/1.1 204 - 1
10.183.253.51 - - [18/Mar/2013:22:30:00 -0700] POST /adserver/getOpenRTBSmaatoInfo HTTP/1.1 204 - 1
10.183.253.51 - - [18/Mar/2013:22:31:00 -0700] POST /adserver/getOpenRTBSmaatoInfo HTTP/1.1 204 - 1
10.183.253.51 - - [18/Mar/2013:22:32:00 -0700] POST /adserver/getOpenRTBSmaatoInfo HTTP/1.1 204 - 1
10.183.253.51 - - [18/Mar/2013:22:33:00 -0700] POST /adserver/getOpenRTBSmaatoInfo HTTP/1.1 204 - 1
10.183.253.51 - - [18/Mar/2013:22:34:00 -0700] POST /adserver/getOpenRTBSmaatoInfo HTTP/1.1 204 - 1
10.183.253.51 - - [18/Mar/2013:22:35:00 -0700] GET /adserver/ready HTTP/1.1 200 1 0
10.183.253.51 - - [18/Mar/2013:22:36:00 -0700] POST /adserver/getOpenRTBSmaatoInfo HTTP/1.1 204 - 1
10.183.253.51 - - [18/Mar/2013:22:37:00 -0700] POST /adserver/getOpenRTBSmaatoInfo HTTP/1.1 204 - 1
10.183.253.51 - - [18/Mar/2013:22:38:00 -0700] POST /adserver/getOpenRTBSmaatoInfo HTTP/1.1 204 - 1
10.183.253.51 - - [18/Mar/2013:22:39:00 -0700] POST /adserver/getOpenRTBSmaatoInfo HTTP/1.1 204 - 1
10.183.253.51 - - [18/Mar/2013:22:40:00 -0700] POST /adserver/getOpenRTBSmaatoInfo HTTP/1.1 204 - 1
10.183.253.51 - - [18/Mar/2013:22:41:00 -0700] POST /adserver/getOpenRTBSmaatoInfo HTTP/1.1 204 - 1
10.183.253.51 - - [18/Mar/2013:22:42:00 -0700] GET /adserver/ready HTTP/1.1 200 1 0
10.183.253.51 - - [18/Mar/2013:22:43:00 -0700] POST /adserver/getOpenRTBSmaatoInfo HTTP/1.1 204 - 1
10.183.253.51 - - [18/Mar/2013:22:44:00 -0700] POST /adserver/getOpenRTBSmaatoInfo HTTP/1.1 204 - 1
10.183.253.51 - - [18/Mar/2013:22:45:00 -0700] POST /adserver/getOpenRTBSmaatoInfo HTTP/1.1 204 - 1
10.183.253.51 - - [18/Mar/2013:22:46:00 -0700] POST /adserver/getOpenRTBSmaatoInfo HTTP/1.1 204 - 1
10.183.253.51 - - [18/Mar/2013:22:47:00 -0700] GET /adserver/ready HTTP/1.1 200 1 0
10.183.253.51 - - [18/Mar/2013:22:48:00 -0700] POST /adserver/getOpenRTBSmaatoInfo HTTP/1.1 204 - 1
10.183.253.51 - - [18/Mar/2013:22:49:00 -0700] POST /adserver/getOpenRTBSmaatoInfo HTTP/1.1 204 - 1
10.183.253.51 - - [18/Mar/2013:22:50:00 -0700] POST /adserver/getOpenRTBSmaatoInfo HTTP/1.1 204 - 1
10.183.253.51 - - [18/Mar/2013:22:51:00 -0700] POST /adserver/getOpenRTBSmaatoInfo HTTP/1.1 204 - 1
10.183.253.51 - - [18/Mar/2013:22:52:00 -0700] GET /adserver/ready HTTP/1.1 200 1 0
10.183.253.51 - - [18/Mar/2013:22:53:00 -0700] POST /adserver/getOpenRTBSmaatoInfo HTTP/1.1 204 - 1
10.183.253.51 - - [18/Mar/2013:22:54:00 -0700] POST /adserver/getOpenRTBSmaatoInfo HTTP/1.1 204 - 1
10.183.253.51 - - [18/Mar/2013:22:55:00 -0700] POST /adserver/getOpenRTBSmaatoInfo HTTP/1.1 204 - 1

x-----------------------------------------------------------------------------------------------------------------------x

This is the bash script

#!/bin/bash

NOW=$( tail -n1 log2 | awk -F'[][]' '{ gsub(/\//," ",$2); sub(/:/," ",$2); "date +%s -d \""$2"\""|getline d; print d;}' ) ## take first time instance ( user specified can also be parsed )
REF=$(( $NOW - 15*60 ))  ##previous 15 minute PN. second time instance ( user specified can also be parsed )
NUM=$REF
LINE=1
while [ $NUM -ge $REF ]
do
NUM=$( tail -n$LINE log2 | head -1 | awk -F'[][]' '{ gsub(/\//," ",$2); sub(/:/," ",$2); "date +%s -d \""$2"\""|getline d; print d;}' )
tail -n$LINE log2 | head -1 | grep -v "click" | cut -d "=" -f4 | cut -d " " -f1 >> number.txt    ## Values retrieved from the log are temporarily stored here
SUM=$(awk '{ SUM += $1} END { printf "%.2f",  SUM }' number.txt) ## values are retireved and added up gradually step by step
let LINE=$LINE+1 ## backward iteration for faster processing
done
rm -fr number.txt ## external temporary file created has been removed
echo "The Sum of Bit is "$SUM"" ##  print the sum

The log file used is "log2"

10.183.252.22 - - [16/Mar/2013:00:00:06 -0700] GET /adtracker/track/track/imp?id=11062&cpnsite_id=23709&bid=0.3 HTTP/1.1 3
10.183.252.22 - - [16/Mar/2013:00:01:06 -0700] GET /adtracker/track/track/imp?id=11390&cpnsite_id=23609&bid=0.2 HTTP/1.1 3
10.183.252.22 - - [16/Mar/2013:00:02:06 -0700] GET /adtracker/track/track/imp?id=11390&cpnsite_id=23609&bid=0.2 HTTP/1.1 3
10.183.252.22 - - [16/Mar/2013:00:03:06 -0700] GET /adtracker/track/track/click?id=11062 HTTP/1.1 3
10.183.252.22 - - [16/Mar/2013:00:04:06 -0700] GET /adtracker/track/track/click?id=11062 HTTP/1.1 3
10.183.252.22 - - [16/Mar/2013:00:05:06 -0700] GET /adtracker/track/track/click?id=11062 HTTP/1.1 3
10.183.252.22 - - [16/Mar/2013:00:06:06 -0700] GET /adtracker/track/track/click?id=11062 HTTP/1.1 3
10.183.252.22 - - [16/Mar/2013:00:07:06 -0700] GET /adtracker/track/track/click?id=11062 HTTP/1.1 3
10.183.252.22 - - [16/Mar/2013:00:08:06 -0700] GET /adtracker/track/track/click?id=11062 HTTP/1.1 3
10.183.252.22 - - [16/Mar/2013:00:09:06 -0700] GET /adtracker/track/track/click?id=11062 HTTP/1.1 3
10.183.252.22 - - [16/Mar/2013:00:10:06 -0700] GET /adtracker/track/track/click?id=11062 HTTP/1.1 3
10.183.252.22 - - [16/Mar/2013:00:11:06 -0700] GET /adtracker/track/track/click?id=11062 HTTP/1.1 3
10.183.252.22 - - [16/Mar/2013:00:12:06 -0700] GET /adtracker/track/track/click?id=11062 HTTP/1.1 3
10.183.252.22 - - [16/Mar/2013:00:13:06 -0700] GET /adtracker/track/track/imp?id=11390&cpnsite_id=23609&bid=0.2 HTTP/1.1 3
10.183.252.22 - - [16/Mar/2013:00:14:06 -0700] GET /adtracker/track/track/imp?id=11390&cpnsite_id=23609&bid=0.2 HTTP/1.1 3
10.183.252.22 - - [16/Mar/2013:00:15:06 -0700] GET /adtracker/track/track/imp?id=11390&cpnsite_id=23609&bid=0.2 HTTP/1.1 3
10.183.252.22 - - [16/Mar/2013:00:16:06 -0700] GET /adtracker/track/track/imp?id=11390&cpnsite_id=23609&bid=0.2 HTTP/1.1 3
10.183.252.22 - - [16/Mar/2013:00:17:06 -0700] GET /adtracker/track/track/imp?id=11390&cpnsite_id=23609&bid=0.2 HTTP/1.1 3
10.183.252.22 - - [16/Mar/2013:00:18:06 -0700] GET /adtracker/track/track/imp?id=11390&cpnsite_id=23609&bid=0.2 HTTP/1.1 3
10.183.252.22 - - [16/Mar/2013:00:19:06 -0700] GET /adtracker/track/track/click?id=11062 HTTP/1.1 3
10.183.252.22 - - [16/Mar/2013:00:20:06 -0700] GET /adtracker/track/track/click?id=11062 HTTP/1.1 3
10.183.252.22 - - [16/Mar/2013:00:21:06 -0700] GET /adtracker/track/track/click?id=11062 HTTP/1.1 3
10.183.252.22 - - [16/Mar/2013:00:22:06 -0700] GET /adtracker/track/track/click?id=11062 HTTP/1.1 3
10.183.252.22 - - [16/Mar/2013:00:23:06 -0700] GET /adtracker/track/track/click?id=11062 HTTP/1.1 3
10.183.252.22 - - [16/Mar/2013:00:24:06 -0700] GET /adtracker/track/track/click?id=11062 HTTP/1.1 3
10.183.252.22 - - [16/Mar/2013:00:26:06 -0700] GET /adtracker/track/track/click?id=11062 HTTP/1.1 3
10.183.252.22 - - [16/Mar/2013:00:27:06 -0700] GET /adtracker/track/track/click?id=11062 HTTP/1.1 3
10.183.252.22 - - [16/Mar/2013:00:28:06 -0700] GET /adtracker/track/track/imp?id=11390&cpnsite_id=23609&bid=0.2 HTTP/1.1 3
10.183.252.22 - - [16/Mar/2013:00:29:06 -0700] GET /adtracker/track/track/imp?id=11390&cpnsite_id=23609&bid=0.2 HTTP/1.1 3
10.183.252.22 - - [16/Mar/2013:00:30:06 -0700] GET /adtracker/track/track/click?id=11062 HTTP/1.1 3

x-----------------------------------------------------------------------------------------------------------------------x

Just enter the below command at the shell

# cat log2 | grep -v "click" | cut -d "?" -f2 | cut -d "&" -f1

The log file used is "log2"

All ready given above.

x-----------------------------------------------------------------------------------------------------------------------x

To increase the file descriptor limit, edit the kernel parameter file /etc/sysctl.conf.

# nano /etc/sysctl.conf

fs.file-max = 600000

Apply the changes without a reboot:

# sysctl -p

To check the new limit :

# more /proc/sys/fs/file-max

x-----------------------------------------------------------------------------------------------------------------------x

Sync Time between servers

Option 1

# date --set="$(ssh user@server date)"

Or

# ssh user@host "date --set \"$(date)\""

manually on all three servers

Option 2

‘ntpdate 192.168.1.1’ to be done on each server

or add

server 192.168.1.1 prefer to /etc/ntp.conf on each server

although we will require a self hosted NTP server.

Option 3

Set all 3 server timezone to same timezone and use ntpdate pool.ntp.org to get them adjusted to same time.
Offset of the servers with each other can be checked by running command ntpq -p and by adding each servers ip's between them.
The offset is usually in milli-seconds and that too because of latency.

x-----------------------------------------------------------------------------------------------------------------------x

Change Timezone of a machine

# cp /etc/localtime /root/old.timezone
# rm /etc/localtime
# ln -s /usr/share/zoneinfo/America/New_York /etc/localtime
# nano /etc/sysconfig/clock  /* to make the change permanent */

ZONE=”America/New_York”  /* change value according to your need */

reboot the machine and then :

# date

or

# ntpdate pool.ntp.org     /* NTP service should be installed and running */

x-----------------------------------------------------------------------------------------------------------------------x

We are assuming here that all 5 servers are already added in known host and id_rsa.pib file has been transferred from sender to each receiving server and added in ~/.ssh/authorized_keys. Also root user is allowed login for all servers.

This is the bash script

#/usr/bin/bash

# There are three variables accepted via commandline
# $1 = first parameter (/source_path/source_filename)
# $2 = second parameter (/target_directory/)
# $3 = third paramter (file that contains list of hosts)

SOURCEFILE=$1
TARGETDIR=$2
HOSTFILE=$3

if [ -f $SOURCEFILE ] ## check for file existence
then
   printf "File found, preparing to transfer\n"
   while read server  ## initialize array
   do
      scp -vp $SOURCEFILE ${server}:$TARGETDIR  ## do transfer accordingly
   done < $HOSTFILE ## list of ip's which are stored in an external file
else
   printf "File \"$SOURCEFILE\" not found\n"
   exit 0
fi
exit 0

Command at prompt # ./scp.sh /file/to/be/transferred /destination/of/file/to/be/ /directory/of/ip/list/network.txt

The file used is "network.txt" for IP list

192.168.119.135
192.168.119.136
192.168.119.137
192.168.119.138
192.168.119.139

x-----------------------------------------------------------------------------------------------------------------------x

No comments:

Post a Comment