Monitor Concurrent Load on a Server

Here is a one-liner that displays the number of currently active TCP connections on a given port, in intervals of 1 second.

 while true; do n=`netstat -n|grep 2707|wc -l`; echo $`date` --- $n ; sleep 1; done;

Useful for load testing a service.

Naming of netty worker threads

One of the slightly annoying quirks in Netty is the fact that OIO worker names are not meaningful.

Here is some sample code to fix this:

/* give worker threads a meaningful name such as "OIO-worker-2" */
try {
    ThreadRenamingRunnable.setThreadNameDeterminer(
        new ThreadNameDeterminer() {
            public String determineThreadName(String currentThreadName,
                                                String proposedThreadName) throws Exception {
          
                StringBuffer sb = new StringBuffer(WORKER_THREADNAME_PREFIX);
                sb.append(currentThreadName.substring(currentThreadName.lastIndexOf('-')));
                return sb.toString();
            }
        });         
} catch (Exception e) {
         
    /* fall back to default thread name, e.g. pool-X-thread-X */
    ThreadRenamingRunnable.setThreadNameDeterminer(ThreadNameDeterminer.CURRENT);
}

Awk Samples

Count rows and compute sum for a column


grep "Summary" *.log|awk '{sum += $7} {count +=1} END {print count, sum}'

This counts each line containing the word "Summary" in all files ending in .log
Value in column 7 is summed.

Sample data:
                                        *               
Summary - Thread 145    20      16      4       16      4
Summary - Thread 148    20      15      5       15      5
Summary - Thread 147    20      15      5       15      5

* this is column 7

Conditional Sum


awk '$5 == 2 {{s += $8} {c += 1}} END {print 2, s/c }' timing.txt >> sum.txt

What this does:

* read file 'timing.txt'
* if 5th column has value 2:
- add value of column 8 to s
- add 1 to c (count occurrences)
* after last record read:
- print literal 2
- print average (sum/count)
* output: append to file 'sum.txt'

Disk Usage by Directory

Show Disk Usage by directory

du -s * | sort -nr

Show details of which files/directories consume how much space (Cygwin for C drive):

du -shc /cygdrive/c/*

Show the same info sorted by size, with largest directories last:

du -sc /cygdrive/c/* | sort -n

Count with sort and uniq

Question: Find all unique IP address that appear in a certain log message, and count how many times they appear.


Answer:
grep "log message" logfile.log | awk '{ print $18 }' | sort | uniq -c


Example:
[cygwin] $ grep "Missing post body" mpi.log |awk '{print $18}' |sort |uniq -c |sort -rn
      6 82.1.184.81
      6 212.121.212.149
      4 62.49.164.229
      2 62.140.196.160
 [cygwin] $

Note: the "sort -rn" at the end sorts results by number of occurrence

Minimal Eclipse

A minimal Eclipse configuration for managing project and support notes:

  • Eclipse 3.7.1 Platform Runtime Binary
  • Eclipse Java Development Tools (for e.g. Ant support)
  • Eclipse Marketplace Client
  • EGit
  • Mylin (Task Management)
  • Jira connector (Atlassian Connector for Eclipse)
  • Web Tools Platform (Eclipse XML Editors and Tools)

Log Monitoring - number of recent events exceeding treshold

This script checks a log file for a certain log event. If the number of these events in the last 3 minutes exceeds a treshold, an alert email is sent

#!/bin/bash
# Check timestamps up to 3 minutes ago
prev3=`date +"%y/%m/%d %H:%M" -d -3minute`
prev2=`date +"%y/%m/%d %H:%M" -d -2minute`
prev1=`date +"%y/%m/%d %H:%M" -d -1minute`
now=`date +"%y/%m/%d %H:%M"`

logevent="Exception"
treshold=2

# check the log file
failCount=` egrep "$prev3|$prev2|$prev1|$now" /var/log/app_logfile.log | grep $logevent | wc -l `


# 3 or more log events in previous 3-4 minutes
if [ $failCount -gt $treshold ]; then
    # notify someone
    echo "failCount ($failCount) exceeds treshold ($treshold)" | mail -s "Alert from ${HOSTNAME}" support@mycompany.com
fi

Compare two directory trees


diff -a -q -r dir1 dir2


Options used:

-a Treat all files as text and compare them line-by-line, even if they do not seem to be text.
-q Report only whether the files differ, not the details of the differences.
-r When comparing directories, recursively compare any subdirectories found.

Run Java Programs from Cygwin

The java exe is a Windows program, so it expects a Windows style classpath.

This will not work:

 [cygwin] release $ java -cp "bin/*:lib/*" com.tnsi.tap.Versin
java.lang.NoClassDefFoundError: com/tnsi/tap/Versin
Caused by: java.lang.ClassNotFoundException: com.tnsi.tap.Versin
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: com.tnsi.tap.Versin.  Program will exit.
Exception in thread "main"  [cygwin] release $


Use a semicolon instead:

 [cygwin] release $ java -cp "bin/*;lib/*" com.tnsi.tap.Version
TNSPay Retail Gateway - TokenisationAdapter 1.0.0.1 23Nov2011
 [cygwin] release $

Linux Runlevels

To quickly switch from GUI to shell-only runlevel, enter the following:

sudo /sbin/init 3    # switch to shell with networking 
sudo /sbin/init 5    # switch back to GUI

To set the default runlevel in CentOS, edit file /etc/inittab (set line id:3:initdefault: to the correct value).