カスタム パターンの例

お困りですか?

アトラシアン コミュニティをご利用ください。

コミュニティに質問

例: SNMP 拡張値

SNMP デバイスの情報は拡張できます。

You need to know which OID-Number returning the Value that should be discovered. In the processing script, the return value of the OID will be mapped to an ExtendedInformation.

次の例は、異なる RAM 情報を検出するために 3 つの OID を送信しています。

3.0 より前のバージョンの場合は、<PatternType>Device</PatternType> <PatternType>SNMPExtendedValues</PatternType> に置き換えてください。

<?xml version="1.0" encoding="utf-8"?>
<ScanPattern xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Version>3.0.1</Version>
<PatternID>RIADA-Cust-SNMP-1</PatternID>
<OrderNr>0</OrderNr>
<ProcessType>SNMP_GET</ProcessType>
<PatternType>Device</PatternType>
<Command>
    <![CDATA[
    1.3.6.1.4.1.2021.4.5.0;1.3.6.1.4.1.2021.4.6.0;1.3.6.1.4.1.2021.4.11.0
    ]]>
</Command>
<Processing>
    <![CDATA[
    using System;
    using System.Collections.Generic;
    using Insight.Discovery.InfoClasses.CommandResult.ResultTypes;
    using Insight.Discovery.Tools;
    using Insight.Discovery.InfoClasses;
    using Insight.Discovery.InfoClasses.CommandResult;
    
    namespace Insight.Discovery {
      public class PatternExec {
        public void PerformAction(object[] parameters)
        {
            DeviceInfo deviceInfo = (DeviceInfo)parameters[2];
            
            if (deviceInfo.ExtendedInformations.IsNullOrEmpty())
                    deviceInfo.ExtendedInformations = new List<ExtendedInformation>();

            try
            {
                var commandResult = (SNMPExecuteResult)parameters[0];
                commandResult.LogResult();

                foreach (KeyValuePair<string, object> item in commandResult)
                {
                    switch (item.Key)
                    {
                        case "1.3.6.1.4.1.2021.4.5.0": // OID to get available RAM
                            if (item.Value != null)
                            {
                                try
                                {
                                    long t = 0;
                                    long.TryParse(item.Value.ToString().Replace("\n", "").Trim(), out t);

                                    if (t > 0)
                                    {
                                        deviceInfo.ExtendedInformations.Add(new ExtendedInformation() { Name = "RAM Total", Value = (t / 1024).ToString() });
                                    }
                                }
                                catch
                                {
                                    //
                                }
                            }

                            break;
                        case "1.3.6.1.4.1.2021.4.6.0": // OID to get used RAM
                            if (item.Value != null)
                            {
                                try
                                {
                                    long t = 0;
                                    long.TryParse(item.Value.ToString().Replace("\n", "").Trim(), out t);

                                    if (t > 0)
                                    {
                                        deviceInfo.ExtendedInformations.Add(new ExtendedInformation()
                                        { Name = "RAM Used", Value = (t / 1024).ToString() });
                                    }
                                }
                                catch
                                {
                                    //
                                }
                            }
                            break;
                        case "1.3.6.1.4.1.2021.4.11.0": // OID to get free RAM
                            if (item.Value != null)
                            {
                                try
                                {
                                    long t = 0;
                                    long.TryParse(item.Value.ToString().Replace("\n", "").Trim(), out t);

                                    if (t > 0)
                                    {
                                        deviceInfo.ExtendedInformations.Add(new ExtendedInformation() { Name = "RAM Free", Value = (t / 1024).ToString() });
                                    }
                                }
                                catch
                                {
                                    //
                                }
                            }
                            break;
                    }
                }
            }
            catch (Exception ex)
            { LogService.Instance.LogDebug("Error getting Extended SNMP RAM Information.", ex); }
        }
  
      }
    }  
    ]]>
  </Processing>
</ScanPattern>

例: アプリ プロダクト キー

It's possible to add a discovered License-Information to an Application. For example, if you know that a License File exists, you can add a Pattern that reads this file.

次の例では、パターンは Linux アプリ「apt」のライセンス キーを含むファイル「lic」を読み取っています。

<?xml version="1.0" encoding="utf-8"?>
<!-- © Mindville -->
<ScanPattern xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Version>1.0.1</Version>
<PatternID>Mindville-Cust-Linux-AppPK-1</PatternID>
<OrderNr>0</OrderNr>
<ProcessType>SSHExecute</ProcessType>
<PatternType>ApplicationProductKey</PatternType>
<ApplicationName>apt</ApplicationName>
<Command>
    <![CDATA[
    cat /etc/apt/lic
    ]]>
