using Newtonsoft.Json.Linq; using PMS.BusinessModels.Account; using PMS.BusinessModels.CloudMonitorManage; using PMS.BusinessModels.SMS; using PMS.BusinessModels.SysManager; using PMS.Interface; using PMS.Interface.CloudMonitorManage; using PMS.Interface.MessageManage; using PMS.Interface.SysManager; using PMS.Plugins.Common; using QWPlatform.SystemLibrary; using QWPlatform.SystemLibrary.Utils; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Text; using System.Threading; using System.Web; using System.Web.Mvc; namespace PMS.WebUI.Controllers { /// /// 云监控 /// public class CloudMonitorController : BaseController { ICloudMonitor _cloudMonitor = InterfaceFactory.CreateBusinessInstance(); //推送消息 IMessageManage notice = InterfaceFactory.CreateBusinessInstance(); /// /// 采集配置 /// /// public ActionResult CollectionConfigIndex() { var cloudMonitorConfig = _cloudMonitor.GetCloudMonitorConfig(); var config = cloudMonitorConfig?.CollectionConfig; if (config == null) config = new CollectionConfig(); ViewBag.CollectionConfig = config; return View(); } /// /// 预警配置 /// /// public ActionResult EarlyWarningConfigIndex() { var cloudMonitorConfig = _cloudMonitor.GetCloudMonitorConfig(); var config = cloudMonitorConfig?.AlertConfig; if (config == null) config = new AlertConfig(); ViewBag.AlertConfig = config; return View(); } /// /// 自定义预警配置 /// /// public ActionResult CustomAlertsConfigIndex() { return View(); } /// /// 自定义预警编辑界面 /// /// public ActionResult EditCustomAlertsConfig() { return View(); } /// /// API预警配置 /// /// public ActionResult APIAlertsConfigIndex() { return View(); } /// /// API预警编辑界面 /// /// public ActionResult EditAPIAlertsConfig() { return View(); } /// /// 服务器硬件监控 /// /// public ActionResult ServerHardwareMonitoringIndex() { return View(); } /// /// 预警管理 /// /// public ActionResult EarlyWarningManagementIndex() { return View(); } /// /// 未处理预警处理 /// /// public ActionResult UnprocessedEarlyWarningManagementIndex() { var cloudMonitorConfig = _cloudMonitor.GetCloudMonitorConfig(); var config = cloudMonitorConfig?.AlertConfig; if (config == null) config = new AlertConfig(); ViewBag.Model = config; return View(); } /// /// 预警详情 /// /// [CheckLogin(false)] public ActionResult EarlyWarningInfo(string id) { ViewBag.Model = _cloudMonitor.GetServerAlertByAlertlId(id); return View(); } /// /// 预警处理 /// /// public ActionResult WarningProcessing() { var data = GetCurrentUser(); ViewBag.UserName = data.Name; return View(); } /// /// 批量预警处理 /// /// public ActionResult BatchWarningProcessing() { var data = GetCurrentUser(); ViewBag.UserName = data.Name; return View(); } /// /// 项目监控管理 /// /// public ActionResult ProjectMonitoringManagementIndex() { return View(); } /// /// 服务器信息 /// /// public ActionResult ServerInfo() { return View(); } /// /// 上传指标记录 /// /// public ActionResult UploadIndicatorRecordIndex() { return View(); } /// /// 保存采集配置 /// /// /// public ActionResult SaveCollectionConfig(CollectionConfig config) { return Content(_cloudMonitor.SaveCollectionConfig(config) ? "1" : "0", "text/plain"); } /// /// 保存预警配置 /// /// /// public ActionResult SaveAlertConfig(AlertConfig config) { return Content(_cloudMonitor.SaveAlertConfig(config) ? "1" : "0", "text/plain"); } /// /// 获取预警配置 /// /// public ActionResult GetAlertConfiguration() { var cloudMonitorConfig = _cloudMonitor.GetCloudMonitorConfig(); var config = cloudMonitorConfig?.AlertConfig; if (config == null) config = new AlertConfig(); return Content(Strings.ObjectToJson(config), "application/json"); } /// /// 获取自定义配置 /// /// public ActionResult GetCustomAlertModel(string search) { var data = _cloudMonitor.GetCustomAlertModelByChannelId(GetAuthDats()?.Channel, search); return Content(Strings.ObjectToJson(data), "application/json"); } /// /// 获取自定义API配置 /// /// public ActionResult GetCustomAPIModel(string search) { var data = _cloudMonitor.GetCustomAPIModelByChannelId(GetAuthDats()?.Channel, search); return Content(Strings.ObjectToJson(data), "application/json"); } /// /// 根据项目id获取 启动的自定义预警配置 /// /// /// public ActionResult GetCustomAlertModelByItemIdAndStart(string itemid) { var data = _cloudMonitor.GetCustomAlertModelByItemIdAndStart(itemid); return Content(Strings.ObjectToJson(data), "application/json"); } /// /// 新增自定义配置 /// /// /// public ActionResult SaveCustomAlert(CustomAlertModel model) { var data = false; if (string.IsNullOrEmpty(model.ID)) { model.ID = Guid.NewGuid().ToString("N"); model.创建人ID = GetCurrentUser().ID; data = _cloudMonitor.InsertCustomAlert(model); } else { data = _cloudMonitor.UpdateCustomAlert(model); } return Content(data ? "1" : "0", "text/plain"); } /// /// 新增自定义API配置 /// /// /// public ActionResult SaveCustomAPI(CustomAPIModel model) { var data = false; if (string.IsNullOrEmpty(model.ID)) { model.ID = Guid.NewGuid().ToString("N"); model.创建人ID = GetCurrentUser().ID; data = _cloudMonitor.InsertCustomAPI(model); } else { data = _cloudMonitor.UpdateCustomAPI(model); } return Content(data ? "1" : "0", "text/plain"); } /// /// 根据项目id获取服务器信息 /// /// /// public ActionResult GetServerByItemId(string itemid) { var data = _cloudMonitor.GetServerByItemId(itemid); return Content(Strings.ObjectToJson(data), "application/json"); } /// /// 根据id删除 /// /// /// public ActionResult DeleteCustomAlertById(string id) { var data = _cloudMonitor.DeleteCustomAlertById(id); return Content(data ? "1" : "0", "text/plain"); } /// /// 根据id删除 /// /// /// public ActionResult DeleteCustomAPIById(string id) { var data = _cloudMonitor.DeleteCustomAPIById(id); return Content(data ? "1" : "0", "text/plain"); } /// /// 获取采集配置 /// /// public ActionResult GetCollectionConfig() { var cloudMonitorConfig = _cloudMonitor.GetCloudMonitorConfig(); var config = cloudMonitorConfig?.CollectionConfig; return Content(Strings.ObjectToJson(config), "application/json"); } /// /// 获取所有服务器信息 /// /// public ActionResult GetServerInfoAll(string search) { var data = _cloudMonitor.GetServerIndexInfoByChannelId(GetAuthDats().Channel, search); return Content(Strings.ObjectToJson(data), "application/json"); } /// /// 获取所有服务器信息 通过项目id过滤 /// /// public ActionResult GetServerInfoByProjectId(string projectId) { var data = new List(); if (!string.IsNullOrEmpty(projectId)) { var projectArr = projectId.Split(','); var authProjectArr = GetAuthDats().Project.Split(','); //传入的渠道id和自身有的权限的渠道id 取交集 var intersection = projectArr.Intersect(authProjectArr); if (intersection.Any()) data = _cloudMonitor.GetServerIndexInfoByProjectId(string.Join(",", intersection)); } return Content(Strings.ObjectToJson(data), "application/json"); } /// /// 获取所有服务器信息 通过渠道id过滤 /// /// public ActionResult GetServerInfoByChannelId(string channelId) { var data = new List(); if (!string.IsNullOrEmpty(channelId)) { var channelArr = channelId.Split(','); var authChannelArr = GetAuthDats().Channel.Split(','); //传入的渠道id和自身有的权限的渠道id 取交集 var intersection = channelArr.Intersect(authChannelArr); if (intersection.Any()) data = _cloudMonitor.GetServerIndexInfoByChannelId(string.Join(",", intersection)); } return Content(Strings.ObjectToJson(data), "application/json"); } /// /// 获取指标详情 根据指标id /// /// public ActionResult GetServerDetailByIndexId(string indexId, DateTime startTime, DateTime endTime) { var data = _cloudMonitor.GetServerIndexDetailByIndexId(indexId, startTime, endTime); return Content(Strings.ObjectToJson(data), "application/json"); } /// /// 获取指标详情 根据服务器id /// /// public ActionResult GetServerDetailByServerId(string serverId, DateTime startTime, DateTime endTime) { var data = _cloudMonitor.GetServerIndexDetailByServerId(serverId, startTime, endTime); return Content(Strings.ObjectToJson(data), "application/json"); } /// /// 获取所有服务器预警 /// /// public ActionResult GetServerAlertAll(string search) { var data = _cloudMonitor.GetServerAlertAll(search); return Content(Strings.ObjectToJson(data), "application/json"); } /// /// 根据条件获取服务器预警 /// /// [HttpPost] public ActionResult GetServerAlertBySearch(ServerAlertSearch search) { if (string.IsNullOrEmpty(search.QDID)) { search.QDID = GetAuthDats().Channel; } var data = _cloudMonitor.GetServerAlertBySearch(search); return Content(Strings.ObjectToJson(data), "application/json"); } /// /// 插入服务器预警处理 /// /// /// public ActionResult InsertServerAlertProcess(ServerAlertProcessModel model) { var data = _cloudMonitor.InsertServerAlertProcess(model); return Content(data ? "1" : "0", "text/plain"); } /// /// 查询服务器预警处理过程 /// /// /// public ActionResult GetServerAlertProcessByAlertId(string alertId) { var data = _cloudMonitor.GetServerAlertProcessByAlertId(alertId); return Content(Strings.ObjectToJson(data), "application/json"); } /// /// 获取全部项目监控数据 /// /// public ActionResult GetProjectMonitorAll() { var data = _cloudMonitor.GetProjectMonitorAll(); return Content(Strings.ObjectToJson(data), "application/json"); } /// /// 根据查询获取全部项目监控 /// /// [HttpPost] public ActionResult GetProjectMonitorBySearch(ServerAlertSearch search) { if (string.IsNullOrEmpty(search.QDID)) { search.QDID = GetAuthDats().Channel; } var data = _cloudMonitor.GetProjectMonitorBySearch(search); return Content(Strings.ObjectToJson(data), "application/json"); } /// /// 停止项目监控 /// /// public ActionResult StopProjectMonitor(string projectId) { var data = _cloudMonitor.StartOrStopProjectMonitor(projectId, false); return Content(data ? "1" : "0", "text/plain"); } /// /// 插入数据库数据 /// /// /// public ActionResult InsertDbInfo(DbInfoModel model) { var data = _cloudMonitor.InsertDbInfo(model); return Content(data ? "1" : "0", "text/plain"); } /// /// 修改数据库数据 /// /// /// public ActionResult UpdateDbInfo(DbInfoModel model) { var data = _cloudMonitor.UpdateDbInfo(model); return Content(data ? "1" : "0", "text/plain"); } /// /// 获取数据库数据,根据服务器ID过滤 /// /// /// public ActionResult GetDbInfoByServerId(string serverId) { var data = _cloudMonitor.GetDbInfoByServerId(serverId); return Content(Strings.ObjectToJson(data), "application/json"); } /// /// 获取数据库数据,根据ID过滤 /// /// /// public ActionResult GetDbInfoById(string id) { var data = _cloudMonitor.GetDbInfoById(id); return Content(Strings.ObjectToJson(data), "application/json"); } /// /// 删除数据库数据,根据ID /// /// /// public ActionResult DeleteDbInfoById(string id) { var data = _cloudMonitor.DeleteDbInfoById(id); return Content(data ? "1" : "0", "text/plain"); } /// /// 查询上传指标数据 /// /// [HttpPost] public ActionResult GetPushIndexData(IndexSearch search) { if (string.IsNullOrEmpty(search.QDID)) { search.QDID = GetAuthDats().Channel; } var data = _cloudMonitor.GetPushIndexData(search); return Content(Strings.ObjectToJson(data), "application/json"); } } /// /// 云监控对外部开放的接口 /// [Encryption()] public class CloudMonitorExternalController : BaseController { ICloudMonitor _cloudMonitor = InterfaceFactory.CreateBusinessInstance(); //推送消息 IMessageManage notice = InterfaceFactory.CreateBusinessInstance(); public static string IsCollection = ConfigurationManager.AppSettings["IsCollection"].ToStringEx(); //执行登录 [HttpPost] [CheckLogin(false)] public ActionResult CheckLogin(DecryInput input) { string account = input.Value.account; string pwd = input.Value.pwd; string vcode = input.Value.vcode; var code = this.Session["vcode"]; var json = new PmsJsonResoult(System.Net.HttpStatusCode.OK, "登录成功", null); if (code == null || code.ToString().ToLower() != vcode.ToLower()) {//验证码不正确 json.msg = "验证码不正确,请重新录入"; json.code = System.Net.HttpStatusCode.PreconditionFailed; return Content(json.ToString(), "application/json"); } //清空验证码 this.Session["vcode"] = null; //读取IP var ip = QWPlatform.SystemLibrary.Utils.Strings.GetWebClientIP(); //到数据库中验证是否正确 var r = account_obj.Login(account, pwd, ip); if (r.Success) {//登录成功 return Content(new PmsJsonResoult(System.Net.HttpStatusCode.OK, r.Message, null).ToString(), "application/json"); } else {//登录失败 return Content(new PmsJsonResoult(System.Net.HttpStatusCode.Forbidden, r.Message, null).ToString(), "application/json"); } } /// /// 获取用户信息 /// /// public ActionResult GetUserInfo() { var data = GetCurrentUser(); var userInfo = new UserInfo(); userInfo.ID = data.ID; userInfo.Account = data.Account; userInfo.Name = data.Name; userInfo.Email = data.Email; userInfo.Company = data.Company; userInfo.CompanyID = data.CompanyID; userInfo.PersonJob = data.PersonJob; return Content(Strings.ObjectToJson(userInfo), "application/json"); } /// /// 获取项目监控状态 /// /// public ActionResult GetItemList() { var data = _cloudMonitor.GetProjectMonitorByChannelId(GetAuthDats().Channel); return Content(Strings.ObjectToJson(data), "application/json"); } /// /// 根据项目id获取服务器信息 /// /// /// public ActionResult GetServerByItemId(string itemid) { var data = _cloudMonitor.GetServerByItemId(itemid); return Content(Strings.ObjectToJson(data), "application/json"); } /// /// 获取采集配置 /// /// public ActionResult GetCollectionConfig() { var cloudMonitorConfig = _cloudMonitor.GetCloudMonitorConfig(); var config = cloudMonitorConfig?.CollectionConfig; return Content(Strings.ObjectToJson(config), "application/json"); } /// /// 根据项目id获取 启动的自定义预警配置 /// /// /// public ActionResult GetCustomAlertModelByItemIdAndStart(string itemid) { var data = _cloudMonitor.GetCustomAlertModelByItemIdAndStart(itemid); return Content(Strings.ObjectToJson(data), "application/json"); } /// /// 根据项目id获取 启动的API预警配置 /// /// /// public ActionResult GetCustomServerModelByItemIdAndStart(string itemid) { var data = _cloudMonitor.GetCustomServerModelByItemIdAndStart(itemid); return Content(Strings.ObjectToJson(data), "application/json"); } /// /// 启动或者停止项目监控 /// /// public ActionResult StartOrStopProjectMonitor(string projectId, bool state, string clientId) { var data = _cloudMonitor.StartOrStopProjectMonitor(projectId, state, clientId); return Content(data ? "1" : "0", "text/plain"); } #region 接收处理上报数据 /// /// 接收上报的服务器信息 /// /// /// [CheckLogin(false)] public ActionResult ReceiveServerInfo(string clientId, DecryInput>> input) { if(IsCollection=="0") { return Content("1"); } List> serverInfos = input.Value; _cloudMonitor.ReceiveServerInfo(clientId, serverInfos); return Content("1"); } /// /// 接收上报的数据库连接数信息 /// /// /// [CheckLogin(false)] public ActionResult ReceiveDbConnectInfo(string clientId, DecryInput>> input) { if (IsCollection == "0") { return Content("1"); } List> dbConnectInfos = input.Value; _cloudMonitor.ReceiveDbConnectInfo(clientId, dbConnectInfos); return Content("1"); } /// /// 接收上报的数据库死锁信息 /// /// /// [CheckLogin(false)] public ActionResult ReceiveDbLockedInfo(string clientId, DecryInput>> input) { if (IsCollection == "0") { return Content("1"); } List> dbLockedInfos = input.Value; _cloudMonitor.ReceiveDbLockedInfo(clientId, dbLockedInfos); return Content("1"); } /// /// 接收上报的服务发现信息 /// /// [CheckLogin(false)] public ActionResult ReceiveServiceWorkStateInfo(string clientId, DecryInput>> input) { if (IsCollection == "0") { return Content("1"); } List> serviceWorkStateInfos = input.Value; _cloudMonitor.ReceiveServiceWorkStateInfo(clientId, serviceWorkStateInfos); return Content("1"); } /// /// 接收上报的自定义预警信息 /// /// [CheckLogin(false)] public ActionResult ReceiveDbCustomAlertInfo(string clientId, DecryInput> input) { if (IsCollection == "0") { return Content("1"); } Ret info = input.Value; _cloudMonitor.ReceiveDbCustomAlertInfo(clientId, info); return Content("1"); } /// /// 接收上报的自定义服务信息 /// /// [CheckLogin(false)] public ActionResult ReceiveCustomServiceInfo(string clientId, DecryInput> input) { if (IsCollection == "0") { return Content("1"); } Ret info = input.Value; _cloudMonitor.ReceiveCustomServiceInfo(clientId, info); return Content("1"); } #endregion } }