Custom pattern examples

Still need help?

The Atlassian Community is here for you.

Ask the community

Example: SNMP Extended Values

It's possible to extend the Information of an SNMP Device.

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.

The following example is sending 3 OID's to discover different RAM Information.

For versions prior to 3.0, replace <PatternType>Device</PatternType> with <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>

Example: Application Product Key

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

In the following example the pattern is reading a file "lic" that contains the License-Key for the Linux-Application "apt".

<?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>

Example: Application Extended Information

It's possible to extend the Information of any object type. In the following example, we extend the host info with some extended information.

<?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>
Last modified on Sep 23, 2022

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.