Determine outgoing apache bandwidth usage with built in commands

To find the bandwidth usage of your Apache server, you can use many existing tools. Like vnstat, awstat.  The most common thing about these tools is they need the utility installed. What if you dont have this installed and you want to calculate your bandwidth? This is can be easily done by parsing Apache access logs. This technique will only work if you are a web master and you have no other bandwidth eating service other than apache. Most web developers will fall in this category. So here is the technique to find apache bandwidth usage.

Note: You need ssh access to perform these actions. Also I assume you have not deleted your log files.

  1. Determine the date range for which you want to find the bandwidth usage for. For example I want to deter mine bandwidth usage from Oct 1st 2012 to Oct 30 2012. Note you must have access log files for that range.
  2. Now the big command. Assuming your apache log directory is /var/log/apache2
    1. find /var/log/apache2 -type f \
          -name '*.gz' \
          -newermt "2012-10-01 00:00:00" \
          -not \
          -newermt "2012-10-30 23:59:59" \ 
          -exec zcat '{}' \; | 
      egrep '"[^"]+" +200 [0-9]+' -o | 
      awk '{sum+=$(NF)} END {print sum/1024/1024/1024 " GB"}'
  3. This will print something like “34.345 GB

Convert Little endian to Big endian in PHP or vice versa

In PHP you might have to convert the endianness of a number. PHP does not provide any function for this even though it has function for almost everything.

So I wrote a function for this,

[code language=”php”]
function chbo($num) {
$data = dechex($num);
if (strlen($data) <= 2) {
return $num;
}
$u = unpack("H*", strrev(pack("H*", $data)));
$f = hexdec($u[1]);
return $f;
}[/code]

Usage:

php > echo var_dump(5254071951610216, chbo(5254071951610216448));
int(5254071951610216)
int(20120214104648)
php > echo var_dump(2147483648, chbo(2147483648));
int(2147483648)
int(128)

Note: this function changes the byte order. If your machines byte-order is little-endian, this function will change it to big-endian. If your machines byte-order is big-endian, it will change the number to big-endian.

All x86 and x86_64 are little-endian. ARM can be both.  More can be found on this wiki article

Convert numbers from English to Bangla

Today Ayon came up  with a problem that he needs to convert English digits to Bangla. The Input would be something like “1 ডলার = 81.55 টাকা” and the output should be “১ ডলার = ৮১.৫৫ টাকা”. How to do it?

Its very easy to do. In fact most developers will be able to do it within few minutes. I just want to share my solution.

I use PHP’s str_replace function.

[code language=”PHP”] $bn_digits=array(‘০’,’১’,’২’,’৩’,’৪’,’৫’,’৬’,’৭’,’৮’,’৯’);
$output = str_replace(range(0, 9),$bn_digits, $input); [/code]

Thats it. You can wrap it with a function and re use it.