Export RRD Values to MS Excel

Sunday, November 1, 2009 Posted by Md. Monjurul Hasan 0 comments
I have used a nice, simple script to export the rrd values into excel using PHP 5. You can find the script here:

http://code.google.com/p/php-excel/

Here is my code for the rrd export (here you'll find two functions - calculate_rrd() and format_bits(), these are described in my previous blog):

/*
Author: Md.Monjurul Hasan

Description
- RRD data exporter - output values into excel file
*/
/***************included files*******/

#include the export-xls.class.php file
require('export-xls.class.php');

$filename = 'rrd_export'.rand(0,10000).'.xls'; // a random filename chosen
#create an instance of the class
$xls = new ExportXLS($filename);

//read config file and get variables
$rrd_array = array ('rra/test1.rrd', 'rra/test2.rrd');
//export to excel
$header = " RRD Export"; // single first col text
$xls->addHeader($header);

#add blank line
$header = null;
$xls->addHeader($header);

#add 2nd header as an array of 3 columns
$header[] = "RRD Name";
$header[] = "Cur In";
$header[] = "";
$header[] = "Cur Out";
$header[] = "";
$header[] = "Avg In";
$header[] = "";
$header[] = "Avg Out";
$header[] = "";
$header[] = "Max In";
$header[] = "";
$header[] = "Max Out";
$header[] = "";
$header[] = "95% In";
$header[] = "";
$header[] = "95% Out";
$header[] = "";

$xls->addHeader($header);

$count = 0;
for ($c = 0; $c < count($rrd_array); $c++) {
$ret = calculate_rrd($rrd_array[$c], $graph_type); // graph type is daily/weekly/yearly and monthly
$row = array();
$row[] = $rrd_array[$c];
$result = format_bits($ret['current_in']);
$row[] = $result[0];
$row[] = $result[1];
$result = format_bits($ret['current_out']);
$row[] = $result[0];
$row[] = $result[1];
$result = format_bits($ret['average_in']);
$row[] = $result[0];
$row[] = $result[1];
$result = format_bits($ret['average_in']);
$row[] = $result[0];
$row[] = $result[1];
$result = format_bits($ret['max_in']);
$row[] = $result[0];
$row[] = $result[1];
$result = format_bits($ret['max_out']);
$row[] = $result[0];
$row[] = $result[1];
$result = format_bits($ret['percentile_in']);
$row[] = $result[0];
$row[] = $result[1];
$result = format_bits($ret['percentile_out']);
$row[] = $result[0];
$row[] = $result[1];
$xls->addRow($row);
}
//output the excel file
$xls->sendFile();

?>

Percentile Calculation using PHP, RRDTool API

Posted by Md. Monjurul Hasan 1 comments
I needed the rrd values in tabular format rather than graph. So i fetched the rrd values using rrd_fetch() function of PHP-RRDTool API. Then I calculated the required Current, Average, Max and 95th Percentile using the following function which may save time of others:

/***
Function RRD Calculation

@input param1: rrd filename with location e.g /var/lib/mrtg/test.rrd
param2: graphtype e.g daily, weekly, monthly,yearly etc
@output array containing current_in, current_out, average_in, average_out, max_in, max_out, percentile_in, percentile_out values (in bits)

***/
function calculate_rrd ($rrd, $graphtype) {

$data_bits_in = array();
$data_bits_out = array();
$data = array();

if($graphtype == "daily") {
$timelength = "-1d";
$endtime = "-5min"; //endtime is required to avoid last nan values
}
else if ($graphtype == "weekly") {
$timelength = "-1w";
$endtime = "-30min";
}
else if ($graphtype == "monthly") {
$timelength = "-1m";
$endtime = "-120min";
}
else {
$timelength = "-1y";
$endtime = "-1440min";
}
$rrdparams = array ("AVERAGE","--start", $timelength, "--end", $endtime,);

//fetch rrd values using API
$ret = rrd_fetch($rrd, $rrdparams, count($rrdparams));

//start calculation
$data = $ret ['data'];
$data_limit = count ($data)-2;
$data_bits_limit = $data_limit/2;

for ($i = 0; $i < $data_limit;$i++) {
if ($i%2 == 0) {
if (is_nan($data[$i])) $data_bits_in[$k] = 0;
else $data_bits_in[$k] = (int)($data[$i]*8); $k++;
} else {
if (is_nan($data[$i])) $data_bits_out[$j] = 0;
else $data_bits_out[$j] = (int)($data[$i]*8); $j++;
}
}

//get current/average/max value from rrd data

$result['current_in'] = $data_bits_in[$data_bits_limit-1]; $result['current_out'] = $data_bits_out[$data_bits_limit-1]; $result['average_in'] = array_sum ($data_bits_in)/$data_bits_limit; $result['average_out'] = array_sum ($data_bits_out)/$data_bits_limit; $result['max_in'] = max ($data_bits_in);
$result['max_out'] = max ($data_bits_out);

//percentile calculation
sort($data_bits_in);
sort($data_bits_out);
$pos = round($data_bits_limit*0.95);
$result['percentile_in'] = $data_bits_in[$pos];
$result['percentile_out'] = $data_bits_out[$pos];

//return the result
return $result;
}
For percentile calculation, I followed the following article:
http://forums.cacti.net/post-95140.html&highlight=

To represent the values in bps/kbps/Mbps, you can use the following function:

/***
Function format value from bit to bps/kbps/Mbps

@input : bits
@output array: array[0] = value
array[1] = bps/kbps/Mbps

***/
function format_bits ($bits) {
$round_precision = 2;
if ($bits < 1000) {
$result[0] = round($bits,$round_precision);
$result[1] = "bps";
}
else if ($bits >= 1000 && $bits < 100000) {
$kbits = $bits/1000;
$result[0] = round($kbits,$round_precision);
$result[1] = "kbps";
}
else {
$mbits = $bits/(1000*1000);
$result[0] = round ($mbits, $round_precision);
$result[1] = "Mbps";
}
return $result;
}

Lyrics :: Rupok by Artcell

Friday, September 11, 2009 Posted by Md. Monjurul Hasan 0 comments
Rupok (Ekti gaan)
Kotha: Rumman

Moner vitor juddho chole amar sharakhon
Neshar moto thomke thake
Klanto a jibon
Dhongsho smritir parey boshe amar ahoban
Dhukkho vule notun kore
Likhchi tomar a gaan

Michil vanga nirjonotay dariye eka
Alor nicher ondhokare smritir dekha
Char deyaler shobdo gula ekhon shudhoi chai
Meghe Dhaka shohor tate tui kebol nai
Moner vetor changa ghorar adim ayojon
Shunnotay dube thake amar sharakhon
Niyom vangar mogoz jure ekhon shudhoi chhok
Tepantor er nirbashone hariye geche rupok

Monei juddho amar nijer shathey lori
Smritir deyal chuye hazar murti gori
Chokher dekhay ja dekhi
Aar hoyni ja dekha
Ovimaaner nodir teer a
Datiye thaki eka

Vul jonme koshto amar lekhar aposhe
Odekha ek shorgo vashe ochena akashe
Moner vetor juddho ekhon
Amar shararaat
Ondhokarer kolahole dhorchi
Tomari haat

Ekla vishon amar ghore
Smritir hahakar
Aloy chaichhi niviye debo
Molin ondhokaar
Moner vetor juddho chole amar sharakhon
Mohakale miliye geli hothat kokhon
Labels: ,

WEB 2.0

Monday, August 17, 2009 Posted by Md. Monjurul Hasan 0 comments
I found some nice videos on zdnet on web 2.0 concepts. Hope it will help others...
Web2.0 - http://news.zdnet.com/2422-13569_22-154425.html
Mashup - http://news.zdnet.com/2422-13569_22-152729.html
SOA - http://news.zdnet.com/2422-13569_22-155698.html
Labels: , ,

FB developer garage in BD

Posted by Md. Monjurul Hasan 0 comments
Yesterday I attended the facebook developer garage'09 in bangladesh. It was held in de ja vue cafe, gulshan. It was really good and inspiring!! Got 2 T-shirts and a pen n nicee notepad...labh hoise giya:-D.

My First Cox's Bazar Tour

Tuesday, August 11, 2009 Posted by Md. Monjurul Hasan 0 comments
It was a great tour with friends. We targeted the rainy weather of the sea beach. Although we didn't get the cats-n-dogs rain, it was not bad at all. We left dhaka on friday night (07/18/09) and came back from tour on tuesday morning(11/08/09). Mentionable places that we went other than sea beach were himchhari, inani beach and moheshkhali.

Digging MacBook Pro

Monday, August 3, 2009 Posted by Md. Monjurul Hasan 0 comments
I got one brandnew MacBook Pro with Leopard 10.5.7 as Mac OS X. Installed windows vista in this Mac using bootcamp and cracked the vista with developer activation :-D. I can explore and copy the files of Mac OS from vista using HFSExplorer. From Mac OS, win vista partitions can be accessed by default with bootcamp. Working lyk charm!!

Lyrics:: Ekdin ghum vanga shohore

Tuesday, July 7, 2009 Posted by Md. Monjurul Hasan 0 comments
ek din, ghum vanga shohore
mayabi shondhay, chand jaga ek rat e...
ekti kishore chele, ekaki shopno dekhe,
hashi ar gaan e, shukher chobi ake
ahaa kije shukh..

shopnera hariye jay, shomoyer shagore e,
bethar abir e, kobita adhar e haray
vabonaro ful jhore jhore jay ahaaa
jiboner o gan hoyna surer kangal...

ekdin ghum vanga shohore..
mayabi shonday chand jaga rat eeee..
ekti kishor chhele, ekaki shopno dekhe....
hashi ar gan e .. shukher chobi akee..
ahaaaa kije shukh....

shoishob bimurto hoy, jay na bojha jayna
ashar jhorna payna shukher thiknaa
hotasha shudhu shathi hoye jaya ahaaa
she kishor ebar jibon chere palay

ekdin ghum vanga shohore

Fix for PHP code and SugarCRM for BD DST

Saturday, June 20, 2009 Posted by Md. Monjurul Hasan 0 comments
Although php date function should take the localtime of the server, seems it did not take last night. so I set the default timezone to "Asia/Dhaka" in my PHP code before calling the date function.

date_default_timezone_set("Asia/Dhaka");

also I needed to change the Asia/Dhaka timezone of SugarCRM in include/timezone/timezones.php file

'Asia/Dhaka' =>
array (
'gmtOffset' => 420, //previously it was 360
)

Tweaks for BD Daylight Saving

Friday, June 19, 2009 Posted by Md. Monjurul Hasan 0 comments
We are working on the timezones of our Linux servers (RHEL, CentOS and Ubuntu) to update them for upcoming BD Daylight Saving. Also some java application runs on its own tzdata. So we updated those too. We put necessary task steps (for both windows and linux) in our official website so that people get benefit from us. Here's the link:

http://www.mango.com.bd/bd-dst.php

A(n)ts - Applicant Tracking System (Open Source)

Thursday, June 4, 2009 Posted by Md. Monjurul Hasan 1 comments
I kept on looking for E-recruitment Software, ofcourse open source and finally I have decided to work on Ants - an open source Applicant Tracking System. Although I need to customize it a lot. Its simple and built on PHP and MySQL. I got a recruitment module in OrangeHRM, an open source HR management software, which has limited features and can't give a complete e-recruitment solution in my opinion. Lets see how Ants work for me!
Labels:

Help cyclone 'aila' affected ppl

Tuesday, June 2, 2009 Posted by Md. Monjurul Hasan 0 comments
We (Our office management) have decided to give our 1 day salary for the 'aila' sufferers through Prothom Alo. Also I have donated today 2000 tk in a NGO. But nothing seems adequate to me. Lots of ppl are suffering. How can I contribute bigger to get them back to their normal life????
Labels:

Binary backup of MySQL table

Monday, June 1, 2009 Posted by Md. Monjurul Hasan 0 comments
Usually we take mysql db/tables sql backup by mysqldump command or from phpmyadmin tool (my favorite). But if we need to take the binary backup, you can't do it . What you need to do, just simply copy all table files (*.frm, *.MYD, and *.MYI files). Usually these files are present in /var/lib/mysql directory in linux OS. If you want to check that you are getting the right data in tables you can use the following command
strings test.frm ,
say you are taking backup of test table. This command will show you all data in that table.

here's a useful link:
http://dev.mysql.com/doc/refman/5.0/en/backup.html
Labels:

Open source photo gallery - Plogger

Tuesday, May 26, 2009 Posted by Md. Monjurul Hasan 0 comments
I was in need of a photo gallery - ofcourse open source, and I got two choices - Plogger and Gallery. Gallery2 will give you many features while plogger is really simple and worth for my small requirements. It has easy admin panel and also gives the basic features including slideshow - so I decided to rely on Plogger. I have modified the default theme and it worked fine.
First I removed the footer-text ('Powered by Plogger') from page (for that I commented out this line - print plogger_link_back(); in ploggerdir/themes/default/footer.php)
and
modified the css to decrease the search box width (added this line

#jump-search-container input[type=text] {
width: 120px;
}

in ploggerdir/themes/default/gallery.css ).

