Exemplo n.º 1
0
    // 指定されたホスト用グラフを有効化する
    public async Task EnableGraphItemsAsync(int hostId, IEnumerable <CactiEnableItem> items, CancellationToken cancel = default)
    {
        // 動的更新
        try
        {
            await RefreshHostAllSnmpDynamicValuesAsync(hostId, cancel);
        }
        catch
        {
        }

        // グラフリストの取得
        CactiGraphList graphList = await GetHostGraphListAsync(hostId, cancel);

        List <string> enableItemIdList = new List <string>();

        foreach (var item in items)
        {
            if (item.GraphNameInStr._IsFilled())
            {
                var graphs = graphList.GraphList.Where(x => x.GraphTitle._InStr(item.GraphNameInStr));

                foreach (var graph in graphs)
                {
                    // あるグラフに関する処理
                    foreach (var graphItem in graph.Items)
                    {
                        if (graphItem.Value.Where(str => str._WildcardMatch(item.ValueNameWildcard, true)).Any())
                        {
                            enableItemIdList.Add(graphItem.Key);
                        }
                    }
                }
            }
        }

        string url = GenerateUrl($"graphs_new.php");

        List <(string name, string?value)> queryParams = new List <(string name, string?value)>();

        //queryParams.Add(("action", "save"));
        //queryParams.Add(("__csrf_magic", this.Magic));
        //queryParams.Add(("host_id", hostId.ToString()));
        //queryParams.Add(("save_component_graph", "1"));
        queryParams.Add(("sgg_1", "14"));
        queryParams.Add(("cg_g", "0"));

        graphList.HiddenValues._DoForEach(x => queryParams.Add((x.Key, x.Value)));

        enableItemIdList.Distinct()._DoForEach(x => queryParams.Add((x, "on")));

        var ret2 = await this.Http.SimpleQueryAsync(WebMethods.POST, url, cancel, null, queryParams.ToArray());

        string body = ret2.ToString();
    }
Exemplo n.º 2
0
    // ホストの SNMP 動的値をすべて再取得する
    public async Task RefreshHostAllSnmpDynamicValuesAsync(int hostId, CancellationToken cancel = default)
    {
        CactiGraphList g = await GetHostGraphListAsync(hostId, cancel);

        foreach (var t in g.GraphList)
        {
            if (t.RefreshUrl._IsFilled())
            {
                // Refresh URL を叩く
                try
                {
                    await Http.SimpleQueryAsync(WebMethods.GET, t.RefreshUrl, cancel);
                }
                catch
                {
                }
            }
        }
    }
