//復旧トラップ public int recoverexecute(Class_keepResult res_cls) { TrapAgent agent = new TrapAgent(); string oid_str = ""; // Variable Binding collection to send with the trap VbCollection col = new VbCollection(); oid_str = oid; //oidが取得できない if (oid_str == "" || oid_str == null) { return(-1); } //varbind col.Add(new Oid(oid_str + ".1"), new Integer32(0)); // Send the trap to the localhost port 162 //1.3.6.1.2.1.1.2.0 agent.SendV1Trap(new IpAddress(trapServer), recieverport, communityname, new Oid(oid_str), new IpAddress(res_cls.address), SnmpConstants.EnterpriseSpecific, 10, 0, col); //正常時 return(1); }
private Class_keepResult argumentValues; //Form1から受け取った引数 public Form_detail(Class_keepResult arguments) { //Form_mainから受け取ったデータをForm_detailのインスタンスのメンバに格納 this.argumentValues = arguments; InitializeComponent(); }
static public string ShowMiniForm(Class_keepResult argments) { Form_detail f = new Form_detail(argments); f.ShowDialog(); string receiveText = f.ReturnValue; f.Dispose(); return(receiveText); }
//詳細画面を表示する private void listView1_MouseDoubleClick(object sender, MouseEventArgs e) { if (listView1.SelectedItems.Count == 0) { return; } //詳細画面表示 ListViewItem itemx = new ListViewItem(); Class_keepResult res_cls = new Class_keepResult(); itemx = listView1.SelectedItems[0]; var invoice = int.Parse(itemx.Name); // キーの存在チェック if (classList.ContainsKey(invoice)) { res_cls = classList[invoice]; } String ss = Form_detail.ShowMiniForm(res_cls); }
//パラメータのアドレスに対して、PINGを実行する。 public void ExecPing(Class_keepResult res_cls) { res_cls.newDt = DateTime.Now; //Pingオブジェクトの作成 System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping(); //IPaddressにPingを送信する 5秒固定 //デフォルトのバッファ・サイズは32bytes System.Net.NetworkInformation.PingReply reply = p.Send(res_cls.address, 5000); //失敗時リトライ //if(reply.Status != System.Net.NetworkInformation.IPStatus.Success) // reply = p.Send(res_cls.address, 5000); //結果を取得 //成功時 if (reply.Status == System.Net.NetworkInformation.IPStatus.Success) { //Console.WriteLine("Reply from {0}:bytes={1} time={2}ms TTL={3}", // reply.Address, reply.Buffer.Length, // reply.RoundtripTime, reply.Options.Ttl); //前回成功時 if (res_cls.status == 1) { //復旧カウントをプラス if (res_cls.recoverCount > 0) { res_cls.recoverCount++; } res_cls.changeflg = 0; } // 前回障害 (復旧) else if (res_cls.status == -1) { // 復旧時 logger.Warn(res_cls.address + ": Ping応答復旧。" + reply.Status); res_cls.status = 1; //カウントを0に戻す res_cls.count = 0; //メッセージを消す res_cls.message = ""; //復旧カウントを開始 res_cls.recoverCount = 1; res_cls.changeflg = 1; } //初回 else { res_cls.status = 1; res_cls.changeflg = 0; } } else { // 失敗時 logger.Warn(res_cls.address + ": Ping応答なし。" + reply.Status); res_cls.message = reply.Status.ToString(); // 前回成功時 if (res_cls.status == 1) { res_cls.lastDt = DateTime.Now; //カウントを開始 res_cls.count = 1; res_cls.status = -1; //復旧カウントを0 res_cls.recoverCount = 0; res_cls.changeflg = 1; } // 障害 else if (res_cls.status == -1) { //カウント追加(カウントが振り切れたら1に戻す) res_cls.count = (res_cls.count == long.MaxValue) ? 1 : res_cls.count + 1; res_cls.changeflg = 0; } // 初回 else { res_cls.lastDt = DateTime.Now; res_cls.status = -1; //カウント追加 カウントが振り切れたら1に戻す res_cls.count = (res_cls.count == long.MaxValue) ? 1 : res_cls.count + 1; res_cls.changeflg = 0; } } p.Dispose(); }
//取得した値を一覧に反映 private void Displist(Class_keepResult res_cls, int newFlg) { //リスト項目の設定 ListViewItem itemx = new ListViewItem(); listView1.BeginUpdate(); //すでに存在する場合は更新 if (newFlg == 0) { foreach (ListViewItem item in listView1.Items) { var invoice = int.Parse(item.Name); if (invoice == res_cls.id) { //結果、カウント、最新を変更する if (res_cls.status == 1) { //正常 item.ImageIndex = 0; } else { //不通 item.ImageIndex = 1; } //カウント item.SubItems[1].Text = res_cls.count.ToString(); //直近の監視日時 DateTime dateDefault = new DateTime(); string str_date; if (res_cls.newDt.CompareTo(dateDefault) == 0) { str_date = ""; } else { str_date = res_cls.newDt.ToString(); } item.SubItems[4].Text = str_date; //障害日時 if (res_cls.lastDt.CompareTo(dateDefault) == 0) { str_date = ""; } else { str_date = res_cls.lastDt.ToString(); } item.SubItems[5].Text = str_date; //変化があった場合色を変える if (res_cls.changeflg == 1) { item.BackColor = Color.Yellow; } else { item.BackColor = SystemColors.Window; } break; } } } //新規挿入のとき else if (newFlg == 1) { if (res_cls.status == 1) { itemx.ImageIndex = 0; } else { itemx.ImageIndex = 1; } itemx.Name = res_cls.id.ToString(); itemx.SubItems.Add(res_cls.count.ToString()); itemx.SubItems.Add(res_cls.address + " "); itemx.SubItems.Add(res_cls.startDt.ToString()); itemx.SubItems.Add(res_cls.newDt.ToString()); DateTime dateDefault = new DateTime(); string str_date; if (res_cls.lastDt.CompareTo(dateDefault) == 0) { str_date = ""; } else { str_date = res_cls.lastDt.ToString(); } itemx.SubItems.Add(str_date); //アイテムをリスビューに追加する listView1.Items.Add(itemx); int count = listView1.Items.Count; if (count > 0) { //listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent); listView1.EnsureVisible(count - 1); } } listView1.EndUpdate(); }
//非同期でPINGを実行する。 private Task <string> pingexe(ArrayList tmpArray, Dictionary <int, Class_keepResult> classList, CancellationToken token) { try { Class_keepResult res_cls; Class_Ping cls_ping = new Class_Ping(); for (int i = 0; i < tmpArray.Count; i++) { int newFlg = 1; // キーの存在チェック if (classList.ContainsKey(i + 1)) { newFlg = 0; //すでにある場合 res_cls = classList[i + 1]; } else { // 存在しない場合 newFlg = 1; res_cls = new Class_keepResult(); res_cls.startDt = DateTime.Now; //結果クラス res_cls.id = i + 1; res_cls.address = (string)tmpArray[i]; res_cls.status = 0; } classList[i + 1] = res_cls; //PING実行 cls_ping.ExecPing(res_cls); TaskBarLabel1.Text = ""; classList[i + 1] = res_cls; //キャンセルが押されたとき token.ThrowIfCancellationRequested(); if (iniData.snmpTrapCheck.IndexOf("yes", StringComparison.OrdinalIgnoreCase) > -1) { //エラー回数が設定数を超えたらメッセージを出す if (res_cls.count > 0) { //int ccount = (int)res_cls.count % iniData.MessageCount; if (iniData.MessageCount == res_cls.count) { Class_sendTrap sendTrap = new Class_sendTrap(); sendTrap.trapServer = iniData.recieveServer; sendTrap.communityname = iniData.community; sendTrap.recieverport = int.Parse(iniData.port); sendTrap.oid = iniData.objectID; int ret = sendTrap.execute(res_cls); //正常 if (ret == 1) { logger.Info("SNMP障害トラップを送信しました。"); TaskBarLabel1.Text = "SNMP障害トラップを送信しました。"; } //エラー else if (ret == -1) { logger.Warn("SNMP障害トラップ送信に失敗しました。OIDが取得できませんでした。"); TaskBarLabel1.Text = "SNMP障害トラップ送信に失敗しました。OIDが取得できませんでした。"; } } } //復旧カウントが設定数を超えていたらトラップを送信 if (res_cls.recoverCount > 0) { int ccount = (int)res_cls.recoverCount % iniData.recoverMsgCount; if (ccount == 0) { Class_sendTrap sendTrap = new Class_sendTrap(); sendTrap.trapServer = iniData.recieveServer; sendTrap.communityname = iniData.community; sendTrap.recieverport = int.Parse(iniData.port); sendTrap.oid = iniData.objectID; int ret = sendTrap.recoverexecute(res_cls); //正常 if (ret == 1) { logger.Info("SNMP復旧トラップを送信しました。"); TaskBarLabel1.Text = "SNMP復旧トラップを送信しました。"; res_cls.recoverCount = 0; } //エラー else if (ret == -1) { logger.Warn("SNMP復旧トラップ送信に失敗しました。OIDが取得できませんでした。"); TaskBarLabel1.Text = "SNMP復旧トラップ送信に失敗しました。OIDが取得できませんでした。"; } } } } //一覧に表示 Invoke(new Listdelegate(Displist), new object[] { res_cls, newFlg }); } } catch (OperationCanceledException) { //キャンセル MessageBox.Show("停止しました。", "pingTool", MessageBoxButtons.OK, MessageBoxIcon.Warning); } catch (Exception ex) { MessageBox.Show("例外が発生しました。" + " pingexe " + ex.Message); logger.Fatal("例外が発生しました。" + " pingexe " + ex.Message); } return(Task.Delay(0) .ContinueWith(t => "Hello")); }