Пример #1
0
    // ホストの登録
    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.");
        }
    }
Пример #2
0
    // ホストがまだ登録されていなければ登録する
    public async Task <int> RegisterOrGetHostIdAsync(CactiHost host, CancellationToken cancel = default)
    {
        int id = await FindHostByHostnameAsync(host.Description, cancel);

        if (id == 0)
        {
            await RegisterHostAsync(host, cancel);

            id = await FindHostByHostnameAsync(host.Description, cancel);

            if (id == 0)
            {
                throw new CoresException($"Failed to register host {host._ObjectToJson(compact: true)}.");
            }
        }

        return(id);
    }