The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
processing file names using text files ligander Shell Programming and Scripting 5 12-01-2008 06:32 PM
Problems with extracting information c0mrade Shell Programming and Scripting 0 11-17-2008 04:17 PM
Extracting data from text file based on configuration set in config file suparnbector Shell Programming and Scripting 3 08-10-2007 02:25 AM
Extracting information from text fields. spindoctor UNIX for Dummies Questions & Answers 24 06-09-2007 01:17 PM
Extracting information from a template Ernst Shell Programming and Scripting 4 03-07-2007 01:18 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-20-2009
oconmx oconmx is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 9
Question Extracting information from Config files /text processing

Hello All,

This is my first post on this forums, which I consider one of the best of its kind. The reason for my post is that I want to export some information form Nagios configuration files to a DB. I know that there are other tools available to do this, like NDO, monarch, etc... But I want to do it my self as I have different requests and I will like to improve my scripting capabilities

This is an example from hosts.cfg file:

Code:
# 'SACVMW02' host definition
define host{
        use                     generic-host            ; Name of host template to use
        host_name               SACVMW02
        alias                   ESX SERVER
        address                 10.10.10.110
        check_command           check-host-alive
        contact_groups          NETWORKSGDL,CSG,DBAS,OPSGSSC
        notification_interval   30
        notification_period     24x7
        notification_options    d,r
        }

# 'CALUX901' host definition
define host{
        use                     generic-host            ; Name of host template to use
        host_name               CALUX901
        alias                   Calgary Unix Servers
        address                 10.120.240.16
        check_command           check-host-alive
        contact_groups          MONITORING,CAL-UNIX-ADMINS,NETWORKSGDL
        notification_interval   60
        notification_period     24x7
        notification_options    d,r
        }
I will like to know if there is a way to use a for loop that reads the text inside the { }'s, to define the first column inside it as a variable (or array in case of contact_groups, which has many comma separated values) and second column as value. It will also have to discard comments (# and ; lines)

Any suggestion is welcome, I already did some scripts that do the job using grep, sed and awk; reading line by line, but I think that there are other easier approaches that might improve the script, like reading text blocks instead of lines....

Thank you all!
Carlos
  #2 (permalink)  
Old 01-21-2009
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,078
hope below perl can help a little.

Just put them in 8 different array.

Code:
#!/usr/bin/perl
$n=0;
open FH,"<a.txt";
while(<FH>){
	next if /(\{|#)/;
  $n++ if /\}/;
	my @tmp=split(/  */,$_,3);
	$tmp[2]=~s/;.*//;
	$user[$n]=$tmp[2] if $tmp[1] eq "use";
	$host_name[$n]=$tmp[2] if $tmp[1] eq "host_name";
	$alias[$n]=$tmp[2] if $tmp[1] eq "alias";
	$address[$n]=$tmp[2] if $tmp[1] eq "address";
	$check_command[$n]=$tmp[2] if $tmp[1] eq "check_command";
	$contact_groups[$n]=$tmp[2] if $tmp[1] eq "contact_groups";
	$notification_interval[$n]=$tmp[2] if $tmp[1] eq "notification_interval";
	$notification_period[$n]=$tmp[2] if $tmp[1] eq "notification_period";
	$notification_options[$n]=$tmp[2] if $tmp[1] eq "notification_options";
}
  #3 (permalink)  
Old 01-21-2009
oconmx oconmx is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 9
Quote:
Originally Posted by summer_cherry View Post
hope below perl can help a little.

Just put them in 8 different array.

Code:
#!/usr/bin/perl
$n=0;
open FH,"<a.txt";
while(<FH>){
	next if /(\{|#)/;
  $n++ if /\}/;
	my @tmp=split(/  */,$_,3);
	$tmp[2]=~s/;.*//;
	$user[$n]=$tmp[2] if $tmp[1] eq "use";
	$host_name[$n]=$tmp[2] if $tmp[1] eq "host_name";
	$alias[$n]=$tmp[2] if $tmp[1] eq "alias";
	$address[$n]=$tmp[2] if $tmp[1] eq "address";
	$check_command[$n]=$tmp[2] if $tmp[1] eq "check_command";
	$contact_groups[$n]=$tmp[2] if $tmp[1] eq "contact_groups";
	$notification_interval[$n]=$tmp[2] if $tmp[1] eq "notification_interval";
	$notification_period[$n]=$tmp[2] if $tmp[1] eq "notification_period";
	$notification_options[$n]=$tmp[2] if $tmp[1] eq "notification_options";
}
Great! it works like a charm.

Another quick question Summer_cherry, how can I do a multi-dimensional array for Contact Groups, Notifications periods and notification options? As you can see in the example, those have different options separated by comma. Is there an easy way to extract those ?

Again, thank you for your support, your script was very helpful and by far better than my existing solution

Regards,
Carlos
  #4 (permalink)  
Old 01-21-2009
oconmx oconmx is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 9
I answer to my self, I used split()

Thank you!
Carlos
Sponsored Links
Closed Thread

Bookmarks

Tags
awk, nagios, scripting, sed, text processing

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:22 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0