// 現在 Cacti に登録されているホストリストの取得 public async Task <List <CactiHost> > GetCurrentRegisteredHostListAsync(CancellationToken cancel = default) { string url = GenerateUrl("host.php"); var ret1 = await this.Http.SimpleQueryAsync(WebMethods.GET, url, cancel, null); string body = ret1.ToString(); var html = body._ParseHtml(); var table = html.ParseTable("/html[1]/body[1]/table[1]/tr[3]/td[2]/div[1]/form[1]/table[1]/tr[1]/td[1]/table[1]", new HtmlTableParseOption(skipHeaderRowCount: 1)); List <CactiHost> ret = new List <CactiHost>(); foreach (var data in table.DataList) { CactiHost h = new CactiHost { Description = data["Description**"].SimpleText, Id = data["ID"].SimpleText._ToInt(), Hostname = data["Hostname"].SimpleText, }; ret.Add(h); } return(ret); }
// ホストの登録 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."); } }
// ホストがまだ登録されていなければ登録する 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); }
public static CactiTask LoadFromData(ReadOnlySpan <byte> data) { string body = data._GetString(); string[] lines = body._GetLines(true); int mode = 0; CactiTask ret = new CactiTask(); string graphName = ""; foreach (string line in lines) { if (line.StartsWith("#") == false && line.StartsWith("//") == false && line.StartsWith(";") == false) { if (mode == 0) { // URL 等 if (line._GetKeyAndValue(out string key, out string value)) { switch (key.ToUpperInvariant()) { case "URL": ret.BaseUrl = value; break; case "USERNAME": ret.Username = value; break; case "PASSWORD": ret.Password = value; break; } } } else if (mode == 1) { string line2 = line.Trim(); // ホスト一覧 (形式は description hostname) if (line2.StartsWith("[") == false) { if (line2._GetKeyAndValue(out string description, out string hostname)) { CactiHost h = new CactiHost { Description = description, Hostname = hostname, }; ret.Hosts.Add(h); } else if (line2._IsFilled()) { CactiHost h = new CactiHost { Description = line2, Hostname = line2, }; ret.Hosts.Add(h); } } } else if (mode == 2) { // 値一覧 if (line.StartsWith("[") == false) { if (line.StartsWith(" ") || line.StartsWith("\t")) { // 値名ワイルドカード if (graphName._IsFilled()) { CactiEnableItem item = new CactiEnableItem(graphName, line.Trim()); ret.Items.Add(item); } } else { // グラフ名 graphName = line.Trim(); } } } switch (line.ToUpperInvariant()) { case "[HOSTS]": mode = 1; break; case "[VALUES]": mode = 2; break; } } } return(ret); }