-
Notifications
You must be signed in to change notification settings - Fork 67
/
AlertSample.java
178 lines (158 loc) · 7.84 KB
/
AlertSample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package com.aliyun.openservices.log.sample;
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.Alert;
import com.aliyun.openservices.log.common.AlertConfiguration;
import com.aliyun.openservices.log.common.Chart;
import com.aliyun.openservices.log.common.Dashboard;
import com.aliyun.openservices.log.common.DingTalkNotification;
import com.aliyun.openservices.log.common.EmailNotification;
import com.aliyun.openservices.log.common.JobSchedule;
import com.aliyun.openservices.log.common.JobScheduleType;
import com.aliyun.openservices.log.common.Notification;
import com.aliyun.openservices.log.common.Query;
import com.aliyun.openservices.log.common.TimeSpanType;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.CreateAlertRequest;
import com.aliyun.openservices.log.request.CreateDashboardRequest;
import com.aliyun.openservices.log.request.DisableAlertRequest;
import com.aliyun.openservices.log.request.EnableAlertRequest;
import com.aliyun.openservices.log.request.GetAlertRequest;
import com.aliyun.openservices.log.request.ListAlertRequest;
import com.aliyun.openservices.log.request.UpdateAlertRequest;
import com.aliyun.openservices.log.response.GetAlertResponse;
import com.aliyun.openservices.log.response.ListAlertResponse;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
public class AlertSample {
public static void main(String[] args) {
String accessId = "";
String accessKey = "";
String project = "ali-cn-devcommon-sls-admin";
String host = "cn-hangzhou-devcommon-intranet.sls.aliyuncs.com";
String alertName = "test-alert";
String dashboardName = "dashboardtest";
String logstore = "logstore-test";
Client client = new Client(host, accessId, accessKey);
try {
// create a dashboard first
Dashboard dashboard = new Dashboard();
dashboard.setDashboardName(dashboardName);
dashboard.setDescription("Dashboard");
// add chart to dashboard
Chart chart = new Chart();
chart.setDisplayName("chart-test");
chart.setQuery("* | select count(1) as count");
chart.setLogstore(logstore);
chart.setTitle("chart-1234567");
chart.setType("table");
chart.setTopic("");
chart.setHeight(5);
chart.setWidth(5);
chart.setStart("-360s");
chart.setEnd("now");
chart.setxPosition(0);
chart.setyPosition(-1);
JSONObject searchAttr = new JSONObject();
searchAttr.put("logstore", logstore);
searchAttr.put("start", chart.getStart());
searchAttr.put("end", chart.getEnd());
searchAttr.put("topic", "");
searchAttr.put("query", chart.getQuery());
searchAttr.put("timeSpanType", "custom");
chart.setRawSearchAttr(searchAttr.toString());
ArrayList<Chart> charts = new ArrayList<Chart>();
charts.add(chart);
dashboard.setChartList(charts);
CreateDashboardRequest createDashboardRequest = new CreateDashboardRequest(project, dashboard);
try {
client.createDashboard(createDashboardRequest);
} catch (LogException ex) {
if (!ex.GetErrorMessage().equals("specified dashboard already exists")) {
throw ex;
}
}
Alert alert = new Alert();
alert.setName(alertName);
alert.setDisplayName("count monitoring");
AlertConfiguration configuration = new AlertConfiguration();
configuration.setCondition("count > 1");
// The name of dashboard this alert associated to
configuration.setDashboard(dashboardName);
List<Query> queries = new ArrayList<Query>();
Query query = new Query();
query.setStart("-360s");
query.setEnd("now");
query.setTimeSpanType(TimeSpanType.CUSTOM);
query.setQuery(chart.getQuery());
query.setLogStore(logstore);
query.setChartTitle(chart.getTitle());
queries.add(query);
configuration.setQueryList(queries);
// Send email if alert fired
EmailNotification notification = new EmailNotification();
notification.setEmailList(Collections.singletonList("kel@test.com"));
notification.setContent("Alerting");
List<Notification> notifications = new ArrayList<Notification>();
notifications.add(notification);
// Send Ding Ding if alert fired
DingTalkNotification dingTalkNotification = new DingTalkNotification();
dingTalkNotification.setServiceUri("https://oapi.dingtalk.com/robot/send?access_token=xxx");
dingTalkNotification.setContent("Ding talk message");
notifications.add(dingTalkNotification);
// send voice, sms, webhook, etc
configuration.setNotificationList(notifications);
configuration.setThrottling("0s");
configuration.setNotifyThreshold(100);
alert.setConfiguration(configuration);
JobSchedule schedule = new JobSchedule();
schedule.setType(JobScheduleType.FIXED_RATE);
schedule.setInterval("60s");
alert.setSchedule(schedule);
client.createAlert(new CreateAlertRequest(project, alert));
// Get alert rule by name
GetAlertResponse response = client.getAlert(new GetAlertRequest(project, alertName));
Alert created = response.getAlert();
System.out.println(created.getName());
System.out.println(created.getDisplayName());
System.out.println(created.getCreateTime());
client.disableAlert(new DisableAlertRequest(project, alertName));
response = client.getAlert(new GetAlertRequest(project, alertName));
Alert alert1 = response.getAlert();
System.out.println(alert1.getState());
client.enableAlert(new EnableAlertRequest(project, alertName));
response = client.getAlert(new GetAlertRequest(project, alertName));
Alert alert2 = response.getAlert();
System.out.println(alert2.getState());
DisableAlertRequest disableAlertRequest = new DisableAlertRequest(project, alertName);
client.disableJob(disableAlertRequest);
response = client.getAlert(new GetAlertRequest(project, alertName));
Alert alert3 = response.getAlert();
System.out.println(alert3.getState());
JobSchedule schedule1 = alert3.getSchedule();
System.out.println(schedule1.getInterval());
System.out.println(schedule1.getType());
Date muteTo = new Date(System.currentTimeMillis() + 60 * 1000);
alert3.getConfiguration().setMuteUntil(muteTo);
client.updateAlert(new UpdateAlertRequest(project, alert3));
for (int i = 0; i < 10; i++) {
alert.setName("alert-" + i);
client.createAlert(new CreateAlertRequest(project, alert));
}
// test list jobs
ListAlertRequest listReq = new ListAlertRequest(project);
listReq.setOffset(0);
listReq.setSize(10);
ListAlertResponse listJobsResponse = client.listAlert(listReq);
System.out.println(listJobsResponse.getTotal());
System.out.println(listJobsResponse.getCount());
for (Alert alert4 : listJobsResponse.getResults()) {
System.out.println(alert4.getName());
}
} catch (LogException lex) {
lex.printStackTrace();
}
}
}