Application Server: Custom REST Pages

From Resin 4.0 Wiki

(Difference between revisions)
Jump to: navigation, search
(Created page with "{{Cookbook}} {{Integration}} {{WebServer}} = Custom REST Pages = Resin actually provides 2 REST interfaces; a newer REST service which enables administration that mirrors ...")
 
 
Line 5: Line 5:
 
Resin actually provides 2 REST interfaces; a newer REST service which enables administration that mirrors Resin's command-line interface, and older customizable REST pages as part of the resin-admin web application.
 
Resin actually provides 2 REST interfaces; a newer REST service which enables administration that mirrors Resin's command-line interface, and older customizable REST pages as part of the resin-admin web application.
  
The newer REST interface is documented thoroughly in the [http://www.caucho.com/resin-4.0/admin/resin-admin-rest.xtp documentation] and [[Application_Server:_REST_Graphing|usage examples] are on the Wiki also.
+
The newer REST interface is documented thoroughly in the [http://www.caucho.com/resin-4.0/admin/resin-admin-rest.xtp Resin documentation] and [[Application_Server:_REST_Graphing|usage examples]] are on the Wiki also.
  
 
== rest.php ==
 
== rest.php ==
Line 13: Line 13:
 
rest.php requires one parameter "q", which is the name of a rest page to display.  rest pages are located in resin-admin's WEB-INF/php/ directory and named with the extension ".rest".
 
rest.php requires one parameter "q", which is the name of a rest page to display.  rest pages are located in resin-admin's WEB-INF/php/ directory and named with the extension ".rest".
  
resin-admin includes packaged 2 .rest pages which you can use, or you can add your own to this directory.
+
resin-admin as packaged includes 2 .rest pages for your use, or you can add your own additional .rest pagesto this directory.
  
 
== mod_status.rest ==
 
== mod_status.rest ==
Line 27: Line 27:
 
This .rest page produces an interactive graph drawn using javascript.  It's intended to be embeddable in your own pages or can just be used as an example of how to write .rest pages.
 
This .rest page produces an interactive graph drawn using javascript.  It's intended to be embeddable in your own pages or can just be used as an example of how to write .rest pages.
  
With no parameters, graph.rest will not graph data.  The parameters are as follows:
+
With no parameters, graph.rest will produce no graph.  The parameters are as follows:
  
* checks: (required) a Resin meter name to graph.  Can be passed more than once to graph multiple statistics
+
* checks: a comma separated list of Resin meter name to graph (required)
* servers: Resin server cluster index, typically 00 though xx (defaults to the local server)
+
* servers: a comma separated list of Resin server cluster indexes, typically 00 though xx (defaults to the local server)
 
* width: graph width (defaults to 640)
 
* width: graph width (defaults to 640)
 
* height: graph height (defaults t 480)
 
* height: graph height (defaults t 480)
Line 40: Line 40:
 
[http://127.0.0.1:8080/resin-admin/rest.php?q=graph&checks=JVM|Thread|JVM%20Thread%20Count&period=3600 http://127.0.0.1:8080/resin-admin/rest.php?q=graph&checks=JVM|Thread|JVM%20Thread%20Count&period=3600]
 
[http://127.0.0.1:8080/resin-admin/rest.php?q=graph&checks=JVM|Thread|JVM%20Thread%20Count&period=3600 http://127.0.0.1:8080/resin-admin/rest.php?q=graph&checks=JVM|Thread|JVM%20Thread%20Count&period=3600]
  
The "checks" parameter accepts the name of a Resin "meters".  Resin tracks many metrics while running, and stores these in an internal database.  They are named hierarchy with keys from least to most specific, separated with a "|" (pipe) character.  So in the example above the metric key was "JVM|Thread|JVM Thread Count".  There is also a "Resin|Thread|Thread Count".  [http://127.0.0.1:8080/resin-admin/?q=meters&s=0 Refer to the Meters page in resin-admin for a more comprehensive list.]
+
Each Resin server in a cluster actually has access to the stats from the other servers, making cluster graphing possible, as shown in this example:
  
== Custom .rest Pages ==
+
[http://127.0.0.1:8080/resin-admin/rest.php?q=graph&checks=JVM|Thread|JVM%20Thread%20Count,Resin|Thread|Thread%20Count&servers=00,01 http://127.0.0.1:8080/resin-admin/rest.php?q=graph&checks=JVM|Thread|JVM%20Thread%20Count,Resin|Thread|Thread%20Count&servers=00,01]
 +
 
 +
The "checks" parameter accepts the names of a Resin Meters.  Resin tracks many metrics while running, and stores these in an internal database.  They are named hierarchy with keys from least to most specific, separated with a "|" (pipe) character.  So in the example above the metric key was "JVM|Thread|JVM Thread Count".  There is also a "Resin|Thread|Thread Count".  [http://127.0.0.1:8080/resin-admin/?q=meters&s=0 Refer to the Meters page in resin-admin for a more comprehensive list.]
 +
 
 +
== Writing a custom REST page ==
 +
 
 +
resin-admin only accesses data via JMX.  To gather JMX data, you'll create a custom REST page, which is just a PHP page accessing JMX. The page has the extension .rest and can be named whatever you like, e.g. mypage.rest. Put the my-app.rest page in WEB-INF/php/mypage.rest.
 +
 
 +
== Location of mypage.rest ==
 +
 
 +
The mypage.rest can be put directly in WEB-INF/php or in a custom location given by "resin_admin_ext_path. We recommend using resin_admin_ext_path so you can keep your extensions distinct from Resin's standard pages. The resin_admin_ext_path is configured in the resin.xml like:
 +
 
 +
<web-app id="/resin-admin" root-directory="${resin.root}/doc/admin">
 +
  <prologue>
 +
    <resin:set var="resin_admin_external" value="false"/>
 +
    <resin:set var="resin_admin_insecure" value="true"/>
 +
    <resin:set var="resin_admin_ext_path"
 +
              value="/var/resin/admin/php"/>
 +
</prologue>
 +
</web-app>
 +
 
 +
== mypage.rest sample code ==
 +
 
 +
The code for a REST page will look like:
 +
 
 +
<?php
 +
 +
header("Content-Type: text/plain; charset=utf-8");
 +
 +
$mbean_server = new MBeanServer();
 +
 +
$jvm_thread = $mbean_server->lookup("java.lang:type=Threading");
 +
 +
printf("JvmThreadCount: %d\n", $jvm_thread->ThreadCount);
 +
 +
?>
 +
 
 +
The $mbean_server is a Resin's PHP interface to Java JMX MBeanServer. The lookup($name) method looks up a MBean by its ObjectName. The field reference is a JXM attribute.

Latest revision as of 00:00, 21 March 2013

Cookbook-48.pngShare-48.pngWeb-48.png

Contents

Custom REST Pages

Resin actually provides 2 REST interfaces; a newer REST service which enables administration that mirrors Resin's command-line interface, and older customizable REST pages as part of the resin-admin web application.

The newer REST interface is documented thoroughly in the Resin documentation and usage examples are on the Wiki also.

rest.php

rest.php, located at doc/admin/rest.php is a component of resin-admin that allows you to easy extend and customize resin-admin. Resin admin actually uses it to enable part of the graphing capability.

rest.php requires one parameter "q", which is the name of a rest page to display. rest pages are located in resin-admin's WEB-INF/php/ directory and named with the extension ".rest".

resin-admin as packaged includes 2 .rest pages for your use, or you can add your own additional .rest pagesto this directory.

mod_status.rest

http://127.0.0.1:8080/resin-admin/rest.php?q=mod_status

This .rest page produces output similar to Apache's mod_status.

graph.rest

http://127.0.0.1:8080/resin-admin/rest.php?q=graph

This .rest page produces an interactive graph drawn using javascript. It's intended to be embeddable in your own pages or can just be used as an example of how to write .rest pages.

With no parameters, graph.rest will produce no graph. The parameters are as follows:

  • checks: a comma separated list of Resin meter name to graph (required)
  • servers: a comma separated list of Resin server cluster indexes, typically 00 though xx (defaults to the local server)
  • width: graph width (defaults to 640)
  • height: graph height (defaults t 480)
  • canvas: name of html 5 canvas DIV on to draw on (defaults to generated named)
  • period: graph domain, in seconds since the current time (defaults to 60)

For example, this URL will produce a graph of the number of JVM threads over the last hour for the current server:

http://127.0.0.1:8080/resin-admin/rest.php?q=graph&checks=JVM|Thread|JVM%20Thread%20Count&period=3600

Each Resin server in a cluster actually has access to the stats from the other servers, making cluster graphing possible, as shown in this example:

http://127.0.0.1:8080/resin-admin/rest.php?q=graph&checks=JVM|Thread|JVM%20Thread%20Count,Resin|Thread|Thread%20Count&servers=00,01

The "checks" parameter accepts the names of a Resin Meters. Resin tracks many metrics while running, and stores these in an internal database. They are named hierarchy with keys from least to most specific, separated with a "|" (pipe) character. So in the example above the metric key was "JVM|Thread|JVM Thread Count". There is also a "Resin|Thread|Thread Count". Refer to the Meters page in resin-admin for a more comprehensive list.

Writing a custom REST page

resin-admin only accesses data via JMX. To gather JMX data, you'll create a custom REST page, which is just a PHP page accessing JMX. The page has the extension .rest and can be named whatever you like, e.g. mypage.rest. Put the my-app.rest page in WEB-INF/php/mypage.rest.

Location of mypage.rest

The mypage.rest can be put directly in WEB-INF/php or in a custom location given by "resin_admin_ext_path. We recommend using resin_admin_ext_path so you can keep your extensions distinct from Resin's standard pages. The resin_admin_ext_path is configured in the resin.xml like:

<web-app id="/resin-admin" root-directory="${resin.root}/doc/admin">
 <prologue>
   <resin:set var="resin_admin_external" value="false"/>
   <resin:set var="resin_admin_insecure" value="true"/>
   <resin:set var="resin_admin_ext_path"
              value="/var/resin/admin/php"/>
</prologue>
</web-app>

mypage.rest sample code

The code for a REST page will look like:

<?php

header("Content-Type: text/plain; charset=utf-8");

$mbean_server = new MBeanServer();

$jvm_thread = $mbean_server->lookup("java.lang:type=Threading");

printf("JvmThreadCount: %d\n", $jvm_thread->ThreadCount);

?>

The $mbean_server is a Resin's PHP interface to Java JMX MBeanServer. The lookup($name) method looks up a MBean by its ObjectName. The field reference is a JXM attribute.

Personal tools
TOOLBOX
LANGUAGES