Automate ,automate automate but why??

I have been asked by many people how important is the knowledge of scripting of native shell or at least one other common languages i.e perl,python and php ? My answer is two fold,first a person claimed to be accustomed with the open system must be very comfortable with it,means scripting. Second it’s a rule of thumb that if some similar or redundant work has to be done twice on the any given day then it should be automated.Now I should add a third point here if anybody wants to best out of the systems as well want to do some customization then scripting is way,moreover it’s very true for proprietory Unices than GNU/Linux.

Now what to automate and how to automate?? Not everything should be automated on the system nor is recommended.So one has to have an idea what task has to be accomplished and in what manner to get the maximum out of the systems.

Over the years I have had done some automation job ..although you might find it very trivial.But the situation ,where it had been created has got utmost importance.Taking out any particular issue will throw some light on it.

I can instantly remember few,one writing Nagios plugin for a specific issue ,update svn repositories after certain code upload,checking the log file in some specific manner to name a few. Like can I check the open port of about 50 servers at once? Will I be able to run a specific configuaration on some specific hosts? Ok I know you are guessing right..lot of wonderful tools are available in the market free and non-free will do that kind of job.And I have had accustomed myself quite a few of them and use it almost regularly.

Knowing bash or korn scripting surely have an edge over anything.Because this are the shells along with csh come along with the open systems.Now days lot many are with those systems i.e zsh. But whatever the environment you choose to operate on ,one must have an idea how thing go underneath. Plus one has to have an clear cut mandate that what will be outcome and how those things going to be impact on the systems.

Automation can not be done at the cost of efficiency rather enhance it. Say for instance I want to check my LVM partition space properly  so I create a python script for that fuse it into the crontab call it after certain intervals.You on open system you can do the same lot many ways …it all about preferences and comfort..so the script is below:

#!/usr/bin/python
import re,sys,commands

#################
#Set variables
command = “df -h /lvm”
critical = 95.0
warning = 75.0
#################

#build regex
dfPattern = re.compile(‘[0-9]+’)

#get disk utilization
diskUtil = commands.getstatusoutput(command)

#split out the util %
diskUtil = diskUtil[1].split()[11]

#look for a match. If no match exit and return an
#UNKNOWN (3) state to Nagios

matchobj = dfPattern.match(diskUtil)
if (matchobj):
diskUtil = eval(matchobj.group(0))
else:
print “STATE UNKNOWN”
sys.exit(3)
#CRITICAL = 2
#WARNING = 1
#OK = 0
if diskUtil >= critical:
print “FREE SPACE CRITICAL: ‘/lvm’ is %.2f%% full” % (float(diskUtil))
sys.exit(2)
elif diskUtil >= warning:
print “FREE SPACE WARNING: ‘/lvm’ is %.2f%% full” % (float(diskUtil))
sys.exit(1)
else:
print “FREE SPACE OK: ‘/lvm’ is %.2f%% full” % (float(diskUtil))
sys.exit(0)

Surely this script is not the benchmark and it has got influence from the other script.Now if I run the script I will get information like this:

bhaskar@bhaskar-laptop_06:08:25_Wed Sep 01:/lvm/Adm_scripts> sudo /usr/bin/python check_lvm.py
FREE SPACE OK: ‘/lvm’ is 37.00% full

Yup, pretty trivial but very informative,scope are there in the script to enhance it according the requirement. It can be accomplish in various other ways too as I said before.

Next I going to present you a script which I wrote to check for Tripwire(a file integrity checker),whether it installed in the system or not:

#!/bin/bash
#This is script which helps to monitor file system provided Tripwire is installed in the system.
HOST_NAME=’uname -n’
if [ ! -e /var/lib/tripwire/${HOST_NAME}.twd ] ; then
echo “**** Error: Tripwire database for ${HOST_NAME} not \
found. ****”
echo “**** Run “/etc/tripwire/twinstall.sh” and/or “tripwire\
—init”. ****”
else
test -f /etc/tripwire/tw.cfg && /usr/sbin/tripwire —check
fi

See a very trivial but essential ,as it spit out required info.You don’t need to reinvent the wheel if it’s there to help or somebody else done the job already..but please don’t forget to give the credit to the original authors.Likewise if I want I can enhance the script with many more thing,but what’s the point to bloated a neat thing.Plus following original Unix philosophy is good .

Hope this will help.

Cheers!

Bhaskar

About unixbhaskar
GNU/Linux Consultant

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: