Java iControl Objects - System Service

This is the fourth article in the “Java iControl Objects” series in which I define a set of objects that implement the iControl methods in various iControl interfaces.  The previous articles covered the Pool and PoolMember interface.

For this article, I’m going to build an object to wrap around the System Services interface.  This interface can be used to control the running services on the BIG-IP.  At the time of this article, there are 47 different services that can be controlled on BIG-IP LTM v10.2.

Prerequisites

The code in this article, relies on the iControl library for Java that can be downloaded in it’s DevCentral labs project.  If you haven’t watched it already, check out my previous article on Configuring Eclipse for iControl development with Java.

The Class

The defining attribute of a service is the service type defined in the iControl.SystemServicesServiceType enumeration.   The constructor for this class takes in an SystemServicesServiceType enumeration value as well as the core iControl.Interfaces API object.  In the class I’ve also added member accessors to get and set these values after the object has been created.

<span id="lnum1" style="color: rgb(96, 96, 96);">   1:</span> package iControl.Objects.System;
<span id="lnum2" style="color: rgb(96, 96, 96);">   2:</span> 
<span id="lnum3" style="color: rgb(96, 96, 96);">   3:</span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">class</span> Service {
<span id="lnum4" style="color: rgb(96, 96, 96);">   4:</span>   <span style="color: rgb(0, 0, 255);">private</span> iControl.Interfaces _interfaces = <span style="color: rgb(0, 0, 255);">null</span>;
<span id="lnum5" style="color: rgb(96, 96, 96);">   5:</span>   <span style="color: rgb(0, 0, 255);">private</span> iControl.SystemServicesServiceType _type =
<span id="lnum6" style="color: rgb(96, 96, 96);">   6:</span>     iControl.SystemServicesServiceType.SERVICE_UNKNOWN;
<span id="lnum7" style="color: rgb(96, 96, 96);">   7:</span> 
<span id="lnum8" style="color: rgb(96, 96, 96);">   8:</span>   <span style="color: rgb(0, 128, 0);">//-----------------------------------------------------------------------</span>
<span id="lnum9" style="color: rgb(96, 96, 96);">   9:</span>   <span style="color: rgb(0, 128, 0);">// Member Accessors</span>
<span id="lnum10" style="color: rgb(96, 96, 96);">  10:</span>   <span style="color: rgb(0, 128, 0);">//-----------------------------------------------------------------------</span>
<span id="lnum11" style="color: rgb(96, 96, 96);">  11:</span>   <span style="color: rgb(0, 0, 255);">public</span> iControl.Interfaces getInterfaces() { <span style="color: rgb(0, 0, 255);">return</span> _interfaces; }
<span id="lnum12" style="color: rgb(96, 96, 96);">  12:</span>   <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">void</span> setInterfaces(iControl.Interfaces interfaces) { _interfaces = interfaces; }
<span id="lnum13" style="color: rgb(96, 96, 96);">  13:</span> 
<span id="lnum14" style="color: rgb(96, 96, 96);">  14:</span>   <span style="color: rgb(0, 0, 255);">public</span> iControl.SystemServicesServiceType getType() { <span style="color: rgb(0, 0, 255);">return</span> _type;  }
<span id="lnum15" style="color: rgb(96, 96, 96);">  15:</span>   <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">void</span> setType(iControl.SystemServicesServiceType type) { _type = type; }
<span id="lnum16" style="color: rgb(96, 96, 96);">  16:</span>
<span id="lnum17" style="color: rgb(96, 96, 96);">  17:</span>   <span style="color: rgb(0, 128, 0);">//-----------------------------------------------------------------------</span>
<span id="lnum18" style="color: rgb(96, 96, 96);">  18:</span>   <span style="color: rgb(0, 128, 0);">// Constructors</span>
<span id="lnum19" style="color: rgb(96, 96, 96);">  19:</span>   <span style="color: rgb(0, 128, 0);">//-----------------------------------------------------------------------</span>
<span id="lnum20" style="color: rgb(96, 96, 96);">  20:</span>   <span style="color: rgb(0, 0, 255);">public</span> Service(iControl.Interfaces interfaces, iControl.SystemServicesServiceType type)
<span id="lnum21" style="color: rgb(96, 96, 96);">  21:</span>   {
<span id="lnum22" style="color: rgb(96, 96, 96);">  22:</span>     _interfaces = interfaces;
<span id="lnum23" style="color: rgb(96, 96, 96);">  23:</span>     _type = type;
<span id="lnum24" style="color: rgb(96, 96, 96);">  24:</span>   }

