Datasource plugin (modeling)
The final capability of Python data source plugins is to make changes to
the Zenoss model. This allows a data source to make changes to the model
in the same way that zenmodeler
does. Having this capability in a data
source allows modeling more frequently than the normal 12
hour zenmodeler
interval.
To demonstrate this through an exercise, we’ll extend the
existing Conditions
plugin to capture the what the Observations API
calls textDescription
which is some text that looks like “Scattered
Clouds” or “Sunny”. We’ll then show this value for each location in the
web interface.
Model updates are much more expensive operations than creating events or collecting datapoints. It is better to perform as much modeling as possible using modeler plugins on their typical 12 hour interval, and perform only the absolutely necessary smaller model updates more frequently using a PythonDataSourcePlugin. Too much modeling activity can result in the degradation of a Zenoss’ systems overall performance.
Add modeling to conditions datasource plugin
-
Edit
$ZP_DIR/zenpack.yaml
.Add the following
weather
property to theNwsStation
class between the existingtimezone
andnws_zone
properties.weather: label: Current Conditions
-
Edit
$ZP_DIR/dsplugins.py
.Add the following needed import to the top of
dsplugins.py
.from Products.DataCollector.plugins.DataMaps import ObjectMap
Add the following code to the
Conditions
class’collect
method right above thereturnValue(data)
line indented one level further. ThereturnValue(data)
line is included in the following update to show where the new code should be placed.data['maps'].append( ObjectMap({ 'relname': 'nwsStations', 'modname': 'ZenPacks.training.NWS.NwsStation', 'id': datasource.component, 'weather': current_observation['textDescription'], })) returnValue(data) # existing line
The
maps
concept here is exactly the same as it is in modeler plugins.data['maps']
can contain anything that a modeler plugin’sprocess
method can return. -
Don’t update the
Station
monitoring template.We’re adding capability to a datasource that’s already configured. No updates are required to the monitoring template.
-
Restart Zenoss.
If we had only updated the
collect
method of theConditions
plugin we would only need to restartzenpython
. However, because we added the newweather
property to theNwsStation
class, we must restart nearly everything, so it’s simpler to restart everything.
Test modeling current conditions
-
Run the following command to collect from
api.weather.gov
.zenpython run -v10 --device=api.weather.gov
There will be a lot of output from this command, but we’re looking for the following line which indicates that our maps were applied:
2019-05-30 14:28:12,050 DEBUG zen.python: api.weather.gov 600 KUNU nws-conditions sending 1 datamaps
You can use a commmand like the following to see just these log lines:
zenpython run -v10 --device=api.weather.gov | grep "sending" | grep "datamaps"
-
Navigate to the Stations on the api.weather.gov device and verify that each location shows something in its Weather column.