Saturday, September 13, 2008

Beware of Google!!

Just now i was chatting with my college friend in Marathi - one of many languages spoken in India. I had missed some of last sentences so so opened it again from "Chats" section in Gmail. Google shows clips and sponesred adds above the main area where mail is displayed. I was amazed to see Adds of site targeted to marathi speking community, to be specific : Matrimony site. How does google know that i am chatting in Marathi and Engilsh mixed and Matrimony site add is relevant to the item to be displaced. Hats of to google!

But at the same time i feel you should be careful about GMail and Google in specific. They are building database of people : your social network, your browsing habits and many other things. Now it has come-up with browser - Chrome. Minimalist one, but behind it Google can store lot of personal information on its servers. Google's mantra is "Don't Be Evil". But after certain limit its really intrusive. Google basically uses all this information for advertising thats there bread-n-butter. But who knows what will they sell to the advertisers. So Beware of Google!

Here is post on HackerNews Network that talks about Dependence on Google
http://news.ycombinator.com/item?id=350968

Saturday, September 6, 2008

Building Performance Monitoring Solution with Nagios and NDOUtils Part 2 PerfNagios

Welcome to part 2 of this series. This took long time but was worth the effort. In this part i am going to describe how to save performance data and integrate a sample monitoring scripts for performance monitoring. In earlier post I wrote about how to install and configure NDOUtils for database support in nagios. We will be using the same setup for storing data but will add some more table to store performance repository. Remember NDOUtils will delete data periodically!.

Lets first discuss how would you save performance data.

PerfNagios

is sourceforge project started by me for implementing this idea. Currently only parsing code is stable, reporting and dashboard functionalities will be added later. Stay connected on this blog for future updates to this project.

PerfNagios is basically small web-interface for displaying nagios related data easily. Eventually it will have good reporting capabilities. Currently it only shows performance data for last 1000 readings. You can see screenshots below.




Following are tables i used for storing performance data.


CREATE TABLE `nagios_metrics` (
`metric_id` int(11) NOT NULL auto_increment,
`instance_id` smallint(6) NOT NULL ,
`host_object_id` smallint(6) NOT NULL ,
`service_object_id` smallint(6) ,
`unit` varchar(60),
`label` varchar(255),
PRIMARY KEY (`metric_id`)
) ENGINE=InnoDB;


CREATE TABLE `nagios_metric_data` (
`metric_data_id` int(11) NOT NULL auto_increment,
`metric_id` int(11) NOT NULL,
`value` double not null,
`warn` double,
`critical` double,
`min` double,
`max` double,
`date` datetime not null,
PRIMARY KEY (`metric_data_id`, `metric_id`)
) ENGINE=InnoDB;


CREATE TABLE `nagios_perf_batches` (
`date` datetime not null,
`last_service_check_id` int(11) not null,
`last_host_check_id` int(11) not null,
`host_checks` int(11) not null,
`service_check` int(11) not null
) ENGINE=InnoDB;


Lets take an example say we need to keep track of how cpu is getting used. For that we need to add service call CPU to monitoring host. Below is small script which outputs important cpu performance metrics : CPU Run length, User, System and Wait-on I/O. Output is like :

CPU : OK 0 | rl=0;2;5 us=2;85;85 sys=0;10;20 wa=0;5;10 total=3;80;90


  • check_java.sh

value=`vmstat 3 3 | awk -f /opt/nagios/libexec/ubuntu-sys-plugins/cpu.awk`
returnvalue=`echo $value | awk '{print $4}'`
echo $value;
exit $returnvalue;


  • and cpu.awk


{
#print $1,$13,$14,$15,$16;
if(NR>=5)
{
rl=rl + $1;
us= us+ $13;
sys = sys + $14;
wa = wa + $16;
}
#print NR,"--->", $0;
}
END {

rl = rl /2;
us= us/2;
sys = sys/2;
wa = wa /2;
total = us + sys + wa;

#print "Total is ", total;

if(total <= 75) { msg = "CPU : OK"; returnvalue=0; } if(toal > 75 && total <= 85) { msg = "CPU : WARINING "; returnvalue = 1; } else if(total > 85)
{
msg = "CPU : CRITICAL";
returnvalue = 2;
}
msg = sprintf("%s %d | rl=%d;2;5 us=%d;85;85 sys=%d;10;20 wa=%d;5;10 total=%d;80;90",msg,returnvalue,rl,us,sys,wa,total);
print msg;
}


  • To include this monitoring script you need to add service for localhost. In nagios 3.x configuration is based on templetes. For defining we need to first add check command in file /opt/nagios/etc/objects/commands.cfg


# 'check_cpu command definition - tushar
define command{
command_name check_cpux
command_line /opt/nagios/libexec/ubuntu-sys-plugins/check_cpu.sh
}

  • Once this is done you need to add service definition in /opt/nagios/etc/objects/localhost.cfg.

define service{
use local-service ; Name of service template to use
host_name localhost
service_description CPU
check_command check_cpux
notifications_enabled 0
}






Restart the Nagios by /etc/init.d/nagios restart and done! You system is now able to monitor cpu information.

Below is the graph drawn in PerfNagios for the same script.





JAMon Data for Java Applications

Recently i used JAMon for gathering important metric in one application which was not j2ee application. For JDK based application how do you get information in absence of JAMon Web Interface. I asked about it to Steve Souza who imeedaitedly returned with answer : MonitorFactory.getData() and MonitorFactory.getHeader(). Based on this data jsmons.jsp is written. For JDK based application i thought we can give same information via JMX. Below is small wrapeer to do

  • Register JAmon Factory as MBean and implement some key functions like Attributes and Operations


JAMonMBean mbean = new JAMon();
ObjectName name = new ObjectName("jamon.perf:type=JAMonBean");
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
mbs.registerMBean(mbean, name);

  • JAMon Client


ObjectName name = new ObjectName("jamon.perf:type=JAMonBean");
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
String arr[] = {key};
String[] signature = new String[] { "java.lang.String" };
obj = mbs.invoke(name,operationName, arr,signature );


  • Start background thread to store information in file


Object[][] data= client.getData();
// record format date : label : Hits : Avg : Total : StdDev
String timestamp = "" + System.currentTimeMillis();
if(data!=null)
{
for(int i=0;i<data.length;i++)
{
StringBuilder sb = new StringBuilder();
sb.append(timestamp).append(" : ");
sb.append(data[i][0]).append(" : ");
sb.append(data[i][1]).append(" : ");
sb.append(data[i][2]).append(" : ");
sb.append(data[i][3]).append(" : ");
sb.append(data[i][4]);
pw.println(sb.toString());
pw.flush();
//System.out.println(sb.toString());
}
}


Equipped with above, I have also written a small utility which outputs information of all counters periodically in the format

timestamp : label : avg. response time : hits : std. deviation

I have uploaded code used to demonstrate the JAMon Bean have been uploaded at http://tushar.khairnar.googlepages.com/perf-sample.zip.

Please see Sample Program for the same.