Solved

scripting file understanding

Posted on 2011-03-05
5
296 Views
Last Modified: 2012-05-11
Hi I am trying to understand this file.
can somebody please explain me its bits

[sml@sys19][~] - cat /net/isilon/ifs/systems/bin/check_lics
#!/bin/bash

# Checking for your expirationzzzzzz
#  29.09.2010

# Setting run mode
WARNING=0

# Setting warning threshold in days
THRESHOLD=11

# Give usage if someone provides a crappy parameter
if [[ ! -z $1 ]]; then
        if [[ "$1" = "--warning" ]]; then
                WARNING=1
        else
                echo "Usage $0 [--warning]"
                exit 1
        fi
fi

# Define your ports and servers here
NUKE='@wiretap'
FUSION='/mnt/licserv1/FlexLM/DFusion.dat'
MAYA='27002@plcserv1'
FRANTIC='27000@plcserv1'
DEFAULT='27000@plcserv1'
MOCHA='27003@plcserv1'

# Tell us which of these to check
LICS=(NUKE FUSION MAYA FRANTIC MOCHA DEFAULT)

# This bit actually does the checking
function getlics {
        plcserv1=$1

        TMPFILE=/tmp/lic-checker-temp-$!
        > $TMPFILE

        FEATURE=""; EXPIRY=""; EPOCH=""; NOW=""; REMAIN=""

        # Get the license expiry information out
        /apps/common/bin/lmdiag -c $plcserv1 -n | grep -B 6 'can be checked' | while read LINE; do
                if [[ "$LINE" =~ 'vendor:' ]]; then
                        FEATURE=`echo $LINE | awk '{print $1}' | sed -e 's/"//g'`
                fi
                if [[ "$LINE" =~ 'expir' ]]; then
                        if [[ "$LINE" =~ 'no expiration' ]]; then
                                EXPIRY="Permanent"
                        else
                                EXPIRY=`echo $LINE | awk '{print $NF}'`
                        fi
                fi
                if [[ "$LINE" =~ 'can be checked' ]] && [[ ! "$EXPIRY" =~ 'Permanent' ]]; then
                        # We have all the info we need for a non-perm license now so output it
                        EPOCH=`date --date="$EXPIRY" +%s`
                        NOW=`date --date='now' +%s`
                        REMAIN=`echo "(($EPOCH-$NOW)/86400)+1" | bc`

                        # Only output licenses with a long time until
                        # expiration if it's not in warning mode
                        if [[ "$REMAIN" -lt "$THRESHOLD" ]] || [[ "$WARNING" -eq "0" ]]; then
                                echo -e "$FEATURE \t $EXPIRY \t $REMAIN days remaining" >> $TMPFILE
                        fi

                        # Variable reset for next license
                        FEATURE=""; EXPIRY=""; EPOCH=""; NOW=""; REMAIN=""
                else
                        if [[ "$LINE" =~ 'can be checked' ]] && [[ "$EXPIRY" =~ 'Permanent' ]] && [[ "$WARNING" -ne "1" ]]; then
                                # This is for permanent licenses which are only output if
                                # Not in warning mode
                                echo -e "$FEATURE \t $EXPIRY" >> $TMPFILE

                                # Variable reset for next license
                                FEATURE=""; EXPIRY=""; EPOCH=""; NOW=""; REMAIN=""
                        fi

                fi
        done

        # Flex has a habit of doubling up
        # Only output anything if there's license info to show
        if [[ `cat "$TMPFILE" | wc -l` -ne "0" ]]; then
                echo $2
                cat $TMPFILE | sort | uniq
                echo ""
        fi

        # Get rid of tempfiles
        rm -f $TMPFILE
}

# Main loop
for LIC in "${LICS[@]}"; do
        getlics ${!LIC} $LIC
done
0
Comment
Question by:aashee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
5 Comments
 
LVL 31

Expert Comment

by:farzanj
ID: 35044238
#Setting variables to be used later
# Define your ports and servers here
NUKE='@wiretap'
FUSION='/mnt/licserv1/FlexLM/DFusion.dat'
MAYA='27002@plcserv1'
FRANTIC='27000@plcserv1'
DEFAULT='27000@plcserv1'
MOCHA='27003@plcserv1'

MADE A LIST STRUCTURE TO RUN IN A LOOP
# Tell us which of these to check
LICS=(NUKE FUSION MAYA FRANTIC MOCHA DEFAULT)

#Remove all quotations from the line
sed -e 's/"//g'

