An Ajax-Enhanced Web-Based Ethernet Analyzer
Listing 4. The Ajax-Enabled JavaScript Code
var capturing = false;
var matchEnd = new RegExp( "END run" );
var r = new getXMLHttpRequest();
function startWatcher() {
setInterval( "updateCaptureData()", 1500 );
capturing = true;
}
function getXMLHttpRequest() {
try {
return new ActiveXObject("Msxml2.XMLHTTP");}
catch(e) {};
try {
return new ActiveXObject("Microsoft.XMLHTTP");}
catch(e) {}
try {
return new XMLHttpRequest(); }
catch(e) {};
return null;
}
function updateCaptureData() {
if (capturing) {
r.open( "GET",
"/cgi-bin/get_watcher_data.cgi",
false );
r.send( null );
displayCaptureData();
}
}
function displayCaptureData() {
var te = document.getElementById("watcherarea");
if ( r.readyState == 4 ) {
if ( r.status == 200 ) {
te.value = r.responseText;
te.scrollTop = te.scrollHeight;
if ( matchEnd.test( te.value ) ) {
capturing = false;
}
}
else
{
te.value =
"Web-based DNS Analysis unavailable.";
}
}
}Listing 4 contains the dns-watcher.js code. A lot of what happens here has been covered by Reuven's excellent Ajax articles. The code starts by declaring some global variables that are used throughout the remainder of the code:
var capturing = false; var matchEnd = new RegExp( "END run" ); var r = new getXMLHttpRequest();
The capturing boolean is set to true while the analyzer is capturing traffic, and to false otherwise. A regular expression is created to match against a string containing the words “END run”. Finally, an Ajax request object is created with a call to the getXMLHttpRequest method, which is taken directly from Reuven's examples.
The startWatcher method starts the heavy lifting by calling the updateCaptureData method every 1.5 seconds and setting capturing to true:
function startWatcher() {
setInterval( "updateCaptureData()", 1500 );
capturing = true;
}It is within the updateCaptureData method that the Ajax call occurs, with the request object being used to execute another CGI script that accesses the dns-watcher.log disk file and returns its contents. (Listing 5 contains the get_watcher_data.cgi script, which is written in Ruby.) Once the CGI script has been invoked on the Web server, a call to displayCapture occurs:
function updateCaptureData() {
if (capturing) {
r.open( "GET",
"/cgi-bin/get_watcher_data.cgi",
false );
r.send( null );
displayCaptureData();
}
}Listing 5. A Simple CGI to Retrieve the Captured Data
#! /usr/bin/ruby -w
puts "Content-type: text/plain\n\n"
IO.foreach("/var/www/watcher/dns-watcher.log") do |l|
puts l
endThe displayCaptureData method is adapted from Reuven's code and processes the results of the Ajax call, which are available from the request object. These are used to update the watcherarea text-area widget within the results Web page:
te.value = r.responseText;
Note the use of the following line of JavaScript to scroll the text area to the bottom of the results:
te.scrollTop = te.scrollHeight;
And, finally, note that the displayCaptureData method sets the capturing boolean to false as soon as a line that matches the regular expression appears within the data coming from the Ajax request (see Figures 1 and 2 to convince yourself that this in fact matches at the end of the network capture):
if ( matchEnd.test( te.value ) ) {
capturing = false;
}This check is very important. Without it, the Web browser continues to send an Ajax request to the server every 1.5 seconds for as long as the watcher.html results page is displayed within the browser, even after the analyzer has finished and isn't generating any more data. With this check in the code, the Ajax behavior is switched off, reducing the load on the Web server (and keeping the Apache2 access log from quickly growing large).
To deploy my solution, I created a simple shell script to copy the required components into the appropriate directory locations on my Web server (which is Apache2 on Ubuntu):
sudo cp watcher.html /var/www/ sudo cp startwatcher.html /var/www/ sudo cp dns-watcher.js /var/www/js/ sudo cp dns-watcher.rb /var/www/watcher/ sudo cp get_watcher_data.cgi /usr/lib/cgi-bin/ sudo cp startwatch.cgi /usr/lib/cgi-bin/
These directory locations may not match those of your Apache2 installation, so adjust accordingly. You also may need to create the js and watcher directories. And, of course, make sure the CGIs have their executable bit set.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.
Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.
Sponsored by ActiveState
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?
| Speed Up Your Web Site with Varnish | Jun 19, 2013 |
| Non-Linux FOSS: libnotify, OS X Style | Jun 18, 2013 |
| Containers—Not Virtual Machines—Are the Future Cloud | Jun 17, 2013 |
| Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer | Jun 12, 2013 |
| Weechat, Irssi's Little Brother | Jun 11, 2013 |
| One Tail Just Isn't Enough | Jun 07, 2013 |
- Speed Up Your Web Site with Varnish
- Containers—Not Virtual Machines—Are the Future Cloud
- Linux Systems Administrator
- Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer
- Senior Perl Developer
- Technical Support Rep
- Non-Linux FOSS: libnotify, OS X Style
- UX Designer
- Web & UI Developer (JavaScript & j Query)
- RSS Feeds
- Varnish works!
8 min 26 sec ago - Reply to comment | Linux Journal
38 min 3 sec ago - Reply to comment | Linux Journal
3 hours 4 min ago - Reply to comment | Linux Journal
7 hours 3 min ago - Yeah, user namespaces are
8 hours 20 min ago - Cari Uang
11 hours 51 min ago - user namespaces
14 hours 44 min ago - yea
15 hours 10 min ago - One advantage with VMs
17 hours 39 min ago - about info
18 hours 12 min ago




Comments
Nice!
Thanks for the excellent follow-up to your TPR article. There are plenty of use cases for a network analyser that do not require promiscuous mode - which would simplify the approach. In general, I prefer not to operate in promiscuous mode since I am only interested in monitoring point-to-point traffic.
As a consultant who frequently performs network analysis and tuning I am keenly interested in a solution like this. While I like the Ajax/Apache approach my customers aren't likely to be crazy about me installing the entire kit on their boxes. Wrapping this approach into one executable would be ideal. Or, simply installing an agent which would send the results to an Apache instance running on my laptop :-)
Cheers
Wrapping this approach into one executable would be ideal
Thanks for the positive comment. If all you need is the results, all your customers need is Ruby (or Perl) installed on their boxes, with a little script that wakes up every now & then and sends stuff to you. My article simply used the analyzer as a way to generate a lot of server side data which allowed me to demo the Ajax solution.
--
Paul Barry
IT Carlow, Ireland
http://glasnost.itcarlow.ie/~barryp
Paul Barry
broken link
Thanks for this nice article!
There is only one thing I want to add:
The above link to the sources appears to be dead...
Here is the fixed one:
ftp://ftp.linuxjournal.com/pub/lj/listings/issue157/9614.tgz