Public Methods

There are only two public methods in this class.

setAction()

The setAction method allows you to pass in an action to take on the specified service type.  The values for this action are:

  • SERVICE_ACTION_START – Start a service.
  • SERVICE_ACTION_STOP – Stop a service.
  • SERVICE_ACTION_REINIT – Reinitialize a service.
  • SERVICE_ACTION_RESTART - Restart a service by stopping and starting the service
  • SERVICE_ACTION_ADD_TO_BOOT_LIST – Add a service to the boot/reboot list. If on this list, the service will be started on bootup and stopped on reboot.
  • SERVICE_ACTION_REMOVE_FROM_BOOT_LIST - Remove a service from the boot/reboot list. If on this list, the service will be started on bootup and stopped on reboot.
  • SERVICE_ACTION_ADD_TO_DEFAULT_LIST - Add a service to the default action list.
  • SERVICE_ACTION_REMOVE_FROM_DEFAULT_LIST - Remove a service from the default action list.

getServiceStatus()

The getServiceStatus returns the current status of the service.  Possible values for the status are:

  • SERVICE_STATUS_NOT_FOUND – The requested service is not configured on the system.
  • SERVICE_STATUS_UP – The service is up and running.
  • SERVICE_STATUS_DOWN – The service is not currently running.
<span id="lnum1" style="color: rgb(96, 96, 96);">   1:</span> <span style="color: rgb(0, 128, 0);">//-----------------------------------------------------------------------</span>
<span id="lnum2" style="color: rgb(96, 96, 96);">   2:</span> <span style="color: rgb(0, 128, 0);">// Public Methods</span>
<span id="lnum3" style="color: rgb(96, 96, 96);">   3:</span> <span style="color: rgb(0, 128, 0);">//-----------------------------------------------------------------------</span>
<span id="lnum4" style="color: rgb(96, 96, 96);">   4:</span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">void</span> setAction(iControl.SystemServicesServiceAction action) throws Exception
<span id="lnum5" style="color: rgb(96, 96, 96);">   5:</span> {
<span id="lnum6" style="color: rgb(96, 96, 96);">   6:</span>   validateMembers();
<span id="lnum7" style="color: rgb(96, 96, 96);">   7:</span> 
<span id="lnum8" style="color: rgb(96, 96, 96);">   8:</span>   iControl.SystemServicesServiceType [] services = { _type };
<span id="lnum9" style="color: rgb(96, 96, 96);">   9:</span>
<span id="lnum10" style="color: rgb(96, 96, 96);">  10:</span>   _interfaces.getSystemServices().set_service(services, action);
<span id="lnum11" style="color: rgb(96, 96, 96);">  11:</span> }
<span id="lnum12" style="color: rgb(96, 96, 96);">  12:</span> 
<span id="lnum13" style="color: rgb(96, 96, 96);">  13:</span> <span style="color: rgb(0, 0, 255);">public</span> iControl.SystemServicesServiceStatusType getServiceStatus() throws Exception
<span id="lnum14" style="color: rgb(96, 96, 96);">  14:</span> {
<span id="lnum15" style="color: rgb(96, 96, 96);">  15:</span>   validateMembers();
<span id="lnum16" style="color: rgb(96, 96, 96);">  16:</span>
<span id="lnum17" style="color: rgb(96, 96, 96);">  17:</span>   iControl.SystemServicesServiceType [] services = { _type };
<span id="lnum18" style="color: rgb(96, 96, 96);">  18:</span>   iControl.SystemServicesServiceStatus [] statuses =
<span id="lnum19" style="color: rgb(96, 96, 96);">  19:</span>     _interfaces.getSystemServices().get_service_status(services);
<span id="lnum20" style="color: rgb(96, 96, 96);">  20:</span>
<span id="lnum21" style="color: rgb(96, 96, 96);">  21:</span>   <span style="color: rgb(0, 0, 255);">return</span> statuses[0].getStatus();
<span id="lnum22" style="color: rgb(96, 96, 96);">  22:</span> }