Still working on it.

Debug Shell and Python Script from console

Tuesday, May 19, 2009 Posted by Md. Monjurul Hasan 0 comments
> When running Shell script, enable the debug mode with -x option.E.g. sh -x yourscript.sh
> When running Python script, import pdb - the Python debugger, call the set_trace() function and enter n in the console for next debug output. E.g.

#!/usr/bin/python
# epdb1.py -- experiment with the Python debugger, pdb
import pdb
def combine(s1,s2): # define subroutine combine, which...
s3 = s1 + s2 + s1 # sandwiches s2 between copies of s1, ...
s3 = '"' + s3 +'"' # encloses it in double quotes,...
return s3 # and returns it.
a = "aaa"
pdb.set_trace()
b = "bbb"
c = "ccc"
final = combine (a,b)
print final
Labels:

Email tips for PHP Pear

Posted by Md. Monjurul Hasan 0 comments
Email facility is required for almost every application. Hence these tips are helpful.

1. For datetime in proper format,
$headers["Date"] = date("r");

2. For both HTML and text mail,
 $headers["Content-Type"] = "multipart/alternative";

3. For sending mail with SMTP Authentication,
$mail = Mail::factory(
"smtp",
array ('host' => $host,
'auth' => 'true',
'username' => $username,
'password' => $password)
);


