02 Feb 2022 02:47 PM
Guys, can you please give me some guidance on the case below?
I created a plugin that does a file check. Apparently everything is working fine, with the exception of the count metric, which is not returning as desired within Dynatrace.
Looking at the log, the metric is right, bringing the correct value, however, inside Dynatrace it returns as 0 or 1
I am not able to identify where the problem is.
Could you support me?
#!/usr/bin/env python3
import logging
import os
from datetime import datetime, timedelta, timezone
import paramiko
from ruxit.api.base_plugin import RemoteBasePlugin
from workadays import workdays as wd
logger = logging.getLogger(__name__)
currentDate = datetime.today()
currentYear = currentDate.year
currentMonth = currentDate.month
currentDay = currentDate.day
br_timezone = timedelta(hours=-3)
currentTime = datetime.now(timezone(br_timezone)).strftime('%H:%M')
def isBetweenTime(start: str, end: str) -> bool:
if (currentTime > start) and (currentTime < end):
return True
else:
return False
def isBetweenDays() -> bool:
try:
weekday = currentDate.weekday
if (weekday >= 1) and (weekday <= 5):
return True
else:
return False
except Exception as e:
return e
class HasFilePlugin(RemoteBasePlugin):
global result_tratado
def query(self, **kwargs):
config = kwargs['config']
server = config['nome']
ip = config['host_ip']
port = config['host_port']
path = config['path_file']
file = config['file']
user = config['host_username']
pwd = config['host_password']
beginning = config['start_time']
end = config['finish_time']
status_ok = "OK"
status_notok = "Bad"
var_cmd_output = 0
try:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(ip, username=user, password=pwd)
cmd = f'ls {path}/{file} | wc -l'
stdin, stdout, stderr = client.exec_command(cmd)
cmd_output = stdout.read()
var_cmd_output = int(cmd_output)
if var_cmd_output == 0:
state_return = status_notok
print("Status", state_return)
print("Qtde Arquivos", var_cmd_output)
else:
state_return = status_ok
print("Status", state_return)
print("Qtde Arquivos", var_cmd_output)
group = self.topology_builder.create_group("Monitoração Arquivos", "Monitoração Arquivos")
device = group.create_element(server, server)
device.add_endpoint(ip=ip, port=port)
device.relative(key='var_cmd_output', value=var_cmd_output)
group.absolute(key='var_cmd_output', value=var_cmd_output)
device.state_metric(key='state_return', value=state_return)
except Exception as e:
logger.error(f'Exception | Exception final é: {e}')
{
"name": "custom.remote.python.hasfile",
"version": "1.0.21",
"type": "python",
"entity": "CUSTOM_DEVICE",
"metricGroup": "tech.python.monitorar_arquivos",
"technologies": [
"PYTHON"
],
"source": {
"package": "custom_python_monitorar_arquivos",
"className": "HasFilePlugin",
"install_requires": [
"workadays",
"paramiko",
"pywinrm"
],
"activation": "Remote"
},
"configUI": {
"displayName": "Monitorar Arquivos",
"properties": [
{
"key": "nome",
"displayName": "Device Name",
"displayOrder": 1,
"displayHint": "Arquivos SPB"
},
{
"key": "system_type",
"displayName": "Sistema Operacional",
"displayOrder": 2,
"displayHint": "Sistema Operacional"
},
{
"key": "host_ip",
"displayName": "IP Host",
"displayOrder": 3,
"displayHint": "IP for the host"
},
{
"key": "host_port",
"displayName": "Port",
"displayOrder": 4,
"displayHint": "Port for the host"
},
{
"key": "host_username",
"displayName": "Username",
"displayOrder": 5,
"displayHint": "Username"
},
{
"key": "host_password",
"displayName": "Password",
"displayOrder": 6,
"displayHint": "Password"
},
{
"key": "path_file",
"displayName": "File directory",
"displayOrder": 7,
"displayHint": "/path/to/your/file"
},
{
"key": "file",
"displayName": "File",
"displayOrder": 8,
"displayHint": "*_carga_in.txt"
},
{
"key": "start_time",
"displayName": "Time to start",
"displayOrder": 9,
"displayHint": "07:30"
},
{
"key": "finish_time",
"displayName": "Time to finish",
"displayOrder": 10,
"displayHint": "17:30"
},
{
"key": "frequency_1",
"displayName": "Frequency 1 (minutes)",
"displayOrder": 11,
"displayHint": "5"
}
]
},
"properties": [
{
"key": "nome",
"type": "String"
},
{
"key": "system_type",
"type": "Dropdown",
"dropdownValues": [
"Windows",
"Unix"
]
},
{
"key": "host_ip",
"type": "String"
},
{
"key": "host_port",
"type": "String"
},
{
"key": "host_username",
"type": "String"
},
{
"key": "host_password",
"type": "Password"
},
{
"key": "path_file",
"type": "String",
"default": "/path/to/your/file"
},
{
"key": "file",
"type": "String",
"default": "your file"
},
{
"key": "start_time",
"type": "String"
},
{
"key": "finish_time",
"type": "String"
},
{
"key": "frequency_1",
"type": "String"
}
],
"ui": {
"keymetrics": [
{
"key": "var_cmd_output",
"aggregation": "Count",
"mergeaggregation": "sum",
"displayname": "Total Arquivos"
},
{
"key": "state_return",
"displayname": "Status",
"statemetric": true
}
],
"keycharts": [
{
"group": "Quantidade Arquivos",
"title": "Quantidade Total",
"series": [
{
"key": "var_cmd_output",
"displayname": "Total Arquivos",
"seriestype": "area",
"color": "#4d4dff"
}
]
},
{
"group": "Status",
"title": "Status",
"series": [
{
"key": "state_return",
"displayname": "Status",
"seriestype": "bar",
"statecolors": [
"#00b33c",
"#ff3333"
],
"statechart": true
}
]
}
]
},
"charts": [
{
"group": "Quantidade Arquivos",
"title": "Quantidade Total",
"series": [
{
"key": "var_cmd_output",
"displayname": "Total",
"seriestype": "area",
"color": "#4d4dff"
}
]
}
],
"metrics": [
{
"statetimeseries": {
"key": "state_return",
"states": [
"OK",
"Bad"
],
"displayname": "Status Arquivo"
},
"entity": "CUSTOM_DEVICE"
},
{
"timeseries": {
"key": "var_cmd_output",
"unit": "Count",
"aggregation": "SUM",
"displayname": "Total Arquivos"
},
"entity": "CUSTOM_DEVICE"
}
]
}
Solved! Go to Solution.
02 Feb 2022 03:20 PM
Hi, go with device.absolute(key='var_cmd_output', value=var_cmd_output) instead of device.relative(key='var_cmd_output', value=var_cmd_output)
You've not said that it's a group metric so it gets added to the device, and the relative metric shows you the difference in the number of files between this poll and the previous one (but never less than 0).
02 Feb 2022 05:23 PM
Sorry @Mike_L , I don't think I understand what you mean
What I want is the following:
03 Feb 2022 07:19 PM
As I understand your question:
You send the value 15 to Dynatrace (you know this by logging it), the next minute you send the value 16 to Dynatrace (again, you know this by looking at the logs), but Dynatrace shows the value 1 instead of 16. This is because you're sending relative metrics to Dynatrace instead of absolute metrics as can be seen in your code. When you send relative metrics the plugin module checks what the delta is from the previous poll, and if it is 0 or more, it sends that value to Dynatrace.