Search code examples
ibm-cloud-infrastructure

Monitoring in SL


I am implementing Monitoring functions, such as Add monitor, Add users and Edit monitoring.. (refer to attached file) To get users to notify, I've called getEligibleAlarmSubscibers(), but no data has been retrieved. Can you provide me any sample code in java to implement monitorings(Add Monitor, Add users, List Existing monitorings)?

List<Agent> agentList = guest.getMonitoringAgents();


        for(Agent agent:agentList){

            System.out.println(" ******************************* " );
            System.out.println(" agent Name  : " + agent.getName());
            System.out.println(" agent Id  : " + agent.getId());

            List<Customer> custList = agent.asService(client).getEligibleAlarmSubscibers();

            for(Customer customer :custList){
                System.out.println(" ******************************* " );
                System.out.println(" Eligible Customer username : " + customer.getUsername());
                System.out.println(" Eligible Customer Email : " + customer.getEmail());
                System.out.println(" Eligible Customer First name : " + customer.getFirstName());       
            }   
        }

Monitoring


Solution

  • 1. To get monitors you should try the following methods:

    Updated

    Here an example for hardware:

    package Monitoring;
    
    import com.softlayer.api.ApiClient;
    import com.softlayer.api.RestApiClient;
    import com.softlayer.api.service.Hardware;
    import com.softlayer.api.service.network.monitor.version1.query.Host;
    
    /**
     * This script retrieve monitors from a server
     *
     * Important Manual Page:
     * http://sldn.softlayer.com/reference/services/SoftLayer_Hardware/getObject
     * http://sldn.softlayer.com/reference/datatypes/SoftLayer_Container_Search_Result
     *
     * @license <http://sldn.softlayer.com/article/License>
     * @authon SoftLayer Technologies, Inc. <sldn@softlayer.com>
     * @version 0.2.2
     */
    public class GetMonitorsFromServer {
        /**
         * This is the constructor, is used to get monitors
         */
        public GetMonitorsFromServer() {
            // Declare your SoftLayer username and apiKey
            String username = "set me";
            String apiKey = "set me";
    
            ApiClient client = new RestApiClient().withCredentials(username, apiKey);
            // Declare the hardware Id
            Long hardwareId = new Long(92862);
            // Define SoftLayer_Hardware_Server service
            Hardware.Service hardwareService = Hardware.service(client, hardwareId);
            // Define an object mask
            hardwareService.withMask().networkMonitors();
            hardwareService.withMask().networkMonitors().id();
            hardwareService.withMask().networkMonitors().ipAddress();
            hardwareService.withMask().networkMonitors().queryType();
            hardwareService.withMask().networkMonitors().responseAction();
            hardwareService.withMask().networkMonitors().lastResult();
            hardwareService.withMask().networkMonitors().status();
            hardwareService.withMask().networkMonitors().waitCycles();
    
    
            try {
                Hardware hardware = hardwareService.getObject();
    
                for(Host host : hardware.getNetworkMonitors())
                {
                    System.out.println("Id: " + host.getId());
                    System.out.println("Primary IP: " + host.getIpAddress());
                    System.out.println("Monitor Type: " + host.getQueryType().getName());
                    System.out.println("Notify: " + host.getResponseAction().getActionDescription());
                    System.out.println("Notify Wait: " + host.getWaitCycles());
                    System.out.println("Status: " + host.getStatus());
                    System.out.println("-------------------------");
                }
    
            } catch (Exception e) {
                System.out.println("Error: " + e);
            }
        }
    
        /**
         * This is the main method which makes use of GetMonitorsFromServer method.
         *
         * @param args
         * @return Nothing
         */
        public static void main(String[] args) {
            new GetMonitorsFromServer();
        }
    
    
    }
    

    2. To add a monitor, you should use the following method:

    Here an example for Hardware:

    package Monitoring;
    
    import com.google.gson.Gson;
    import com.softlayer.api.ApiClient;
    import com.softlayer.api.RestApiClient;
    import com.softlayer.api.service.network.monitor.version1.query.Host;
    
    /**
     * This script adds a monitor
     *
     * Important Manual Page:
     * http://sldn.softlayer.com/reference/services/SoftLayer_Network_Monitor_Version1_Query_Host/createObject
     * http://sldn.softlayer.com/reference/datatypes/SoftLayer_Network_Monitor_Version1_Query_Host
     *
     * @license <http://sldn.softlayer.com/article/License>
     * @authon SoftLayer Technologies, Inc. <sldn@softlayer.com>
     * @version 0.2.2
     */
    public class AddMonitor {
        /**
         * This is the constructor, is used to Add Monitor
         */
        public AddMonitor() {
            // Declare your SoftLayer username and apiKey
            String username = "set me";
            String apiKey = "set me";
    
            // Define Hardware identifier
            Long hardwareId = new Long(92862);
            // Define Ip Address
            String ipAddress = "10.60.9.72";
            // Define Query Type (1 = SERVICE PING, 17 = SLOW PING)
            Long queryTypeId = new Long(1);
            // Define Response action id (1 = Do Nothing, 2 = Notify Users)
            Long responseActionId = new Long(2);
            // Define waitCycles 0 = Immediately, 1 = 5 Minutes, 2 = 10 Minutes, etc
            Long waitCycles = new Long(1);
    
            // Create client
            ApiClient client = new RestApiClient().withCredentials(username, apiKey);
    
            // Define SoftLayer_Network_Monitor_Version1_Query_Host service
            Host.Service hostService = Host.service(client);
    
            // Build a SoftLayer_Network_Monitor_Version1_Query_Host object that you wish to create
            Host templateObject = new Host();
            templateObject.setHardwareId(hardwareId);
            templateObject.setIpAddress(ipAddress);
            templateObject.setQueryTypeId(queryTypeId);
            templateObject.setResponseActionId(responseActionId);
            templateObject.setWaitCycles(waitCycles);
            try {
                Host result = hostService.createObject(templateObject);
                Gson gson = new Gson();
                System.out.println(gson.toJson(result));
    
            } catch (Exception e) {
                System.out.println("Error: " + e);
            }
        }
    
        /**
         * This is the main method which makes use of AddMonitor method.
         *
         * @param args
         * @return Nothing
         */
        public static void main(String[] args) {
            new AddMonitor();
        }
    
    
    }
    

    If you want to add a monitor for Virtual Guest, you should define guestId instead of hardwareId.

    3. To add users to notify:

    Here an example using SoftLayer_User_Customer_Notification_Hardware::createObject:

    package Monitoring;
    
    import com.google.gson.Gson;
    import com.softlayer.api.ApiClient;
    import com.softlayer.api.RestApiClient;
    import com.softlayer.api.service.user.customer.notification.Hardware;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * This script adds a users to notify.
     *
     * Important Manual Page:
     * http://sldn.softlayer.com/reference/services/SoftLayer_User_Customer_Notification_Hardware/createObjects
     * http://sldn.softlayer.com/reference/datatypes/SoftLayer_Dns_Domain
     *
     * @license <http://sldn.softlayer.com/article/License>
     * @authon SoftLayer Technologies, Inc. <sldn@softlayer.com>
     * @version 0.2.2
     */
    public class AddUsersToNotify {
        /**
         * This is the constructor, is used to Add users to notify
         */
        public AddUsersToNotify() {
            // Declare your SoftLayer username and apiKey
            String username = "set me";
            String apiKey = "set me";
    
            // Define the hardware and user identifiers
            Long hardwareId = new Long(92862);
            Long userId = new Long(141202);
    
            // Create client
            ApiClient client = new RestApiClient().withCredentials(username, apiKey);
    
            // Define SoftLayer_User_Customer_Notification_Hardware service
            Hardware.Service hardwareService = Hardware.service(client);
    
            // Declare SoftLayer_User_Customer_Notification_Hardware List
            List<Hardware> templateObjects = new ArrayList<Hardware>();
    
            // Build a SoftLayer_User_Customer_Notification_Hardware objects that you wish to create
            Hardware templateObject = new Hardware();
            templateObject.setUserId(userId);
            templateObject.setHardwareId(hardwareId);
    
            // Add templateObject to templateObjects
            templateObjects.add(templateObject);
    
            try {
                Gson gson = new Gson();
                Hardware results = hardwareService.createObject(templateObject);
                System.out.println(gson.toJson(results));
    
            } catch (Exception e) {
                System.out.println("Error: " + e);
            }
        }
    
        /**
         * This is the main method which makes use of AddUsersToNotify method.
         *
         * @param args
         * @return Nothing
         */
        public static void main(String[] args) {
            new AddUsersToNotify();
        }
    
    
    }
    

    I hope it really help you.