Java + ONRPC
Un dialogo tipico en NYC, Times Square

Si la vida te da limones, entonces haz limonada. No siempre tenemos SNMP para monitorear nuestros servidores, pero por otro lado muchas veces nos dejan usar NFS y Rstatd. Por ejemplo, mire las pruebas de Junit que escribí para jugar con esos servicios:

JAVA:
  1. package com.kodegeek.blog.monitoring.rpc;
  2.  
  3. import java.net.InetAddress;
  4. import java.util.ResourceBundle;
  5.  
  6. import junit.framework.Assert;
  7. import junit.framework.TestCase;
  8.  
  9. import org.acplt.oncrpc.OncRpcClientAuthUnix;
  10. import org.acplt.oncrpc.OncRpcProtocols;
  11.  
  12. public class TestRpcStubs extends TestCase {
  13.  
  14.     /**
  15.      * Default max timeout value, in miliseconds
  16.      */
  17.     public static final int MAX_TIMEOUT = 1000*60;
  18.    
  19.     private static ResourceBundle NFS_BUNDLE;
  20.     private static ResourceBundle RSTAT_BUNDLE;
  21.    
  22.     {
  23.         NFS_BUNDLE = ResourceBundle.getBundle(NfsPing.class.getName());
  24.         RSTAT_BUNDLE = ResourceBundle.getBundle(RstatPing.class.getName());
  25.     }
  26.    
  27.     protected void setUp() throws Exception {
  28.         super.setUp();
  29.         System.out.println("Make sure all the services are running with '/usr/sbin/rpcinfo -p'");
  30.     }
  31.    
  32.     /**
  33.      * Simple test for the NFS client, using the generated RPC stubs directly
  34.      *
  35.      */
  36.     public void testNfs() {
  37.         MountClient mount = null;
  38.         exports exportsList = null;
  39.         fhstatus fhStatus = null;
  40.         OncRpcClientAuthUnix authUnix = null;
  41.         try {
  42.             authUnix = new OncRpcClientAuthUnix(
  43.                     System.getProperty("test.nfs.server"),
  44.                     Integer.parseInt(NFS_BUNDLE.getString("NfsPing.user.id")),
  45.                     Integer.parseInt(NFS_BUNDLE.getString("NfsPing.user.group"))
  46.             );
  47.             Assert.assertNotNull("Auth Unix is null", authUnix);
  48.             // Get the list of exported directories
  49.             mount = new MountClient(
  50.                     InetAddress.getByName(System.getProperty("test.nfs.server")),
  51.                     OncRpcProtocols.ONCRPC_TCP);
  52.             Assert.assertNotNull("Mount is null", mount);
  53.             exportsList = mount.MOUNTPROC_EXPORT_1();
  54.             Assert.assertNotNull("Export list is null", exportsList);
  55.             exportnode node = exportsList.value;
  56.             Assert.assertNotNull("No nodes are being exported", node);
  57.             while (node != null) {
  58.                 Object [] nodeForm = new Object[1];
  59.                 nodeForm[0] = node.ex_dir.value;
  60.                 System.out.printf(
  61.                         NFS_BUNDLE.getString("NfsPing.msg.exportedFs"), nodeForm
  62.                 );
  63.                
  64.                 mount.getClient().setAuth(authUnix);
  65.                 mount.getClient().setTimeout(MAX_TIMEOUT);
  66.                
  67.                 fhStatus = mount.MOUNTPROC_MNT_1(new dirpath(System.getProperty("test.nfs.dir")));
  68.                 if (fhStatus == null) {
  69.                     Assert.fail("unable to get fhStatus for: " + System.getProperty("test.nfs.dir"));
  70.                 }
  71.                 // Get the next node
  72.                 node = node.ex_next.value;
  73.             }
  74.         } catch (Throwable throwbl) {
  75.             Assert.fail("Got an exception: " + throwbl.toString());
  76.             throwbl.printStackTrace();
  77.         } finally {
  78.             // Empty for now
  79.         }
  80.     }
  81.  
  82.     /**
  83.      * Simple test for Rstat, using the generated RPC stubs directly
  84.      * Timestamps are separated into seconds (standard UNIX time) and
  85.      * microseconds. The availability of a current timestamp allows proper
  86.      * calculation of the interval between measurements without worrying
  87.      * about network latency.
  88.      *
  89.      * Most values are counters. To get the real numbers you have to
  90.      * fetch() samples regularly and divide the counter increments
  91.      * by the time interval between the samples.
  92.      *
  93.      * The cpu_time array holds the ticks spent in the various CPU states
  94.      * (averaged over all CPUs). If you know the regular tick rate of the target
  95.      * system you may calculate the number of CPUs from the sum of C<cpu_time>
  96.      * increments and the time interval between the samples. Most often you
  97.      * will be interested in the percentage of CPU states only.
  98.      */
  99.     public void testRstat() {
  100.         OncRpcClientAuthUnix authUnix = null;
  101.         RstatClient rstat = null;
  102.         statstime stats = null;
  103.         statstime stats2 = null;
  104.         try {
  105.             authUnix = new OncRpcClientAuthUnix(
  106.                     System.getProperty("test.rstat.server"),
  107.                     Integer.parseInt(RSTAT_BUNDLE.getString("RstatPing.user.id")),
  108.                     Integer.parseInt(RSTAT_BUNDLE.getString("RstatPing.user.group"))
  109.             );
  110.             Assert.assertNotNull("Auth Unix is null", authUnix);
  111.            
  112.             rstat = new RstatClient(
  113.                     InetAddress.getByName(System.getProperty("test.rstat.server")),
  114.                     OncRpcProtocols.ONCRPC_UDP
  115.                     );
  116.             Assert.assertNotNull("Rstat is null", rstat);
  117.             rstat.getClient().setAuth(authUnix);
  118.             rstat.getClient().setTimeout(MAX_TIMEOUT);
  119.             stats = rstat.RSTATPROC_STATS_3();
  120.             Assert.assertNotNull("statsswtch stats is null", stats);
  121.            
  122.             // Now wait a little bit before taking the next snapshot
  123.             long wait = Long.parseLong(System.getProperty("test.rstat.wait"));
  124.             Thread.sleep(wait);
  125.            
  126.             // Get another snapshot
  127.             stats2 = rstat.RSTATPROC_STATS_3();
  128.            
  129.             long timeElaps =  stats2.getCurtime().tv_sec - stats.getCurtime().tv_sec;
  130.            
  131.             Object [] statColl = new Object[23];
  132.             statColl[0] = new Long((stats2.if_collisions - stats.if_collisions) / timeElaps);
  133.             statColl[1] = new Long((stats2.if_ierrors - stats.if_ierrors) / timeElaps);
  134.             statColl[2] = new Long((stats2.if_ipackets - stats.if_ipackets) / timeElaps);
  135.             statColl[3] = new Long((stats2.if_oerrors - stats.if_oerrors) / timeElaps);
  136.             statColl[4] = new Long((stats2.if_opackets - stats.if_opackets) / timeElaps);
  137.             statColl[5] = new Long((stats2.v_intr - stats.v_intr) / timeElaps);
  138.             statColl[6] = new Long((stats2.v_pgpgin - stats.v_pgpgin) / timeElaps);
  139.             statColl[7] = new Long((stats2.v_pgpgout - stats.v_pgpgout) / timeElaps);
  140.             statColl[8] = new Long((stats2.v_pswpin - stats.v_pswpin) / timeElaps);
  141.             statColl[9] = new Long((stats2.v_pswpout - stats.v_pswpout) / timeElaps);
  142.             statColl[10] = new Long((stats2.v_swtch - stats.v_swtch) / timeElaps);
  143.             statColl[11] = new Double(stats2.getAvenrun(0) / 256.0);
  144.             statColl[12] = new Double(stats2.getAvenrun(1) / 256.0);
  145.             statColl[13] = new Double(stats2.getAvenrun(2) / 256.0);
  146.             statColl[14] = new Long((stats2.getCp_time(0) - stats.getCp_time(0)) / timeElaps);
  147.             statColl[15] = new Long((stats2.getCp_time(1) - stats.getCp_time(1)) / timeElaps);
  148.             statColl[16] = new Long((stats2.getCp_time(2) - stats.getCp_time(2)) / timeElaps);
  149.             statColl[17] = new Long((stats2.getCp_time(3) - stats.getCp_time(3)) / timeElaps);
  150.             statColl[18] = new Long((stats2.getDk_xfer(0) - stats.getDk_xfer(0)) / timeElaps);
  151.             statColl[19] = new Long((stats2.getDk_xfer(1) - stats.getDk_xfer(1)) / timeElaps);
  152.             statColl[20] = new Long((stats2.getDk_xfer(2) - stats.getDk_xfer(2)) / timeElaps);
  153.             statColl[21] = new Long((stats2.getDk_xfer(3) - stats.getDk_xfer(3)) / timeElaps);
  154.             statColl[22] = new Long(stats2.getBoottime().tv_sec);
  155.            
  156.             StringBuffer info = new StringBuffer();
  157.             info.append("Rstat unit test\n");
  158.             info.append("Time elapsed between measures (seconds): " + timeElaps + "\n");
  159.             info.append("if_collisions=%s\n");
  160.             info.append("if_ierrors=%s\n");
  161.             info.append("if_ipackets=%s\n");
  162.             info.append("if_oerrors=%s\n");
  163.             info.append("if_opackets=%s\n");
  164.             info.append("v_intr=%s\n");
  165.             info.append("v_pgpgin=%s\n");
  166.             info.append("v_pgpgout=%s\n");
  167.             info.append("v_pswpin=%s\n");
  168.             info.append("v_pswpout=%s\n");
  169.             info.append("v_swtch=%s\n");
  170.             info.append("Avenrun (CPU load) 0=%s\n");
  171.             info.append("Avenrun (CPU load) 1=%s\n");
  172.             info.append("Avenrun (CPU load) 2=%s\n");
  173.             info.append("CP_time 0 Usr=%s\n");
  174.             info.append("CP_time 1 System=%s\n");
  175.             info.append("CP_time 2 Wio=%s\n");
  176.             info.append("CP_time 3 Idle=%s\n");
  177.             info.append("Dk_xfer 0=%s\n");
  178.             info.append("Dk_xfer 1=%s\n");
  179.             info.append("Dk_xfer 2=%s\n");
  180.             info.append("Dk_xfer 3=%s\n");
  181.             info.append("Raw uptime in seconds=%s\n");
  182.            
  183.             // Spit the results out
  184.             System.out.printf(
  185.                     info.toString(),
  186.                     statColl
  187.                     );
  188.         } catch (Throwable throwbl) {
  189.             Assert.fail("Got an exception: " + throwbl.toString());
  190.             throwbl.printStackTrace();
  191.         } finally {
  192.             // Empty for now
  193.         }
  194.     }
  195. }

Así que si la curiosidad le pica, lo invito a leer el resto de el articulo aquí


2 Respuestas a “Echando código: ¿Monitoreando servicios RPC usando Java, ONC RPC y el protocolo Jabber? (I)”

  1. 1 okickoff

    A veces basta con sólo decir: Gracias por el post.

  2. 2 EDISON

    gRACIAS FUE DE MUCHA AYUDA


RSS feeds

Suscríbete a nuestros RSS Feeds