satishgaudo.com

Understanding technology

Linux command to replace a string across multiple files

sed -i ’s/product1.php/product.php/g’ *.php

In the command above  “product1.php” is replaced with “product.php” across all files with extension “.php” within the project folder.

Bookmark and Share
26 August 2010 at 11:27 - Comments

Learn at Ease through videos: Khan Academy

The Khan Academy is a not-for-profit 501(c)(3) with the mission of providing a world-class education to anyone, anywhere. Despite being the work of one man, Salman Khan, this 1600+ video library is the most-used educational video resource as measured by YouTube video views per day and unique users per month. We are complementing this ever-growing library with user-paced exercises–developed as an open source project–allowing the Khan Academy to become the free classroom for the World.

Check the link : http://www.khanacademy.org/

Bookmark and Share
26 August 2010 at 11:20 - Comments

SPARQL :An RDF query language

SPARQL (pronounced “sparkle”) is an RDF query language; its name is a recursive acronym that stands for SPARQL Protocol and RDF Query Language. It was standardized by the RDF Data Access Working Group (DAWG) of the World Wide Web Consortium, and is considered a key semantic web technology. On 15 January 2008, SPARQL became an official W3C Recommendation.

SPARQL allows for a query to consist of triple patterns, conjunctions, disjunctions, and optional patterns

Check this: http://en.wikipedia.org/wiki/SPARQL

SPARQL Browser for DbPedia  : http://dbpedia.org/snorql/?describe=http%3A//dbpedia.org/resource/Nokia

Bookmark and Share
23 August 2010 at 19:09 - Comments

DBpedia:sophisticated queries against Wikipedia

DBpedia is a community effort to extract structured information from Wikipedia and to make this information available on the Web. DBpedia allows you to ask sophisticated queries against Wikipedia, and to link other data sets on the Web to Wikipedia data. We hope this will make it easier for the amazing amount of information in Wikipedia to be used in new and interesting ways, and that it might inspire new mechanisms for navigating, linking and improving the encyclopaedia itself.

http://dbpedia.org/About

Bookmark and Share
23 August 2010 at 19:03 - Comments

Ganglia : monitoring framework

Ganglia is a monitoring framework for clusters of servers. It records many statistics and can record custom defined ones too. It works in a distributed manner, with each machine you wish to collect statistics for running the Ganglia monitor deamon, gmond. Each monitoring deamon’s statistics are collected by a metadata daemon, gmetad, running on either one of the monitored hosts or a separate machine. Ganglia provides a PHP frontend which displays the data from gmetad in the form of pretty graphs.

For extra details: http://www.ibm.com/developerworks/wikis/display/WikiPtype/ganglia

On installation ganglia will show default metrics like cluster load, cpu status etc.

Ganglia web front

Ganglia web front

Admin can define metrics that he want using “gmetric” command like

/usr/bin/gmetric  -u hits -tfloat -n apache_status_200  -v 100

where n is the metric name and v is the value

Following are the PHP scripts called on crontab,

1. Apache Access log metric to count hits status:

$gmetric_command = “/usr/bin/gmetric”;
//cat access.log | grep “31/Jul/2010:09″ | cut -f9 -d ” ” | sort | uniq -c
$sApacheLogFile=APACHE_LOG_DIR.”access.log”;
$sCurrentDate=date(”d/M/Y:H”);
$sStatCommand=”cat “.$sApacheLogFile.” | grep ‘”.$sCurrentDate.”‘ | cut -f9 -d ‘ ‘ | sort | uniq -c”;
$aOutput=Array();
$sOp=exec($sStatCommand,$aOutput);
if(is_array($aOutput)){
foreach($aOutput as $iK =>$sValue) {
$aData=Array();
$aData=explode(” “,trim($sValue));
if($aData[0]>0){
system($gmetric_command . ” -u hits -tfloat -n apache_status_”.$aData[1].” -v ” . $aData[0]);
}
}
}

2.  Mysql status metrics:

//gmetric command
$stats_command = “/usr/bin/mysqladmin -u root –password=mAnG0pp1 extended-status”;
$gmetric_command = “/usr/bin/gmetric”;
$aCounterMetrics = Array(
“Bytes_received” => “bytes”,
“Bytes_sent” => “bytes”,
“Com_delete” => “operations”,
“Com_insert” => “operations”,
“Com_replace” => “operations”,
“Com_select” => “operations”,
“Com_update” => “operations”,
“Key_reads” => “operations”,
“Qcache_hits” => “hits”,
“Questions” => “queries”,
“Connections” => “connections”,
“Threads_created” => “threads”,
“Slow_queries” => “queries”
);
$aAbsoluteMetrics = array(
“Threads_connected” => “threads”,
“Threads_running” => “threads”
);
$sMysqlLogFile=LOG_DIR.”mysql_stats.txt”;

if(!file_exists($sMysqlLogFile)){
//if no file , create a one
system(”$stats_command > $sMysqlLogFile”);
}else {
//get the old stats data
$aStatsOld=file($sMysqlLogFile);
$aMyStatsOld=Array();
$i=0;
if(is_array($aStatsOld)&& count($aStatsOld)>0){
for($i=3;$i<(count($aStatsOld)-1);$i++){
$aStatTemp=explode(”|”,$aStatsOld[$i]);
$aMyStatsOld[trim($aStatTemp[1])]=trim($aStatTemp[2]);
}
}
unset($aStatTemp);
//get the time file modified last time
$sFileModTime=filemtime($sMysqlLogFile);
unlink($sMysqlLogFile);
//get the current stats
system(”$stats_command > $sMysqlLogFile”);
$sCurrentTime=time();
$aStatsNew=file($sMysqlLogFile);
$aMyStatsNew=Array();
if(is_array($aStatsNew)&& count($aStatsNew)>0){
for($i=3;$i<(count($aStatsNew)-1);$i++){
$aStatTemp=explode(”|”,$aStatsNew[$i]);
$aMyStatsNew[trim($aStatTemp[1])]=trim($aStatTemp[2]);
}
}
$iTimeDiff=$sCurrentTime-$sFileModTime;
if($iTimeDiff<1){
die(”Time difference can’t be less than 1″);
}
# Calculate deltas for counter metrics and send them to ganglia
foreach($aCounterMetrics as $sMetric =>$sUnit){
//print $aMyStatsNew[$sMetric].” – “.$aMyStatsOld[$sMetric].”===<br>”;
$iRate = ($aMyStatsNew[$sMetric] – $aMyStatsOld[$sMetric]) / $iTimeDiff;

if ($iRate>=0) {
system($gmetric_command . ” -u ‘$sUnit/sec’ -tfloat -n mysqld_” . $sMetric . ” -v ” . $iRate);
}
}
//Just send absolute metrics. No need to calculate delta
foreach($aAbsoluteMetrics as $sMetric =>$sUnit){
system($gmetric_command . ” -u $sUnit -tuint16 -n mysqld_” .$sMetric. ” -v ” .$aMyStats[$sMetric]);
}
}

3.  Memcache status:

require_once(CLASS_PATH.’memcache.class.php’);
$oCache = new Cache();
$gmetric_command = “/usr/bin/gmetric”;
$sMemcacheLogFile=LOG_DIR.”memcache_stats.txt”;
$aOldStats=Array();
//get the memcache stats
$aNewStats=$oCache->getExtendedStats();
if(!file_exists($sMemcacheLogFile)){
$f=fopen($sMemcacheLogFile,”w”);
fwrite($f,serialize($aNewStats));
fclose($f);
}else {
$aOldStats=unserialize(file_get_contents($sMemcacheLogFile));
$sFileModTime=filemtime($sMemcacheLogFile);
$iTimeDiff=time()-$sFileModTime;

$f=fopen($sMemcacheLogFile,”w”);
fwrite($f,serialize($aNewStats));
fclose($f);
}

