TECHZEN Zenoss User Community ARCHIVE  

Import / export of triggers and notifications

Subject: Import / export of triggers and notifications
Author: Jane Curry
Posted: 2017-05-15 09:27

Before I set off and write something.....
Does anyone already have a utility for importing and exporting triggers and notifications to/from Zenoss that they are willing to share?  I am thinking particularly in the context of a migration tool but I'm sure there are other good uses.

Cheers,
Jane

------------------------------
Jane Curry Zenoss Master
Skills 1st Ltd United Kingdom
jane.curry@skills-1st.co.uk
------------------------------


Subject: RE: Import / export of triggers and notifications
Author: [Not Specified]
Posted: 2017-05-17 10:43

These are the zendmd commands we used to export and import triggers and notifications.  This worked exporting from 4.2.4  to 5x.  There were a few caveats that we encountered.  The major one being that the trigger and notification user permissions were not preserved and no other user information was carried over i.e email address for email notifications.

 

This will get you in the right direction.

 

From zendmd:

 

 

Prepare notifications for export:

 

%cpaste

tf = getFacade('triggers')

from Products.Zuul import marshal

def exportNotifications(tf, notifications):

    configs = []

    junkColumns = ('id', 'newId', 'uid', 'inspector_type', 'meta_type')

    for notificationInfo in notifications:

        config = marshal(notificationInfo)

        for item in junkColumns:

            if item in config:

                del config[item]

        contentsTab = tf._extractNotificationContentInfo(config)

        del config['content']

        config.update(contentsTab)

        #config['recipients'] = [r['label'] for r in config['recipients']]

        config['subscriptions'] = [x['name'] for x in config['subscriptions']]

        windows = []

        for window in notificationInfo._object.windows():

            winconfig = marshal(IInfo(window))

            for witem in ('meta_type', 'newId', 'id', 'inspector_type', 'uid'):

                del winconfig[witem]

            windows.append(winconfig)

        config['windows'] =  windows

        configs.append(config)

    return configs

--

 

Export to pickle file 

 

import pickle

pickle.dump(exportNotifications(tf, tf.getNotifications()), open("/home/zenoss/prod_notifications.pickle", "wb"))

 

Export triggers to pickle file:

 

triggers = tf.getTriggers()

triggerdata = tf.exportTriggers(triggers)

pickle.dump(triggerdata, open("/home/zenoss/prod_triggers.pickle", "wb"))

 

Import Triggers:

import pickle

tf = getFacade('triggers')

triggers = pickle.load(open('/home/zenoss/prod_triggers.pickle', 'rb'))

tf.importTriggers(triggers)

 

commit()

 

 

import Notifications:

import pickle

 

tf = getFacade('triggers')

notifications = pickle.load(open('/home/zenoss/prod_notifications.pickle', 'rb'))

tf.importNotifications(notifications)

 

commit()

 



Subject: RE: Import / export of triggers and notifications
Author: Jane Curry
Posted: 2017-05-17 15:13

Many thanks for this. 

I have spent the last couple of days trying to make the importConfiguration / exportConfiguration methods from the JSON API Triggers class work.  Some success with triggers in and out and limited success exporting notifications but importing notifications looks broken (at least with Zenoss 4.2.5 SUP 671).

Has anyone else tried the JSON API methods??? It would at least be interesting to know if it is possible to make them work - they look like exactly the right thing. 

Cheers,
Jane

------------------------------
Jane Curry
Skills 1st United Kingdom
jane.curry@skills-1st.co.uk
------------------------------


Subject: RE: Import / export of triggers and notifications
Author: Tim Caiazza
Posted: 2017-05-18 00:10

I was actually playing around with adding and updating triggers and notifications via the JSON API.  I'm working on adding a bunch of functions into iamseth/python-zenoss but since I'm slow here is what I have so far.  I hope to have at least a commit up by the end of next week.

note all requests go tohttps://zenoss/zport/dmd/triggers_router

Here is what to post to get all triggers

[{"tid": 0, "type": "rpc", "data": [{}], "method": "getTriggers", "action": "TriggersRouter"}]

Here is what to post to get all notifications

