cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Custom metrics does not show

alexandre_marce
Newcomer

Hi Everyone,


I'm writing a custom pluggin allowing me to monitor the host units used by my tenant. The pluggin works well on the simulator, the custom metrics is displayed in the custom chart window but there is no data in the chart. Does someone know where the problem can be ?

Here are the code of the plugin and the json, considering the token ID and env ID are well informed :

from requests import get as rg import json from ruxit.api.base_plugin import BasePlugin from ruxit.api.snapshot import pgi_name # Calculate the licence number for a given environement def calcHostUnits(envID, token):     #initialize values     hostUnits = 0     dynatraceBaseUrl = "https://XXXXXXXXXXXXX/e/" + envID + "/api/v1/entity/infrastructure/hosts"     headers = {         "api-token": token     }     #API call     response = rg(dynatraceBaseUrl, params = headers)     response = response.json()     #If response not empty, get consumedHostUnits for each host     if len(response) != 0:         for entry in response:             hostUnits += entry['consumedHostUnits']     return hostUnits class DemoPlugin(BasePlugin):     def query(self, **kwargs):         hostUnits = 0         token = self.config["Token"]         ID = self.config["EnvID"]         hostUnits = calcHostUnits(ID, token)         print("On consomme ", hostUnits)         self.results_builder.absolute(key='hostUnits', value=hostUnits)
{     "name": "custom.python.totalhostunits",     "version": "0.3",     "type": "python",     "entity": "HOST",     "metricGroup": "My_Metrics.plugins",     "favicon": "./favicon.jpg",     "source": {         "package": "demo_plugin",         "className": "DemoPlugin",         "install_requires": [             "requests>=2.6.0"         ],         "activation": "Singleton"     },     "metrics": [         {             "timeseries": {                 "key": "hostUnits",                 "unit": "PerMinute",                 "displayname": "Host Units consumed"             }         }     ],     "configUI": {         "displayName": "API calls configuration",         "properties": [             {                 "key": "Token",                 "displayName": "Token",                 "displayHint": "Use a Token authorized on this tenant"             },             {               "key": "EnvID",               "displayName": "Environment ID",               "displayHint": "Enter yout environment ID, can be find in the URL"             }         ]     },     "ui": {       "charts":[{         "group": "Dynatrace Informations",         "title": "Host Unit Consumption",         "series":[{                 "key": "hostUnits",                 "aggregation": "SUM",                 "displayname": "Host Units consumed",                 "seriestype": "line"               }]       }],         "keymetrics": [             {                 "key": "hostUnits",                 "aggregation": "count",                 "mergeaggregation": "avg",                 "displayname": "Host Unit Counter"             }         ],         "keycharts": [           {             "group": "Dynatrace Informations",             "title": "Host Unit Consumption",             "series": [               {                 "key": "hostUnits",                 "aggregation": "SUM",                 "displayname": "Host Units consumed",                 "seriestype": "line"               }             ]           }         ]     },     "properties": [       {         "key": "Token",         "type": "String",         "default_value": "XXXXXXXXXXXXX"       },       {         "key": "EnvID",         "type": "String",         "default_value": "XXXXXXXXX"       }     ] } 
10 REPLIES 10

Mike_L
Dynatrace Guru
Dynatrace Guru

I think that an ActiveGate plugin makes more sense for the use case. If you want to do it as a host plugin I'd go with something like this as the last line of your Python script:

self.results_builder.add_absolute_result(PluginMeasurement(key="hostUnits", value=hostUnits, entity_selector=HostSelector()))

I'm not sure it's required, but my host extensions always have this line in them as well in the root of the plugin.json:

"processTypes": [0],
Mike

patmis
Contributor

Hi together,

 

We encounter a similar issue. We have quite a simple extension. Currently it would only generate some mock data. 

 

The Python code:

import random
from ruxit.api.base_plugin import BasePlugin
from ruxit.api.data import PluginMeasurement
from ruxit.api.selectors import HostSelector

logger = logging.getLogger(__name__)

class Ports(BasePlugin):
    def query(self, **kwargs):
        logger.info("Activation context: %s", self.get_activation_context)

		# create some random mock data
        used_ports=random.randrange(10)
        free_ports=random.randrange(10)
        total_port_range=used_ports+free_ports

		# write results
        self.results_builder.add_absolute_result(PluginMeasurement(key='used_ports', value=used_ports, entity_selector=HostSelector()))
        self.results_builder.add_relative_result(PluginMeasurement(key='free_ports', value=free_ports, entity_selector=HostSelector()))
        self.results_builder.add_relative_result(PluginMeasurement(key='total_port_range', value=total_port_range, entity_selector=HostSelector()))

 

And the plugin.json looks like this:

{
  "name": "custom.python.ports",
  "version": "0.0.19",
  "type": "python",
  "entity": "HOST",
  "technologies": [ "PYTHON" ],
  "metricGroup": "custom.python.ports",
  "processTypes": [0],
  "source": {
    "package": "ports",
    "className": "Ports",
    "install_requires": [],
    "activation": "Singleton"
  },
  
  "metrics": [
    {
      "timeseries": {
        "key": "used_ports",
        "unit": "Count",
        "displayname": "Used ports"
      }
    },
    {
      "timeseries": {
        "key": "free_ports",
        "unit": "Count",
        "displayname": "Free ports"
      }
    },
    {
      "timeseries": {
        "key": "total_port_range",
        "unit": "Count",
        "displayname": "Total port range"
      }
    }
  ],

  "configUI": {
    "displayName": "OneAgent Ports extension"
  }
}

However, we do not get any data in Dynatrace. 

Mike_L
Dynatrace Guru
Dynatrace Guru

Can you remove this line from your plugin.json and try again? Do you see anything in the plugin log file on the OA where you placed the files?

"technologies": [ "PYTHON" ],

 

Mike

patmis
Contributor

Hi Mike!

 

Yes it looks like this does the trick!

 

Thank you and kind regards,

Patrick

Hi Mike,


I have a follow-up question. The documentation states the following:

processTypes

Integer Array

Type of process that plugin monitors, as defined in the ID column of the known process types table. This serves as an alternative to processTypeNames. The method is kept for compatibility, we recommend to use improved “technologies” instead.

 
Why does it not work whith 
"technologies": [ "PYTHON" ],​

?

 

Kind Regards,

Patrick

Mike_L
Dynatrace Guru
Dynatrace Guru

It doesn't work because you're not monitoring a Python process, you're monitoring the whole host.

Mike

Ok, I understand. Is there another way to do it?

Mike_L
Dynatrace Guru
Dynatrace Guru

Yes, by removing it like you did. That worked, right?

Mike

Yes, it works. However, I was just wondering if this is the right way to do it. Thank you!

Mike_L
Dynatrace Guru
Dynatrace Guru

My team did 350 or so extensions and that method is used by several of them. If it works it works 🙂

Mike

Featured Posts