on 10-Nov-2010 10:21
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.
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 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.
1: package iControl.Objects.System;
2:
3: public class Service {
4: private iControl.Interfaces _interfaces = null;
5: private iControl.SystemServicesServiceType _type =
6: iControl.SystemServicesServiceType.SERVICE_UNKNOWN;
7:
8: //-----------------------------------------------------------------------
9: // Member Accessors
10: //-----------------------------------------------------------------------
11: public iControl.Interfaces getInterfaces() { return _interfaces; }
12: public void setInterfaces(iControl.Interfaces interfaces) { _interfaces = interfaces; }
13:
14: public iControl.SystemServicesServiceType getType() { return _type; }
15: public void setType(iControl.SystemServicesServiceType type) { _type = type; }
16:
17: //-----------------------------------------------------------------------
18: // Constructors
19: //-----------------------------------------------------------------------
20: public Service(iControl.Interfaces interfaces, iControl.SystemServicesServiceType type)
21: {
22: _interfaces = interfaces;
23: _type = type;
24: }
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:
getServiceStatus()
The getServiceStatus returns the current status of the service. Possible values for the status are:
1: //-----------------------------------------------------------------------
2: // Public Methods
3: //-----------------------------------------------------------------------
4: public void setAction(iControl.SystemServicesServiceAction action) throws Exception
5: {
6: validateMembers();
7:
8: iControl.SystemServicesServiceType [] services = { _type };
9:
10: _interfaces.getSystemServices().set_service(services, action);
11: }
12:
13: public iControl.SystemServicesServiceStatusType getServiceStatus() throws Exception
14: {
15: validateMembers();
16:
17: iControl.SystemServicesServiceType [] services = { _type };
18: iControl.SystemServicesServiceStatus [] statuses =
19: _interfaces.getSystemServices().get_service_status(services);
20:
21: return statuses[0].getStatus();
22: }
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.
1: //-----------------------------------------------------------------------
2: // Public Static Methods
3: //-----------------------------------------------------------------------
4: public static iControl.Objects.System.Service []
5: getServices(iControl.Interfaces interfaces) throws Exception
6: {
7: iControl.SystemServicesServiceType [] serviceTypes =
8: interfaces.getSystemServices().get_list();
9:
10: Service [] services = new Service[serviceTypes.length];
11: for(int i=0; i<serviceTypes.length; i++)
12: {
13: services[i] = new Service(interfaces, serviceTypes[i]);
14: }
15:
16: return services;
17: }
18:
19: public static void rebootSystem(iControl.Interfaces interfaces, long seconds) throws Exception
20: {
21: interfaces.getSystemServices().reboot_system(seconds);
22: }
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.
1: public void testServices(String [] args)
2: {
3: if ( args.length >= 2)
4: {
5: try
6: {
7: iControl.Interfaces interfaces = new iControl.Interfaces();
8: interfaces.initialize(args[0], args[1], args[2]);
9:
10: // Get status for all the services...
11: iControl.Objects.System.Service [] services =
12: iControl.Objects.System.Service.getServices(interfaces);
13: System.out.println("Service Statuses");
14: for(int i=0; i<services.length; i++)
15: {
16: System.out.println(services[i].getType() + " : " +
17: services[i].getServiceStatus().toString());
18: }
19:
20: // Create SNMP service object
21: iControl.Objects.System.Service sshd =
22: new iControl.Objects.System.Service(
23: interfaces, iControl.SystemServicesServiceType.SERVICE_SSHD);
24:
25: // Get the current status
26: iControl.SystemServicesServiceStatusType statusType =
27: sshd.getServiceStatus();
28: System.out.println("SNMP Service state: " + statusType.toString());
29:
30: // stop it
31: sshd.setAction(iControl.SystemServicesServiceAction.SERVICE_ACTION_STOP);
32:
33: // Get the current status
34: statusType =
35: sshd.getServiceStatus();
36: System.out.println("SNMP Service state: " + statusType.toString());
37:
38: // start it
39: sshd.setAction(iControl.SystemServicesServiceAction.SERVICE_ACTION_START);
40:
41: // Get the current status
42: statusType =
43: sshd.getServiceStatus();
44: System.out.println("SNMP Service state: " + statusType.toString());
45: }
46: catch(Exception ex)
47: {
48: ex.printStackTrace(System.out);
49: }
50: }
51: }
The source code can be found in the iControl CodeShare under JavaObjectSystemService.
Related Articles on DevCentral