Pulling correct value from Mapfile


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pulling correct value from Mapfile
# 1  
Old 07-03-2009
Pulling correct value from Mapfile

Hi all,

I am trying to pull one value from a Mapfile, but the result is that I am getting all the values. The Mapfile maps the host name from the Backup Server to the host name in our Network Monitoring Server, as shown here:

Mapfile
Code:
cat zabbixhosts 
Helpdesk-fd:Server_Helpdesk
Knowledge-fd:Server_Knowledge
BaculaServer-fd:Server_Bacula
Zabbix-fd:Server_Zabbix_1.4.2


The section of script that is failing is here:
Code:
client=$(tail -n 38 /var/lib/bacula/log | grep -m 1 -F "Client" | awk -F" " '{ print $2; }')
echo Client is $client
echo

# get hostname from the Hostsfile
ZABBIX_HOST=`grep "[$client]" $ZABBIX_HOSTSFILE`

if [ $? -eq 0 ]; then
        hostname=`echo "$ZABBIX_HOST" | cut -f2 -d:`
else
        hostname=""
fi
echo Hostname is $hostname

When I run that script the output is as follows:

Code:
./bacula2zabbix.sh 
Client is "BaculaServer-fd"

Hostname is Server_Helpdesk Server_Knowledge Server_Bacula Server_Zabbix_1.4.2


Any advice would be greatly appreciated.
Thanks.

Last edited by MrKen; 07-03-2009 at 03:16 AM.. Reason: Bad cut n paste
# 2  
Old 07-03-2009
Did you initialize host file name?

I tried the same code snippet.The result is

Client is "BaculaServer-fd"

Hostname is Server_Bacula

ZABBIX_HOSTSFILE="path of map file"
I made two changes:

1.Initialized ZABBIX_HOSTSFILE to map file name


2.Removed []
grep "[$client]" $ZABBIX_HOSTSFILE
grep "$client" $ZABBIX_HOSTSFILE
# 3  
Old 07-03-2009
Thanks for the quick reply.

When you say 'initialize' the host file name, I guess you mean do I have this:

ZABBIX_HOSTSFILE="/etc/bacula/zabbixhosts";

Yes, I have that already. That's why there is a result to the output in the first post.

When I remove the [ ] as suggested, the output is now:

Code:
Client is "BaculaServer-fd"

Hostname is

Any more ideas?
# 4  
Old 07-03-2009
Code:
hostname=`grep $client zabbixhosts | cut -f2 -d:`

I tried to simulate your scenario in my linux machine. Assuming that your client variable contains <BaculaServer-fd >.
From the above code, variable hostname contains Server_Bacula if grep is successfull and will contain no value if it fails. Something like this....

Code:
$ cat zabbixhosts
Helpdesk-fd:Server_Helpdesk
Knowledge-fd:Server_Knowledge
BaculaServer-fd:Server_Bacula
Zabbix-fd:Server_Zabbix_1.4.2
 
$ echo $client
sai
 
$  hostname=`grep $client zabbixhosts | cut -f2 -d:` ; echo $hostname
 
$ client="BaculaServer-fd"
 
$ hostname=`grep $client zabbixhosts | cut -f2 -d:` ; echo $hostname
Server_Bacula
 
$ hostname=`grep -w $client zabbixhosts | cut -f2 -d:`; echo $hostname
Server_Bacula

# 5  
Old 07-03-2009
reddybs, thanks for your reply.

If I change "[$client]" to $client, then I get no hostname output.
If I use -w with grep, then I get no hostname output.

So now I have this, but still the output is incorrect.
Code:
echo Client is $client
echo

# get hostname from the Hostsfile
hostname=`grep "[$client]" $ZABBIX_HOSTSFILE | cut -f2 -d:` ; echo $hostname

and the output:
Code:
Client is "BaculaServer-fd"

Server_Helpdesk Server_Knowledge Server_Bacula Server_Zabbix_1.4.2