</Command>
<Processing>
    <![CDATA[
    using System;
    using Insight.Discovery.InfoClasses;
    using Insight.Discovery.Tools;
    using Insight.Discovery.InfoClasses.CommandResult.ResultTypes;
    using System.Collections.Generic;
    
    namespace Insight.Discovery {
      public class PatternExec {        
          public void PerformAction(object[] parameters)
        {
            HostInfo hostInfo = (HostInfo) parameters[2];

            try
            {
                SSHExecuteResult sshExecuteResult = (SSHExecuteResult)parameters[0];
                sshExecuteResult.LogResult();

                string input = sshExecuteResult;

                if (input != string.Empty)
                {
                    if(hostInfo.OS.License == null)hostInfo.OS.License = new LicenseInfo();

                    hostInfo.OS.License.LicenseKey = input.Trim();
                }
            }
            catch (Exception ex)
            { LogService.Instance.LogDebug("Error getting apt product key Information", ex); }
            
        }
      }
    }
    ]]>
  </Processing>
</ScanPattern>

例: アプリ拡張情報

任意のオブジェクト タイプの情報を拡張できます。次の例では、ホスト情報をいくつかの拡張情報で拡張しています。

<?xml version="1.0" encoding="utf-8"?>
<ScanPattern xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Version>1.0.1</Version>
<PatternID>Mindville-Cust-Linux-OpenPorts-1</PatternID>
<OrderNr>700</OrderNr>
<ProcessType>SSHExecute</ProcessType>
<PatternType>Host</PatternType>
<Command>
    <![CDATA[
    netstat -an
    ]]>
</Command>
<Processing>
    <![CDATA[
    using System;
    using System.Collections.Generic;
    using Insight.Discovery.Tools;
    using Insight.Discovery.InfoClasses;
    using Insight.Discovery.InfoClasses.CommandResult.ResultTypes;
    using Insight.Discovery.Tools.Networking;
    
    namespace Insight.Discovery {
      public class PatternExec {
        public void PerformAction(object[] parameters)
        {
            HostInfo hostInfo = (HostInfo)parameters[2];
            try
            {
                SSHExecuteResult sshExecuteResult = (SSHExecuteResult)parameters[0];
                sshExecuteResult.LogResult();

                string input = sshExecuteResult;

                if (input != string.Empty)
                {
                    if (hostInfo != null)
                    {
                        string[] lines = input.Split('\n');
                        ExtendedInformation tcpPortInfo = new ExtendedInformation() { Name = "TCP Ports", Value = string.Empty };
                        ExtendedInformation udpPortInfo = new ExtendedInformation() { Name = "UDP Ports", Value = string.Empty };

                        for (int i = 0; i < lines.Length; i++)
                        {
                            if (!string.IsNullOrEmpty(lines[i]) && lines[i].Contains(":")
                                && (lines[i].ToLower().StartsWith("tcp") || lines[i].ToLower().StartsWith("udp")))
                            {
                                string[] parts = lines[i].TrimReduce().Split(' ');

                                try
                                {
                                    for (int x = 0; x < parts.Length; x++)
                                    {
                                        if (parts[x].Contains("]"))
                                        {
                                            parts[x] = parts[1].Substring(parts[x].IndexOf("]"));
                                        }

                                        if (parts[0].ToLower().StartsWith("tcp") && !string.IsNullOrEmpty(parts[x]) &&
                                            parts[x].Contains(":"))
                                        {
                                            if (!tcpPortInfo.Value.Contains(parts[x].Split(':')[1]))
                                            {
                                                tcpPortInfo.Value += parts[x].Split(':')[1] + ",";
                                                break;
                                            }
                                        }

                                        if (parts[0].ToLower().StartsWith("udp") && !string.IsNullOrEmpty(parts[x]) &&
                                            parts[x].Contains(":"))
                                        {
                                            if (!udpPortInfo.Value.Contains(parts[x].Split(':')[1]))
                                            {
                                                udpPortInfo.Value += parts[x].Split(':')[1] + ",";
                                                break;
                                            }
                                        }
                                    }
                                }
                                catch
                                {
                                    //
                                }
                            }
                        }

                        if (hostInfo.ExtendedInformations.IsNullOrEmpty())
                            hostInfo.ExtendedInformations = new List<ExtendedInformation>();

                        if (!string.IsNullOrEmpty(tcpPortInfo.Value) && tcpPortInfo.Value.EndsWith(","))
                        {
                            tcpPortInfo.Value = tcpPortInfo.Value.Substring(0, tcpPortInfo.Value.Length - 1);
                            hostInfo.ExtendedInformations.Add(tcpPortInfo);
                        }
                        if (!string.IsNullOrEmpty(udpPortInfo.Value) && udpPortInfo.Value.EndsWith(","))
                        {
                            udpPortInfo.Value = udpPortInfo.Value.Substring(0, udpPortInfo.Value.Length - 1);
                            hostInfo.ExtendedInformations.Add(udpPortInfo);
                        }
                    }
                }
            }
            catch (Exception ex)
            { LogService.Instance.LogDebug("Error getting ReferencedHosts Information", ex); }

          }
        }
    }
    ]]>
  </Processing>
</ScanPattern>
最終更新日 2024 年 5 月 1 日

この内容はお役に立ちましたか?

はい
いいえ
この記事についてのフィードバックを送信する
Powered by Confluence and Scroll Viewport.