OuterIntfCtrl.java
12.2 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package com.ui.ctrl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ui.User.DnsRefreshLocal;
import com.ui.User.UserAuthLocal;
import com.ui.common.OrderTokenConstant;
import com.ui.http.HttpRestClient;
import com.ui.model.BaseResponse;
import com.ui.model.MonitAlarmInfo;
import com.ui.model.WebHooksReleaseBO;
import com.ui.model.alarm.CommonAlarmData;
import com.ui.model.alarm.CommonData;
import com.ui.model.domain.QcloudSmsUp;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
*
* @author yoho
* 对外提供接口
*
*/
@Controller
@RequestMapping("/outer")
public class OuterIntfCtrl {
SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Logger log = LoggerFactory.getLogger(OuterIntfCtrl.class);
@Autowired
private HttpRestClient httpClient;
@Autowired
UserAuthLocal userAuthLocal;
@Autowired
DnsRefreshLocal dnsRefreshLocal;
/**
* 对外接口,通用指标上报
*/
@RequestMapping("/commonAlarmDataUpLoad")
@ResponseBody
public void commonAlarmDataUpLoad(@RequestBody CommonData<CommonAlarmData> data){
httpClient.defaultPost("/commonAlarmData/upLoadData",data,BaseResponse.class);
//System.out.println(baseResponse);
}
/**
* 对外接口,提供报警
* @return
*/
@RequestMapping("/recvMonitAlarm")
@ResponseBody
public BaseResponse recvMonitAlarm(@RequestBody MonitAlarmInfo monitAlarmInfo){
BaseResponse response=httpClient.defaultPost("/recvMonitAlarm",monitAlarmInfo,BaseResponse.class);
return response;
}
@RequestMapping("/maliciousIpAlarm")
@ResponseBody
public void maliciousIpAlarm(String info) {
httpClient.defaultGet("/maliciousIpAlarm?info="+info,BaseResponse.class);
}
/**
* 对外接口,提供报警
* 举例
* http://www.yohops.com/outer/recvMonitAlarmInfo?info=测试短信&idc=yoho_aws&mobiles=15699999,18988888
*/
@RequestMapping(value="/recvMonitAlarmInfo" ,method= RequestMethod.POST,headers={"Accept=application/json","content-type=application/json"})
@ResponseBody
public BaseResponse recvMonitAlarmInfo(@RequestBody MonitAlarmInfo monitAlarmInfo,HttpServletRequest request ){
/*Enumeration e=request.getHeaderNames();
while(e.hasMoreElements()){
String key = (String)e.nextElement();//调用nextElement方法获得元素
System.out.println(key +"--->" +request.getHeader(key) );
}*/
log.info("recvMonitAlarmInfoPost {}",monitAlarmInfo);
BaseResponse response=httpClient.defaultPost("/recvMonitAlarmInfo",monitAlarmInfo,BaseResponse.class);
return response;
}
/**
* 对外接口,提供报警
* 举例
* http://www.yohops.com/outer/recvMonitAlarmInfo?info=测试短信&idc=yoho_aws&mobiles=15699999,18988888
*/
@RequestMapping(value="/recvMonitAlarmInfo")
@ResponseBody
public BaseResponse recvMonitAlarmInfoOther( MonitAlarmInfo monitAlarmInfo,HttpServletRequest request){
log.info("recvMonitAlarmInfoOther {}",monitAlarmInfo);
BaseResponse response=httpClient.defaultPost("/recvMonitAlarmInfo",monitAlarmInfo,BaseResponse.class);
return response;
}
/**
* 捕获的恶意ip写入运维系统的redis
* 自动封杀和告警提示
* @return
*/
@RequestMapping("/writeMipsObjToOpsReids")
@ResponseBody
public void writeMipsObjToOpsReids(@RequestBody String ipsObjs) {
httpClient.defaultPostJson("/maliciousIp/writeMipsObjToOpsReids",ipsObjs,String.class);
}
/**
* 捕获的恶意ip写入运维系统的mysql
* 大数据算法
* @return
*/
@RequestMapping("/writeMipsInfoToOpsDb")
@ResponseBody
public void writeMipsInfoToOpsDb(@RequestBody String ipsObjs) {
httpClient.defaultPostJson("/maliciousIp/writeMipsInfoToOpsDb",ipsObjs,String.class);
}
@RequestMapping("/getHostIpByTags")
@ResponseBody
public String getHostIpByTags(String tags) {
List<String> ips= httpClient.defaultPost("/hostInfo/getHostIpByTags?tags="+tags,null,List.class);
return JSON.toJSONString(ips);
}
/**********************************************************************
* dock项目
*********************************************************************/
/**
* 发布工单_jenkins:更新镜像的状态
*/
@RequestMapping("/updateJenkinsStatus")
@ResponseBody
public BaseResponse updateJenkinsStatus(String ticketID,String jobID,String status) {
Map<String,String> map=new HashMap<>();
map.put("ticketID",ticketID);
map.put("jobID",jobID);
map.put("status",status);
return httpClient.defaultGet("/dockerProject/updateJenkinsStatus",BaseResponse.class,map);
}
/**
* docker自动发布的回调
*/
@RequestMapping("/gitWebHook")
@ResponseBody
public BaseResponse gitWebHook(@RequestBody com.alibaba.fastjson.JSONObject jsonObject) throws Exception {
log.info("gitWebHookAppCollectLog get the webhook from git begin --{}", jsonObject.toJSONString());
WebHooksReleaseBO bo=releaseWebHooksNoCommits(jsonObject);
return httpClient.defaultPost("/dockerProject/gitWebHook",bo,BaseResponse.class);
}
private WebHooksReleaseBO releaseWebHooksNoCommits(JSONObject jsonObject) {
WebHooksReleaseBO webHooksReleaseBO = new WebHooksReleaseBO();
String object_kind = jsonObject.getString("object_kind");
String ref = jsonObject.getString("ref");
String userName = jsonObject.getString("user_name");
String userEmail = jsonObject.getString("user_email");
Integer total_commits_count = jsonObject.getInteger("total_commits_count");
String projectName = jsonObject.getJSONObject("project").getString("name");
String projectNameSpace = jsonObject.getJSONObject("project").getString("namespace");
String gitUrl = jsonObject.getJSONObject("project").getString("ssh_url");
Set<String> list = new HashSet<>();
JSONArray jsonArrays = jsonObject.getJSONArray("commits");
Iterator<Object> it = jsonArrays.iterator();
while (it.hasNext()) {
JSONObject ob = (JSONObject) it.next();
JSONArray jsonArrayAdded = ob.getJSONArray("added");
JSONArray jsonArrayModified = ob.getJSONArray("modified");
JSONArray jsonArrayRemoved = ob.getJSONArray("removed");
addChange(jsonArrayAdded, list);
addChange(jsonArrayModified, list);
addChange(jsonArrayRemoved, list);
}
webHooksReleaseBO.setChangFileForDockerFile(list);
webHooksReleaseBO.setObject_kind(object_kind);
webHooksReleaseBO.setRef(ref);
webHooksReleaseBO.setTotal_commits_count(total_commits_count);
webHooksReleaseBO.setProjectName(projectName);
webHooksReleaseBO.setProjectNameSpace(projectNameSpace);
webHooksReleaseBO.setGitUrl(gitUrl);
webHooksReleaseBO.setUser_name(userName);
webHooksReleaseBO.setUser_email(userEmail);
return webHooksReleaseBO;
}
private void addChange(JSONArray jsonArray, Set<String> list) {
if (Objects.nonNull(jsonArray) && jsonArray.size() > NumberUtils.INTEGER_ZERO) {
for (int i = 0; i < jsonArray.size(); i++) {
String newAdd = jsonArray.get(i).toString();
if (StringUtils.isNotBlank(newAdd)) {
list.add(newAdd);
}
}
}
}
/**
* 大数据上报的数据
*
{
"name":"搜素用户word2vector特征",
"script":"word-2-vector.sh",
"status":"success",
"task_id" : 123 ,
"count_name": "xxxx" ,
"count": 1000
}
*/
@RequestMapping("/bigDataKeyInfoReport")
@ResponseBody
public BaseResponse bigDataKeyInfoReport(@RequestBody com.alibaba.fastjson.JSONObject jsonObject) throws Exception {
return httpClient.defaultPost("/bigdata/bigDataKeyInfoReport",jsonObject,BaseResponse.class);
}
/**
*
* Grafana alert webhook. refer to:http://docs.grafana.org/alerting/notifications/
*
* <pre>
*
* {
"title": "My alert",
"ruleId": 1,
"ruleName": "Load peaking!",
"ruleUrl": "http://url.to.grafana/db/dashboard/my_dashboard?panelId=2",
"state": "alerting",
"imageUrl": "http://s3.image.url",
"message": "Load is peaking. Make sure the traffic is real and spin up more webfronts",
"evalMatches": [
{
"metric": "requests",
"tags": {},
"value": 122
}
]
}
* </pre>
*
* Created by chunhua.zhang on 2017/5/26.
*/
/**
* 接口:grafana报警提示
* @param request
* @return
*/
@RequestMapping(value = "/grafanaWebHook")
@ResponseBody
public BaseResponse grafanaWebHook(HttpServletRequest request) {
try {
String jsonBody = IOUtils.toString(request.getInputStream(),"utf-8");
log.info("receive grafana webhook message:{}", jsonBody);
JSONObject alert = (JSONObject) JSON.parse(jsonBody);
String title = "";
String state = "";
String imgUrl = "";
String message = "";
if(alert!=null){
title = alert.getString("title");
state = alert.getString("state");
imgUrl = alert.get("imageUrl")==null?"":alert.getString("imageUrl");
message = alert.get("message")==null?"":alert.getString("message");
}
Map<String, String> map=new HashMap<>();
map.put("title",title);
map.put("state",state);
map.put("imgUrl",imgUrl);
map.put("message",message);
return httpClient.defaultGet("/grafanaAlert/sendDing",BaseResponse.class,map);
} catch (IOException e) {
e.printStackTrace();
}
return new BaseResponse(201);
}
/***
* DNS 更新接口
*/
@RequestMapping("/dnsJustForShell")
@ResponseBody
public String dnsJustForShell(String domain, String cloud, String type, String operate, String record, String value) {
return dnsRefreshLocal.dnsJustForShell(domain, cloud, type, operate, record, value);
}
@RequestMapping("/dnsJustForShellUpdate")
@ResponseBody
public String dnsJustForShellUpdate( String cloud, String record, String value) {
return dnsRefreshLocal.dnsJustForShellUpdate(cloud,record,value);
}
@RequestMapping("/dnsJustForShellCommit")
@ResponseBody
public String dnsJustForShellCommit( String cloud) {
return dnsRefreshLocal.dnsJustForShellCommit(cloud);
}
/**
* 刷新用户权限
*/
@RequestMapping("/flushUserModule")
@ResponseBody
public BaseResponse flushUserModule(@RequestBody String userNames) {
if(userNames!=null&&userNames.length()>0){
for(String name:userNames.split(",")){
if(name!=null&&name.length()>0){
userAuthLocal.flushUser(name);
}
}
}
return new BaseResponse();
}
/**
*短信收到后确定
*/
@RequestMapping("/smsConfirm")
@ResponseBody
public BaseResponse smsConfirm() {
QcloudSmsUp qcloudSmsUp=new QcloudSmsUp();
qcloudSmsUp.setMobile("188888888");
qcloudSmsUp.setText("confirm");
qcloudSmsUp.setSign("YOHO运维");
//qcloudSmsUp.setTime(time.format(new Date()));
httpClient.defaultPost("/smsup/smsUp", qcloudSmsUp, BaseResponse.class);
return new BaseResponse();
}
@RequestMapping("/collectNginxAgentLog")
@ResponseBody
public BaseResponse collectNginxAgentLog(@RequestBody com.alibaba.fastjson.JSONObject jsonObject) throws Exception {
log.info("collectNginxAgentLog content is {}",jsonObject);
return httpClient.defaultPost("/nginxsync/collectNginxAgentLog",jsonObject,BaseResponse.class);
}
/***
* test 接口 比较zk
*/
@RequestMapping("/compareTwoZkRootTest")
@ResponseBody
public BaseResponse compareTwoZkRootTest(String zkOne,String zkTwo,String root) {
if (StringUtils.isBlank(zkOne) || StringUtils.isBlank(zkTwo)) {
return new BaseResponse(201,"param error");
}
Map<String, String> map=new HashMap<>();
map.put("zkOne",zkOne);
map.put("zkTwo",zkTwo);
map.put("root",StringUtils.isBlank(root)?"":root);
return httpClient.defaultGet("/zkMonitor/compareTwoZkRootTest",BaseResponse.class,map);
}
/**
* Opsmanage token校验
*/
@RequestMapping("/checkOrderToken4Opsmanage")
@ResponseBody
public boolean checkOrderToken4Opsmanage(String token) {
boolean pass=false;
if (StringUtils.isNotBlank(token)&&OrderTokenConstant.orderTokens.contains(token)){
pass=true;
OrderTokenConstant.orderTokens.remove(token);
}
return pass;
}
}