find largest file

for i in G M K; do du -ah | grep [0-9]$i | sort -nr -k 1; done | head -n 11

So the idea is very simple and works wonders. Here is what this does. The for loop goes thru the Gs first, then the Ms, then the Ks, It sorts them and the end result is they are all concattaned on the screen and sorted from biggest to smallest from G to M to K.
1. take list of numbers and grep out numbers that have “number and G” and sort that
2. Do step one but instead of G use M
3. Do step one but instead of G use K
4. Concatenate the results


http://ram.kossboss.com/linux-sort-cant-sort-human-readable-fixworkaround-sorting-human-reable/

find . -newer modsecurity.conf -exec rm -rf {} \;  
##deleting lates files newer than the file ‘modsecurity.conf’

network bandwidth analysis with iperf

yum install iperf

Before going ahead with the test, lets understand some networking concepts related to speed test.
Network Throughput
Transfer rate of data from one place to another with respect to time is called as throughput. Throughput is considered a quality measuring metric for hard disks,network etc. Its measured in Kbps(Kilo bits per second),Mbps(Mega bits per second),Gbps(Giga bits per second.)
TCP Window
TCP (Transmission Control Protocol), is a reliable transport layer protocol used for network communications. How TCP works, is beyond the scope of this article. TCP works on a reliable manner, by sending messages and waiting for acknowledgement from the receiver.
Whenever two machine's are communicating with each other, then each of them will inform the other, about the amount of bytes it is ready to receive at one time.
In other words, the maximum amount of data that a sender can send the other end, without an acknowledgement is called as Window Size. This TCP window size affects network throughput very badly sometimes. Lets take an example.
Suppose you want to send a 500MB of data from one machine to the other, with the tcp window size of 64KB.
Which means for sending the whole 500MB data, the sending machine has to wait 800 times for an acknowledgement from the receiver.
500MB / 64KB = 800
So you can clearly see that, if you increase the Window size a little bit to tune TCP, it can bring significant difference to the throughput achieved.
Suppose you have a windows machine and want to measure the speed between it and another Linux machine, then you need to make one as client, and another as the server. Lets see how.

route configuration

[root@support network-scripts]# cat route-eth0
# Static route for metadata service
169.254.169.254 via 0.0.0.0 dev eth0

[root@support network-scripts]# cat ifcfg-eth0
DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes
TYPE=Ethernet
USERCTL=yes
PEERDNS=yes
IPV6INIT=no
PERSISTENT_DHCLIENT=yes



nrpe dead but subsys locked

nrpe dead but subsys locked  error
service dead but subsys locked

check if the port used by nrpe is  already used by other processess like xinetd
netstat -tulnp | grep 5666
or
lsof -ni udp:5666
lsof -ni tcp:5666


