Welcome!

Apache Authors: Pat Romanski, Liz McMillan, Elizabeth White, Christopher Harrold, Janakiram MSV

Related Topics: Apache, Java IoT

Apache: Article

Analyzing Load on Apache Web Server

Monitoring performance over any desired time period

A web server plays an increasingly important role in the computing world as the world has shifted away from traditional stand-alone desktop computing to Internet's Client-Server and its variants based computing paradigm. Almost everything on networks is consumed either by using web services or web-pages, for all of which, a web-server is integral. As such, it becomes imperative for organizations to be able to finely monitor their servers' load usage and average performance over different times of day and according to the kind of resources hosted on the server. This raises the need for a solution through which organizations and individuals can monitor their web servers' performance to an arbitrary level of granularity depending on their needs.

This article addresses the same issue and implements an approach for the same in Java programming language.

Problem Statement - Use Case Scenario
Consider the case where you want to monitor the performance of an Apache Web Server for some past time. You would want to know what are the numbers of pages served by the server in that elapsed time span so as to identify the performance as well as utilization of the Web Server.

The mod_status module in the Apache Web Server already provides us with a status file which determines the performance of the server through several statistics including number of worker, requests, time the server has been running since it was started, and number of requests amongst other things.

But a major disadvantage of mod_status file is that it tells us about the average number of requests since the time server was started and this may not reflect the actual performance of the server in various instances. For example, consider that the server was started at 10:00 AM and it ran till 12:00 Noon. In those 2 hours, it server 120 requests. So the number of request returned by mod_status file would be 1 request per minute. But, during that time span, let us say the server was idle between 10:00 - 11:00 AM and it actually server 120 request in a time span of one hour only. So the actual performance would be 2 requests per minute from 11:00-12:00. There is no way in mod_status file to the status for different time instances. It returns the average only from the time it was started to the present time.

In this article we will implement a way to find out the average performance of the server for different time periods without the need to stop/start the server again and again. We will also store those values for a fairly large period in a database so that we can get a better know-how of the server performance over different time periods.

Tech and Services to be Used
We are going to implement the solution in Java and will make use of the below mentioned services and tech:

  1. Apache Web Server: We will be using an instance of Apache Web Serve in order to monitor it and send requests to it to fetch the machine readable status file generated by mod_status module.
  2. A listener to receive the records. In this example we will showcase it through H2 Database. It will be used to store the values fetched from the status module. Any relational database can be used instead of H2. Users are free to register another listener and implement it the way they want.
  3. Servlets and JSP (Optional): The implementation would communicate with the Apache web server using a servlet hosted on tomcat server. The same can be achieved using command line.

Implementation

Let us now delve into the actual implementation of the application. This article uses Eclipse as a development environment and assumes familiarity with Eclipse. The article also assumes that you have an Apache web server installed and running on your system. If you do not have Apache Web Server installed, you can download and install it from http://httpd.apache.org/download.cgi

Implementation Roadmap
In this implementation, we will first get the machine readable file which contains server statistics from the Apache web server. We will store these values in a database for a number of time stamps in a convenient fashion. These values would then be used for the calculation of the average number of requests served for different time periods by the Apache Web Server. In the final step, we will allow this app to be used through a Servlet. Alternatively, users can also issue commands through a command line or design a UI for the same which allows them to specify the time frame for which they want the average number of requests for the Apache Web server.

Enable and Test Status Support for mod_status

In order to get the server statistics from mod_status, you first have to enable it so that it can be queried to generate the status file. In order to do so, go to the Apache Web Server installation directory and traverse to the httpd.conf file in the conf directory and add the following to the file:

Also, edit the httpd.conf and mark ExtendedStatus of the server as On.

ExtendedStatus On

You should be able to get the server statistics now. Hit the following URL in the web browser to get the status file:

http://your.server.name/server-status

The file is generated in two formats. One is the human readable one, which is what you get when you hit the above mentioned URL in the browser. The second is the machine readable file, which is like a normal text file and can be directly used by an application to use the statistics reported by the file. For our purpose, we will be calling this machine readable file directly from our application. The machine readable file can be accessed by using the following link:

