async Task <double> PerformOneAsync(IPAddress ipAddress, int count, int timeout, int interval, CancellationToken cancel = default) { await LocalNet.SendPingAsync(ipAddress, timeout : timeout, pingCancel : cancel); int numOk = 0; for (int i = 0; i < count; i++) { cancel.ThrowIfCancellationRequested(); var result = await LocalNet.SendPingAsync(ipAddress, timeout : timeout, pingCancel : cancel); if (result.Ok) { numOk++; } if (interval >= 1) { await cancel._WaitUntilCanceledAsync(interval); } } return((double)(count - numOk) / (double)count); }
// ホストの登録 public async Task RegisterHostAsync(CactiHost h, CancellationToken cancel = default) { // 接続チェック bool pingOk = false; for (int i = 0; i < CactiConsts.PingRetry; i++) { var r = await LocalNet.SendPingAsync(h.Hostname, pingCancel : cancel, dnsCancel : cancel, pingTimeout : CactiConsts.PingTimeout, dnsTimeout : CactiConsts.DnsTimeout); if (r.Ok) { pingOk = true; break; } } if (pingOk == false) { throw new CoresException($"Ping to {h.Hostname} error."); } string url = GenerateUrl("host.php"); var ret2 = await this.Http.SimpleQueryAsync(WebMethods.POST, url, cancel, null, ("action", "save"), ("__csrf_magic", this.Magic), ("description", h.Description), ("hostname", h.Hostname), ("host_template_id", "9"), ("device_threads", "1"), ("availability_method", "2"), ("ping_method", "2"), ("ping_port", "23"), ("ping_timeout", "400"), ("ping_retries", "1"), ("snmp_version", "2"), ("snmp_community", "public"), ("snmp_username", "admin"), ("snmp_password", "pass"), ("snmp_auth_protocol", "MD5"), ("snmp_priv_protocol", "DES"), ("snmp_port", "161"), ("snmp_timeout", "500"), ("max_oids", "0"), ("notes", ""), ("id", "0"), ("_host_template_id", "0"), ("Create", "Create"), ("save_component_host", "1")); string body = ret2.ToString(); if (body._InStr("Save Successful") == false) { throw new CoresException($"Register host {h._ObjectToJson(compact: true)} failed."); } }
protected override async Task GetValueAsync(SortedDictionary <string, string> ret, RefInt nextPollingInterval, CancellationToken cancel = default) { SnmpWorkSettings settings = Host.Settings; if (settings.PingTargets._IsSamei("none") || settings.PingTargets._IsSamei("null")) { return; } string hopsStr = "Hops"; if (settings.HopsToTTL) { hopsStr = "TTL"; } string[] pingTargets = settings.PingTargets._Split(StringSplitOptions.RemoveEmptyEntries, ","); numPerform++; foreach (string pingTarget in pingTargets) { cancel.ThrowIfCancellationRequested(); ParseTargetString(pingTarget, out string hostname, out string alias); bool ok = false; try { IPAddress ipAddress = await LocalNet.GetIpAsync(hostname, cancel : cancel); if (FirstPing.IsFirstCall()) { // JIT 対策 try { await LocalNet.SendPingAsync(ipAddress, pingCancel : cancel); } catch { } } int numTry = 3; if (numPerform >= 2) { // SpeedTest が動作中の場合は SpeedTest が完了するまで待機する await TaskUtil.AwaitWithPollAsync(Timeout.Infinite, 10, () => !SpeedTestClient.IsInProgress, cancel); // 試行回数を指定する numTry = Math.Min(Math.Max(settings.PingNumTry, 1), 100); } SendPingReply reply = await LocalNet.SendPingAndGetBestResultAsync(ipAddress, pingCancel : cancel, numTry : numTry); if (reply.Ok) { double rtt = reply.RttDouble; rtt = Math.Min(rtt, 2.0); int ttl = reply.Ttl; bool ttl_ok = false; if (ttl == 0) { // Use ping command to get TTL try { var result = await EasyExec.ExecAsync(ipAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6?Consts.LinuxCommands.Ping6 : Consts.LinuxCommands.Ping, $"-W 1 -c 1 {ipAddress.ToString()}", cancel : cancel, throwOnErrorExitCode : false); string[] lines = result.OutputStr._GetLines(true); foreach (string line in lines) { OneLineParams param = new OneLineParams(line, ' ', false); string ttlStr = param._GetStrFirst("ttl"); if (ttlStr._IsFilled()) { ttl = ttlStr._ToInt(); ttl_ok = true; break; } } } catch (Exception ex) { ex._Debug(); } } else { ttl_ok = true; } ret.TryAdd($"Time - {alias}", (rtt * 1000.0).ToString("F3")); if (ttl > 128) { ttl -= 128; } else if (ttl > 64) { ttl -= 64; } int hops = 64 - ttl; if (ttl_ok == false) { hops = 0; } hops._SetMax(0); hops._SetMin(64); ret.TryAdd($"{hopsStr} - {alias}", hops.ToString()); ok = true; } } catch (Exception ex) { ex._Debug(); } if (ok == false) { ret.TryAdd($"Time - {alias}", ""); ret.TryAdd($"{hopsStr} - {alias}", "0"); } } }