#################################################################################
# Memcache Hit Ratio
#################################################################################
foreach($aNewStats as $sHost =>$aData){
$sDisplayHost=$sHost;
$sDisplayHost=str_replace(”.”,”_”,$sDisplayHost);
$sDisplayHost=str_replace(”:”,”_”,$sDisplayHost);
$iHits   = $aData['get_hits'];
$iMisses = $aData['get_misses'];
$iTotalReq = $iHits + $iMisses;
$iHitRatio=0;
if ($iTotalReq == 0 ) {
$iHitRatio = 0.0;
} else {
$iHitRatio = $iHits / $iTotalReq;
}

system($gmetric_command . ” -u ratio -tfloat -n memcache_hit_ratio”.$sDisplayHost.” -v ” . $iHitRatio);
#################################################################################
# Calculate Memcache Fill Ratio
#################################################################################
$iTotalBytes = 0;
$iUsedBytes  = 0;
$iCurrConnections = 0;
$iCurrItems = 0;
$iFillRatio = 0;
$iEvictions = 0;
$iBytesRead = 0;
$iBytesWritten = 0;
$iTotalBytes = $aData['limit_maxbytes'];
$iUsedBytes  = $aData['bytes'];
$iFillRatio = $iUsedBytes/$iTotalBytes;
$iCurrConnections = $aData['curr_connections'];
$iCurrItems = $aData['curr_items'];

$iEvictions = $aData['evictions'];
$iBytesRead = $aData['bytes_read'];
$iBytesWritten = $aData['bytes_written'];

system($gmetric_command . ” -u ratio -tfloat -n memcache_fill_ratio”.$sDisplayHost.” -v ” . $iFillRatio );
system($gmetric_command . ” -u connections -tfloat -n memcache_curr_connections”.$sDisplayHost.” -v ” . $iCurrConnections );
system($gmetric_command . ” -u items -tfloat -n memcache_curr_items”.$sDisplayHost.” -v ” . $iCurrItems );
//calculate counter metrics such as bytes in/out and evictions

if (isset($aOldStats[$sHost]) && $iBytesRead >= $aOldStats[$sHost]['bytes_read']) {
system($gmetric_command . ” -u number -tuint32 -n memcache_evictions”.$sDisplayHost.” -v ” . ($iEvictions – $aOldStats[$sHost]['evictions']) );
system($gmetric_command . ” -u bytes/s -tfloat -n memcache_bytes_read”.$sDisplayHost.” -v ” . ( ($iBytesRead – $aOldStats[$sHost]['bytes_read']) / $iTimeDiff)  );
system($gmetric_command . ” -u bytes/s -tfloat -n memcache_bytes_written”.$sDisplayHost.” -v ” . ( ($iBytesWritten – $aOldStats[$sHost]['bytes_written']) / $iTimeDiff ) );
}

}

—memcache.class.php

<?php
/**
*
* Setup:
*
edit the singleton() metod
and define the list of memcached servers in a 2-d array
in the format
array(
array(’192.168.0.1′=>’11211′),
array(’192.168.0.2′=>’11211′),
);
*
*
* Usage:
*
<?php
//include the class name
include (’memcache.class.php’);

//store the variable
Cache::set(’key’,'abc’);

//increment/decrement the integer value
Cache::increment(’key’);
Cache::decrement(’key’);

//fetch the value by it’s key
echo Cache::get(’key’);

//delete the data
echo Cache::delete(’key’);

//Clear the cache memory on all servers
Cache::flush();

Cache::replace() and Cache::add are implemented also.

More information can be obtained here:
http://www.danga.com/memcached/
http://www.php.net/memcache

*/
/** @ingroup classes
*  This is the Classes group
*  @{
*/
/**
* @brief class Cache The class makes it easier to work with memcached servers and provides hints in the IDE like Zend Studio
* @version 1
*
*/