This means the service was running at one time, but has crashed.
When you start a service, it creates a "lock" file to indicate that the service is running. This helps avoid multiple instances of the service. When you stop a service, this lock file is removed.
When a running service crashes, the lock file exists but the process no longer exists. Thus, the message.
Look at the two areas /var/run/*.pid and /var/lock/subsys/*. These are expected to agree with each other. That is, if the (emtpy file) lockfile /var/lock/subsys/crond exists, then the first line of the file /var/run/crond.pid is expected to contain the PID of the process running for this service. If no such process is running, then something is wrong. If a process is indeed running (as you see) but it is not that PID, then something is probably confused.



http://serverfault.com/questions/26276/what-does-service-name-dead-but-subsys-locked-mean

http://www.linuxquestions.org/questions/linux-networking-3/service-dead-but-subsys-locked-376488/

Create a New User and Grant Permissions in MySQL

create/delete users 
-------------------- 

CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';

DROP USER ‘demo’@‘localhost’;

Change passwords
-------------------
mysqladmin -u USER -p password NEWPASSWORD

mysqladmin -u marcelo -p oLdpa$$word 'NEwP@$$'
mysql> use mysql;

SET PASSWORD FOR 'user-name-here'@'hostname-name-here' = PASSWORD('new-password-here');
flush privileges;

GRANTING/REVOKE PRIVILEGES
--------------------------

GRANT ALL PRIVILEGES ON *.* TO 'newuser'@'localhost';

OR

GRANT ALL PRIVILEGES ON demoDB.* TO 'newuser'@'localhost';

GRANT SELECT,UPDATE,DELETE ON testDB.* TO 'demo-user'@'localhost';

REVOKE UPDATE ON testDB.* FROM 'demo-user'@'localhost';

GRANT ALL ON testDB.* TO 'demo-user'@'localhost';

FLUSH PRIVILEGES;
show grants for 'demo-user'@'localhost';


Privileges:-
= = = = = = 
ALL PRIVILEGES,CREATE,DROP,DELETE,INSERT,SELECT,UPDATE,GRANT OPTION

  • ALL PRIVILEGES- as we saw previously, this would allow a MySQL user all access to a designated database (or if no database is selected, across the system)
  • CREATE- allows them to create new tables or databases
  • DROP- allows them to them to delete tables or databases
  • DELETE- allows them to delete rows from tables
  • INSERT- allows them to insert rows into tables
  • SELECT- allows them to use the Select command to read through databases
  • UPDATE- allow them to update table rows
  • GRANT OPTION- allows them to grant or remove other users' privileges
To provide a specific user with a permission, you can use this framework:
 GRANT [type of permission] ON [database name].[table name] TO ‘[username]’@'localhost’;


The root login can be changed with the following command:
---------------------------------------------------------- 
rename user 'root'@'localhost' to 'newAdminUser'@'localhost';




setup and use GIT

signup / singn in  to github.com


yum install git

git config --global user.name "urgensherpa"
git config --global user.email  "urgen.sherpa@outlook.com"


create new repository in github <using webbrowser>
eg.  repo name is   test


#mkdir  /home/nepal/test
#cd /home/nepal/test
git init
git remote add origin https://github.com/urgensherpa/test.git
                                                                        ^               ^
            <username>/<reponame>

make some files in /home/nepal/test

git add  dbconnect.php     <update only 1 file>
git add .                   <update all/multiple current dir>
git commit  -m 'first commit'
git  push origin master
<enter your user name and password for github>

check in your github.com account to see changes

when you change your files

git add  .
git commit -m 'second commit'
git push origin master
<enter your user/password for github.com>

edit some file  eg.  function.php
git  add  .
git commit -m 'third commit'
git reset --soft HEAD^    #this will reset the commit but the change made in file function.php is as is


git reset --hard HEAD^  #it will reset + changes made is reverted back to last commit that is second commit

==============cloning=================
git clone https://github.com/urgensherpa/test.git

ls -l     #see the cloned repo in your local current dir
make changes in files
echo  "test abcd" >>  dbconnect.php
git add .
git commit -m "added new file"
git push origin master
<user/pass for github.com>

===================================
http://stackoverflow.com/questions/7106012/download-a-single-folder-or-directory-from-a-github-repo


Create a new repository on GitHub. To avoid errors, do not initialize the new repository with README, license, or gitignore files. You can add these files after your project has been pushed to GitHub.

  1. Open Terminal (for Mac users) or the command prompt (for Windows and Linux users).
  2. Change the current working directory to your local project.
  3. Initialize the local directory as a Git repository.
    git init
    
  4. Add the files in your new local repository. This stages them for the first commit.
    git add .
    # Adds the files in the local repository and stages them for commit. To unstage a file, use 'git reset HEAD YOUR-FILE'.
  1. Commit the files that you've staged in your local repository.
    git commit -m 'First commit'
    # Commits the tracked changes and prepares them to be pushed to a remote repository. To remove this commit and modify the file, use 'git reset --soft HEAD~1' and commit and add the file again.
  1. Copy remote repository URL fieldAt the top of your GitHub repository's Quick Setup page, click
  1. to copy the remote repository URL.
  2. In the Command prompt, add the URL for the remote repository where your local repository will be pushed.
    git remote add origin remote repository URL
    # Sets the new remote
    git remote -v
    # Verifies the new remote URL
    
  3. Push the changes in your local repository to GitHub.
    git push origin master
    # Pushes the changes in your local repository up to the remote repository you specified as the origin 
     
     
     
    git init
    git add .
    git commit -m "Initial commit"
    git remote add origin <project url>
    git push -f origin master 

cpulimit linux

apt-get install cpulimit
yum install cpulimit    <enable epel>

#Eg. . on centos 6.5   with cpulimit-0.1-1.20140722gitcabeb99.el6.x86_64.rpm

cpulimit -p 8645 -l 10    
#limits this process to 10 % of cpu

cpulimit -e /usr/local/bin/myprog -l 20
#limits the CPU usage of /usr/local/bin/myprog to 20%

X% of one CPU. So for example, if you set "-l 100" on a dual-core machine, you are limiting the usage to 50% of available CPU resource.

To find out PID of the process use any of the following:
# ps aux | less
# ps aux | grep firefox
# pgrep -u vivek php-cgi
# pgrep lighttpd



LVM PART 1 data restore after accidental removal of logical volume

 Data Restore after  accidental  remove/delete of logical volume                     
Preparing  testing enviroment :

After creating partition use below command

partx        /dev/sda
pvcreate     /dev/sda4   /dev/sda5
vgcreate  VG1    /dev/sda4 /dev/sda5
vgs                                                              #display vg's
lvcreate  -L  4G -n  lv001   VG1
lvscan         or           lvdisplay


mke2fs -t  ext4  -j  
/dev/volgroup001/lv001
Add more space to logical volume (here lv001 is increased from 4gb to 6gb)
lvresize -L 6GB /dev/volgroup001/lv001                     
After resizing
 resize2fs   /dev/volgroup001/lv001

Accidental removal of logical volume

#lvremove    /dev/volgroup001/lv001     
#ll /etc/lvm/archive   ##contains the arhive of lvm data
[root@demo1 ~]# ll /etc/lvm/archive/
total 44
-rw-------. 1 root root 1761 Oct  8 15:46 vg_demo1_00000-1563239603.vg
-rw-------. 1 root root 1135 Oct 20 18:34 volgroup001_00000-1225438891.vg
-rw-------. 1 root root 1135 Oct 20 18:36 volgroup001_00001-1361506140.vg
-rw-------. 1 root root 1731 Oct 21 19:56 volgroup001_00002-1489665094.vg
-rw-------. 1 root root 1731 Oct 22 18:46 volgroup001_00003-1149849750.vg
-rw-------. 1 root root 1733 Oct 23 10:46 volgroup001_00004-1224808495.vg
-rw-------. 1 root root 1741 Oct 23 10:46 volgroup001_00005-843600971.vg
-rw-------. 1 root root 1733 Oct 23 10:56 volgroup001_00006-965431690.vg
-rw-------. 1 root root 1734 Oct 23 13:24 volgroup001_00007-1544816747.vg
-rw-------. 1 root root 1130 Oct 23 13:29 volgroup001_00008-1538079247.vg
-rw-------. 1 root root 1552 Oct 23 18:02 volgroup001_00009-1668760531.vg

#vgcfgrestore --list  volgroup001                              

  File:        /etc/lvm/archive/volgroup001_00008-1538079247.vg
  VG name:        volgroup001
  Description:    Created *before* executing 'lvcreate -L 4G -n lv001 volgroup001'
  Backup Time:    Thu Oct 23 13:29:51 2014

  
  File:        /etc/lvm/archive/volgroup001_00009-1668760531.vg
  VG name:        volgroup001
  Description:    Created *before* executing 'lvresize -L 6GB /dev/volgroup001/lv001'
  Backup Time:    Thu Oct 23 18:02:56 2014

  
  File:        /etc/lvm/backup/volgroup001
  VG name:        volgroup001
  Description:    Created *after* executing 'lvresize -L 6GB /dev/volgroup001/lv001'
  Backup Time:    Thu Oct 23 18:02:56 2014

#######
read the description and backuptime and select the file eg. /etc/lvm/archive/volgroup001_00009-
1668760531.vg    
#######

######   NOW RESTORRING   ######

#vgcfgrestore   -f    /etc/lvm/archive/volgroup001_00009-1668760531.vg     volgroup001
#lvscan
#lvchange -ay /dev/volgroup001/lv001         ##lvchange= change attribute ,  -ay = activateall -yes
#lvscan




vgcfgrestore -f   /etc/lvm/backup/volgroup001


now mount the lvm again

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

parted
select /dev/sdb

or   parted /dev/sdb
mklabel msdos
mkpart primary 1  10GB                                #1MB to 10GB = approx. 10 GB partition is created

print  or p ##view partition table

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  525MB   524MB   primary  ext4         boot
 2      525MB   21.5GB  20.9GB  primary               lvm



set   <partition-number>  lvm on
quit

partx /dev/sdb

pvcreate  /dev/sdb5 /dev/sdb6 /dev/sdb7

or

pvcreate /dev/sdb[567]

##extend volume group with name volgroup 001

vgextend  volgroup001 /dev/sdb5 /dev/sdb6 /dev/sdb7  

To expand rest of space in VG enter similar command like below:-

lvextend -l +100%FREE /dev/myvg/testlvm

icinga monitor mysql slave replication

icinga monitor msyql slave replication status

At SLAVE mysql server create a user with replication privileges
mysql> GRANT REPLICATION CLIENT on *.* TO 'nagios'@'%' IDENTIFIED BY '1LumbIni2$';
mysql> flush privileges;

 service mysql restart

At  ICINGA server
download nagios mysql slave monitoring script

wget  http://www.claudiokuenzler.com/nagios-plugins/check_mysql_slavestatus.sh
chown icinga.icinga check_mysql_slavestatus.sh; chmod +x check_mysql_slavestatus.sh;

  #./check_mysql_slavestatus.sh -H 198.50.176.187 -u nagios -p 1LumbIni2$ -P 3306
OK: Slave SQL running: Yes Slave IO running: Yes / master: 5.9.240.235 / slave is 0 seconds behind master | delay=0s

Define commands in commands.cfg
# 'check_mysql_slavestatus' command definition
define command{
command_name check_mysql_slavestatus
command_line $USER1$/check_mysql_slavestatus.sh -H $HOSTADDRESS$ -P $ARG1$ -u $ARG2$ -p $ARG3$
}

# 'check_mysql_slavestatus' command definition with delay check
define command{
command_name check_mysql_slavestatus
command_line $USER1$/check_mysql_slavestatus.sh -H $HOSTADDRESS$ -P $ARG1$ -u $ARG2$ -p $ARG3$ -w $ARG4$ -c $ARG5$
}

Now create service for checking

define service{
use generic-service ; Name of service template to use
host_name gskhanalslave
service_description MySQL Replication Status
check_command check_mysql_slavestatus!3306!nagios!1LumbIni2$
}


inotify inodes

What are inodes?

In a nutshell, inodes are the data structure Linux servers use to store information about a file. inodes are set to a specific number on your server. When you write files to the server, you are using an inode for each file. When your inodes reach their limit, you will get Disk Space errors and other strange behaviors with your site.

How do I view my servers inodes?

Below is the Linux command for finding the inodes for your server. This command is ran through shell.
df -i
Get to know inotify
Inotify is a Linux kernel feature that monitors file systems and immediately alerts an attentive application to relevant events, such as a delete, read, write, and even an unmount operation. 
http://www.infoq.com/articles/inotify-linux-file-system-event-monitoring
Inotify is a file change notification system in the Linux kernel, available since version 2.6.13. 
grep INOTIFY_USER /boot/config-$(uname -r)
CONFIG_INOTIFY_USER=y
# pwd
/proc/sys/fs/inotify
root@web01 [/proc/sys/fs/inotify]# ls
./  ../  max_queued_events  max_user_instances  max_user_watches 
#bydefault value in max_user_watches is = 8192 
#echo 65535 > /proc/sys/fs/inotify/max_user_watches
# cat /proc/sys/fs/inotify/max_user_watches          
65536 
To make the change permanent, edit the file /etc/sysctl.conf and add this line to the end of the file:
fs.inotify.max_user_watches=65535
If this server is a VPS and it is running in a OpenVZ container then you are NOT allowed to modify any kernel parameter of that container. and you will get below error
root@vps01 [/proc/sys/fs/inotify]# sysctl -w fs.inotify.max_user_watches=65535 error: permission denied on key 'fs.inotify.max_user_watches'
change it from host