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

'''
Eos Init script to report certain system values, installed as 
/usr/bin/EosInitReport. It is executed by 
/etc/rc.d/init.d/EosInitRptlate in the system bootup process.
'''

import os, re
os.environ['NOPDB'] = '1'

#must come after setting NOPDB.   
import Tac


def reportSysMem():
   # This function goes to the Linux meminfo file in
   # in /proc and reads the total memory value and 
   # then writes the value to the console.

   # Assuptions are made about the format of the meminfo file.
   # In particular, we search for 'MemTotal' to find the total
   # memory value.  If different versions of Linux write this
   # differently then this routine won't work.
   
   if os.path.exists( '/proc/meminfo' ):
      memFile = open( '/proc/meminfo', 'r' )

      # find the MemTotal line
      for line in memFile:
         if line.startswith( 'MemTotal:', 0 ):
            # make the regular expression to pull out the mem size
            bSizeExp = re.compile( r'\b\d+\.*\d* *\w*\b' )

            # the first occurance of a numeric field is the total.
            print "System RAM: " + bSizeExp.findall( line )[0]
            break
      else:
         # couldn't find the matching string.
         print "MemTotal not found in meminfo. Moving on..."
         
   else:
      # file not found
      print "Linux meminfo file not found. Moving on..."
   

def reportFlashMem():
   # This function calls the 'df' command to get the flash
   # memory information.  By the time this function is called
   # the flash is mounted and appears as a drive.

   # There are assumptions made in this routine that rely on
   # the string '/mnt/flash' being the name of the mount.
   # When breadth testing use a different path. Use the one
   # supplied by the caller in the environment variable altMountPt.
   flashDir = os.getenv( 'altMountPt', '/mnt/flash' )   

   # try capturing the text spewn forth by df.
   cmdOutput = Tac.run( [ "df", "-h", flashDir ], stdout = 
                        Tac.CAPTURE, stderr = Tac.DISCARD )

   # break df's output into individual text lines (2) and  
   # use the second line. 
   line = cmdOutput.splitlines()[1]

   # make the regular expression to pull out the mem size
   bSizeExp = re.compile( r'\b\d+\.*\d* *\w+\b' )

   # the first occurance of a numeric field is the total.
   print "Flash Memory size:  " + bSizeExp.findall( line )[0] 


   

def reportHwType():
   # This function goes to the EOS fdl file in /etc and reads
   # the Sku and Serial Number and reports them on the console.

   import Fdl

   # fdl file path is '/etc' except when breadth testing
   fdlDir = os.getenv( 'fdlFileLocation', '/etc' )

   fdlFilePath = os.path.join( fdlDir, 'fdl' )
   if os.path.exists( fdlFilePath ):
      fd = Fdl.fdlFromFile( fdlFilePath )

      # find the Sku
      skuStr = fd.get('Sku')
      if skuStr:
         print "Model: " + skuStr
      else:
         print "Model: unknown."       
       
      # find the Serial Number
      serNumStr = fd.get('SerialNumber')
      if serNumStr:
         print "Serial Number: " + serNumStr
      else:
         print "Serial Number: unknown."       
         
   else:
      #fdl file not found is an acceptable condition sometimes...
      print "Model and Serial Number: unknown"       
             
      


if __name__ == "__main__":   
   reportHwType()
   reportSysMem()
   reportFlashMem()


      