A quick helpful tutorial: http://email.about.com/od/emailprogrammingtips/qt/et073006.htm
Labels: , ,

Upgraded to Windows® 7 RC

Posted by Md. Monjurul Hasan 0 comments
Just safely upgraded my windows from vista to 7 without taking backup. It required additional 8 GB in installation directory. Looks pretty cool. Like the new taskbar. This version is valid till June,2010. So I am an original windows user at last!!
Labels: ,

HTTP VS HTTPS

Posted by Md. Monjurul Hasan 0 comments
I was trying to study the basics of them. Summarized here what I have got.

> http send everything you do in plan text for any one to read.

> https is slower and requires a secure server and therefore would be much more expensive. Browsing doesn't require secure socket layer.

https encrypts everything you do so that no one can read what you type but the recipient.

The problem with encrypting data is that you cant just encrypt it and say only yahoo can read it. Both you and yahoo have to have a secret key so that yahoo can decrypt what you sent and encrypt private stuff for you to read.

This is accomplised by an encryption scheme known as public key. Yahoo puts out a public key so that every one can encrypt stuff that only yahoo can read its like a one way key: you can package stuff up and send it to yahoo so that they can read it with theire private key but some one with a public key cant see what you encrypted.

So you package up a key for yahoo to use to talk to you and you are all set.

