Solved

scripting file understanding

Posted on 2011-03-05
5
292 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now