Translating script to perl

awk/shell:

       

                UWARNING=90
                UCRITICAL=97

                PXNAME=$(echo $line | awk -F, '{print $1}')
                SVNAME=$(echo $line | awk -F, '{print $2}')

                FRUPERCENT=$(echo $line | awk -F, '{print ($5 / $7) * 100}')
                FRUSTATUS=$(echo $line | awk -F, '{print $18}')

                if [ "${FRUSTATUS}" = "OPEN" ] ; then

                        awk "BEGIN {

                                if($FRUPERCENT<$UWARNING)
                                        {
                                                print \"OK: svname($SVNAME)=${FRUPERCENT}% , STATUS_OPEN , pxname($PXNAME)\" ; exit 0 }

                                else if(($FRUPERCENT>=$UWARNING) && ($FRUPERCENT<$UCRITICAL))
                                        {
                                                print \"WARNING: svname($SVNAME)=${FRUPERCENT}% , STATUS_OPEN , pxname($PXNAME)\" ; exit 1 }
                                else if ($FRUPERCENT>=$UCRITICAL)
                                        {
                                                print \"CRITICAL: svname($SVNAME)=${FRUPERCENT}% , STATUS_OPEN , pxname($PXNAME)\" ; exit 2 }
                                else
                                        {
                                                print \"UNKNOWN: svname($SVNAME)=${FRUPERCENT}% , STATUS_OPEN , pxname($PXNAME)\" ; exit 3}
                                }"

                elif [ "${FRUSTATUS}" != "OPEN" ] ; then

                        awk "BEGIN {

                                if($FRUPERCENT<$UWARNING)
                                        {
                                                print \"CRITICAL: STATUS_NOTOPEN , svname($SVNAME)=${FRUPERCENT}% , pxname($PXNAME)\" ; exit 2 }
                                else if(($FRUPERCENT>=$UWARNING) && ($FRUPERCENT<$UCRITICAL))
                                        {
                                                print \"CRITICAL: STATUS_NOTOPEN , svname($SVNAME)=${FRUPERCENT}% , pxname($PXNAME)\" ; exit 2 }
                                else if ($FRUPERCENT>=$UCRITICAL)
                                        {
                                                print \"CRITICAL: STATUS_NOTOPEN , svname($SVNAME)=${FRUPERCENT}% , pxname($PXNAME)\" ; exit 2 }
                                else
                                        {
                                                print \"UNKNOWN: STATUS_NOTOPEN , svname($SVNAME)=${FRUPERCENT}% , pxname($PXNAME)\" ; exit 3}
                                }"
                fi

is there anyway this can be translated to perl? this is part of a very large script. if i can translate the above to perl, i can redo the other script based on how the above is translated to perl.

any help is much appreciated.

While not recommended, the perl monks admit this sh to PERL app exists: Clive Darke / App-sh2p-0.06 - search.cpan.org

1 Like

This isn't just shell script you have submitted so any translator is probably not going to work. This also contains awk script. There have been a number of times in my career where there just isn't an easy way to handle large and many times poorly written scripts. You just have to attempt to understand them in a top down fashion and re-write them. In this case, I always make sure I am not trying to correct something that really isn't broken. What problem will you solve by converting this to perl?

1 Like

hand translate yourself and learn some Perl. for example this

PXNAME=$(echo $line | awk -F, '{print $1}')

is just getting the first field of a comma delimited line. you can
use split() in Perl for this.

Skysmart,
Rather than asking someone to change entire script to perl :o , you could have understand the logic and ask doubts in that.

By seeing, it is not a very big script. It just have some "if" conditions.
So try to do it yourself and ask any doubts :rolleyes: either in bash or in perl.

Happy coding :b:

it isn't by choice that i have to write this in perl. i've been doing this long enough to know the language used to write a script is utterly irrelevant as long as you know precisely what it is you want and you also make efficiency a number 1 priority to you. in this case, the above script needs to be translated to perl, just because of the culture of the environment here. perl is all they know. so yeah. there's really nothing i have to solve. awk has been more than enough for me in the past. if things really need to get really fancy, then php to the rescue.

The less an author knows, the longer, more fragile and more complicated the script. This problem might be withing the reach of bash or ksh, if the problem and tool are both clearly understood.