Why all internet communication isn't done like this is because of what is known as the man in the middle attack, and its solution.

It's quite simply to pretend to be yahoo.com if you know what you doing. so I pretend to be yahoo and all traffic you think is going to yahoo comes to me. you ask me for my public key I respond back with an fake public private key pair that I made then I ask yahoo for there public key and every thing you to I do I just watch for anything interesting like Credit cards etc.

To be continued...
Labels: ,

Tips for Dom PDF

Posted by Md. Monjurul Hasan 0 comments
See what I have experienced so far. May be useful for you too.

  1. No need to install, can be called directly.But be sure php-xml package is installed. You can be sure from phpinfo().

  2. When using, change the path in dompdf_config.inc if necessary

  3. Be sure that the html code is clean i.e. tags are closed

  4. Dom PDF can not render multiple html pages with a single table. I was facing of getting a single page while original was more than one


To be continued....
Labels:

SugarCRM: Search directly using url

Posted by Md. Monjurul Hasan 0 comments
This url can save your time! It takes you to the SugarCRM search form and gives the search result for the desired string in Accounts module. Can search in multiple modules (use &)

http://yoursugarcrm.com/index.php?module=Home&query_string=test&advanced=true&action=UnifiedSearch&search_form=false&query_string=yourstring&search_mod_Accounts=true