Static Methods

There are a few methods in the System.Services interface that are not tied to a specific service type.  I’ve added a few of these as static methods.

getServices()

The getServices() method will return a list of Service objects for each of the defined services on the system.

rebootSystem()

There is also functionality in here to issue a reboot command to the system.  This can be done with the rebootSystem() method by passing in the desired time in seconds to wait for the reboot to happen.

<span id="lnum1" style="color: rgb(96, 96, 96);">   1:</span> <span style="color: rgb(0, 128, 0);">//-----------------------------------------------------------------------</span>
<span id="lnum2" style="color: rgb(96, 96, 96);">   2:</span> <span style="color: rgb(0, 128, 0);">// Public Static Methods</span>
<span id="lnum3" style="color: rgb(96, 96, 96);">   3:</span> <span style="color: rgb(0, 128, 0);">//-----------------------------------------------------------------------</span>
<span id="lnum4" style="color: rgb(96, 96, 96);">   4:</span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">static</span> iControl.Objects.System.Service []
<span id="lnum5" style="color: rgb(96, 96, 96);">   5:</span>   getServices(iControl.Interfaces interfaces) throws Exception
<span id="lnum6" style="color: rgb(96, 96, 96);">   6:</span> {
<span id="lnum7" style="color: rgb(96, 96, 96);">   7:</span>   iControl.SystemServicesServiceType [] serviceTypes =
<span id="lnum8" style="color: rgb(96, 96, 96);">   8:</span>     interfaces.getSystemServices().get_list();
<span id="lnum9" style="color: rgb(96, 96, 96);">   9:</span>
<span id="lnum10" style="color: rgb(96, 96, 96);">  10:</span>   Service [] services = <span style="color: rgb(0, 0, 255);">new</span> Service[serviceTypes.length];
<span id="lnum11" style="color: rgb(96, 96, 96);">  11:</span>   <span style="color: rgb(0, 0, 255);">for</span>(<span style="color: rgb(0, 0, 255);">int</span> i=0; i<serviceTypes.length; i++)
<span id="lnum12" style="color: rgb(96, 96, 96);">  12:</span>   {
<span id="lnum13" style="color: rgb(96, 96, 96);">  13:</span>     services[i] = <span style="color: rgb(0, 0, 255);">new</span> Service(interfaces, serviceTypes[i]);
<span id="lnum14" style="color: rgb(96, 96, 96);">  14:</span>   }
<span id="lnum15" style="color: rgb(96, 96, 96);">  15:</span> 
<span id="lnum16" style="color: rgb(96, 96, 96);">  16:</span>   <span style="color: rgb(0, 0, 255);">return</span> services;
<span id="lnum17" style="color: rgb(96, 96, 96);">  17:</span> }
<span id="lnum18" style="color: rgb(96, 96, 96);">  18:</span> 
<span id="lnum19" style="color: rgb(96, 96, 96);">  19:</span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">static</span> <span style="color: rgb(0, 0, 255);">void</span> rebootSystem(iControl.Interfaces interfaces, <span style="color: rgb(0, 0, 255);">long</span> seconds) throws Exception
<span id="lnum20" style="color: rgb(96, 96, 96);">  20:</span> {
<span id="lnum21" style="color: rgb(96, 96, 96);">  21:</span>   interfaces.getSystemServices().reboot_system(seconds);
<span id="lnum22" style="color: rgb(96, 96, 96);">  22:</span> }

