美文网首页
第三十章 使用系统监视器 - 编写用户定义的应用程序监视器类

第三十章 使用系统监视器 - 编写用户定义的应用程序监视器类

作者: Cache技术分享 | 来源:发表于2022-12-31 08:19 被阅读0次

    第三十章 使用系统监视器 - 编写用户定义的应用程序监视器类

    编写用户定义的应用程序监视器类

    除了提供的系统类之外,还可以编写监视器类和示例类来监视用户应用程序数据和计数器。

    监视器类是从抽象的监视器类%Monitor.Adaptor继承的任何类;%Monitor.System类就是此类的示例。要创建自己的用户定义的监视器类,请执行以下操作:

    1. 在要监视数据的命名空间中运行^%MONAPPMGR。使用选项2列出监视器类,并在该菜单中使用选项3注册监视器系统类。
    SAMPLES>d ^%MONAPPMGR
     
     
    1) Set Sample Interval
    2) Manage Monitor Classes
    3) Change Default Notification Method
    4) Manage Email Options
    5) Manage Alerts
    6) Exit
     
    Option? 2
     
    1) Activate/Deactivate Monitor Class
    2) List Monitor Classes
    3) Register Monitor System Classes
    4) Remove/Purge Monitor Class
    5) Set Class Sample Interval
    6) Exit
     
    Option? 3
    Exporting to XML started on 06/21/2022 12:52:36
    Exporting class: Monitor.Sample
    Export finished successfully.
     
    Load started on 06/21/2022 12:52:36
    Loading file C:\InterSystems\SRCCTRL\mgr\Temp\t0jFhPqLkZoYAA.stream as xml
    Imported class: Monitor.Sample
    Compiling class Monitor.Sample
    Compiling table Monitor.Sample
    Compiling routine Monitor.Sample.1
    Load finished successfully.
     
     
    1) Activate/Deactivate Monitor Class
    2) List Monitor Classes
    3) Register Monitor System Classes
    4) Remove/Purge Monitor Class
    5) Set Class Sample Interval
    6) Exit
     
    Option?
    
    1. 编写从%Monitor or.Adaptor继承的类。继承提供持久性、参数、属性、代码生成和从类定义生成监视器元数据的投影。
    2. 编译类。编译从%Monitor or.Adaptor继承的类将在USERS类的子包中生成新的样例类,该子包称为Sample。例如,如果编译A.B.MyMetric,则会在A.B.Sample.MyMetric中生成一个新类。不需要对生成的类执行任何操作。

    重要提示:在删除应用程序监视器类时,只应删除监视器类;也就是说,不要删除生成的示例类。使用管理门户仅删除从中生成示例类(例如A.B.Sample.MyMetric)的监视器类(例如A.B.MyMetric);这会自动删除监视器类和生成的示例类。

    所有示例类都自动启用 CSP,因此可以通过指向 A.B.Sample.MyMetric.cls查看用户指标的示例数据。如果该类已被激活,Application Monitor 会自动调用该类并生成数据和警报;有关激活监视器类的信息,请参阅管理监视器类。。

    重要提示:SECURITYRESOURCE 参数在 %Monitor.Adaptor 中为空,因此在从 %Monitor.Adaptor 继承的用户类中为空,除非明确修改。代码生成将 SECURITYRESOURCE 值从用户定义的类复制到生成的示例类中。

    以下简单示例检索 IRIS 实例中每个数据集的可用空间。

    每次采样请求样本数据对象的 n 个实例,每个实例对应一个数据集。在此示例中,每个实例只有一个属性,即收集样本时该数据集中可用的可用空间。

    1. 创建一个继承自 %Monitor.Adaptor 的类
    Class MyMetric.Freespace Extends %Monitor.Adaptor [ ProcedureBlock ]
    {
    }
    
    1. 添加成为示例数据一部分的属性。它们的类型必须是 %Monitor 包中的类:
    • Gauge

    • Integer

    • Numeric

    • String

    Class MyMetric.Freespace Extends %Monitor.Adaptor [ ProcedureBlock ]
    {
    /// Name of dataset
    Property DBName As %Monitor.String(CAPTION = "Database Name");
    
    /// Current amount of Freespace
    Property FreeSpace As %Monitor.String;
    }
    
    1. 添加一个 INDEX 参数,告诉哪些字段在示例实例中形成唯一键:
    Parameter INDEX = "DBName";
    
    1. 根据需要添加控件属性,将它们标记为 [Internal],这样它们就不会成为生成类中存储定义的一部分。
    /// Result Set for use by the class
    Property Rspec As %SQL.StatementResult [Internal];
    
    1. 覆盖 Initialize() 方法。Initialize() 在每次指标收集运行开始时被调用。
    /// Initialize the list of datasets and freespace.
    Method Initialize() As %Status
    {
        set stmt=##class(%SQL.Statement).%New()
        set status= stmt.%PrepareClassQuery("SYS.Database","FreeSpace")
        set ..Rspec = stmt.%Execute()
        return $$$OK
    }
    
    1. 覆盖 GetSample() 方法。重复调用 GetSample() 直到返回状态 0。编写代码来填充每个示例实例的指标数据。
    /// Get dataset metric sample.
    /// A return code of $$$OK indicates there is a new sample instance.
    /// A return code of 0 indicates there is no sample instance.
    Method GetSample() As %Status
    {
        // Get freespace data
        set stat = ..Rspec.%Next(.sc)
    
        // Quit if we have done all the datasets
        if 'stat {
            Quit 0
        }
    
        // populate this instance
        set ..DBName = ..Rspec.%Get("Directory")
        set ..FreeSpace = ..Rspec.%Get("Available")
    
        // quit with return value indicating the sample data is ready
        return $$$OK
    }
    
    1. 编译类。该类如下所示:
    Class MyMetric.Freespace Extends %Monitor.Adaptor
    {
    Parameter INDEX = "DBName";
    
    /// Name of dataset
    Property DBName As %Monitor.String;
    
    /// Current amount of Freespace
    Property FreeSpace As %Monitor.String;
    
    /// Result Set
    Property Rspec As %SQL.StatementResult [Internal];
    
    /// Initialize the list of datasets and freespace.
    Method Initialize() As %Status
    {
        set stmt=##class(%SQL.Statement).%New()
        set status= stmt.%PrepareClassQuery("SYS.Database","FreeSpace")
        set ..Rspec = stmt.%Execute()
        return $$$OK
    }
    
    /// Get routine metric sample. 
    /// A return code of $$$OK indicates there is a new sample instance. 
    /// Any other return code indicates there is no sample instance. 
    Method GetSample() As %Status
    {
        // Get freespace data
        set stat = ..Rspec.%Next(.sc)
    
        // Quit if we have done all the datasets
        if 'stat {
            Quit 0
        }
    
        // populate this instance
        set ..DBName = ..Rspec.%Get("Directory")
        set ..FreeSpace = ..Rspec.%Get("Available")
    
        // quit with return value indicating the sample data is ready
        return $$$OK
    }
    
    }
    
    1. 此外,可以覆盖 Startup()Shutdown() 方法。这些方法在采样开始时调用一次,因此可以打开通道或执行其他一次性初始化:
    /// Open a tcp/ip device to send warnings
    Property io As %Status;
    
    Method Startup() As %Status
    {
        set ..io="|TCP|2"
        set host="127.0.0.1"
        open ..io:(host:^serverport:"M"):200
    }
    
    Method Shutdown() As %Status
    {
        close ..io
    }
    
    1. 编译该类会在 MyMetric.Sample 包中创建一个新类 MyMetric.Sample.Freespace
    /// Persistent sample class for MyMetric.Freespace
    Class MyMetric.Sample.Freespace Extends Monitor.Sample 
    {
    Parameter INDEX = "DBName";
    
    Property Application As %String [ InitialExpression = "MyMetric" ];
    
    /// Name of dataset
    Property DBName As %Monitor.String(CAPTION = "");
    
    /// Current amount of Freespace
    Property FreeSpace As %Monitor.String(CAPTION = "");
    
    Property GroupName As %String [ InitialExpression = "Freespace" ];
    
    Property MetricsClass As %String [ InitialExpression = "MyMetric.Freespace" ];
    
    }
    

    注意:不应修改此类。但是,可以继承它以针对示例数据编写自定义查询。

    重要提示:如果确实修改并重新编译了一个活动的用户定义的应用程序监视器类,则该类将被停用,并且特定于类的采样间隔覆盖(如果有)将被删除;要恢复它,必须激活它,根据需要重置采样间隔,然后重新启动系统监视器。

    相关文章

      网友评论

          本文标题:第三十章 使用系统监视器 - 编写用户定义的应用程序监视器类

          本文链接:https://www.haomeiwen.com/subject/yagfcdtx.html