Exemplo n.º 3
0
    // ホスト用のグラフリストを取得する
    public async Task <CactiGraphList> GetHostGraphListAsync(int hostId, CancellationToken cancel = default)
    {
        string url = GenerateUrl($"graphs_new.php?host_id={hostId}");

        var response = await Http.SimpleQueryAsync(WebMethods.GET, url, cancel);

        string body = response.ToString();

        List <string> alreadyCreatedIDs = new List <string>();

        // "var created_graphs = new Array()" 以降のスクリプトを Parse し、作成済みグラフ ID 一覧を取得
        string tag2 = "var created_graphs = new Array()";
        int    j    = body.IndexOf(tag2);

        if (j != -1)
        {
            string script = body.Substring(j + tag2.Length);

            int endOfScript = script.IndexOf("</script>");
            if (endOfScript != -1)
            {
                script = script.Substring(0, endOfScript);
            }

            while (true)
            {
                j = script.IndexOf('\'');
                if (j == -1)
                {
                    break;
                }

                script = script.Substring(j + 1);

                int k = script.IndexOf('\'');
                if (k != -1)
                {
                    string candidate = script.Substring(0, k);
                    if (candidate.Length == 32)
                    {
                        try
                        {
                            byte[] data = candidate._GetHexBytes();
                            if (data.Length == 16)
                            {
                                alreadyCreatedIDs.Add(candidate);
                            }
                        }
                        catch
                        {
                        }
                    }
                    else if (candidate.Length <= 10)
                    {
                        if (Str.IsNumber(candidate))
                        {
                            alreadyCreatedIDs.Add(candidate);
                        }
                    }
                }
            }
        }

        var html = body._ParseHtml();

        List <HtmlAgilityPack.HtmlNode> tables = new List <HtmlAgilityPack.HtmlNode>();

        var children = html.DocumentNode.GetAllChildren();

        CactiGraphList ret = new CactiGraphList();

        // hidden 値を追加
        var hiddenValues = children.Where(x => x.Name == "input" && x.Attributes["type"].Value == "hidden");

        hiddenValues._DoForEach(x => ret.HiddenValues.Add(x.Attributes["name"].Value, x.Attributes["value"].Value));

        // HTML 中からテーブル列挙
        for (int i = 0; i < 20; i++)
        {
            HtmlAgilityPack.HtmlNode node = html.DocumentNode.SelectSingleNode($"/html[1]/body[1]/table[1]/tr[3]/td[2]/div[1]/form[1]/table[{i}]");

            if (node != null)
            {
                tables.Add(node);
            }
        }

        // 列挙したテーブル (グラフごとに 1 テーブル) をパースして処理
        foreach (var node2 in tables)
        {
            var node = node2;

            HtmlParsedTableWithHeader?table = null;
            try
            {
                table = node.ParseTable(new HtmlTableParseOption(skipHeaderRowCount: 2));
            }
            catch
            {
                // "Graph Templates" のみ Table in Table である
                node = node.SelectSingleNode("tr").SelectSingleNode("td").SelectSingleNode("table");

                try
                {
                    table = node.ParseTable(new HtmlTableParseOption(skipHeaderRowCount: 1));
                }
                catch { }
            }

            if (table != null)
            {
                string title = table.TableNode.SelectNodes("tr").First().SelectNodes("td").First().GetSimpleText()._DecodeHtml();

                string refreshPath = "";
                try
                {
                    refreshPath = table.TableNode.SelectSingleNode("tr").SelectSingleNode("td").SelectSingleNode("table")
                                  .SelectSingleNode("tr").SelectNodes("td").ElementAt(1).SelectSingleNode("a").Attributes["href"].Value._DecodeHtml();
                }
                catch
                {
                }

                string refreshUrl = refreshPath._IsFilled() ? GenerateUrl(refreshPath) : "";

                int id = 0;

                if (refreshUrl._IsFilled())
                {
                    refreshUrl._ParseUrl(out _, out QueryStringList qs);

                    id = qs._GetStrFirst("id")._ToInt();
                }

                CactiGraph g = new CactiGraph
                {
                    GraphTitle = title,
                    GraphId    = id,
                    RefreshUrl = refreshUrl,
                };

                foreach (var row in table.DataList)
                {
                    List <string> strList = new List <string>();

                    foreach (var kv in row)
                    {
                        if (kv.Key._IsSamei("index") == false)
                        {
                            if (kv.Value.SimpleText._IsFilled())
                            {
                                string text = kv.Value.SimpleText;

                                string tag = "Create:";
                                if (text.StartsWith(tag))
                                {
                                    text = text.Substring(tag.Length);
                                }
                                strList.Add(text.Trim());
                            }
                        }
                    }

                    string checkBoxId = row.Last().Value.TdNode.SelectNodes("input").Where(x => x.Attributes["type"].Value == "checkbox").Single().Attributes["name"].Value;

                    if (checkBoxId._IsFilled())
                    {
                        if (alreadyCreatedIDs.Where(id => checkBoxId._InStr("_" + id, true)).Any() == false)
                        {
                            g.Items.Add(checkBoxId, strList);
                        }
                    }
                }

                ret.GraphList.Add(g);
            }
        }

        return(ret);
    }