Example Code

The following sample code will query all of the system services and then request their current status and print it out to the console.  It will then create a SSHD service object and stop and start it.

<span id="lnum1" style="color: rgb(96, 96, 96);">   1:</span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">void</span> testServices(String [] args)
<span id="lnum2" style="color: rgb(96, 96, 96);">   2:</span> {
<span id="lnum3" style="color: rgb(96, 96, 96);">   3:</span>   <span style="color: rgb(0, 0, 255);">if</span> ( args.length >= 2)
<span id="lnum4" style="color: rgb(96, 96, 96);">   4:</span>   {
<span id="lnum5" style="color: rgb(96, 96, 96);">   5:</span>     <span style="color: rgb(0, 0, 255);">try</span>
<span id="lnum6" style="color: rgb(96, 96, 96);">   6:</span>     {
<span id="lnum7" style="color: rgb(96, 96, 96);">   7:</span>       iControl.Interfaces interfaces = <span style="color: rgb(0, 0, 255);">new</span> iControl.Interfaces();
<span id="lnum8" style="color: rgb(96, 96, 96);">   8:</span>       interfaces.initialize(args[0], args[1], args[2]);
<span id="lnum9" style="color: rgb(96, 96, 96);">   9:</span>
<span id="lnum10" style="color: rgb(96, 96, 96);">  10:</span>       <span style="color: rgb(0, 128, 0);">// Get status for all the services...</span>
<span id="lnum11" style="color: rgb(96, 96, 96);">  11:</span>       iControl.Objects.System.Service [] services =
<span id="lnum12" style="color: rgb(96, 96, 96);">  12:</span>         iControl.Objects.System.Service.getServices(interfaces);
<span id="lnum13" style="color: rgb(96, 96, 96);">  13:</span>       System.<span style="color: rgb(0, 0, 255);">out</span>.println(<span style="color: rgb(0, 96, 128);">"Service Statuses"</span>);
<span id="lnum14" style="color: rgb(96, 96, 96);">  14:</span>       <span style="color: rgb(0, 0, 255);">for</span>(<span style="color: rgb(0, 0, 255);">int</span> i=0; i<services.length; i++)
<span id="lnum15" style="color: rgb(96, 96, 96);">  15:</span>       {
<span id="lnum16" style="color: rgb(96, 96, 96);">  16:</span>         System.<span style="color: rgb(0, 0, 255);">out</span>.println(services[i].getType() + <span style="color: rgb(0, 96, 128);">" : "</span> +
<span id="lnum17" style="color: rgb(96, 96, 96);">  17:</span>             services[i].getServiceStatus().toString());
<span id="lnum18" style="color: rgb(96, 96, 96);">  18:</span>       }
<span id="lnum19" style="color: rgb(96, 96, 96);">  19:</span>
<span id="lnum20" style="color: rgb(96, 96, 96);">  20:</span>       <span style="color: rgb(0, 128, 0);">// Create SNMP service object</span>
<span id="lnum21" style="color: rgb(96, 96, 96);">  21:</span>       iControl.Objects.System.Service sshd =
<span id="lnum22" style="color: rgb(96, 96, 96);">  22:</span>         <span style="color: rgb(0, 0, 255);">new</span> iControl.Objects.System.Service(
<span id="lnum23" style="color: rgb(96, 96, 96);">  23:</span>             interfaces, iControl.SystemServicesServiceType.SERVICE_SSHD);
<span id="lnum24" style="color: rgb(96, 96, 96);">  24:</span>
<span id="lnum25" style="color: rgb(96, 96, 96);">  25:</span>       <span style="color: rgb(0, 128, 0);">// Get the current status</span>
<span id="lnum26" style="color: rgb(96, 96, 96);">  26:</span>       iControl.SystemServicesServiceStatusType statusType =
<span id="lnum27" style="color: rgb(96, 96, 96);">  27:</span>         sshd.getServiceStatus();
<span id="lnum28" style="color: rgb(96, 96, 96);">  28:</span>       System.<span style="color: rgb(0, 0, 255);">out</span>.println(<span style="color: rgb(0, 96, 128);">"SNMP Service state: "</span> + statusType.toString());
<span id="lnum29" style="color: rgb(96, 96, 96);">  29:</span>
<span id="lnum30" style="color: rgb(96, 96, 96);">  30:</span>       <span style="color: rgb(0, 128, 0);">// stop it</span>
<span id="lnum31" style="color: rgb(96, 96, 96);">  31:</span>       sshd.setAction(iControl.SystemServicesServiceAction.SERVICE_ACTION_STOP);
<span id="lnum32" style="color: rgb(96, 96, 96);">  32:</span> 
<span id="lnum33" style="color: rgb(96, 96, 96);">  33:</span>       <span style="color: rgb(0, 128, 0);">// Get the current status</span>
<span id="lnum34" style="color: rgb(96, 96, 96);">  34:</span>       statusType =
<span id="lnum35" style="color: rgb(96, 96, 96);">  35:</span>         sshd.getServiceStatus();
<span id="lnum36" style="color: rgb(96, 96, 96);">  36:</span>       System.<span style="color: rgb(0, 0, 255);">out</span>.println(<span style="color: rgb(0, 96, 128);">"SNMP Service state: "</span> + statusType.toString());
<span id="lnum37" style="color: rgb(96, 96, 96);">  37:</span>
<span id="lnum38" style="color: rgb(96, 96, 96);">  38:</span>       <span style="color: rgb(0, 128, 0);">// start it</span>
<span id="lnum39" style="color: rgb(96, 96, 96);">  39:</span>       sshd.setAction(iControl.SystemServicesServiceAction.SERVICE_ACTION_START);
<span id="lnum40" style="color: rgb(96, 96, 96);">  40:</span> 
<span id="lnum41" style="color: rgb(96, 96, 96);">  41:</span>       <span style="color: rgb(0, 128, 0);">// Get the current status</span>
<span id="lnum42" style="color: rgb(96, 96, 96);">  42:</span>       statusType =
<span id="lnum43" style="color: rgb(96, 96, 96);">  43:</span>         sshd.getServiceStatus();
<span id="lnum44" style="color: rgb(96, 96, 96);">  44:</span>       System.<span style="color: rgb(0, 0, 255);">out</span>.println(<span style="color: rgb(0, 96, 128);">"SNMP Service state: "</span> + statusType.toString());
<span id="lnum45" style="color: rgb(96, 96, 96);">  45:</span>     }
<span id="lnum46" style="color: rgb(96, 96, 96);">  46:</span>     <span style="color: rgb(0, 0, 255);">catch</span>(Exception ex)
<span id="lnum47" style="color: rgb(96, 96, 96);">  47:</span>     {
<span id="lnum48" style="color: rgb(96, 96, 96);">  48:</span>       ex.printStackTrace(System.<span style="color: rgb(0, 0, 255);">out</span>);
<span id="lnum49" style="color: rgb(96, 96, 96);">  49:</span>     }
<span id="lnum50" style="color: rgb(96, 96, 96);">  50:</span>   }
<span id="lnum51" style="color: rgb(96, 96, 96);">  51:</span> }

View The Source

The source code can be found in the iControl CodeShare under JavaObjectSystemService.

Related Articles on DevCentral

Technorati Tags: Java, iControl, Objects, Eclipse, Joe Pruitt