#!/usr/bin/env python
"""
Show SNMP interface details
"""

import sys,os

from seine.snmp.script import SNMPScript,ScriptError
from seine.snmp import SNMPError,SNMP_VERSIONS
from seine.snmp.devices.process import SNMPProcessStatus
DEFAULT_CACHE_PATH = os.path.expanduser('~/.snmp_process_index.cache')

script = SNMPScript(index_cache_path=DEFAULT_CACHE_PATH)
script.add_argument('--update-indexes',action='store_true', help='Update device process index cache')
args = script.parse_args()

processes = SNMPProcessStatus(**script.client_kwargs)
if args.update_indexes:
    processes.update_indexes()

values = sorted(processes.indexes.values(),lambda x,y: cmp(x['mem'],y['mem']))
for v in filter(lambda v: v['mem']>0, values):
    try:
        script.message('%(mem)8s %(name)s %(args)s' % v)
    except KeyError,emsg:
        script.message('ERROR looking up "%s" from %s' % (emsg,v))

