This provides an example of how to use a Python script to define a custom monitor.
Python script configuration
The script executes for the metrics at a periodic interval that can be set.
Python sample.py:
"
/metricName::metricName1|metricName2
/metric::displayName1|displayName2
/warn::warn1|warn2
/critical::crit1|crit2
/alert::do_alert1|do_alert2
/params::args_string1|args_string2
"
Parse arguments
Use the following code snippet as an example of how to parse custom monitor arguments:
api = None
try:
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))
from ScriptMonitorPythonApi import ScriptMonitorPythonApi
api = ScriptMonitorPythonApi(sys.argv)
except Exception, emsg:
sys.exit()
arrayLen = len(api.metricNames)
metrics = api.metrics
warning_thres = api.warnings
critical_thres = api.criticals
do_alerts = api.doAlerts
userParams = api.userParams
Variables | Description |
---|---|
arrayLen | Number of metrics to be monitored. |
metrics | Array contains all metrics display names. Metric display names are needed to graphically display the JSON payload. For example, "metric" : "displayName1" |
warning_thres | Array contains all metrics warning threshold values. |
critical_thres | Array contains all metrics critical threshold values. Critical threshold values help calculate the metric alert state. |
do_alerts | Array contains all metrics alerts flag values. |
userParams | Array contains all metrics respective user parameters.Note:The user parameters must not contain the following strings:
|
Display graphs
For graphing the metric values and for alerting, the script must write the output to the console, as shown in the following JSON-formatted example:
[
{
"metric" : "displayName1",
"component" : "instance1",
"value" : "value1",
"state" : "alert state1",
"alert_desc" : "alert description1",
"hostname/address/id/container_id" : "host1/container_id"
},
{
"metric" : "displayName2",
"component" : "instance2",
"value" : "value2",
"state" : "alert state2",
"alert_desc" : "alert description2",
"hostname/address/id/container_id" : "host2/container_id"
}
]
Values | Description | Required |
---|---|---|
metric | Metric name. | Yes |
component | Metric instance name. | Include only when there are multiple instances. |
value | Metric or instance value. | Yes |
state | Metric alert state. Valid state values:
| Include only if the agent needs to handle alerts. |
alert_desc | Alert description. | Include only if the agent needs to handle alerts. |
hostname | Remote host hostname. For example, to send metric data to a remote host named XYZ , the JSON object notation is "hostname": 'XYZ' . | Include when needed to post metrics to other hosts. |
address | Remote host IP address. | Include only when needed to post metrics to other hosts. |
id | Remote host resource ID. For example, to send metric data to a remote host with IP address x.x.x.x , the JSON object notation is "id": 'x.x.x.x' . | Include only when needed to post metrics to other hosts. |
container_id | Remote host container ID. | Include only if the metrics need to be plotted against a container. Otherwise, omit this field. |
View graphical representations
To view the graphical representation of the monitoring:
From All Clients, select a client.
Go to Infrastructure > Resources and select a resource type.
Click the resource name to view the Overview page.
In the left panel, click Metrics to display a graph with the following display names:
displayName1
Displays three valuesval1
,val2
, andval3
plotted againstkey1
,key2
andkey3
, respectively.displayName2
Displays two valuesval1
andval2
plotted againstkey1
andkey2
, respectively.