Created
October 29, 2016 12:11
-
-
Save jcurry/d97bc81c9bd050af78a20c9e18f07a1b to your computer and use it in GitHub Desktop.
Supply process class name and get all devices running that process
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Author: Jane Curry | |
# Date: October 28th, 2016 | |
# Description: Takes a process name as a parameter and delivers | |
# all devices running that process | |
# Note that strictly the process parameter is a "Process Class name" | |
# - check your Process definitions from INFRASTRUCTURE -> Processes | |
# Also may take output file parameter - default is /tmp/get_devices_with_process.out | |
# Requires parameter: | |
# -p <process> | |
# Optional parameter: | |
# -f <output file> | |
# eg. -p "firefox" -f /tmp/fred | |
import time | |
from optparse import OptionParser | |
import Globals | |
from Products.ZenUtils.Utils import unused | |
unused(Globals) | |
from Products.ZenUtils.ZenScriptBase import ZenScriptBase | |
def get_cli_options(): | |
"""Get command line options. Return them in a usable form.""" | |
parser = OptionParser() | |
parser.add_option( | |
'-p', '--process', | |
dest='process', default='', | |
help='process class name string to match') | |
parser.add_option( | |
'-f', '--file', | |
dest='outputFile', default='/tmp/get_devices_with_process.out', | |
help='Please specify full path to output file', metavar='FILE') | |
options, args = parser.parse_args() | |
return options | |
def main(): | |
options = get_cli_options() | |
if not options.process: | |
print 'Incorrect parameters supplied, please use --help for usage' | |
return | |
proc = options.process | |
of = open(options.outputFile, "w") | |
localtime = time.asctime( time.localtime(time.time()) ) | |
of.write(localtime + "\n\n") | |
# Need noopts=True or it barfs with the script options | |
dmd = ZenScriptBase(connect=True, noopts=True).dmd | |
# Do top-level processes | |
for p in dmd.Processes.osProcessClasses(): | |
if p.id == proc: | |
for i in p.instances(): | |
print 'Process class %s, instance %s, device %s ' % (p.id, i.id, i.os.device().titleOrId()) | |
of.write( 'Process class %s, instance %s, device %s \n' % (p.id, i.id, i.os.device().titleOrId())) | |
break | |
# Check process organizers - this does not include top-level processes | |
for o in dmd.Processes.getSubOrganizers(): | |
for p in o.osProcessClasses(): | |
if p.id == proc: | |
for i in p.instances(): | |
print 'Process organizer %s, Process class %s, instance %s, device %s ' % (o.id, p.id, i.id, i.os.device().titleOrId()) | |
of.write( 'Process organizer %s, Process class %s, instance %s, device %s \n' % (o.id, p.id, i.id, i.os.device().titleOrId())) | |
break | |
of.close() | |
# If we're being called as a stand-alone script. Not imported. | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment