Skip to content

Instantly share code, notes, and snippets.

View nirbhabbarat's full-sized avatar
🎯
Focusing

Nirbhab Barat nirbhabbarat

🎯
Focusing
  • Gurgaon
View GitHub Profile
@nirbhabbarat
nirbhabbarat / percona56.sh
Last active August 29, 2015 14:03
Percona Server 5.6 installation on Centos 6.5
rpm --nodeps -e mysql-libs percona-xtrabackup-20
rpm -qa | grep percona-release-0.0-1* || rpm -ivh http://www.percona.com/downloads/percona-release/percona-release-0.0-1.x86_64.rpm
yum -y install perl-DBD-MySQL perl-DBI
yum -y install Percona-Server-client-56 Percona-Server-devel-56 Percona-Server-server-56 Percona-Server-shared-56 percona-xtrabackup percona-toolkit
touch /var/log/mysql-slow.log
chown mysql.mysql /var/log/mysql-slow.log
chmod 755 /var/log/mysql-slow.log
wget "https://packages.chef.io/files/stable/chef-server/12.14.0/el/6/chef-server-core-12.14.0-1.el6.x86_64.rpm"
rpm -Uvh chef-server-core-12.14.0-1.el6.x86_64.rpm
chef-server-ctl reconfigure
chef-server-ctl install chef-manage
chef-server-ctl reconfigure
chef-manage-ctl reconfigure
@nirbhabbarat
nirbhabbarat / monitor_and_restart_rabbitmq.sh
Created February 4, 2018 03:16
Check rabbitmq is running or not, else restart and send customer message to slack, email, sms or phone call API
#!/bin/bash
# port 15672 belongs to rabbitmq management portal, if not enabled use below command.
# rabbitmq-plugins enable rabbitmq_management
# guest:guest replace it with username:password of rabbitmq readonly user
status=$(curl -s -o /dev/null -w "%{http_code}" localhost:15672/api/overview --user guest:guest)
if [ $status = "200" ]; then
@nirbhabbarat
nirbhabbarat / aws_delete_ami_boto3.py
Created February 9, 2018 06:56
Delete/Deregister 30 days old AMI in AWS using boto3 and python
#!/usr/bin/env python
##### USE ON YOUR OWN RISK - THIS IS GOING TO DEREGISTER AMI OLDER THAN 30 DAYS
import boto3
from dateutil.parser import parse
import datetime
age = 30
aws_profile_name = 'prod'
def days_old(date):
get_date_obj = parse(date)
@nirbhabbarat
nirbhabbarat / aws_cost_leak.py
Created February 12, 2018 17:18
find out idle_elb, idle_rds_instances, underutilized_ebs_volume, legacy_instance_type, low_utilization_ec2, idle_eip
#!/usr/bin/python
#This script is useful for reducing the unnecessary cost inccured while using AWS
#It checks
#1.for all the idle load balancers in your account
#2.Checks the number of RDS connections in the past 1 week
#3.Identifies all the idle EBS Volumes
#4.Checks for all the Legacy instances which are being used
#5.Checks whether the ec2 instances are being fully utilised or not
#6.Checks all the unassociated Elastic IP's
@nirbhabbarat
nirbhabbarat / create_status_check_90_cpu_ec2.py
Created February 12, 2018 17:34
Create status check and 90% CPU alert on all EC2 machines in AWS
#!/usr/bin/env python
# find REPLACE_WITH_ARN and replace with real SNS ARN
import boto3
from dateutil.parser import parse
import datetime
aws_profile_name = 'prod'
def get_instance_name(fid):
@nirbhabbarat
nirbhabbarat / delete_unused_eip.py
Created February 12, 2018 17:37
As EIP are billed if unused in AWS, simple script to delete all unused EIPs
import os
import boto3
key=os.environ['TF_VAR_aws_access_key']
secret=os.environ['TF_VAR_aws_secret_key']
client = boto3.client('ec2', region_name=os.environ['TF_VAR_region'], aws_access_key_id=key, aws_secret_access_key=secret)
def elastic_ips_cleanup(client_connection):
""" Cleanup elastic IPs that are not being used """
addresses_dict = client_connection.describe_addresses()
@nirbhabbarat
nirbhabbarat / cleanup_aws_volume_snapshot.py
Created February 18, 2018 06:48
Delete AWS volume snapshots older than 30 days via python boto3
#!/usr/bin/env python
##### USE ON YOUR OWN RISK - THIS IS GOING TO delete_snapshot OLDER THAN 30 DAYS
import datetime
import sys
import boto3
age = 30
aws_profile_name = 'prod'
def days_old(date):
date_obj = date.replace(tzinfo=None)
diff = datetime.datetime.now() - date_obj
@nirbhabbarat
nirbhabbarat / rotate_ondemand_spotinst_groups.py
Last active February 19, 2018 08:23
Rotate all the ondemand machines which are not in spot - spotinst
import time
import datetime
import requests
import boto3
aws_profile_name = 'default'
token = 'XXX' # generate token from http://docs.spotinst.com/#page:authentication,header:header-permanent-access-token
headers = {'Authorization': 'Bearer '+token}
def get_instance_by_id(id):
@nirbhabbarat
nirbhabbarat / s3_tag_name.py
Created September 10, 2018 09:04
Add Name tag to S3 bucket same as bucket name
import boto3
s3 = boto3.client('s3')
response = s3.list_buckets()
buckets = [bucket['Name'] for bucket in response['Buckets']]
for bucket in buckets:
print bucket
tag={'TagSet':[{'Key': 'Name', 'Value': bucket}]}
try:
response = s3.put_bucket_tagging(Bucket=bucket, Tagging=tag)