// POST api/<controller> public HttpResponseMessage Post([FromBody] ReportData reportData) { string method = "ReportController.Post"; SCTracer.Info(method, string.Format("Start. {0}, {1}", reportData.ReporterName, reportData.ReporterHostName)); bool hasHandle = false; try { // 送信されたデータでreport.jsonを更新 string reportPath = HostingEnvironment.MapPath(Common.JSON_FILE_VPATH); Common.CollectDataList data; // ファイル操作のためにミューテックス取る (※ファイルの排他取るだけでもいいのでは?) hasHandle = mutex.WaitOne(); if (File.Exists(reportPath)) { data = JsonConvert.DeserializeObject <Common.CollectDataList>(File.ReadAllText(reportPath)); } else { SCTracer.Info(method, "Not found ." + reportPath); data = new Common.CollectDataList(); } // 古いデータ(ホスト名が一致)があれば更新、なければ新規追加 int oldDataIndex = data.DataList.FindIndex(x => x.Data.ReporterHostName == reportData.ReporterHostName); if (oldDataIndex < 0) { SCTracer.Info(method, "Add new information."); data.DataList.Add(new Common.CollectData() { Data = reportData }); } else { SCTracer.Info(method, "Update old information."); data.DataList[oldDataIndex].UpdateReportData(reportData); } File.WriteAllText(reportPath, JsonConvert.SerializeObject(data)); SCTracer.Info(method, "End."); return(new HttpResponseMessage(HttpStatusCode.OK)); } catch (Exception e) { SCTracer.Error(method, "Unexpected Error Occurred."); SCTracer.Exception(method, e); throw; } finally { if (hasHandle) { mutex.ReleaseMutex(); } } }
static int Main(string[] args) { string method = "Reporter.Main"; SCTracer.Info(method, "Start."); try { // 送信データ1: Reporter実行マシンの情報: configファイルから設定を読み取る string reporterName = System.Configuration.ConfigurationManager.AppSettings["ReporterName"]; string destinationURL = System.Configuration.ConfigurationManager.AppSettings["DestinationURL"]; ReportData reportData = new ReportData(); reportData.ReporterName = reporterName; reportData.ReporterHostName = System.Net.Dns.GetHostName(); // 送信データ2: ログインセッション情報 List <ReportData.LoginSession> sessions; ReportResult result = GetLoginSessions(out sessions); if (result != ReportResult.Success) { SCTracer.Error(method, "GetLoginSessions failed. result = " + result); return((int)result); } reportData.Sessions = sessions; // 送信データ3:インストール済みソフトウェアを取得 string checkSoftwareNames = System.Configuration.ConfigurationManager.AppSettings["CheckSoftwareNames"]; reportData.Softwares = GetInstalledSoftwares(checkSoftwareNames.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)); // 送信データをJsonにシリアライズし、送信する string json = JsonConvert.SerializeObject(reportData); try { ReportJson(json, destinationURL).Wait(); } catch (Exception e) { SCTracer.Error(method, "Reporting Failed. Need Retry Again. return " + ReportResult.CommunicateServerFailed); SCTracer.Exception(method, e); return((int)ReportResult.CommunicateServerFailed); } } catch (Exception e) { SCTracer.Error(method, "Unexpected Error Occured. return " + ReportResult.UnexpectedError); SCTracer.Exception(method, e); return((int)ReportResult.UnexpectedError); } SCTracer.Info(method, "End."); return((int)ReportResult.Success); }
// GET api/<controller> public HttpResponseMessage Get() { string method = "ReportController.Get"; SCTracer.Info(method, "Start."); bool hasHandle = false; try { string reportPath = HostingEnvironment.MapPath(Common.JSON_FILE_VPATH); string data; hasHandle = mutex.WaitOne(); if (File.Exists(reportPath)) { data = File.ReadAllText(reportPath); } else { data = JsonConvert.SerializeObject(new Common.CollectDataList()); } mutex.ReleaseMutex(); hasHandle = false; HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK); response.Content = new StringContent(data, System.Text.Encoding.UTF8, @"application/json"); SCTracer.Info(method, "End."); return(response); } catch (Exception e) { SCTracer.Error(method, "Unexpected Error Occurred."); SCTracer.Exception(method, e); throw; } finally { if (hasHandle) { mutex.ReleaseMutex(); } } }
static string HostToIPAddress(string hostname) { string method = "Reporter.HostToIPAddress"; SCTracer.Info(method, "Start. hostname = " + hostname); try { // Consoleログインで、ホスト名が空文字とかだと、ローカルマシンのIPを返してしまう。 if (string.IsNullOrWhiteSpace(hostname)) { SCTracer.Info(method, "End. Skip blank hostname."); return(string.Empty); } IPHostEntry ipInfo = Dns.GetHostEntry(hostname); if (ipInfo.AddressList != null && ipInfo.AddressList.Length > 0) { IPAddress result = ipInfo.AddressList.FirstOrDefault(x => x.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork); if (result != null) { SCTracer.Warn(method, "End. result: " + result.ToString()); return(result.ToString()); } else { SCTracer.Warn(method, "Not found IPv4 Address.."); } } else { SCTracer.Warn(method, "Not found AddressList."); } return(string.Empty); } catch (Exception e) { SCTracer.Exception(method, e); return(string.Empty); } }
public MonitorModel(string filePath) { string method = "MonitorModel.MonitorModel"; bool hasHandle = false; string value = System.Configuration.ConfigurationManager.AppSettings["UnknownStatusThresholdMinutes"]; if (!int.TryParse(value, out unknownStatusThresholdMinutes)) { SCTracer.Error(method, "UnknownStatusThresholdMinutes: failed to parse " + value + " to int."); } value = System.Configuration.ConfigurationManager.AppSettings["MonitorSoftwareNames"]; monitorSoftwareNames = value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList(); for (int i = 0; i < monitorSoftwareNames.Count; i++) { monitorSoftwareNames[i] = monitorSoftwareNames[i].Trim(); } try { hasHandle = mutex.WaitOne(); if (File.Exists(filePath)) { data = JsonConvert.DeserializeObject <Common.CollectDataList>(File.ReadAllText(filePath)); } } catch (Exception e) { SCTracer.Exception(method, e); throw; } finally { if (hasHandle) { mutex.ReleaseMutex(); } } }