Any more ideas?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Partial file pulling

I am connecting to another server through sftp. I am running one batch script to pull file from another server. sometimes i am receiving partial files. I am using below commands in batch script. ls -ltr new.txt mget new.txt bye The file is of 1 MB only.In most of the cases , i received... (6 Replies)
Discussion started by: srinath01
6 Replies

2. Shell Programming and Scripting

Pulling data from xml

Hi there, Please could anyone help with this. I have an xml file that contains repeating values eg <Rule name> AAAAA <Action> BBBBB </Action> <Data> CCCCC </Data> <Type> DDDDD </Type> </Rule name> <Rule name> A1A1A1A1 <Action> B1B1B1B1 </Action> <Data> C1C1C1C </Data> <Type>... (4 Replies)
Discussion started by: ssideel
4 Replies

3. Shell Programming and Scripting

Strange behavior with readarray (aka mapfile)

$ readarray list <<< "a b c d e f g" $ echo ${list} a b c d e f g $ echo "a b c d e f g" | readarray list2 $ echo ${list2} a blank lineI read a post the same thing happens with cat vs < on another forum but the poster did not know why. Any ideas? Mike (3 Replies)
Discussion started by: Michael Stora
3 Replies

4. Shell Programming and Scripting

Pulling remote hostname into variable

I'm trying to write a shell script using bash that connects to a remote server, runs a command that generates a file, pulls the file over, then renames it with the hostname of the remote server and a an extension. So far, I'm able to everything but pull the hostname of the remote server into a... (2 Replies)
Discussion started by: hobbes80
2 Replies

5. Shell Programming and Scripting

pulling out the value from rows

Hi, I'm trying to pull all occurrence of value , for example "xy1234" from several rows, out of a file. The file might as well contain other different type of rows. 20100121 04:00:02 37a00452 <ABCN:ABXC> From MV Modify <uid=xy1234,ou=Internal2,ou=people,dc=abc,dc=example1,dc=com> I am trying... (4 Replies)
Discussion started by: john_prince
4 Replies

6. Shell Programming and Scripting

Capacity of directory... Pulling hair out :-)

I am new to scripting and thought I was doing rather well however I ran into a issue and I am not sure how to fix it. I am using the following command to obtain the capacity percent of the directory listed however it seems that this command gets the capacity of the whole mount rather then just the... (8 Replies)
Discussion started by: LRoberts
8 Replies

7. Shell Programming and Scripting

Pulling the first and last character/number from a string.

Let's say I have a word "foobar23" in a file, and I want to pull the first "f" and last "3" character out of the world, how would I accomplish that? # cat file foobar23 I want the output to be: f3 (3 Replies)
Discussion started by: LinuxRacr
3 Replies

8. Shell Programming and Scripting

Pulling data and following lines from file

I saw a few posts close to what i want to do, but they didn't look like they would work exactly.. or I need to think out of the box on this. I have a file that I keep server stats in for my own performance analysis. this file has the output from many commands in it (uptime, vmstats, ps, swap... (2 Replies)
Discussion started by: MizzGail
2 Replies

9. UNIX for Dummies Questions & Answers

pulling the following line from a file

I have return files from a process that has then original input record followed on the next line by a response record..either AA,........... for accepted or EE,.......... for errored. i.e 11,new,123 AA,accepted 12,exist,443 EE,rejected 13,old,223 AA,accepted I want to write a small... (4 Replies)
Discussion started by: peter.herlihy
4 Replies

10. UNIX for Advanced & Expert Users

Pulling out fields from a file

Hi, I have a file that contains 1400 lines similar to the one shown below: NAME=sara, TOWN=southampton, POSTCODE=SO18777, EMAIL=sara@hotmail.com, PASSWORD=asjdflkjds etc etc (note: this is one line). Each line has the same fields, but on each line they are in a different order. Eg. the line... (2 Replies)
Discussion started by: Saz
2 Replies
Login or Register to Ask a Question