diff --git a/.gitignore b/.gitignore index 3b735ec..9508bfd 100755 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,7 @@ # Go workspace file go.work + +*.db +*.log +log diff --git a/TODOLIST.md b/TODOLIST.md index b3841ed..9a12ded 100755 --- a/TODOLIST.md +++ b/TODOLIST.md @@ -11,8 +11,15 @@ - tool : 在线ping ok - tool : 获取证书 ok - tool : 网站信息获取 ok -- master 添加网站 +- master 添加网站 ok +- master 监测网站列表 - master 获取网站基础信息 +- master 删除网站监测 +- master 监测网站采集到url +- master 监测设置 +- master 图表 +- master 报警信息 +- master 报警信息 - master 任务表 - monitor 每次启动拉一次监测任务表 - master 获取monitor在线情况 diff --git a/master/constname/variable.go b/master/constname/variable.go index 07a8131..f4c7065 100755 --- a/master/constname/variable.go +++ b/master/constname/variable.go @@ -13,3 +13,16 @@ const ( DayLayout = "20060102" MasterVersion = "v0.1" ) + +// default param +var ( + DefaultMonitorRate int64 = 15 + DefaultContrastUrl = "https://www.baidu.com" + DefaultContrastTime int64 = 1000 + DefaultPing = "8.8.8.8" + DefaultPingTime int64 = 1000 + DefaultUriDepth int64 = 2 + DefaultScanRate int64 = 24 + DefaultWebsiteSlowResponseCount int64 = 3 + DefaultSSLCertificateExpire int64 = 14 +) diff --git a/master/data.db b/master/data.db index d7c2334..24228a1 100755 Binary files a/master/data.db and b/master/data.db differ diff --git a/master/entity/website.go b/master/entity/website.go index 966c207..9efa3d2 100755 --- a/master/entity/website.go +++ b/master/entity/website.go @@ -49,6 +49,7 @@ type WebsiteScanCheckUp struct { ScanRate int64 `json:"scanRate"` // 扫描网站频率 单位秒 ScanExtLinks bool `json:"scanExtLinks"` // 是否检查外链,对比上一次扫描数据判别外链是否变化 ScanBadLink bool `json:"scanBadLink"` // 是否扫描死链接 + ScanTDK bool `json:"scanTDK"` // 是否扫描进行TDK检查 // TODO... 安全扫描, Sql注入, XSS等等... } diff --git a/master/handler/website_api.go b/master/handler/website_api.go new file mode 100644 index 0000000..f26c84a --- /dev/null +++ b/master/handler/website_api.go @@ -0,0 +1,140 @@ +package handler + +import ( + "github.com/mangenotwork/common/ginHelper" + "github.com/mangenotwork/common/log" + "time" + "website-monitor/master/constname" + "website-monitor/master/dao" + "website-monitor/master/entity" +) + +type WebsiteAddParam struct { + Host string `json:"host"` + Notes string `json:"notes"` + MonitorRate int64 `json:"monitorRate"` + ContrastUrl string `json:"contrastUrl"` + ContrastTime int64 `json:"contrastTime"` + Ping string `json:"ping"` + PingTime int64 `json:"pingTime"` + UriDepth int64 `json:"uriDepth"` + ScanRate int64 `json:"scanRate"` + ScanBadLink bool `json:"scanBadLink"` + ScanTDK bool `json:"scanTDK"` + ScanExtLinks bool `json:"scanExtLinks"` + WebsiteSlowResponseTime int64 `json:"websiteSlowResponseTime"` + WebsiteSlowResponseCount int64 `json:"websiteSlowResponseCount"` + SSLCertificateExpire int64 `json:"SSLCertificateExpire"` +} + +func WebsiteAdd(c *ginHelper.GinCtx) { + param := &WebsiteAddParam{} + err := c.GetPostArgs(param) + if err != nil { + c.APIOutPutError(err, "参数或参数类型错误") + return + } + if len(param.Host) < 1 { + c.APIOutPutError(nil, "参数错误: host不能为空") + return + } + if param.MonitorRate < 1 { + param.MonitorRate = constname.DefaultMonitorRate + } + if len(param.ContrastUrl) < 1 { + param.ContrastUrl = constname.DefaultContrastUrl + } + if param.ContrastTime < 1 { + param.ContrastTime = constname.DefaultContrastTime + } + if len(param.Ping) < 1 { + param.Ping = constname.DefaultPing + } + if param.PingTime < 1 { + param.PingTime = constname.DefaultPingTime + } + if param.UriDepth < 1 { + param.UriDepth = constname.DefaultUriDepth + } + if param.ScanRate < 1 { + param.ScanRate = constname.DefaultScanRate + } + if param.WebsiteSlowResponseTime < 100 { + c.APIOutPutError(nil, "网站响应慢不能小于100ms") + return + } + if param.WebsiteSlowResponseCount < 1 { + param.WebsiteSlowResponseCount = constname.DefaultWebsiteSlowResponseCount + } + if param.SSLCertificateExpire < 1 { + param.SSLCertificateExpire = constname.DefaultSSLCertificateExpire + } + + log.Info("param = ", param) + website := &entity.Website{ + Host: param.Host, + MonitorRate: param.MonitorRate, + ContrastUrl: param.ContrastUrl, + ContrastTime: param.ContrastTime, + Ping: param.Ping, + PingTime: param.PingTime, + Notes: param.Notes, + Created: time.Now().Unix(), + } + alarmRule := &entity.WebsiteAlarmRule{ + Host: param.Host, + WebsiteSlowResponseTime: param.WebsiteSlowResponseTime, + WebsiteSlowResponseCount: param.WebsiteSlowResponseCount, + SSLCertificateExpire: param.SSLCertificateExpire, + NotTDK: param.ScanTDK, + BadLink: param.ScanBadLink, + ExtLinkChange: param.ScanExtLinks, + } + scan := &entity.WebsiteScanCheckUp{ + Host: param.Host, + ScanDepth: param.UriDepth, + ScanRate: param.ScanRate, + ScanTDK: param.ScanTDK, + ScanBadLink: param.ScanBadLink, + ScanExtLinks: param.ScanExtLinks, + } + err = dao.NewWebsite().Add(website, alarmRule, scan) + if err != nil { + c.APIOutPutError(err, "创建失败, err = "+err.Error()) + return + } + c.APIOutPut("创建成功", "创建成功") + return +} + +func WebsiteList(c *ginHelper.GinCtx) { + +} + +func WebsiteDelete(c *ginHelper.GinCtx) { + +} + +func WebsiteInfo(c *ginHelper.GinCtx) { + +} + +func WebsiteUrls(c *ginHelper.GinCtx) { + +} + +func WebsiteEdit(c *ginHelper.GinCtx) { + +} + +func WebsiteChart(c *ginHelper.GinCtx) { + +} + +func WebsiteAlertList(c *ginHelper.GinCtx) { + +} + +func WebsiteAlertDel(c *ginHelper.GinCtx) { + +} diff --git a/master/routers/base.go b/master/routers/base.go index bbbebd2..b58baa5 100755 --- a/master/routers/base.go +++ b/master/routers/base.go @@ -59,6 +59,17 @@ func API() { api := Router.Group("/api").Use(AuthAPI()) api.GET("/out", handler.Out) + // website + api.POST("/website/add", ginHelper.Handle(handler.WebsiteAdd)) // 创建网站监测 + api.GET("/website/list", ginHelper.Handle(handler.WebsiteList)) // TODO 监测网站列表 + api.GET("/website/delete/:hostId", ginHelper.Handle(handler.WebsiteDelete)) // TODO 删除网站监测 + api.GET("/website/info/:hostId", ginHelper.Handle(handler.WebsiteInfo)) // TODO 监测网站详情 + api.GET("/website/urls/:hostId", ginHelper.Handle(handler.WebsiteUrls)) // TODO 监测网站采集到url + api.POST("/website/edit", ginHelper.Handle(handler.WebsiteEdit)) // TODO 监测设置 + api.GET("/website/chart/:hostId", ginHelper.Handle(handler.WebsiteChart)) // TODO 图表 + api.GET("/website/alert/:hostId", ginHelper.Handle(handler.WebsiteAlertList)) // TODO 报警信息 + api.GET("/website/alert/del/:hostId", ginHelper.Handle(handler.WebsiteAlertDel)) // TODO 报警信息 + // mail api.GET("/mail/init", ginHelper.Handle(handler.MailInit)) // 是否设置邮件 api.POST("/mail/conf", ginHelper.Handle(handler.MailConf)) // 设置邮件配置 diff --git a/master/static/css/page.css b/master/static/css/page.css index 2c692c7..1656e1e 100755 --- a/master/static/css/page.css +++ b/master/static/css/page.css @@ -217,4 +217,8 @@ thead tr th { .box:hover { box-shadow: 4px 16px 32px 4px rgba(48, 55, 66, 0.3);/* 盒子悬浮时阴影 */ +} + +.addTipNow { + color: #0a53be; } \ No newline at end of file diff --git a/master/static/js/public.js b/master/static/js/public.js index e24e682..d92bd8b 100755 --- a/master/static/js/public.js +++ b/master/static/js/public.js @@ -48,8 +48,8 @@ class Utils { ToastShow(msg) { const toastLiveExample = $('#liveToast') const toast = new bootstrap.Toast(toastLiveExample) - $("#liveToastMsg").append(msg) - console.log("ToastShow...") + $("#liveToastMsg").empty(msg); + $("#liveToastMsg").append(msg); toast.show() } } @@ -115,12 +115,135 @@ let Mail= { let AddWebSite = { api: "/api/website/add", - param : { - host: "", - healthUri: "", - rate: 10, - alarmResTime: 3000, - uriDepth: 2, - uriUpdateRate: 24, + param : {}, + common: new Utils(), + next1Param: { + hostProtocol: "https://", + hostUrl: "", + notes: "", + monitorRate: 15, + contrastUrl: "https://www.baidu.com", + contrastTime: 1000, + ping: "8.8.8.8", + pingTime: 1000, + }, + next2Param: { + uriDepth: 2, + scanRate: 24, + scanBadLink: true, + scanTDK: false, + scanExtLinks: false, + }, + next3Param: { + websiteSlowResponseTime: 3000, + websiteSlowResponseCount: 3, + SSLCertificateExpire: 14, + }, + addNext1: function () { + console.log(AddWebSite.next1Param); + if (AddWebSite.next1Param.hostUrl === "") { + common.ToastShow("请输入检测网站的URL"); + return + } + if (AddWebSite.next1Param.monitorRate === 0) { + common.ToastShow("检测频率建议大于1s"); + return + } + if (AddWebSite.next1Param.contrastUrl === "") { + common.ToastShow("对照组不能为空"); + return + } + if (AddWebSite.next1Param.contrastTime === "") { + common.ToastShow("对照组慢响应时间不低于100ms"); + return + } + if (AddWebSite.next1Param.ping === "") { + common.ToastShow("Ping地址不能为空"); + return + } + if (AddWebSite.next1Param.pingTime === "") { + common.ToastShow("Ping慢响应时间不低于100ms"); + return + } + $("#next1Forms").hide(); + $("#next2Forms").show(); + $("#next1").removeClass("addTipNow"); + $("#next2").addClass("addTipNow"); + $("#next1But").hide(); + $("#next2But").show(); + }, + addReturn1: function () { + $("#next1Forms").show(); + $("#next2Forms").hide(); + $("#next1").addClass("addTipNow"); + $("#next2").removeClass("addTipNow"); + $("#next1But").show(); + $("#next2But").hide(); + }, + addNext2: function () { + console.log(AddWebSite.next2Param); + if (AddWebSite.next2Param.uriDepth <1) { + common.ToastShow("扫描网站深度不能小于1"); + return + } + if (AddWebSite.next2Param.scanRate <1) { + common.ToastShow("扫描网站频率不能小于1"); + return + } + $("#next2Forms").hide(); + $("#next3Forms").show(); + $("#next2").removeClass("addTipNow"); + $("#next3").addClass("addTipNow"); + $("#next2But").hide(); + $("#next3But").show(); + }, + addReturn2: function () { + let t = this; + $("#next2Forms").show(); + $("#next3Forms").hide(); + $("#next2").addClass("addTipNow"); + $("#next3").removeClass("addTipNow"); + $("#next2But").show(); + $("#next3But").hide(); + }, + addSubmit: function () { + console.log(AddWebSite.next3Param); + if (AddWebSite.next3Param.websiteSlowResponseTime <100) { + common.ToastShow("网站响应慢不能小于100ms"); + return + } + if (AddWebSite.next3Param.websiteSlowResponseCount <1) { + common.ToastShow("网站响应慢发送邮件连次数不能小于1次"); + return + } + if (AddWebSite.next3Param.SSLCertificateExpire <1) { + common.ToastShow("网站证书过期触发报警天数不能小于1天"); + return + } + AddWebSite.param.host = AddWebSite.next1Param.hostProtocol + AddWebSite.next1Param.hostUrl; + AddWebSite.param.notes = AddWebSite.next1Param.notes; + AddWebSite.param.monitorRate = Number(AddWebSite.next1Param.monitorRate); + AddWebSite.param.contrastUrl = AddWebSite.next1Param.contrastUrl; + AddWebSite.param.contrastTime = Number(AddWebSite.next1Param.contrastTime); + AddWebSite.param.ping = AddWebSite.next1Param.ping; + AddWebSite.param.pingTime = Number(AddWebSite.next1Param.pingTime); + AddWebSite.param.uriDepth = Number(AddWebSite.next2Param.uriDepth); + AddWebSite.param.scanRate = Number(AddWebSite.next2Param.scanRate); + AddWebSite.param.scanBadLink = AddWebSite.next2Param.scanBadLink; + AddWebSite.param.scanTDK = AddWebSite.next2Param.scanTDK; + AddWebSite.param.scanExtLinks = AddWebSite.next2Param.scanExtLinks; + AddWebSite.param.websiteSlowResponseTime = Number(AddWebSite.next3Param.websiteSlowResponseTime); + AddWebSite.param.websiteSlowResponseCount = Number(AddWebSite.next3Param.websiteSlowResponseCount); + AddWebSite.param.SSLCertificateExpire = Number(AddWebSite.next3Param.SSLCertificateExpire); + + console.log(AddWebSite.param); + + common.AjaxPost(AddWebSite.api, AddWebSite.param, function (data){ + console.log(data); + if (data.code === 0) { + $("#addHostModal").modal('toggle'); + } + common.ToastShow(data.msg); + }); } } \ No newline at end of file diff --git a/master/views/section/add_host_modal.html b/master/views/section/add_host_modal.html index e4bf48d..dbd8c78 100755 --- a/master/views/section/add_host_modal.html +++ b/master/views/section/add_host_modal.html @@ -1,70 +1,152 @@ -