Guest User

Untitled

a guest
Oct 26th, 2009
828
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.23 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import Globals
  3. from Products.ZenUtils.ZenScriptBase import ZenScriptBase
  4. from transaction import commit
  5.  
  6. dmd = ZenScriptBase(connect=True, noopts=True).dmd
  7.  
  8. from Products.ZenUtils import Time
  9. import re
  10. import sys
  11.  
  12. from optparse import OptionParser
  13.  
  14. parser = OptionParser()
  15.  
  16. parser.add_option("-s", "--start-date", dest="startDate",
  17.                   help="Select Start Date [MM/DD/YYYY]", metavar="StartDate")
  18.  
  19. parser.add_option("-e", "--end-date", dest="endDate",
  20.                   help="Select End Date [MM/DD/YYYY]", metavar="EndDate")
  21.  
  22. parser.add_option("-c", "--consolidation", dest="how", default="AVERAGE",
  23.                   help="Select consolidation [MINIMUM, AVERAGE, MAXIMUM]", metavar="FUNCTION")
  24.  
  25. parser.add_option("-y", "--epoch-start", dest="startDateEpoch",
  26.                   help="Select start time in seconds", metavar="SECONDS")
  27.  
  28. parser.add_option("-z", "--epoch-end", dest="endDateEpoch",
  29.                   help="Select end time in seconds", metavar="SECONDS")
  30.  
  31. (options, args) = parser.parse_args()
  32.  
  33. if not options.startDate and not options.endDate and not options.startDateEpoch and not options.endDateEpoch:
  34.     parser.print_help()
  35.     sys.exit()
  36.  
  37. how = options.how
  38.  
  39. if not options.startDateEpoch and not options.endDateEpoch:
  40.     if options.startDate and options.endDate:
  41.         startDate = options.startDate
  42.         endDate = options.endDate
  43.         startDate, endDate = map(Time.ParseUSDate, (startDate, endDate))
  44.         startDate = min(startDate, endDate - 1)
  45.     else:
  46.         print "Start and end options need to be used in conjunction with each other."
  47.         sys.exit()
  48. else:
  49.     if options.startDateEpoch and options.endDateEpoch:
  50.         startDate = options.startDateEpoch
  51.         endDate = options.endDateEpoch
  52.     else:
  53.         print "Start and end options need to be used in conjunction with each other."
  54.         sys.exit()
  55.  
  56. print "Device,Interface,Speed,Input,Output,Total,% Util"
  57.  
  58. def percent(partial, total):
  59.     if partial is None or total is None:
  60.         return None
  61.     if not total:
  62.         return None
  63.     return partial * 100 / total
  64.  
  65. for d in dmd.Devices.getSubDevices():
  66.  
  67.     isLocal = re.compile(d.zLocalInterfaceNames)
  68.     for i in d.os.interfaces():
  69.     percentUsed = 0
  70.         if isLocal.match(i.name()): continue
  71.         if not i.monitored(): continue
  72.         if i.snmpIgnore(): continue
  73.         if not i.speed: continue
  74.         total = None
  75.         results = i.getRRDValues(['ifHCInOctets',
  76.                                   'ifInOctets',
  77.                                   'ifOutOctets',
  78.                                   'ifHCOutOctets'],
  79.                                  start=startDate, end=endDate, function=how)
  80.         input = results.get('ifHCInOctets',
  81.                             results.get('ifInOctets', None))
  82.         output = results.get('ifHCOutOctets',
  83.                              results.get('ifOutOctets', None))
  84.  
  85.         if None not in [input, output]:
  86.             total = input + output
  87.  
  88.         if total and i.speed:
  89.             percentUsed = percent(total, i.speed / 8)
  90.             percentUsed = round(percentUsed, 2)
  91.  
  92.         print "%s,%s,%s,%s,%s,%s,%s" % (d.id, i.id, i.speed, input, output, total, percentUsed)
Advertisement
Add Comment
Please, Sign In to add comment