AWS Script For Getting Bill

#!/bin/sh
export AWS_CLOUDWATCH_HOME=/home/charlie/cloudwatch/CloudWatch-1.0.13.4
export JAVA_HOME=/usr/lib/jvm/default-java

# Get the timestamp from 5 hours ago, to avoid getting > 1440 metrics (which errors).
# also, remove the +0000 from the timestamp, because the cloudwatch cli tries to enforce
# ISO 8601, but doesn’t understand it.
DATE=$(date –iso-8601=hours -d “5 hours ago” |sed s/+.*//)

#echo $COST

SERVICES=’AmazonS3 ElasticMapReduce AmazonRDS AmazonDynamoDB AWSDataTransfer AmazonEC2 AWSQueueService’

for service in $SERVICES; do

COST=$(/home/charlie/cloudwatch/CloudWatch-1.0.13.4/bin/mon-get-stats EstimatedCharges –aws-credential-file ~/.ec2_credentials –namespace “AWS/Billing” –statistics Sum –dimensions “ServiceName=${service},Currency=USD” –start-time $DATE |tail -1 |awk ‘{print $3}’)

if [ -z $COST ]; then
echo “failed to retrieve $service metric from CloudWatch..”
else
echo “stats.prod.ops.billing.ec2_${service} $COST `date +%s`” |nc graphite.example.com 2023
fi

done

Advertisements

Cacti Server

Installing and configuring Cacti Server Step by Step:

Cacti is an open source web based network monitoring tool front-end for the data logging tool called RRDtool. It’s allow users to check the services at an interval of time and resulting in the graph format.

SO First of all Install following Packages or Install LAMP Server

 Install apache, php and mysql

# yum install httpd httpd-devel

# yum install mysql mysql-server

# yum install php-mysql php-pear php-common php-gd php-devel php php-mbstring php-cli

Install SNMP and rrd-tool.

# yum install php-snmp

# yum install net-snmp-utils net-snmp-libs

# yum install rrdtool

 Configuring SNMP

# mv /etc/snmp/snmpd.conf snmpd.conf.org

# touch /etc/snmp/snmpd.conf

com2sec local     localhost           public

group MyRWGroup v1         local

group MyRWGroup v2c       local

group MyRWGroup usm       local

view all   included .1                               80

access MyRWGroup “”     any       noauth   exact all   all   none

syslocation srvroom , linux monitor

syscontact krizna (admin@example.com)

and save

# chkconfig –level 235 snmpd on

# service snmpd start

Now Configure MySQl Database:

 # mysqladmin -u root password YOUR-PASSWORD-HERE

# mysql -u root -p.

mysql> create database Jitendra_bauddha;

mysql> show databases;

mysql> GRANT ALL ON Jitendra_bauddha.* TO jitendra@localhost IDENTIFIED BY ‘somepassword’;

mysql> flush privileges;

mysql> exit;

Now Install Cacti Server :

## RHEL/CentOS 6 64-Bit ##

# wget http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

# rpm -ivh epel-release-6-8.noarch.rpm

# yum install cacti

# rpm -ql cacti | grep cacti.sql

Now Configure Cacti Server :

# mysql -u cacti -p cacti < /usr/share/doc/cacti-0.8.8b/cacti.sql

# vi /etc/cacti/db.php

$database_default = “cactidb”;

$database_hostname = “localhost”;

$database_username = “usercacti”;

$database_password = “passwordcacti”;

Now

Open /etc/httpd/conf.d/cacti.conf file and comment all “Deny from all” lines.

# vi /etc/httpd/conf.d/cacti.conf

# service httpd restart

Now Configure IP tables for cacti :

# iptables -A INPUT -p udp -m state –state NEW –dport 80 -j ACCEPT

# iptables -A INPUT -p tcp -m state –state NEW –dport 80 -j ACCEPT

# service iptables save

# systemctl restart httpd.service

#*/5 * * * *   cacti   /usr/bin/php /usr/share/cacti/poller.php > /dev/null 2>&1

Script for Server Health Check

#################################################################################
# This Script is written by – Jitendra Bauddha                                                                                # #################################################################################
#! /bin/bash
# (copy paste there lines and in a file and save file with .sh extention and give u+x permission)

# clear the screen
clear

unset tecreset os architecture kernelrelease internalip externalip nameserver loadaverage

while getopts iv name
do
case $name in
i)iopt=1;;
v)vopt=1;;
*)echo “Invalid arg”;;
esac
done

if [[ ! -z $iopt ]]
then
{
wd=$(pwd)
basename “$(test -L “$0” && readlink “$0” || echo “$0″)” > /tmp/scriptname
scriptname=$(echo -e -n $wd/ && cat /tmp/scriptname)
su -c “cp $scriptname /usr/bin/monitor” root && echo “Congratulations! Script Installed, now run monitor Command” || echo “Installation failed”
}
fi

