Posts

Showing posts from March, 2016

How to run multiple commands on servers via Ansible

Ansible is a very powerful tool for central management of servers and we can run commands on servers from a central location but there is limitation that we can only run one command at a time on servers using command module and cat perform complex commands so to over come it following script is very helpful.

We are creating a script and placing it in /usr/bin/ so that can be run from any location.
[root@ip-10-0-1-231 ravi]# which prod-command.sh /usr/bin/prod-command.sh
Following is the script which is being used to run multiple commands of servers:

[root@ip-10-0-1-231 ec2-user]# cat /usr/bin/prod-command.sh #!/bin/bash #To run commands on server in group tag_Prod_VPC #By Ravi Gadgil echo -e "Running command on Production Server... " for i in "$@" ; do ansible tag_Prod_VPC -u ec2-user -s -m shell -a "$i" ; done
$@ : Will take the variables from the scripts which can be of n numbers.
ansible : To run ansible command line.
tag_Prod_VPC : Server Host gro…

How to check most memory utilization service in Linux

Linux has lots of tools such as free, top, htop, vmstat etc to show system utilization but in order to find exact service which is using the maximum system memory in descending order following command is very helpful.
[root@ip-10-0-1-231 ravi]# ps aux --sort -rss USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND rundeck 1308 28.6 61.2 2092252 371628 ? Ssl 10:33 0:56 /usr/bin/java -Djava.security.auth.login.con jenkins 1272 13.7 23.6 1301096 143400 ? Ssl 10:33 0:27 /etc/alternatives/java -Dcom.sun.akuma.Daemo root 1561 0.0 0.4 181944 2492 pts/0 S 10:35 0:00 sudo su root 1229 0.0 0.3 91012 2256 ? Ss 10:33 0:00 sendmail: accepting connections root 1563 0.0 0.3 115432 2068 pts/0 S 10:35 0:00 bash root 1549 0.0 0.2 73688 1796 ? Ss 10:35 0:00 ssh: /root/.ansible/cp/ansible-ssh-52.74.164 root 1519 0.0 0.2 113428 1772 ? Ss 10:35 0:00 sshd: ec2-user [priv] smms…

How to add multiple users in Linux with or without password

Image
To add multiple users in Linux with or without password, refer to following video:


For details to add multiple users in Linux following steps can be used.

Create a file having list of user which need to be added:
[root@localhost ravi]# cat add.user user1 user2 user3 user4 user5
Run the following command to add users:
[root@localhost ravi]# for i in `cat add.user` ; do useradd $i ; done
i : its the variable used to have values from add.user.
add.user : Its the file having name of the users which you want to added.
useradd : Command used to add user.

To check is users are created or not:
[root@localhost ravi]# for i in `cat add.user` ; do id $i ; done uid=501(user1) gid=501(user1) groups=501(user1) context=root:system_r:unconfined_t:SystemLow-SystemHigh uid=502(user2) gid=502(user2) groups=502(user2) context=root:system_r:unconfined_t:SystemLow-SystemHigh uid=503(user3) gid=503(user3) groups=503(user3) context=root:system_r:unconfined_t:SystemLow-SystemHigh uid=504(user4) gid=504(user4)…

Run mysqltuner on AWS RDS

In order to tune up AWS MySQL RDS following script is very helpful as it can find out quite a few flaws in DB and can provide good recommendation to make RDS better.

To download the script:
wget http://mysqltuner.pl/ -O mysqltuner.pl
Run script on RDS by providing the amount of memory allocated to DB server:
[root@ip-10-0-1-55 ravi]# ./mysqltuner.pl --host rds-staging.DB.com --user root --password dbpassword --forcemem 75000 >> MySQLTuner 1.6.4 - Major Hayden <major@mhtx.net> >> Bug reports, feature requests, and downloads at http://mysqltuner.com/ >> Run with '--help' for additional options and output filtering [--] Performing tests on rds-staging.DB.com:3306 Please enter your MySQL administrative password: [--] Skipped version check for MySQLTuner script [--] Assuming 75000 MB of physical memory [!!] Assuming 0 MB of swap space (use --forceswap to specify) [OK] Currently running supported MySQL version 5.6.19-log -------- Storage Engine Statist…

How to do automated NFS failover in AWS via script

NFS is always consider to be Single point of failure and if we are using it so in order to over come we can use clusters, glusterfs, DRBD etc but in case you wanna do a manual fail over following script can be very helpful.

The scenario which is being for this script is:
1. The elastic IP is attached to NFS server. 
2. The lsync is used to keep main server and secondary in sync.
3. Main server is pinged every minute and if 5 continuous ping fails do the required fail over.
4. Restart the netfs service in all the client servers to avoid NFS tail error.

 Script:
#!/bin/bash #Script to make secondary server as primary NFS storage server. #By Ravi Gadgil. #To check 54.254.X.X is up or not for 5 consecutive times count=$(ping -c 5 54.254.X.X | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }') if [ $count -eq 0 ]; then # 100% failed echo "Host : 54.254.X.X is down (ping failed) at $(date)" #To disassociate IP from primar…