Sunday, October 10, 2010

Analyse Disk Activities in Linux

To see disk activities, you can simply issue this command:
# e.g. on /dev/sda
$ cat /sys/block/sda/stat
626485464 806972148 28053848010 1557001361 853157995 3445307000 34392097768 2090234158        0 444939685 3653907428

Using iostat:
# Display every 5 seconds
$ iostat -xm -d 5

# Display on one disk
$ iostat -p /dev/sda -d 5

Sunday, October 3, 2010

Helpful Diagnostic Non-Responsive Server Comandlines

How many HTTP connections?
$ netstat -n | grep :80 | wc -l

How many HTTPS connections?
$ netstat -n | grep :443 | wc -l

How many connections are from an IP?
$ netstat -n | grep XXX.XXX.XXX.XXX

Monday, September 20, 2010

Bash if

Syntax

if [ <condition expression> ]
then
<commands>
elif [ <condition expression> ]
then
<commands>
else
<commands>
fi
One liner
if [ <condition expression> ]; then <commands>; fi
Variations
[ <condition expression> ] && (<commands>)
test <condition expression> && (<command>; ... <command>; exit;)
&& means 'and' while || means 'or'.
If you invoke exit at the end of a sub-shell means it will not pass the return value back to parent shell.
Use {...} instead of (...) to avoid creating a sub-shell.

Condition expression operators

[ -a FILE ] True if FILE exists.
[ -b FILE ] True if FILE exists and is a block-special file.
[ -c FILE ] True if FILE exists and is a character-special file.
[ -d FILE ] True if FILE exists and is a directory.
[ -e FILE ] True if FILE exists.
[ -f FILE ] True if FILE exists and is a regular file.
[ -g FILE ] True if FILE exists and its SGID bit is set.
[ -h FILE ] True if FILE exists and is a symbolic link.
[ -k FILE ] True if FILE exists and its sticky bit is set.
[ -p FILE ] True if FILE exists and is a named pipe (FIFO).
[ -r FILE ] True if FILE exists and is readable.
[ -s FILE ] True if FILE exists and has a size greater than zero.
[ -t FD ] True if file descriptor FD is open and refers to a terminal.
[ -u FILE ] True if FILE exists and its SUID (set user ID) bit is set.
[ -w FILE ] True if FILE exists and is writable.
[ -x FILE ] True if FILE exists and is executable.
[ -O FILE ] True if FILE exists and is owned by the effective user ID.
[ -G FILE ] True if FILE exists and is owned by the effective group ID.
[ -L FILE ] True if FILE exists and is a symbolic link.
[ -N FILE ] True if FILE exists and has been modified since it was last read.
[ -S FILE ] True if FILE exists and is a socket.
[ FILE1 -nt FILE2 ] True if FILE1 has been changed more recently than FILE2, or if FILE1 exists and FILE2 does not.
[ FILE1 -ot FILE2 ] True if FILE1 is older than FILE2, or is FILE2 exists and FILE1 does not.
[ FILE1 -ef FILE2 ] True if FILE1 and FILE2 refer to the same device and inode numbers.
[ -o OPTIONNAME ] True if shell option "OPTIONNAME" is enabled.
[ -z STRING ] True of the length if "STRING" is zero.
[ -n STRING ] or [ STRING ] True if the length of "STRING" is non-zero.
[ STRING1 == STRING2 ] True if the strings are equal. "=" may be used instead of "==" for strict POSIX compliance.
[ STRING1 != STRING2 ] True if the strings are not equal.
[ STRING1 < STRING2 ] True if "STRING1" sorts before "STRING2" lexicographically in the current locale.
[ STRING1 > STRING2 ] True if "STRING1" sorts after "STRING2" lexicographically in the current locale.
[ ARG1 OP ARG2 ] "OP" is one of -eq, -ne, -lt, -le, -gt or -ge. These arithmetic binary operators return true if "ARG1" is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to "ARG2", respectively. "ARG1" and "ARG2" are integers.

Combined expressions
[ ! EXPR ] True if EXPR is false.
[ ( EXPR ) ] Returns the value of EXPR. This may be used to override the normal precedence of operators.
[ EXPR1 -a EXPR2 ] True if both EXPR1 and EXPR2 are true.
[ EXPR1 -o EXPR2 ] True if either EXPR1 or EXPR2 is true.


Ref: http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html

Differences with brackets

Using double brackets, [[...]], means:
  • If variable values is a string with spaces, it will not be splitted, so you could leave out the double quotes. So, instead of
    $ FOO="I am a foo"
    $ [ "$FOO" == "I am a foo" ] && echo "Indeed, I am a foo."
    
    you can do
    $ [[ $FOO == "I am a foo" ]] && echo "Indeed, I am a foo."
    
  • Wildcards do not expand to filenames, e.g.
    $ ls
    index.html
    $ FOO="index.*"
    $ echo $FOO
    index.html
    $ echo "$FOO"
    index.*
    $ echo '$FOO'
    $FOO
    
    Single bracket examples
    $ [ $FOO == index.html ] && echo "This is true"
    This is true
    $ [ $FOO == "index.html" ] && echo "This is true"
    This is true
    $ [ $FOO == index.* ] && echo "This is true"
    This is true
    $ [ $FOO == "index.*" ] || echo "This is false"
    This is false
    
    #c.f. this with double bracket examples
    $ [ "$FOO" == index.html ] || echo "This is false"
    This is false
    $ [ "$FOO" == "index.html" ] || echo "This is false"
    This is false
    $ [ "$FOO" == index.* ] || echo "This is false"
    This is false
    $ [ "$FOO" == "index.*" ] && echo "This is true"
    This is true
    
    Double bracket examples
    $ [[ $FOO == index.html ]] || echo "This is false"
    This is false
    $ [[ $FOO == "index.html" ]] || echo "This is false"
    This is false
    $ [[ $FOO == index.* ]] && echo "This is true"
    This is true
    $ [[ $FOO == "index.*" ]] && echo "This is true"
    This is true