Wednesday, June 1, 2011

Automating Zenoss Multi-Graph Reports

Surprisingly, I discovered that my spur-of-the-moment python/TAL hack for Zenoss 3 years ago still shows up on their message board. However, with the modifications made to the Zenoss Reports architecture I do not think I can revitalize the Dynamic Zenoss Graph Reports.

Previously, I had posted about my zenossYAMLTool that can be used to import and export Zenoss objects into YAML for the sake of manipulating the database without ZenPacks. I made some minor changes to my original zenossYAMLTool so that it can be imported into another python script. Using the various export methods, you can easily generate Multi-Report graphs be gathering the necessary information and then importing them back into Zenoss after you've manipulated the data.

Here is an example:

import zenossYAMLTool as z
class NextGraph(Exception): pass
for dclass in sorted(list(set(['/'.join(dc.split('/')[0:-1])
    for dc in z.list_devices() if dc.startswith('/Server')]))):
    seq = -1
    graphs = []
    groups = []
    for gpName in [ 'laLoadInt15', 'ssCpuUser', 'ssIORawReceived' ]:
        seq = seq + 1
        for t in z.export_templates(dclass):
            try:
                for g in t['GraphDefs']:
                    for p in g['GraphPoints']:
                        if p['gpName'] == gpName:
                            g['gdName'] = '%s (%s)' % (g['gdName'], p['legend'])
                            newgraph = g
                            p['legend'] = '${dev/id | here/name | here/id} ${graphPoint/id}'
                            p['lineType'] = 'LINE'
                            p['sequence'] = seq
                            newgraph['GraphPoints'] = [p]
                            newgraph['GraphPoints'] = [p]
                            graphs.append(newgraph)
                            newgroup = { 'collectionId': dclass.split('/')[-1],
                                'combineDevices': True, 'ggName': g['gdName'],
                                'graphDefId': g['gdName'], 'sequence': seq }
                            groups.append(newgroup)
                            raise NextGraph
            except NextGraph:
                break
    colls = [{ 'collectionName': dclass.split('/')[-1],
        'CollectionItems': [{ 'collectionItem': 'Item',
            'compPath': '', 'deviceId': '',
            'deviceOrganizer': '/Devices' + dclass,
            'recurse': False, 'sequence': seq }] }]
    report = {
        'action': 'add_multireport',
        'numColumns': 2, 'title': '',
        'reportName': dclass.split('/')[-1],
        'reportPath': '/Multi-Graph Reports/Screens',
        'GraphGroups': groups,
        'Collections': colls,
        'GraphDefs': graphs, }
    z.import_yaml([report])

Download my python script that will build a Multi-Graph Report for all DeviceClasses in your system displaying: Load, CPU, Memory, IO, Throughput, Disk and OSProcesses. Modify it accordingly by refactoring the python code or altering the related cf file that controls the script.

No comments:

Post a Comment