Solved

scripting file understanding

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to get maximum transfer speed over LAN 4 84
High Available Storage based on linux 6 84
Run same command on multiple files in Linux 3 32
linux 13 50
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
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…
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.

803 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