A simple implementation of the cluster CPU usage monitoring system (2) Implementation of the monitoring end

Source: Internet
Author: User

 

1. Create a folder named monitor in the webapps folder.

2 monitor to create a WEB-INF and SRC

3 New web. xml and classes in the WEB-INF

4. Web. XML content

 

 

<? XML version = "1.0" encoding = "ISO-8859-1"?> </P> <p> <web-app xmlns = "http://java.sun.com/xml/ns/javaee" <br/> xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xsi: schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" <br/> Version = "2.5"> <br/> <description> <br/> Monitoring CPU, mem, disk and netstate <br/> </description> <br/> <display-Name> mymonitor </display-Name> </P> <p> <! -- Define servlets --> <br/> <servlet-Name> recvinfo </servlet-Name> <br/> <servlet-class> recvinfo </ servlet-class> <br/> </servlet> </P> <p> <servlet-mapping> <br/> <servlet-Name> recvinfo </servlet-Name> <br/> <URL-pattern>/recvinfo </url-pattern> <br/> </servlet-mapping> </P> <p> </Web-app> <br/> 

5. The servlet accepted by write processing is shown below. As follows:

Import Java. io. ioexception; <br/> Import Java. io. printwriter; <br/> Import Java. io. filereader; <br/> Import Java. io. bufferedreader; <br/> Import Java. io. filewriter; <br/> Import javax. servlet. servletexception; <br/> Import javax. servlet. HTTP. httpservlet; <br/> Import javax. servlet. HTTP. httpservletrequest; <br/> Import javax. servlet. HTTP. httpservletresponse; <br/> public class recvinfo extends httpservlet {< Br/> Public void dopost (httpservletrequest request, httpservletresponse response) <br/> throws servletexception, ioexception {<br/> // returns the value of a request parameter as a string, or null if the <br/> // parameter does not exist. <br/> string CPU = request. getparameter ("CPU"); <br/> If (null! = CPU) {<br/> filewriter fw = new filewriter (<br/> "/opt/Apache-Tomcat-6.0.26/webapps/monitor/WEB-INF/classes/cpu.txt ", true); <br/> printwriter PW = new printwriter (FW); <br/> PW. println (CPU); <br/> PW. close (); <br/> FW. close (); <br/>}else {<br/> response. setcontenttype ("text/html"); <br/> printwriter out = response. getwriter (); <br/> out. println ("<HTML>"); <br/> out. println ("<body>"); <br/> out. println ("<Head>"); <br/> out. println ("</pead>"); <br/> out. println ("<body> "); </P> <p> filereader Fr = new filereader (<br/> "/opt/Apache-Tomcat-6.0.26/webapps/monitor/WEB-INF/classes/cpu.txt "); <br/> bufferedreader BR = new bufferedreader (FR); <br/> string Arow = NULL; <br/> while (Arow = BR. readline ())! = NULL) <br/> out. println ("<br>" + Arow + "<br>"); <br/> out. println ("</body>"); <br/> out. println ("</ptml>"); <br/> Fr. close (); <br/> Br. close (); <br/> out. close (); <br/>}</P> <p >}< br/> Public void doget (httpservletrequest request, httpservletresponse response) <br/> throws servletexception, ioexception {<br/> dopost (request, response); <br/>}< br/> 

 

 

If it is a request sent by curl, the value of the CPU usage is obtained and written to the cpu.txt file. If the user accesses this servlet through a browser, http: // localhost: 8080/monitor/recvinfo

Read the cpu.txt file and write the content to your browser through HTML.

 

Compile the Java file to the WEB-INF's classes:

Javac-classpath/opt/Apache-Tomcat-6.0.26/lib/servlet-api.jar-D/opt/Apache-Tomcat-6.0.26/webapps/monitor/WEB-INF/classes recvinfo. Java

 

6. start Tomcat

Sudo-E./startup. Sh

Disable sudo./shutdown. Sh

 

7. Start the CPU. Sh on the monitored end. He starts to collect the CPU usage of the Local Machine and sends an HTTP request to the monitoring end recvinfo servlet.

 

After 8 seconds, CTRL + C stops CPU. SH and check the Monitoring Result:

(1)cpu.txt

(2) Access servlet through a browser, http: // localhost: 8080/monitor/recvinfo

 

9 you can also use http: // localhost: 8080/monitor/recvinfo in the browser? CPU = 123 submit CPU data. Do not correct this bug.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.