if [[ ! -z $vopt ]]
then
{
echo -e “Server_health monitor version 1.0nDesigned by Jitendra BauddhanReleased Under Apache 2.2 License”
}
fi

if [[ $# -eq 0 ]]
then
{

# Define Variable tecreset
tecreset=$(tput sgr0)

# Check if connected to Internet or not
ping -c 1 google.com &> /dev/null && echo -e ‘E[32m'”Internet: $tecreset Connected” || echo -e ‘E[32m'”Internet: $tecreset Disconnected”

# Check OS Type
os=$(uname -o)
echo -e ‘E[32m'”Operating System Type :” $tecreset $os

# Check OS Release Version and Name
cat /etc/os-release | grep ‘NAME|VERSION’ | grep -v ‘VERSION_ID’ | grep -v ‘PRETTY_NAME’ > /tmp/osrelease
echo -n -e ‘E[32m'”OS Name :” $tecreset && cat /tmp/osrelease | grep -v “VERSION” | cut -f2 -d”
echo -n -e ‘E[32m'”OS Version :” $tecreset && cat /tmp/osrelease | grep -v “NAME” | cut -f2 -d”

# Check Architecture
architecture=$(uname -m)
echo -e ‘E[32m'”Architecture :” $tecreset $architecture

# Check Kernel Release
kernelrelease=$(uname -r)
echo -e ‘E[32m'”Kernel Release :” $tecreset $kernelrelease

# Check hostname
echo -e ‘E[32m'”Hostname :” $tecreset $HOSTNAME

# Check Internal IP
internalip=$(hostname -I)
echo -e ‘E[32m'”Internal IP :” $tecreset $internalip

# Check External IP
externalip=$(curl -s ipecho.net/plain;echo)
echo -e ‘E[32m'”External IP : $tecreset “$externalip

# Check DNS
nameservers=$(cat /etc/resolv.conf | sed ‘1 d’ | awk ‘{print $2}’)
echo -e ‘E[32m'”Name Servers :” $tecreset $nameservers

# Check Logged In Users
who>/tmp/who
echo -e ‘E[32m'”Logged In users :” $tecreset && cat /tmp/who

# Check RAM and SWAP Usages
free -h | grep -v + > /tmp/ramcache
echo -e ‘E[32m'”Ram Usages :” $tecreset
cat /tmp/ramcache | grep -v “Swap”
echo -e ‘E[32m'”Swap Usages :” $tecreset
cat /tmp/ramcache | grep -v “Mem”

# Check Disk Usages
df -h| grep ‘Filesystem|/dev/sda*’ > /tmp/diskusage
echo -e ‘E[32m'”Disk Usages :” $tecreset
cat /tmp/diskusage

# Check Load Average
loadaverage=$(top -n 1 -b | grep “load average:” | awk ‘{print $10 $11 $12}’)
echo -e ‘E[32m'”Load Average :” $tecreset $loadaverage

# Check System Uptime
tecuptime=$(uptime | awk ‘{print $3,$4}’ | cut -f1 -d,)
echo -e ‘E[32m'”System Uptime Days/(HH:MM) :” $tecreset $tecuptime

# Unset Variables
unset tecreset os architecture kernelrelease internalip externalip nameserver loadaverage

# Remove Temporary Files
rm /tmp/osrelease /tmp/who /tmp/ramcache /tmp/diskusage
}
fi
shift $(($OPTIND -1))

MySQL Script For Database Backup to Amazon S3

Script Specification:

• It will take MySQL Database backup automatically.
• Upload it to Amazon S3
• After Uploading MySQL Backup to Amazon S3 it automatically delete backup more then 14 days old.
• This script will run on every Wednesday at 5:30 AM

# vi mysqlscript.sh
#############################################################
Insert following Lines in it.
##############################################################
#!/bin/sh

DB_NAME=”your_DB_Name”
DB_USER=”Your_DB_User_Name”
DB_PASSWORD=”Your_DB_password”
DB_PATH=/tmp/DB_Backup
DATE=`date +%d%m%y%H%M`

# export database
mysqldump -u $DB_USER -p$DB_PASSWORD $DB_NAME | gzip>$DB_PATH/MySQL_BACKUP_$DATE.sql.gz

# sync to amazon
s3cmd sync $DB_PATH/MySQL_BACKUP_$DATE.sql.gz s3://q2kdbbackup

# remove backups older than 14 days
find $DB_PATH/MySQL_BACKUP* -mtime +14 -exec rm {} ;

################################################################
Crontab Entry
################################################################
30 5 * * wed /home/jitendra/script/mysqldb.sh.