Wednesday, 19 August 2015

Random Stuff 2

Calculate Response time of web page

#!/bin/sh

while true
do
script -c "time -p wget http://52.20.204.148/ -q --output-document=/dev/null 2>/tmp/tmp.log" -f /tmp/tmp.log > /dev/null
sleep 5
NUM=$(cat /tmp/tmp.log | grep "real" | cut -d " " -f2 | tr -cd '[:digit:].')
curl -X POST -d '[{"name":"foo","columns":["val"],"points":[['$NUM']]}]' "http://52.20.204.148:8086/db/server1/series?u=root&p=root"
done

========================================================================

Type of web server response per minute

#!/bin/bash

while true
do
#set -x
mkdir -p /home/ec2-user/tmp/percfile
rm -f /home/ec2-user/tmp/percfile/*
dd=`date +%d`
mon=`date +%m`
yea=`date +%Y`
for (( i = 1; i >0; i-- ))
do
grep $(date +%d/%b/%Y:%H:%M -d "-$i  min") /var/log/httpd/access_log >> /home/ec2-user/tmp/percfile/log1mins
GOOD=$(grep "200" /home/ec2-user/tmp/percfile/log1mins | wc -l)
BAD=$(grep -v "200" /home/ec2-user/tmp/percfile/log1mins | wc -l)
curl -X POST -d '[{"name":"response","columns":["good","bad"],"points":[['$GOOD','$BAD']]}]' "http://localhost:8086/db/server1/series?u=root&p=root"
done
sleep 1m
done

========================================================================

No of hits to a web server per second

#!/bin/sh
frequency=1
lastCount=`wc -l /var/log/httpd/access_log | sed 's/\([0-9]*\).*/\1/'`
while true
do
newCount=`wc -l /var/log/httpd/access_log | sed 's/\([0-9]*\).*/\1/'`
diff=$(( newCount - lastCount ))
rate=$(echo "$diff / $frequency" |bc -l)
#echo $rate
curl -X POST -d '[{"name":"hps","columns":["val"],"points":[['$rate']]}]' "http://localhost:8086/db/server1/series?u=root&p=root"
lastCount=$newCount
sleep $frequency
done

========================================================================

Gets your public IP

#!/bin/bash
while true
do
sleep 1
myip=$(curl wgetip.com 2> /dev/null) > /dev/null
sleep 1
curl -X POST -d '[{"name":"response","columns":["val1"],"points":[["'$myip'"]]}]' "http://$myip:8086/db/server1/series?u=root&p=root"
sleep 1
done

========================================================================

Service status in boolean

#!/bin/bash
while true
do
sleep 1
pidof  httpd >/dev/null
if [[ $? -eq 0 ]] ; then
curl -X POST -d '[{"name":"response","columns":["val2"],"points":[[0]]}]' "http://localhost:8086/db/server1/series?u=root&p=root"
else
curl -X POST -d '[{"name":"response","columns":["val2"],"points":[[1]]}]' "http://localhost:8086/db/server1/series?u=root&p=root"
sleep 1
fi
done

========================================================================

Type of web server response per minute

#!/bin/sh

#while true
#do
NOW=$( tail -n1 /var/log/httpd/access_log | head -1 | awk -F'[][]' '{ gsub(/\//," ",$2); sub(/:/," ",$2); "date +%s -d \""$2"\""|getline d; print d;}' )
REF=$(( $NOW - 60 ))
NUM=$REF
LINE=1
COUNT=0
BAD=0
while [ $NUM -ge $REF ]
do
NUM=$( tail -n$LINE /var/log/httpd/access_log | head -1 | awk -F'[][]' '{ gsub(/\//," ",$2); sub(/:/," ",$2); "date +%s -d \""$2"\""|getline d; print d;}' )
tail -n$LINE /var/log/httpd/access_log | head -1 | grep -i "200" >/dev/null && let COUNT=$COUNT+1
tail -n$LINE /var/log/httpd/access_log | head -1 | grep -v "200" >/dev/null && let BAD=$BAD+1
let LINE=$LINE+1
done
let COUNT=$COUNT-1
let BAD=BAD-1
curl -X POST -d '[{"name":"response","columns":["good","bad"],"points":[['$COUNT','$BAD']]}]' "http://localhost:8086/db/server1/series?u=root&p=root"
#sleep 15m
#done

========================================================================

Sql access through php

<?php
$servername = "localhost";
$username = "root";
$password = "qwe";
$dbname = "grafana";
$homepage = file_get_contents("/home/comb.txt");

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "UPDATE dashboard SET data='$homepage' WHERE slug='dash2'";

if (mysqli_query($conn, $sql)) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . mysqli_error($conn);
}

mysqli_close($conn);
?>

========================================================================


Run local script on remote server ( with root priviliges )

Parent Script :

#!/bin/bash
SOURCEFILE=$1
SOURCEIP=$2
TARGETIP=$3
FILE=$(basename $1)
echo "Enter the path"
read path
scp -3 -r -i /root/asw/Admaxim-23-06-2015.pem ec2-user@$SOURCEIP:$SOURCEFILE ec2-user@$TARGETIP:/tmp    
ssh -t -t -i /root/asw/Admaxim-23-06-2015.pem ec2-user@$TARGETIP sudo -i "bash -s" -- < ./remold.sh "$FILE" "$path"
exit


Child Script :

#!/bin/bash
cd /tmp
yes | cp -r $1 $2
rm -fr $1
exit
exit

========================================================================

SSH into a remote server with non root account with login key and go root automatically :


#!/bin/bash
ssh -t -t -i /root/asw/keyfile.pem user@$1 sudo -i "bash -s"

========================================================================

Pass defined column value to loop  :


#!/bin/bash

while read server
do
echo ${server}
done < final | awk '{print $1}'

========================================================================

DB creation :


curl -X POST "http://localhost:8086/db?u=root&p=root" -d '{"name": "'${server}'"}'

========================================================================

No comments:

Post a Comment