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.
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- New Products
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Home, My Backup Data Center
- RSS Feeds
- Trying to Tame the Tablet
- What's the tweeting protocol?
- New Products
- Validate an E-Mail Address with PHP, the Right Way
- Drupal is an Awesome CMS and a Crappy development framework
1 hour 28 min ago - IT industry leaders
3 hours 50 min ago - Reply to comment | Linux Journal
20 hours 39 min ago - Reply to comment | Linux Journal
23 hours 11 min ago - Reply to comment | Linux Journal
1 day 29 min ago - great post
1 day 1 hour ago - Google Docs
1 day 1 hour ago - Reply to comment | Linux Journal
1 day 6 hours ago - Reply to comment | Linux Journal
1 day 7 hours ago - Web Hosting IQ
1 day 8 hours 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