Thursday, August 27, 2009

File size in human format

I needed to see some file sizes in a human format (in a PHP page) so I created this function some of you might find useful:


/**Returns the size of data (provided in bytes) in a human readable
* format
* @size - the the size of data in bytes
* @author alex from scriptoid.com
*/
function humanSize($size){
if($size < pow(1024, 1)){
return $size . 'Bytes';
}
else if ($size < pow(1024, 2)){
return sprintf("%.2f", $size/pow(1024, 1)) . 'Kb'; //Kilo
}
else if ($size < pow(1024, 3)){
return sprintf("%.2f", $size/pow(1024, 2)) . 'Mb'; //Mega
}
else if ($size < pow(1024, 4)){
return sprintf("%.2f",$size/pow(1024, 3)) . 'Gb'; //Giga
}
else if ($size < pow(1024, 5)){
return sprintf("%.2f",$size / pow(1024, 4)) . 'Tb'; //Tera
}
else if ($size < pow(1024, 6)){
return sprintf("%.2f",$size / pow(1024, 5)) . 'Pb'; //Peta
}
else if ($size < pow(1024, 7)){
return sprintf("%.2f",$size / pow(1024, 6)) . 'Eb'; //Exa
}
else if ($size < pow(1024, 8)){
return sprintf("%.2f",$size /pow(1024, 7)) . 'Zb'; //Zeta
}
else if ($size < pow(1024, 9)){
return sprintf("%.2f",$size /pow(1024, 8)) . 'Yb'; //Yota
}
else{
return 'too big'; //:D
}
}

Sunday, August 23, 2009

Low level HTTP(s) testing

For http use:
$ telnet localhost 80
GET / HTTP/1.0

For https use (openssl tool):
$ openssl s_client -connect localhost:443 -state -debug
GET / HTTP/1.0

Before the actual HTTP response you will receive detailed information about the SSL handshake.


More...ff you want to use HTTP/1.1 use
GET / HTTP/1.1

Even More...if you use virtual servers enter (after you get a prompt from GET / HTTP/1.1)
Host: your_virtual_server_name

Monday, June 22, 2009

Time Tracking 2.0 going alfa

Time tracking 2.0 entered alfa level

Thursday, May 28, 2009

Linux - count my processes

Whenever I'm in need to count how many processes are running on my Linux box I use:

ps aux | wc -l


Mount Samba remote folders - Suse Linux

The need:
I needed to mount a remote (located on 10.0.0.7) Samba folder (named solaria) so that I can use .iso file without downloading it to my local machine (Suse 11.1)

The solution
You can mount the remote driver by hand but I'm lazy so I want it mounted automatically so here is what I did:

1. Created a [solaria] folder in /mnt folder

2. Added the following line in /etc/fstab
//10.0.0.7/solaria/ /mnt/solaria cifs auto,password= 0 0

Now the next time you start you OS you will see the content of the remote folder in your /mnt/fstab

See that I used a blank (no) password for the smb account as I allow guest logins

You can also mount it with the next command
mount /mnt/solaria
and unmount it with
umount /mnt/solaria

Note
One interesting stuff is that the old smbfs was renamed to cifs. Here is where I found this information.

Tuesday, March 31, 2009

Linux scan for open ports

A nice command to scan open ports for a host/ip is:

nmap -sS -O 127.0.0.1

You will get an answer like this:

PORT STATE SERVICE
22/tcp open ssh
25/tcp open smtp
80/tcp open http
110/tcp open pop3
111/tcp open rpcbind
119/tcp open nntp
135/tcp filtered msrpc
137/tcp filtered netbios-ns
138/tcp filtered netbios-dgm
139/tcp filtered netbios-ssn
445/tcp filtered microsoft-ds
1720/tcp filtered H.323/Q.931
3306/tcp open mysql
8080/tcp open http-proxy
8081/tcp open blackice-icecap
8443/tcp open https-alt
Device type: general purpose


If you need to see the open ports from inside (your server) use netstat:

netstat -a

You will get something like:

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:cvspserver *:* LISTEN
tcp 0 0 *:mysql *:* LISTEN
tcp 0 0 *:netbios-ssn *:* LISTEN
tcp 0 0 *:sunrpc *:* LISTEN
tcp 0 0 *:http-alt *:* LISTEN
tcp 0 0 *:microsoft-ds *:* LISTEN
tcp 0 0 *:www-http *:* LISTEN
tcp 0 0 *:ssh *:* LISTEN
tcp 0 0 *:https *:* LISTEN
udp 0 0 *:netbios-ns *:*
udp 0 0 *:netbios-dgm *:*
udp 0 0 *:tftp *:*
.......

Active UNIX domain sockets (servers and established)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 5170 @/tmp/hald-local/dbus-B cG6NtYfGm
unix 2 [ ACC ] STREAM LISTENING 7389 /var/lib/mysql/mysql.so ck
unix 2 [ ] DGRAM 8071089 /var/lib/dhcp/dev/log
unix 2 [ ] DGRAM 8071091 /var/lib/named/dev/log
unix 2 [ ] DGRAM 2177 @/org/kernel/udev/udevd
unix 2 [ ] DGRAM 5171 @/org/freedesktop/hal/u dev_event
............

Thursday, March 26, 2009

Linux: sort your folder by size

In case you need to sort your folders by size, in Linux, you can use the following command:

du --max-depth=1 /home/ | sort -n -r

of course replace '/home/' with your desired folder.

Why automate your business?

Why do you need to automate your business?

The answer is very simple: reduce human time which is VERY expensive.
Someone said that silicate (Si) is cheaper than carbon (C) and that is true.

I started this journey - to automate my business - as I had a lot of problem with employees and I'm searching for a solution.

A friend of mine said that if an employee has a personal problem that problem will become yours.

I started by developing a time tracking software for our company needs and it turned out that it worth - a lot. Now I know exactly where time was spent and more important I can detect very easily who is lagging behind with schedules.

I do not start praising our application - I think any time tracking application can be very helpful.

The main idea is that if you have many automated control mechanisms in place you can sleep better - not as a baby but better than without them.

I hope I will be able to add more and more scripts & software to Scriptoid so that anyone that need to automate his business to find a good solution.

Also we will create a repository for developers to reuse codes, snippets, recipes and programs we are using every day and find it useful.