我正在实施监控功能,例如添加监控,添加用户和编辑监控..(参考附件) 为了让用户通知,我调用了getEligibleAlarmSubscibers(),但没有检索到任何数据。您能否在java中为我提供任何示例代码来实现监控(添加监控,添加用户,列出现有监控)?
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());
}
}
答案 0 :(得分:0)
<强> 1。要获得监视器,您应该尝试以下方法:
更新
这是硬件的一个例子:
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。要添加监视器,您应该使用以下方法:
这是硬件的一个例子:
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();
}
}
如果要为虚拟访客添加监视器,则应定义 guestId 而不是 hardwareId 。
第3。要添加要通知的用户:
这是使用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();
}
}
我希望它对你有所帮助。
答案 1 :(得分:0)
这是我尝试添加显示器的内容..
import com.google.gson.Gson;
import com.softlayer.api.ApiClient;
import com.softlayer.api.RestApiClient;
import com.softlayer.api.service.monitoring.Agent;
import com.softlayer.api.service.user.Customer;
import com.softlayer.api.service.virtual.Guest;
import com.softlayer.api.service.Hardware;
import com.softlayer.api.service.network.monitor.version1.query.Host;
private void AddMonitoringInfo(){
Long hardwareId = new Long(848429);
// Define Ip Address
String ipAddress = "173.192.36.250";
// 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);
// 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);
}
}
答案 2 :(得分:0)
尝试使用此代码进行编辑:
package Monitoring;
import com.softlayer.api.ApiClient;
import com.softlayer.api.RestApiClient;
import com.softlayer.api.service.network.monitor.version1.query.Host;
/**
* This script edits a monitor
*
* Important Manual Page:
* http://sldn.softlayer.com/reference/services/SoftLayer_Network_Monitor_Version1_Query_Host/editObject
*
* @license <http://sldn.softlayer.com/article/License>
* @authon SoftLayer Technologies, Inc. <sldn@softlayer.com>
* @version 0.2.2
*/
public class EditMonitor {
/**
* This is the constructor, is used to Add Monitor
*/
public EditMonitor() {
// Declare your SoftLayer username and apiKey
String username = "set me";
String apiKey = "set me";
// Define Hardware identifier
Long hostId = new Long(3514022);
// Define Query Type (1 = SERVICE PING, 17 = SLOW PING)
Long queryTypeId = new Long(17);
// Define Response action id (1 = Do Nothing, 2 = Notify Users)
Long responseActionId = new Long(1);
// Define waitCycles 0 = Immediately, 1 = 5 Minutes, 2 = 10 Minutes, etc
Long waitCycles = new Long(0);
// Create client
ApiClient client = new RestApiClient().withCredentials(username, apiKey);
// Define SoftLayer_Network_Monitor_Version1_Query_Host service
Host.Service hostService = Host.service(client, hostId);
// Build a SoftLayer_Network_Monitor_Version1_Query_Host object that you wish to edit
Host templateObject = new Host();
templateObject.setQueryTypeId(queryTypeId);
templateObject.setResponseActionId(responseActionId);
templateObject.setWaitCycles(waitCycles);
try {
boolean result = hostService.editObject(templateObject);
System.out.println(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 EditMonitor();
}
}
您应该指定 hostId (您要编辑的监视器ID),我更新了getMonitors脚本,为了提供ID,请参阅 getMonitorsFromServer 脚本,在我的第一个答案