AIX : Focus on CPU-intensive user

This handy one-liner :
ps -ef | egrep -v "STIME|$LOGNAME" | sort +3 -r | head -n 15 is focusing on the highest recently used CPU-intensive user processes in the system. This may be useful when troubleshooting applications or looking at users’ times.

Published in: on April 30, 2008 at 3:21 pm Leave a Comment

perl and the power of ‘pack’

One of my favorites is definitely perl, check out the following piece of code :
perl -e 'print pack(c5, (41*2), sqrt(7056), (unpack(c,H)-2), oct(115), 10)'
This cryptic one-liner actually prints : RTFM on your terminal.
The power of perl and the pack function may be discussed for a long time, check the pack for yourself.

Published in: on April 29, 2008 at 2:28 pm Leave a Comment

Handy mplayer alias

I found a fancy one-liner in another blog, but I don’t remember where :)
locate -i "whatever band" | grep -i "whatever album/song" | perl -ne 's/^(.*)$/"$1"/ && print' | xargs mplayer – This can be easily parametrized with $1, $2 on bash or $band = $ARGV[0];
$song = $ARGV[1]; in perl and can be aliased in .bashrc for example.
I can’t use it currently because of mplayer issues, but they will be resolved in the near future :)

Published in: on April 25, 2008 at 3:45 pm Leave a Comment

Find the size of MySQL tables

I’ve been searching the net for a clue on how to find the size of mysql tables – here’s what I found :
mysql test -e 'show table status' | perl -lane '$size += $F[6]+$F[8];END{print $size}'
mysql will assume that you have an alias in .bashrc for example, as follows :

alias mysql=’/path/to/mysql/bin/mysql –defaults-file=/path/to/mysql/conf/mysql.conf -pPassword’

Then, test is the mysql DB, then you execute “show table status”, then the perl takes care of the rest, the output will be in bytes, I hope this will help someone, who as me, doesn’t care about having php installed.

Published in: on April 24, 2008 at 10:08 am Leave a Comment

Python script to check for opened ports

In another article, I’ve mentioned the use of netcat to check for a listener on remote port at particular machine.
Here’s another example, yet more enhanced, written in python. The original idea belongs to a member of unix.com forums, nickname [Ghostdog74], I believe, again, I’ve modified it a little bit to serve my needs.
Most of you probably work in a multi-server environment, where you have all kinds of OSes – Linux, UNIX, Windows, etc – so called “TheServerFarm” :)
As soon as you want to check whether a certain port is opened on a remote machine (iptables may be turned on) you can use the following python script :

#!/usr/bin/python

import socket
import sys

if ( len(sys.argv) != 2 ):
print "Usage: " + sys.argv[0] + " Enter IP or FQDN as argument"
sys.exit(1)

remote_host = sys.argv[1]

for remote_port in [21,22,23,25,80,139,139,389,443,445,3128,3306,3389]:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(20)
try:
sock.connect((remote_host, remote_port))
except Exception,e:
print "%d closed " % remote_port
else:
print "%d open" % remote_port
sock.close()

The possible ports to be checked are listed in the following line :
for remote_port in [21,22,23,25,80,139,389,443,445,3128,3306,3389]
Those are just generic ports, you can change the numbers to suit your needs, if, for example, your applications are running on specific ports. Here’s a sample report :

[unixgate@unix.com ~]$ monports.py 10.10.1.21
21 closed
22 open
23 closed
80 open

Just change the IP address or supply Fully Qualified Domain Name.

Published in: on April 23, 2008 at 9:31 am Leave a Comment

Check to file permissions

If, for some reason, you’re not allowed to see the permissions of a file, or you don’t understand the rw-rwx-x format, you can use the following script :

#!/bin/bash

echo -n "Enter file name : "
read file

# find out if file has write permission or not
[ -w $file ] && W="Write = yes" || W="Write = No"
# find out if file has excute permission or not
[ -x $file ] && X="Execute = yes" || X="Execute = No"
# find out if file has read permission or not
[ -r $file ] && R="Read = yes" || R="Read = No"
echo "$file's permissions, as follows : "
echo "$W"
echo "$R"
echo "$X"

