Fortinet white logo
Fortinet white logo

User Guide

26.1.0

Custom metric caching

Custom metric caching

Custom metric caching solves the challenge of monitoring device data used in custom plugins that may be redundant with data already polled. In these cases, it is more efficient to cache the results where a plugin can reference them.

Example custom metric plugin

CacheTestPlugin.py

from CustomMetricPlugin import CustomMetricPlugin
import time


class CacheTestPlugin(CustomMetricPlugin):

    # This textkey identifies the plugin, not individual metric types.  It should be
    # of the form "com.example.sample" which uniquely identifies your organization and
    # the purpose of the plugin
    textkey = "com.cache.test"

    # This is the name of the plugin, which will be used when displaying metric
    # options supported by the plugin.
    name = "Cache Test Plugin"

    def get_metadata(self):
        return [
            {
                "metric_textkey": "test",
                "name": "test",
                "resource_options": None,
                "resource_option_type": "none",
                "description": "test function",
            },
        ]

    def get_data(
        self,
        textkey,
        option,
        instance_id,
        hostname,
        device_type,
        device_sub_type,
        tags,
        attributes,
    ):

        if textkey == "test":
            cached = self.get_cache("test")
            if cached is not None:
                self.logger.info(f"cache found: {cached}")
                return cached
            else:
                self.logger.info(f"cache not found")
                value = int(time.time()) % 86400
                self.set_cache("test", value, ttl=60)
                return value

Cache functions

    def set_cache(self, key, value, ttl=3600):
        """
        Store the value of the given key in the cache
        ttl is time to live in seconds (default 1 hour)
        """
        PluginCache.set_cache(key, value, ttl)

    def get_cache(self, key, default=None):
        """
        Get a value for the given key from the cache
        default is the value to return if the key is not found. If not provided, None is returned.
        """
        return PluginCache.get_cache(key, default)

Custom metric caching

Custom metric caching

Custom metric caching solves the challenge of monitoring device data used in custom plugins that may be redundant with data already polled. In these cases, it is more efficient to cache the results where a plugin can reference them.

Example custom metric plugin

CacheTestPlugin.py

from CustomMetricPlugin import CustomMetricPlugin
import time


class CacheTestPlugin(CustomMetricPlugin):

    # This textkey identifies the plugin, not individual metric types.  It should be
    # of the form "com.example.sample" which uniquely identifies your organization and
    # the purpose of the plugin
    textkey = "com.cache.test"

    # This is the name of the plugin, which will be used when displaying metric
    # options supported by the plugin.
    name = "Cache Test Plugin"

    def get_metadata(self):
        return [
            {
                "metric_textkey": "test",
                "name": "test",
                "resource_options": None,
                "resource_option_type": "none",
                "description": "test function",
            },
        ]

    def get_data(
        self,
        textkey,
        option,
        instance_id,
        hostname,
        device_type,
        device_sub_type,
        tags,
        attributes,
    ):

        if textkey == "test":
            cached = self.get_cache("test")
            if cached is not None:
                self.logger.info(f"cache found: {cached}")
                return cached
            else:
                self.logger.info(f"cache not found")
                value = int(time.time()) % 86400
                self.set_cache("test", value, ttl=60)
                return value

Cache functions

    def set_cache(self, key, value, ttl=3600):
        """
        Store the value of the given key in the cache
        ttl is time to live in seconds (default 1 hour)
        """
        PluginCache.set_cache(key, value, ttl)

    def get_cache(self, key, default=None):
        """
        Get a value for the given key from the cache
        default is the value to return if the key is not found. If not provided, None is returned.
        """
        return PluginCache.get_cache(key, default)