Advertisment

Using Scripts to Automate Tasks

author-image
PCQ Bureau
New Update

In our last issue we took you through a Windows script that

helps you add users in a Windows Active Directory. Continuing this series

further, this month we will take you through a Linux script related to server

monitoring. Server and network monitoring is an important part of any enterprise

sysadmin's tasks. By 'monitoring' we mean an automated mechanism to test,

track and report on the availability and condition of the system services.  

Advertisment
Direct

Hit!
Applies

to:
Server and network admins
USP:

Learn to write a script to take snapshots of a Linux server and e-mail them 
Links:

http://www.tzscheutschler.de/ 
Google keywords:

Shell Scripts
On PCQLive CD:

pcq_labsmarch\Code\ mailsysinfo.sh and \sysinfo.sh 

This article's sample script takes a snapshot of things

like disk space, users logged in, mail queue, Inetd Services, RAM, Swap Space,

CPU SPEED, services, network connections, print queues etc and mails all the

details to you, thus, enabling you to do remote monitoring. We have put a copy

of this script on this month's PCQLive CD. There are two scripts-sysinfo.sh

and mailsysinfo.sh. The first one collects all the information of the server and

stores it in a htm file called  info.html in /tmp directory.Before you

start using the script make sure that a mail server is running on your Linux

server.

To schedule these scripts to run at particular time we are

going to use Cron daemon, which is a Linux task scheduler. Cron wakes up every

minute and looks for jobs it has to execute at that moment. Details of which job

to execute and when to execute are present in its configuration file called

crontab.

Advertisment
Our sysinfo script generates this output which is then e-mailed by mailsysinfo

There are different ways to use Cron. In the /etc directory

you will probably find some sub-directories called 'cron.hourly', 'cron.daily',

'cron.weekly' and 'cron. monthly'. If you place a script into one of those

directories it will be run either hourly, daily, weekly or monthly, depending on

the name of the directory. Here we want our script to run daily at particular

time. To edit the crontab file, browse to 

/etc/crontab 

Advertisment

and open crontab file with your favorite text editor. Its

will look something like this. Each line in this file has the following

format:

minute   hour

day   month   dayofweek   command

Now add the following line to it.

Advertisment

30 22 *  *  *

/root/desktop/mailsysinfo.sh


Where '*' is a wild-card meaning 'ALL', and

/root/desktop/mailsysinfo.sh is the path script is. You will have to change the

path according to where you have kept the scripts, but make sure that both the

scripts are in the same directory. Save the file and close it. Our script is

scheduled to run everyday at Minute="30" Hour="10">

10:30

PM.You can change the time to what you want but remember that cron works on a

24 hourly clock.

Script editing



The only script you need to edit here is mailsysinfo.sh, unless you are

planning to add new fields to sysinfo.sh. Here you need to change the e-mail

address.

Advertisment

Explanation



We shall now explain what both the scripts that we have discussed in this

story deliver.

Script 1: mailsysinfo.sh



The following script first executes sysinfo.sh file and then mails the

output file info.html using the mail utility.

/opt/sysinfo.sh; cat /tmp/info.html

| mail -s "subject of the mail" username@domain.com


Advertisment
Cron allows one to schedule commands to be run at intervals. Here we schedule our mailsysinfo script to run at 10:30 PM

The mail utility allows you to compose, send, receive,

forward and reply to mail. Here —s is for the subject and after it's the

e-mail address to which you want to send the e-mail.

Script 2: sysinfo.sh



This is the Script which retrieves all the information from the system and

saves the output in a html file.

Advertisment

HOSTNAME=`hostname`



VERSION=`cat /proc/version`


DATE=`date`


OUT="/tmp/info.html"



Here HOSTNAME and VERSION are two variables defined

containing the command hostname and VERSION contains version of the kernel

respectively. OUT is another variable which stores the path of the output file

of the script.

PMODEL=`cat /proc/cpuinfo |

grep vendor_id | awk -F\: '{print $2}'`



PNAME=`cat /proc/cpuinfo  | grep model | awk -F\: '{print $2}'`


PSPEED=`cat /proc/cpuinfo | grep MHz | awk -F\: '{print $2}' | awk -F\. '{print
$1}'`



In this part three variables are defined which store the

output of  /proc/cpuinfo according to the given parameters. /proc/ cpuinfo

contains information about the system CPU (or CPUs for a multi-processor

machine).  'awk' is a utility for handling text processing tasks.

With -F, awk uses a set of

patterns it reads from the file.



echo " Disk

Configuration" >> $OUT



df -hv  >> $OUT


echo "ifconfig -a"        >>
$OUT 



ifconfig -a
>> $OUT




Here 'df' displays the amount of disk space available

on the file system. '—h' argument prints the sizes in human readable

format.  'ifconfig —a' causes ifconfig to print information on all

interfaces.Other than simply e-mailing this information as shown above, it is

possible to build complex scripts that also perform corrective action when

problems arise like -clearing a disk of unnecessary file if the disk fills up.

So while you make yourself comfortable with this script, we assure you that you

won't leave empty handed.

Swapnil Arora

Advertisment