class Cache {
/**
* Resources of the opend memcached connections
* @var array [memcache objects]
*/
var $mc_servers = array();
/**
* Quantity of servers used
* @var int
*/

var $servers = array(

array(’127.0.0.1′=>’11211′)
);

var $mc_servers_count=1;

/**
* Accepts the 2-d array with details of memcached servers
*
* @param array $servers
*/
function Cache(){

/*
for ($i = 0, $n = count($this->servers); $i < $n; ++$i){
( $con = memcache_connect(key($this->servers[$i]), current($this->servers[$i])) )&&
$this->mc_servers[] = $con;
}
$this->mc_servers_count = count($this->mc_servers);
if (!$this->mc_servers_count){
$this->mc_servers[0]=null;
}
*/
}

/**
* Get statistics of the server
*
* @param string $key
* @param mix $value
* @return bool
*/
function getExtendedStats() {
$num=0;
foreach ($this->servers[$num] as $host => $port) {
$memcache_obj = memcache_pconnect($host, $port);
}
if (!$memcache_obj) return false;
return $memcache_obj->getExtendedStats();
}

//class end
}

?>

Bookmark and Share
30 July 2010 at 16:37 - Comments

jquery-oembed:

A simple jQuery plugin that uses OEmbed API to help displaying embedded content (such as photos or videos) in your website.

This plugin can be downloaded from google code base code.google.com

http://satishgaudo.com/satblog/ext_img/oEmbed/index.html

Quick explicit example:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jquery-oembed explicit insert example</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/
jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="jquery.oembed.js"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function() {
 $("#container").oembed("http://www.flickr.com/photos/
14516334@N00/345009210/");
});
</script>
<div id="container"></div>
</body>
</html>

In  this example the div#container will display the photo from flickr.
Bookmark and Share
30 June 2010 at 16:12 - Comments

oEmbed

oEmbed is a format for allowing an embedded representation of a URL on third party sites. The simple API allows a website to display embedded content (such as photos or videos) when a user posts a link to that resource, without having to parse the resource directly.

Full Spec

This spec is broken into three parts – configuration, the consumer request and the provider response.

An oEmbed exchange occurs between a consumer and a provider. A consumer wishes to show an embedded representation of a third party resource on their own web site, such as a photo or an embedded video. A provider implements the oEmbed API to allow consumers to fetch that representation.

oEmbed providers can choose to make their oEmbed support discoverable by adding elements to the head of their existing (X)HTML documents.

Example :
Providers: Youtube.com

URL: http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=iYMMv_JN3e0&feature=fvhl&format=xml

Response:

http://www.youtube.com/ Lord Balram & Krishna – Part 1 – Animated Short Stories
<object width=”425″ height=”344″><param name=”movie” value=”http://www.youtube.com/v/iYMMv_JN3e0&fs=1″></param><param name=”allowFullScreen” value=”true”></param><param name=”allowscriptaccess” value=”always”></param><embed src=”http://www.youtube.com/v/iYMMv_JN3e0&fs=1″ type=”application/x-shockwave-flash” width=”425″ height=”344″ allowscriptaccess=”always” allowfullscreen=”true”></embed></object>
RajshriKids
344

480 425
1.0
http://www.youtube.com/user/RajshriKids

YouTube http://i2.ytimg.com/vi/iYMMv_JN3e0/hqdefault.jpg video

360

For complete information,please read the reference url below:
oEmbed: http://www.oembed.com/

Supported OEmbed providers¶

* 5min
* Amazon Product Images
* Flickr
* Google Video
* Hulu
* Imdb
* Metacafe
* Myspace Videos
* Qik
* Revision3
* Screenr
* Slideshare
* Twitpic
* Viddler
* Vimeo
* Wikipedia
* WordPress
* YouTube

Bookmark and Share
30 June 2010 at 15:44 - Comments

HTML 5: Google and Apple unveils new sites to showcase HTML5

Earlier this month, Apple and Google unveiled new sites to showcase HTML5. On it, they showed off a number of impressive web demos coded using only HTML5 technologies.

Google’s HTML 5 site: http://www.html5rocks.com/

Apple’s HTML 5 site: http://www.apple.com/html5/

Bookmark and Share
24 June 2010 at 14:22 - Comments

ab – Apache HTTP server benchmarking tool

ab is a tool for benchmarking your Apache Hypertext Transfer Protocol (HTTP) server. It is designed to give you an impression of how your current Apache installation performs. This especially shows you how many requests per second your Apache installation is capable of serving.

Apache Manual URL

Usage: ab [options] [http://]hostname[:port]/path
Options are:
-n requests Number of requests to perform
-c concurrency Number of multiple requests to make
-t timelimit Seconds to max. wait for responses
-p postfile File containing data to POST
-T content-type Content-type header for POSTing
-v verbosity How much troubleshooting info to print
-w Print out results in HTML tables
-i Use HEAD instead of GET
-x attributes String to insert as table attributes
-y attributes String to insert as tr attributes
-z attributes String to insert as td or th attributes
-C attribute Add cookie, eg. ‘Apache=1234. (repeatable)
-H attribute Add Arbitrary header line, eg. ‘Accept-Encoding: gzip’
Inserted after all normal header lines. (repeatable)
-A attribute Add Basic WWW Authentication, the attributes
are a colon separated username and password.
-P attribute Add Basic Proxy Authentication, the attributes
are a colon separated username and password.
-X proxy:port Proxyserver and port number to use
-V Print version number and exit
-k Use HTTP KeepAlive feature
-d Do not show percentiles served table.
-S Do not show confidence estimators and warnings.
-g filename Output collected data to gnuplot format file.
-e filename Output CSV file with percentages served
-h Display usage information (this message)

Example:

1] ab -n 100 -c 10 www.celebrity.com/qna/qna.php?starid=1
– No table indexing
– No code optimization

This is ApacheBench, Version 2.0.41-dev <$Revision: 1.141 $> apache-2.0
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 1998-2002 The Apache Software Foundation, http://www.apache.org/

Benchmarking www.celebrity.com (be patient)…..done

Server Software: Apache/2.0.52
Server Hostname: www.celebrity.com
Server Port: 80

Document Path: /qna/qna.php?starid=1
Document Length: 8237 bytes

Concurrency Level: 10
Time taken for tests: 12.370747 seconds
Complete requests: 100
Failed requests: 0
Write errors: 0
Total transferred: 841100 bytes
HTML transferred: 823700 bytes
Requests per second: 8.08 [#/sec] (mean)
Time per request: 1237.075 [ms] (mean)
Time per request: 123.707 [ms] (mean, across all concurrent requests)
Transfer rate: 66.37 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 346 1169 1023.3 906 7490
Waiting: 345 1168 1023.3 905 7489
Total: 346 1169 1023.3 906 7490

Percentage of the requests served within a certain time (ms)
50% 906
66% 1064
75% 1240
80% 1363
90% 2402
95% 2618
98% 6031
99% 7490
100% 7490 (longest request)

2] ab -n 100 -c 10 www.celebrity.com/qna/qna.php?starid=1
– implemented table indexing
– code optimized

This is ApacheBench, Version 2.0.41-dev <$Revision: 1.141 $> apache-2.0
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 1998-2002 The Apache Software Foundation, http://www.apache.org/

Benchmarking www.celebrity.com (be patient)…..done

Server Software: Apache/2.0.52
Server Hostname: www.celebrity.com
Server Port: 80

Document Path: /qna/qna.php?starid=1
Document Length: 8237 bytes

Concurrency Level: 10
Time taken for tests: 10.537165 seconds
Complete requests: 100
Failed requests: 0
Write errors: 0
Total transferred: 841100 bytes
HTML transferred: 823700 bytes
Requests per second: 9.49 [#/sec] (mean)
Time per request: 1053.716 [ms] (mean)
Time per request: 105.372 [ms] (mean, across all concurrent requests)
Transfer rate: 77.91 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 354 1008 438.9 914 2314
Waiting: 353 1007 438.9 913 2313
Total: 354 1008 438.9 914 2314

Percentage of the requests served within a certain time (ms)
50% 914
66% 1104
75% 1261
80% 1353
90% 1590
95% 1913
98% 2296
99% 2314
100% 2314 (longest request)

Bookmark and Share
19 June 2010 at 15:08 - Comments

Tuning LAMP systems, Part 2: Optimizing Apache and PHP

Here is an link from digg.com, Tuning LAMP systems, Part 2: Optimizing Apache and PHP

Tuning LAMP systems, Part 2: Optimizing Apache and PHP

Bookmark and Share
7 May 2010 at 10:47 - Comments