[{"tid": 1, "type": "rpc", "data": [{}], "method": "getNotifications", "action": "TriggersRouter"}]

Add trigger:
[{"tid": 2, "type": "rpc", "data": [{"newId": "Apples"}], "method": "addTrigger", "action": "TriggersRouter"}] 


Update a trigger:

[{"tid": 4, "type": "rpc", "data": [{"globalRead": false, "globalManage": false, "globalWrite": false, "enabled": true, "uuid": "499b31ca-8750-40ab-9973-03b9052097e4", "rule": {"source": "evt.severity == 5"}, "name": "Apples"}], "method": "updateTrigger", "action": "TriggersRouter"}]

Add new notification

[{"tid": 5, "type": "rpc", "data": [{"action": "email", "newId": "JSON Test"}], "method": "addNotification", "action": "TriggersRouter"}]


Link the trigger to the notification. So almost all of the data that you need for the notification is default info which you can get from grabbing all the notifications. Under the subscriptions part of the data below is a list of 2 triggers uuids, which you can grab from getting all the triggers.

[  
   {
      "tid":8,
      "type":"rpc",
      "data":[
         {
            "globalManage":false,
            "uid":"/zport/dmd/NotificationSubscriptions/Test",
            "send_initial_occurrence":true,
            "newId":"Test",
            "id":"Test",
            "globalWrite":false,
            "content":{
               "items":[
                  {
                     "items":[
                        {
                           "vtype":null,
                           "fieldLabel":"Body Content Type",
                           "xtype":"autoformcombo",
                           "labelStyle":null,
                           "value":"html",
                           "name":"body_content_type",
                           "disabled":false,
                           "values":[
                              [
                                 "html",
                                 "html"
                              ],
                              [
                                 "text",
                                 "text"
                              ]
                           ],
                           "inputAttrTpl":" data-qtip='Thecontent type of the body for emails.' ",
                           "anchor":"85%",
                           "allowBlank":true
                        },
                        {
                           "vtype":null,
                           "fieldLabel":"Message (Subject) Format",
                           "xtype":"textfield",
                           "labelStyle":null,
                           "value":"[zenoss] ${evt/device} ${evt/summary}",
                           "disabled":false,
                           "allowBlank":true,
                           "inputAttrTpl":" data-qtip='The template for the subject for emails.' ",
                           "anchor":"85%",
                           "name":"subject_format"
                        },
                        {
                           "vtype":null,
                           "fieldLabel":"Body Format",
                           "xtype":"textarea",
                           "labelStyle":null,
                           "value":"\nDevice: ${evt/device}\nComponent: ${evt/component}\nSeverity: ${evt/severity}\nTime: ${evt/lastTime}\nMessage:\n${evt/message}\n<a href=\"${urls/eventUrl}\">Event Detail</a>\n<a href=\"${urls/ackUrl}\">Acknowledge</a>\n<a href=\"${urls/closeUrl}\">Close</a>\n<a href=\"${urls/eventsUrl}\">Device Events</a>\n",
                           "disabled":false,
                           "allowBlank":true,
                           "inputAttrTpl":" data-qtip='The template for the body for emails.' ",
                           "anchor":"85%",
                           "name":"body_format"
                        },
                        {
                           "vtype":null,
                           "fieldLabel":"Clear Message (Subject) Format",
                           "xtype":"textfield",
                           "labelStyle":null,
                           "value":"[zenoss] CLEAR: ${evt/device} ${clearEvt/summary}",
                           "disabled":false,
                           "allowBlank":true,
                           "inputAttrTpl":" data-qtip='The template for the subject for CLEAR emails.' ",
                           "anchor":"85%",
                           "name":"clear_subject_format"
                        },
                        {
                           "vtype":null,
                           "fieldLabel":"Body Format",
                           "xtype":"textarea",
                           "labelStyle":null,
                           "value":"\nEvent: '${evt/summary}'\nCleared by: '${evt/clearid}'\nAt: ${evt/stateChange}\nDevice: ${evt/device}\nComponent: ${evt/component}\nSeverity: ${evt/severity}\nMessage:\n${evt/message}\n<a href=\"${urls/reopenUrl}\">Reopen</a>\n",
                           "disabled":false,
                           "allowBlank":true,
                           "inputAttrTpl":" data-qtip='The template for the body for CLEAR emails.' ",
                           "anchor":"85%",
                           "name":"clear_body_format"
                        },
                        {
                           "vtype":null,
                           "fieldLabel":"From Address for Emails",
                           "xtype":"textarea",
                           "labelStyle":null,
                           "value":"zenossuser_admin@shnetmonitor5:8000",
                           "disabled":false,
                           "allowBlank":true,
                           "inputAttrTpl":" data-qtip='The user from which the e-mail originated on the Zenoss server.' ",
                           "anchor":"85%",
                           "name":"email_from"
                        },
                        {
                           "vtype":null,
                           "fieldLabel":"SMTP Host",
                           "xtype":"textarea",
                           "labelStyle":null,
                           "value":"",
                           "disabled":false,
                           "allowBlank":true,
                           "inputAttrTpl":" data-qtip='Simple Mail Transport Protocol (aka E-mail server).' ",
                           "anchor":"85%",
                           "name":"host"
                        },
                        {
                           "vtype":null,
                           "fieldLabel":"SMTP Port (usually 25)",
                           "xtype":"numberfield",
                           "labelStyle":null,
                           "value":"",
                           "name":"port",
                           "disabled":false,
                           "decimalPrecision":2,
                           "inputAttrTpl":" data-qtip='TCP/IP port to access Simple Mail Transport Protocol (aka E-mail server).' ",
                           "anchor":"85%",
                           "allowBlank":true
                        },
                        {
                           "vtype":null,
                           "checked":false,
                           "xtype":"checkbox",
                           "labelStyle":"display:none",
                           "value":false,
                           "name":"useTls",
                           "disabled":false,
                           "boxLabel":"Use TLS?",
                           "inputAttrTpl":" data-qtip='Use Transport Layer Security for E-mail?' ",
                           "anchor":"85%",
                           "allowBlank":true
                        },
                        {
                           "vtype":null,
                           "fieldLabel":"SMTP Username (blank for none)",
                           "xtype":"textarea",
                           "labelStyle":null,
                           "value":"",
                           "disabled":false,
                           "allowBlank":true,
                           "inputAttrTpl":" data-qtip='Use this only if authentication is required.' ",
                           "anchor":"85%",
                           "name":"user"
                        },
                        {
                           "vtype":null,
                           "fieldLabel":"SMTP Password (blank for none)",
                           "xtype":"password",
                           "labelStyle":null,
                           "value":"",
                           "disabled":false,
                           "allowBlank":true,
                           "inputAttrTpl":" data-qtip='Use this only if authentication is required.' ",
                           "anchor":"85%",
                           "name":"password"
                        }
                     ],
                     "xtype":"fieldset"
                  }
               ]
            },
            "delay_seconds":0,
            "globalRead":false,
            "description":"",
            "recipients":[

            ],
            "subscriptions":[
               "f1f9eb4b-090b-4021-8e26-e535b29077c5",
               "f57b89c0-679b-499f-b9e5-a08b013a18d1"
            ],
            "action":"email",
            "name":"Test",
            "userWrite":1,
            "enabled":false,
            "repeat_seconds":0,
            "meta_type":"NotificationSubscription",
            "userRead":true,
            "userManage":1,
            "inspector_type":"NotificationSubscription",
            "send_clear":false
         }
      ],
      "method":"updateNotification",
      "action":"TriggersRouter"
   }
]



------------------------------
Tim Caiazza
------------------------------


Subject: RE: Import / export of triggers and notifications
Author: Tim Caiazza
Posted: 2017-05-31 14:53

I just posted a PR, https://github.com/iamseth/python-zenoss/pull/17, to the python-zenoss repo that adds a bunch of functions including ones to get and set triggers and notifications, if anyone is interested.

------------------------------
Tim Caiazza
Compu Global Hypermega Net
------------------------------


< Previous
Set Threshold as percentage of a data point
  Next
Zenoss 4.2.5 event monitoring
>