http://your.server.name/server-status?auto

Create a Listener:
You can extend the IApacheSnapshotProcessor interface which has two methos.  The implementation is upto the user. A sample implementation in provided through H2Persiter which extends the IApacheSnapshotProcessor interface.

Create Schema in H2
As a second step, we will create a schema and a table for storing the values retrieved from the machine readable file. The application would repeatedly ping the Apache Web server URL to get the machine readable file at different time intervals. We will be parsing the values obtained from the file and store them in the database for future use.

Download and install the H2 database from http://www.h2database.com/html/main.html.

Connect to H2 using the following credentials:

Create a schema and table in H2 to store the values. The table should contain the following fields:

1.        ID (serves as the primary key)

2.       TOTAL_ACCESS (for storing the total number of accesses from the time server started running)

3.       TOTAL_KBYTES (total number of KBs since the time server was running)

4.       UPTIME

5.       REQ_PS

6.       BYTES_PS

7.       BYTES_PR

8.       BUSY_WORKER

9.       IDLE_WORKER

10.   SNAPSHOT_TIME_DIFF (this field will be used as integer count for the count difference between the number of request at the time first server snapshot was taken and the number of request for which the current snapshot has been taken. So let's say that we take server snapshot every 2 sec and the server started at 10:00 AM. So for the first request at 10:00:00 AM SNAPSHOT_TIME_DIFF value would be 0. At 10:00:02 AM, value would be 1 and so on.)

11.   SNAPSHOT_TIME

Our table is now ready and we can use it to store values of all the fields which we get from the mod_status file.

Getting Values from the Machine Readable File
We now have to get the values from the machine readable mod_status file and parse them before storing those values into the database. Create a class (RequestCounter.java) which will access the URL at regular intervals, will read the file, and parse the values into variables, and these variables will then be passed on to a persister class which will then store them in the database.

We will first construct the URL in the proper format as required by Apache Web Server in a method (pool())

This method will again call the pool() method which will then get the Apache Server status from inside of a method (TimerTask). This method will run till the time we have specified. Suppose, we want to store the values for last 2 hours, then we can set the timer for that time period. After that time period is elapsed, it will call the cancel() method and will stop hitting the Apache Web Server URL.

getApacheStatus(url); calls the method which gets the machine readable file.

Once we have the file, we read it line by line and parse it and store the values in private variables.

Storing Values in the Database
Once we have the parsed values from the file, we now need to store them into the database. For this, create a file (H2Persister.java) which will create a connection to the database and will insert values in the database. Later on, we will use the same file to get average performance values from the database as well.

Create a method (connect()) which will create a JDBC connection object for the database.

Class.forName("org.h2.Driver"); specifies the JDBC driver name. We get the connection from DriverManager using getConnection method, and this assign this to conn variable which is an instance of the Connection object.

Create a method (process()), which will take as arguments the values which we parsed, and will then prepare an sql query and store those values in the database by executing that query.

Statement stmt = conn.createStatement();creates a Statement object. We prepare a query which will insert the values into the database.

String sql = "insert into APACHE_MOD_STATUS.COUNT_HANDLER values (DEFAULT," + TOTAL_ACCESS + "," + TOTAL_KBYTES + "," + UPTIME + "," + REQ_PS + "," + BYTES_PS + "," + BYTES_PR + "," + BUSY_WORKER + "," + IDLE_WORKER     + "," + (System.currentTimeMillis() / 1000 - startupTime) + ", DEFAULT" + ")";

Afterwards, we execute the query to insert the values in the database.

We call this process() method from our previously created RequestCounter file and pass the values which we parsed there to the process() method.

hPersister.process(totalAccesses, totalKBytes, uptime, reqPerSec, ytesPerSec, bytesPerReq, busyWorkers,idleWorkers);

Getting Average Values

We now have to have a mechanism to get the values stored in the database and process them to get the average number of requests during the time frame requested by the user. For this, create a method (getAverage) in H2Persister.java file which will do the same. This method takes the time frame as an argument. First of all, create a SQL query to retrieve the desired values from the database. For our purpose, we need Total_ACCESS and UPTIME fields values from the database. The following query does just that:

String sql = "SELECT ID,TOTAL_ACCESS,UPTIME FROM APACHE_MOD_STATUS.COUNT_HANDLER where SNAPSHOT_TIME_DIFF>"    + (System.currentTimeMillis() / 1000 - startupTime - timeslot - 1);

Statement stmt = conn.createStatement();

ResultSet rs = stmt.executeQuery(sql);

Create an ArrayList which will store the values retrieved by the ResultSet object.

ArrayList<RPSTuple> rpsTupleList = new ArrayList<RPSTuple>();

This ArrayList is of the type RPSTuple which has two private fields totalAccess and uptime and getter and setter methods for the same.

Iterate over the ResultSet object rs and store the values into the ArrayList one by one.

Once we have the desired values in the ArrayList, let us now process them to get the average number of requests for the server over the specified period of time.

From the rpsTupleList, we first get its first node and last node.

From this, we get the average by subtracting total access numbers of last node from first node, and divide that by difference of first node and last node uptime.

lastNode.getTotalAccess()-firstNode.getTotalAccess()) / (lastNode.getUptime()-firstNode.getUptime()

This gives us the performance of an Apache Web Server link in terms of average number of requests over a desired time frame and not just the time frame from which the server started running to the current one.

Create a Servlet
We will now create a Servlet (CounterServlet.java) which will display the average number of requests which we get from the above.

In addition to a Servlet, one can also call the same app from a command line as well.

Summary
This article describes the process of monitoring an Apache Web Server link performance by giving the users and system administrators the facility to monitor the performance over any desired time period. This enables the users more control over the unraveling and understanding of the usage statistics of their servers over different periods of time so that they can better utilize their systems according to peak loads and various other factors.

More Stories By Peeyush Taori

Peeyush Taori is a Senior Systems Engineer with Infosys Technologies Limited and has worked in Java Technology Domain for more than 5 years

More Stories By Kumar Tiwari

Kumar Manava Tiwari has six years of IT experience. He has worked in Cloud Computing, software factories, enterprise application development and application frameworks development. He is currently working as a Technology Lead with Infosys Technologies Limited, India.

@ThingsExpo Stories
SYS-CON Events announced today that DXWorldExpo has been named “Global Sponsor” of SYS-CON's 21st International Cloud Expo, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Digital Transformation is the key issue driving the global enterprise IT business. Digital Transformation is most prominent among Global 2000 enterprises and government institutions.
SYS-CON Events announced today that NetApp has been named “Bronze Sponsor” of SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. NetApp is the data authority for hybrid cloud. NetApp provides a full range of hybrid cloud data services that simplify management of applications and data across cloud and on-premises environments to accelerate digital transformation. Together with their partners, NetApp em...
SYS-CON Events announced today that SIGMA Corporation will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. uLaser flow inspection device from the Japanese top share to Global Standard! Then, make the best use of data to flip to next page. For more information, visit http://www.sigma-k.co.jp/en/.
SYS-CON Events announced today that N3N will exhibit at SYS-CON's @ThingsExpo, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. N3N’s solutions increase the effectiveness of operations and control centers, increase the value of IoT investments, and facilitate real-time operational decision making. N3N enables operations teams with a four dimensional digital “big board” that consolidates real-time live video feeds alongside IoT sensor data a...
Real IoT production deployments running at scale are collecting sensor data from hundreds / thousands / millions of devices. The goal is to take business-critical actions on the real-time data and find insights from stored datasets. In his session at @ThingsExpo, John Walicki, Watson IoT Developer Advocate at IBM Cloud, will provide a fast-paced developer journey that follows the IoT sensor data from generation, to edge gateway, to edge analytics, to encryption, to the IBM Bluemix cloud, to Wa...
There is huge complexity in implementing a successful digital business that requires efficient on-premise and cloud back-end infrastructure, IT and Internet of Things (IoT) data, analytics, Machine Learning, Artificial Intelligence (AI) and Digital Applications. In the data center alone, there are physical and virtual infrastructures, multiple operating systems, multiple applications and new and emerging business and technological paradigms such as cloud computing and XaaS. And then there are pe...
DevOps at Cloud Expo – being held October 31 - November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA – announces that its Call for Papers is open. Born out of proven success in agile development, cloud computing, and process automation, DevOps is a macro trend you cannot afford to miss. From showcase success stories from early adopters and web-scale businesses, DevOps is expanding to organizations of all sizes, including the world's largest enterprises – and delivering real r...
SYS-CON Events announced today that B2Cloud will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. B2Cloud specializes in IoT devices for preventive and predictive maintenance in any kind of equipment retrieving data like Energy consumption, working time, temperature, humidity, pressure, etc.
SYS-CON Events announced today that Massive Networks, that helps your business operate seamlessly with fast, reliable, and secure internet and network solutions, has been named "Exhibitor" of SYS-CON's 21st International Cloud Expo ®, which will take place on Oct 31 - Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. As a premier telecommunications provider, Massive Networks is headquartered out of Louisville, Colorado. With years of experience under their belt, their team of...
SYS-CON Events announced today that Suzuki Inc. will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Suzuki Inc. is a semiconductor-related business, including sales of consuming parts, parts repair, and maintenance for semiconductor manufacturing machines, etc. It is also a health care business providing experimental research for...
SYS-CON Events announced today that Fusic will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Fusic Co. provides mocks as virtual IoT devices. You can customize mocks, and get any amount of data at any time in your test. For more information, visit https://fusic.co.jp/english/.
SYS-CON Events announced today that Ryobi Systems will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Ryobi Systems Co., Ltd., as an information service company, specialized in business support for local governments and medical industry. We are challenging to achive the precision farming with AI. For more information, visit http:...
SYS-CON Events announced today that Keisoku Research Consultant Co. will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Keisoku Research Consultant, Co. offers research and consulting in a wide range of civil engineering-related fields from information construction to preservation of cultural properties. For more information, vi...
SYS-CON Events announced today that Daiya Industry will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Daiya Industry specializes in orthotic support systems and assistive devices with pneumatic artificial muscles in order to contribute to an extended healthy life expectancy. For more information, please visit https://www.daiyak...
SYS-CON Events announced today that Interface Corporation will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Interface Corporation is a company developing, manufacturing and marketing high quality and wide variety of industrial computers and interface modules such as PCIs and PCI express. For more information, visit http://www.i...
SYS-CON Events announced today that Mobile Create USA will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Mobile Create USA Inc. is an MVNO-based business model that uses portable communication devices and cellular-based infrastructure in the development, sales, operation and mobile communications systems incorporating GPS capabi...
In his session at @ThingsExpo, Greg Gorman is the Director, IoT Developer Ecosystem, Watson IoT, will provide a short tutorial on Node-RED, a Node.js-based programming tool for wiring together hardware devices, APIs and online services in new and interesting ways. It provides a browser-based editor that makes it easy to wire together flows using a wide range of nodes in the palette that can be deployed to its runtime in a single-click. There is a large library of contributed nodes that help so...
Elon Musk is among the notable industry figures who worries about the power of AI to destroy rather than help society. Mark Zuckerberg, on the other hand, embraces all that is going on. AI is most powerful when deployed across the vast networks being built for Internets of Things in the manufacturing, transportation and logistics, retail, healthcare, government and other sectors. Is AI transforming IoT for the good or the bad? Do we need to worry about its potential destructive power? Or will we...
SYS-CON Events announced today that mruby Forum will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. mruby is the lightweight implementation of the Ruby language. We introduce mruby and the mruby IoT framework that enhances development productivity. For more information, visit http://forum.mruby.org/.
SYS-CON Events announced today that Nihon Micron will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Nihon Micron Co., Ltd. strives for technological innovation to establish high-density, high-precision processing technology for providing printed circuit board and metal mount RFID tags used for communication devices. For more inf...