Create the ZenPack
The first thing we’ll need to do is create the NWS ZenPack. We’ll
use the zenpacklib script to create this ZenPack from the command line using the
following steps. These commands should be run as the zenoss user.
cd /z
zenpacklib --create ZenPacks.training.NWS
You should see output similar to the following. Most importantly that
zenpack.yaml file is being created.
Creating source directory for ZenPacks.training.NWS:
- making directory: ZenPacks.training.NWS/ZenPacks/training/NWS
- creating file: ZenPacks.training.NWS/setup.py
- creating file: ZenPacks.training.NWS/MANIFEST.in
- creating file: ZenPacks.training.NWS/ZenPacks/__init__.py
- creating file: ZenPacks.training.NWS/ZenPacks/training/__init__.py
- creating file: ZenPacks.training.NWS/ZenPacks/training/NWS/datasources/__init__.py
- creating file: ZenPacks.training.NWS/ZenPacks/training/NWS/thresholds/__init__.py
- creating file: ZenPacks.training.NWS/ZenPacks/training/NWS/parsers/__init__.py
- creating file: ZenPacks.training.NWS/ZenPacks/training/NWS/migrate/__init__.py
- creating file: ZenPacks.training.NWS/ZenPacks/training/NWS/resources/__init__.py
- creating file: ZenPacks.training.NWS/ZenPacks/training/NWS/modeler/__init__.py
- creating file: ZenPacks.training.NWS/ZenPacks/training/NWS/tests/__init__.py
- creating file: ZenPacks.training.NWS/ZenPacks/training/NWS/libexec/__init__.py
- creating file: ZenPacks.training.NWS/ZenPacks/training/NWS/modeler/plugins/__init__.py
- creating file: ZenPacks.training.NWS/ZenPacks/training/NWS/lib/__init__.py
- creating file: ZenPacks.training.NWS/ZenPacks/training/NWS/__init__.py
- creating file: ZenPacks.training.NWS/ZenPacks/training/NWS/zenpack.yaml
Define zProperties and classes
The zenpack.yaml file that’s created within the ZenPack source directory
above contains only the absolute minimum to be a valid YAML file. Let’s
take a look at its current contents.
First let’s set a couple of environment variables to reduce some typing.
export ZP_TOP_DIR=/z/ZenPacks.training.NWS
export ZP_DIR=$ZP_TOP_DIR/ZenPacks/training/NWS
Now let’s look at the contents of zenpack.yaml.
cd $ZP_DIR
cat zenpack.yaml
You should only see the following line.
name: ZenPacks.training.NWS
Replace the contents of zenpack.yaml with the following.
name: ZenPacks.training.NWS
zProperties:
DEFAULTS:
category: National Weather Service
zNwsStates:
type: lines
default:
- TX
- OK
classes:
NwsDevice:
base: [zenpacklib.Device]
label: National Weather Service API
NwsStation:
base: [zenpacklib.Component]
label: Station
properties:
country_code:
label: Country Code
timezone:
label: Time Zone
nws_zone:
label: Zone
county:
label: County
latitude:
label: Latitude
grid_display: False
longitude:
label: Longitude
grid_display: False
class_relationships:
- NwsDevice 1:MC NwsStation
You can see this YAML defines the following important aspects of our ZenPack.
-
The
namefield is mandatory. It must match the name of the ZenPack’s source directory. -
The
zPropertiesfield contains configuration properties we want the ZenPack to add to the Zenoss system when it is installed.Note that
DEFAULTSis not added as configuration property. It is a special value that will cause its properties to be added as the default for all of the other listed zProperties. Specifically in this case it will cause thecategoryofzNwsStatesto be set toNational Weather Service. This is a convenience to avoid having to repeatedly type the category for each added property. Since we're only adding one zProperty, it doesn't really save us anything in this case, but it is still a best practice. In the future, you may need to add additional zProperties, and you'd need to then refactor to set the default.The
zNwsStatesproperty uses thelinestype which allows the user to specify multiple lines of text. Each line will be turned into an element in a list which you can see is also how the default value is specified. The idea here is that unless the user configures otherwise, we will default to monitoring weather alerts and conditions for stations in Texas and Oklahoma. -
The
classesfield contains each of the object classes we want the ZenPack to add.In this case we’re adding
NwsDevicewhich, becausebaseis set toDevice,will be a subclass or specialization of the standard Zenoss device type. We’re also addingNwsStationwhich, becausebaseis set toComponent,will be a subclass of the standard component type.The
labelfor each is simply the human-friendly name that will be used to refer to the resulting objects when they’re seen in the Zenoss web interface.The
propertiesforNwsStationare extra bits of data we want to model from the API and show to the user in the web interface. Settinggrid_displayto false forlattitudeandlongitudewill allow them to be shown in the details panel of the component, but not in the component grid. -
class_relationshipsuses a simple syntax to define a relationship betweenNwsDeviceandNwsStation. Specifically it is saying that each (1)NwsDeviceobject can contain many (MC)NwsStationobjects.
Install the ZenPack
Creating the ZenPack with the zenpacklib script doesn’t install the ZenPack for
you. So you must now install the ZenPack in developer (--link) mode.
Run the following command to install the ZenPack in developer mode.
zenpack --link --install $ZP_TOP_DIR