Solved

scripting file understanding

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
centos linux 65 159
Delete a folder on a linux computer on a regular basis 10 41
open source backup solution 1 36
awk variable in printf 1 34
Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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…

820 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