The script source is originally taken from cybercity, I’ve changed it a little bit.
Here is a sample report :

[unixgate@unix.com ~]$ ./filePerms.sh
Enter file name : template.doc
template.doc's permissions, as follows :
Write = yes
Read = yes
Execute = No

Published in: on at 9:18 am Leave a Comment

netcat – using it as port scanner

nc stands for NetCat – arbitrary TCP and UDP connections and listens – as stated in the manual, I love a lot of things written by BSD community, the *BSD itself as well, so this command too.

You can use it very easily to check for remote port like this :

nc -z serverName.net 22  - where nc is the executable, assuming it’s in your $PATH, -z flag stands for : “just scan for listening daemons, without sending any data to them”, serverName.net should be replaced with your server FQDN, and 22 in my case is the port number, you can choose whatever port you want. Here, the netcat will return : Connection to serverName.net 22 port [tcp/ssh] succeeded! – which means that there’s a listener / daemon on the remote side. Soon I will share another approach, in python, for scanning wider range on ports.

Published in: on April 22, 2008 at 2:51 pm Leave a Comment

Use Time Sync

In order to be sync-ed, you can use ntpdate binary under Linux. You can put the following in a cron job : /usr/sbin/ntpdate 10.10.101.1 > /dev/null 2>&1
This will ensure that you’re up-to-date :) , just replace the IP 10.10.101.1 with the IP of the target server.

Published in: on April 21, 2008 at 10:56 am Leave a Comment

apache benchmarks

There are two good apache benchmarks that I’m aware of :

ab – Apache HTTP server benchmarking tool

and siege – An HTTP/HTTPS stress tester

In details :

a) ab : written in perl, open source, various options, the one that I’m using mostly is – ab -n 1000 -c 5 http://myServerAddress.com/myfiles.cgi – those are examples , where -n means “Number of requests to perform for the benchmarking session”, and -c flag means “Number of multiple requests to perform at a time” the sample output is :

Benchmarking myServerAddress.com (be patient)

Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Finished 1000 requests

Server Software: Apache/2.2.8
Server Hostname: myServerAddress.com

Document Path: /myfiles.cgi
Document Length: 526 bytes
Concurrency Level: 5
Time taken for tests: 1.608432 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 796590 bytes
HTML transferred: 527052 bytes
Requests per second: 621.72 [#/sec] (mean)
Time per request: 8.042 [ms] (mean)
Time per request: 1.608 [ms] (mean, across all concurrent requests)
Transfer rate: 483.08 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 2 2.5 2 17
Processing: 2 4 2.7 4 29
Waiting: 0 2 2.6 2 27
Total: 4 7 2.9 6 29

Percentage of the requests served within a certain time (ms)
50% 6
66% 7
75% 7
80% 7
90% 8
95% 17
98% 18
99% 20
100% 29 (longest request)

This utility comes pre-installed with most of the Linux distros, so just type in shell session : ab and see if it comes out with something. If not, install it depending on your package management system.

Now, let’s see how siege is doing, sample line :

siege -c 50 -r 50 http://myServerAddress.com/yetAnotherFile, where -c is concurrent users, and -r is number of iterations, here’s the sample output :

Transactions: 2500 hits
Availability: 100.00 %
Elapsed time: 34.53 secs
Data transferred: 488.39 MB
Response time: 0.01 secs
Transaction rate: 72.40 trans/sec
Throughput: 14.14 MB/sec
Concurrency: 0.95
Successful transactions: 2500
Failed transactions: 0
Longest transaction: 0.16
Shortest transaction: 0.00

Both are very good, they have different features that might suit your needs, check them out.

Published in: on April 18, 2008 at 3:01 pm Leave a Comment

new day, new post

I used to have this theme a while ago, on another domain… here it comes again.

Published in: on at 2:28 pm Leave a Comment