Operating System - HP-UX
1833515 Members
3055 Online
110061 Solutions
New Discussion

Create a lookup file or an array

 
SOLVED
Go to solution
James Andertom
Esteemed Contributor

Create a lookup file or an array

Hi All,

I was looking for some ideas on a config file which will be used as a lookup.

What I am trying to achieve is I will be running a monitor script and when I receive an elert I would like to then check a config file to see who is the support team and the support manager. This will be based on the server name, application and maybe some other data I receive.

I know I could have an entry in a config file like below and then use a while loop or a for loop to read in each line.

# Support Team Manager Server App Object
Email Email Joe Bloggs eclipse Exch mail

The while loop would then read in the file and check the detailes against the alert that has been generated from the server and send an email to the correct support team and manager if necessary. Do you think the above method is a good way to do this or any ideas on keeping this data so it is easy to maintain and add to or delete.

Thanks
11 REPLIES 11
A. Clay Stephenson
Acclaimed Contributor

Re: Create a lookup file or an array

The one thing that I would suggest is that you add some sort of delimiter other than between the fields so that the fields themselves might contain spaces. I am a big fan of myself rather than colon's or comma's because I just might legitimately need those characters as part of a field.

What you describe would be very easy and fast in Perl, also awk, and doable in the shell.
I would lean towards Perl.
If it ain't broke, I can fix that.
Sridhar Bhaskarla
Honored Contributor

Re: Create a lookup file or an array

Hi,

My vote goes to the lookup file. This way you don't have to modify your script and you can give control to others to modify only the configuration.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Caesar_3
Esteemed Contributor

Re: Create a lookup file or an array

Hello!

This looks ok just use sleep in your loop
you don't need to check about 50 times in sec.

Also check that you work with file so if you
open it close in the end (all in loop)

In the end see that this script when he run
the preformance of running.

Caesar
Jim Mallett
Honored Contributor

Re: Create a lookup file or an array

I'm doing something along those lines where I have a file called Phones.db and it contains contact information for my department.
I then have a script monitoring for particular issues and if one arises, depending on the issue I just do a for loop against the Phones.db file and a grep for the name I want. When a match is found, I pass that variable to sendmail and send a message out to that users phone.

excerpt from Phones.DB
Name Phone# Email
Jim XXXXXX XXXXX
Bob XXXXXX XXXXX

excerpt from Monitor.sh
for i in `grep $USER Phones.DB | /usr/bin/awk '{print$2}'`

I would probably use Perl as Clay said but don't know it yet, I will soon hopefully though as I picked up a couple of books he recommended.

Jim Mallett




Hindsight is 20/20
Chris Vail
Honored Contributor

Re: Create a lookup file or an array

The world-class cinch way to do this is to put the server name in front of every chunk of data. Then do something like:
for USERINFO in `grep HOSTNAME /usr/configfile|awk '{ print $2,$3, $4 }'
do
echo $USERINFO
done




Chris
curt larson_1
Honored Contributor

Re: Create a lookup file or an array

your application is only going to be as fast as it can get the information. And, reading from a file line by line is about as slow as it gets. But, it seems like your only going to have a few lines to deal with and it shouldn't impact performance very much. And, your scripting is going to be simplier and faster to complete.

something faster, but more complex would be to do something like NIS and create files with appropriate indexs from your configuration file. Then, instead of looping through every line, a simple grep using the index will return your desired information.

This method does require updating your index files whenever there is a change to your configuration file, creating an extra step whenever a change is made. But, your probably going to create a front end script to make changes to your configuration file.

A script that asks the user for the required information and does some error checking/data validation before adding/deleting it to/from your configuration file. This front end would also limit one user at a time to be making changes.

It would be easy to add your index updating to such a front end. But, while simple to do it still requires someone to write and that will take time. And, the extra step adds complexity.

NIS already has the scripts to index things like the password file, the hosts file, email aliases, etc., so it isn't like your starting from stratch. You only have to make copies and then modify your copies to slice and dice your configuration file appropriately for your application.

Not only will indexing make your application run faster but by properly organizing your data, it will be simplier to write and therefore, easier to maintain also.
Donny Jekels
Respected Contributor

Re: Create a lookup file or an array

the fastest way to do lookups in perl is to use hashes.

%oncall_person ( manager -> 1232342424,
test -> 32323,
amen -> 666 ),


this will always remain in memory - fastest way to lookup - lookup tables.
"Vision, is the art of seeing the invisible"
James Andertom
Esteemed Contributor

Re: Create a lookup file or an array

Hi All,

Thanks for all the helpful feedback. I will be using a shell script to search for the entry in the config file as I do not know perl as well as shell scripting. Its another thing to put on my long list of things to learn!!

Curt, I do not have any experience with NIS, so I was wondering if you could elaborate on how NIS uses index files and then checks the config file so I can then decide if it is a better solution.

Thanks again to everyone for their input.

Regards
curt larson_1
Honored Contributor

Re: Create a lookup file or an array

 
curt larson_1
Honored Contributor

Re: Create a lookup file or an array

curt larson_1
Honored Contributor
Solution

Re: Create a lookup file or an array

lets see if we can get a perl script for returning a key value. I'm not a perl programmer either, so if those of your that are have suggestions, please give them

#!/usr/local/bin/perl -w
#
# output the value of a key from a DBM
# database given the database name and key.
#
use Fcntl;
use NDBM_File;

if ($#ARGV != 2) {
print "usage: $ARGV[0] databaseName keyValue\n";
exit 1;
}

$dbName = $ARGV[1];
$key = $ARGV[2];

tie %hash, "NDBM_File", $dbName, O_RDONLY|O_EXCL, 0644; ||
die "Cannot open database file, $dbName: $!\n";

$value = $hash{$key};
if (!defined($value)) {
print "$key not stored\n";
exit 2;
} else {
print "$value\n";
}

untie %hash;
exit 0;

that should be a good start on a perl script for returning a value from a DBM database given the database name and key.