# This bit actually does the checking
function getlics {
        plcserv1=$1

        TMPFILE=/tmp/lic-checker-temp-$!
        > $TMPFILE

        FEATURE=""; EXPIRY=""; EPOCH=""; NOW=""; REMAIN=""

#Get the output of the script in italics.  If vendor is found, get the first field of the line and remove all quotations.  Save it into the variable FEATURE
        # Get the license expiry information out
        /apps/common/bin/lmdiag -c $plcserv1 -n | grep -B 6 'can be checked' | while read LINE; do
                if [[ "$LINE" =~ 'vendor:' ]]; then
                        FEATURE=`echo $LINE | awk '{print $1}' | sed -e 's/"//g'`
                fi
if expir is found in a line, check it also has no expiration.  If so, variable EXPIRY is set to Permanent, otherwise, it is set to the last field of the line

                if [[ "$LINE" =~ 'expir' ]]; then
                        if [[ "$LINE" =~ 'no expiration' ]]; then
                                EXPIRY="Permanent"
                        else
                                EXPIRY=`echo $LINE | awk '{print $NF}'`
                        fi
                fi
0
 
LVL 31

Expert Comment

by:farzanj
ID: 35044306
If a line contains can be checked and variable EXPIRY for this line doesn't contain the word Permanent then EPOCH would be the numbers of seconds since 1970 and NOW is also the number of seconds since 1970.  Subtracting both and dividing by the number of seconds in a day 86400, would be the REMAIN (add one day to it by using a calculator bc
if [[ "$LINE" =~ 'can be checked' ]] && [[ ! "$EXPIRY" =~ 'Permanent' ]]; then
                        # We have all the info we need for a non-perm license now so output it
                        EPOCH=`date --date="$EXPIRY" +%s`
                        NOW=`date --date='now' +%s`
                        REMAIN=`echo "(($EPOCH-$NOW)/86400)+1" | bc`

#If the remaining number of days is less than the THRESHOLD value OR WARNING is zero, put value of FEATURE <tab> EXPIRY <tab> REMAIN days remaining into the temp file set above.  The words in capitals are variables
                        # Only output licenses with a long time until
                        # expiration if it's not in warning mode
                        if [[ "$REMAIN" -lt "$THRESHOLD" ]] || [[ "$WARNING" -eq "0" ]]; then
                                echo -e "$FEATURE \t $EXPIRY \t $REMAIN days remaining" >> $TMPFILE
                        fi

                        # Variable reset for next license basically setting variables to nothing
                        FEATURE=""; EXPIRY=""; EPOCH=""; NOW=""; REMAIN=""
                else
                        OTHERWISE
0
 
LVL 31

Expert Comment

by:farzanj
ID: 35044367
This is otherwise to the first condition where it was checking if [[ "$LINE" =~ 'can be checked' ]] && [[ ! "$EXPIRY" =~ 'Permanent' ]]
If the line contains the phrase can be checked, EXPIRY variable contains 'Permanent' and WARNING variable is not one.
Put in the TEMPFILE value of <FEATURE <tab> EXPIRY <tab>

if [[ "$LINE" =~ 'can be checked' ]] && [[ "$EXPIRY" =~ 'Permanent' ]] && [[ "$WARNING" -ne "1" ]]; then
                                # This is for permanent licenses which are only output if
                                # Not in warning mode
                                echo -e "$FEATURE \t $EXPIRY" >> $TMPFILE

                                # Variable reset for next license
                                FEATURE=""; EXPIRY=""; EPOCH=""; NOW=""; REMAIN=""
                        fi

                fi
        done
0
 
LVL 31

Expert Comment

by:farzanj
ID: 35044384
#Read the temp file only if it contains something.  Then display the second parameter followed by the file with sorted and no duplicate lines
 # Flex has a habit of doubling up
        # Only output anything if there's license info to show
        if [[ `cat "$TMPFILE" | wc -l` -ne "0" ]]; then
                echo $2
                cat $TMPFILE | sort | uniq
                echo ""
        fi

       # Get rid of tempfiles
        rm -f $TMPFILE
0
 
LVL 31

Accepted Solution

by:
farzanj earned 500 total points
ID: 35044404
Looping through all the servers/ports declared above

# Main loop
for LIC in "${LICS[@]}"; do
        getlics ${!LIC} $LIC
done
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Network Interface Card (NIC) bonding, also known as link aggregation, NIC teaming and trunking, is an important concept to understand and implement in any environment where high availability is of concern. Using this feature, a server administrator …
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question