Remember, you have to login into your SugarCRM before using this url.
Labels:

Dom PDF: PDF support for Smarty templates

Posted by Md. Monjurul Hasan 0 comments
I was having some troubles with PDF report generation. I used Smarty Templates, so I was looking for PDF class that will support Smarty output.

Finally I stayed on Dom PDF. It supports PHP 5 and CSS 2.1.


//include the dom pdf
require('dompdf/dompdf_config.inc.php');

//fetch the HTML output from smarty template
$htmlcontent=$smarty->fetch('smarty_template.tpl');

//call dom pdf class
$dompdf = new DOMPDF();
$dompdf->load_html($htmlcontent);
$dompdf->render();
//display output in browser
$dompdf->stream();
//or, save as a pdf
$pdf = $dompdf->output();
file_put_contents("tmp/filename.pdf", $pdf);


There are other options like set paper size etc. You can try this.
Labels: ,

Get the browser's response from authenticated site using curl

Posted by Md. Monjurul Hasan 0 comments
Some days ago I needed the images generated by a secure webpage on a routine basis (you have to enter by giving username and password and when it is clicked it runs its cgi script/php file). Without going to the browser I simply ran a shell script from cron which mainly has the following command.

curl -u username:password url

saved a lot of tym!!
Labels: ,

SugarCRM: Adding custom fields in custom module without using studio

Posted by Md. Monjurul Hasan 0 comments
SugarCRM is really handy using studio. Otherwise, Its giving me pain. Currently I am using SugarCRM version 5.0.0f.

I am trying to merge my changes from development CRM to main CRM without using studio. For this I need to add the custom fields,relations and change the current view (EditView, DetailView, ListView and SearchPanels) since I have modified it.

Lets c guys what I have done in the meantime.

  1. Added two database modulename_cstm and fields_meta_data and dump them from my dev crm database.
    if you have already these database, then just insert the new entries.

  2. For custom relationship, created a metadata file (module1module2MetaData.php) in sugarcrm/metadata dir and included that file in two files of custom directory.
    custom/application/Ext/Include/modules.ext.php and
    custom/Extension/application/Ext/TableDictionary/modulename.php

  3. Created an entry in following files
    custom/Extension/modules/ModuleName/Ext/Layoutdefs/modulename.php
    custom/Extension/modules/ModuleName/Ext/Vardefs/modulename.php

  4. Created folder in custom/Extension/modules/ same as other module [name it as your related module]
    watch out Layout and Vardef file in that directory

  5. Change in files modules/ModuleName/Ext/Vardef and modules/ModuleName/Ext/Layout if necessary

  6. Label of the fields should be present in custom/modules/ModuleName/language/en_us.lang.php

  7. New viewdefs files should be present at custom/modules/ModuleName/metadata/

  8. Do not forget to quick repair and rebuild from sugar admin


After all these works, I have found my changes merged and working, but the studio views are still like previous. So I am working on it.

Cheers!
Labels: