#!/usr/bin/env arista-python
# Copyright (c) 2011 Arista Networks, Inc.  All rights reserved.
# Arista Networks, Inc. Confidential and Proprietary.

from EntityTreeMonitor import createRemoteMonitor
import sys

# pylint: disable-next=pointless-string-statement
'''
This command can be used to monitor any changes to entities/attributes anywhere
along on a given entity path.
You can select an agent through which you want to do the monitoring. This can be
Sysdb itself for monitoring any path that is mounted. For non-mounted entities,
you need to hook on to the respective agents.
One issue with hooking on to agents( for mounted/non-mounted entities ) is that
events might show up in incorrect order since any attr change can cause
another attr to change and so on. This is less likely for Sysdb which has only a
limited number of reactors( in SysdbPlugins ), so event log are more intuitive.

Also note that if the mount points are huge, it may be worthwhile to split the
path among multiple subpaths so that a single entity walk doesn't take too long
causing ProcMgr to kill the agent. For example,"monitor /ar/Sysdb" can be replaced
with "monitor /ar/Sysdb/briding /ar/Sysdb/hardware ..."

Calling serviceProcMgr stoppm is anyway preferable just for safety when running
the monitor command.
'''

if __name__ == '__main__':
   def exitUsage():
      print( "Usage: EntityTreeMonitor <sysname> <agent> <cmd> ..." )
      print( "EntityTreeMonitor ar Sysdb monitor /ar/Sysdb/bridging/config ..." )
      print( "EntityTreeMonitor ar Sysdb blacklist /ar/Sysdb/bridging/config ..." )
      print( "EntityTreeMonitor ar Ebra summary" )
      print( "EntityTreeMonitor ar Ebra logging on|off" )
      sys.exit( 1 )

   if len( sys.argv ) < 4:
      exitUsage()
   sysname = sys.argv[ 1 ]
   agentName = sys.argv[ 2 ]
   cmd = sys.argv[ 3 ]
   if cmd not in [ 'monitor', 'summary', 'logging', 'blacklist' ]:
      exitUsage()
   mon = createRemoteMonitor( sysname=sysname, agentName=agentName )
   if cmd == 'monitor':
      mon.addPaths( sys.argv[ 4: ], changeAllowed=True )
   elif cmd == 'summary':
      if len( sys.argv ) != 4:
         exitUsage()
      mon.showSummary()
   elif cmd == 'blacklist':
      mon.blacklist( sys.argv[ 4: ] )
   else:
      assert cmd == 'logging'
      if len( sys.argv ) != 5:
         exitUsage()
      if sys.argv[ 4 ] not in [ 'on', 'off' ]:
         exitUsage()
      mon.loggingEnabledIs( sys.argv[ 4 ] == 'on' )
