Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I am writing a collectd nagios custom check script in bash. The problem I am having is that nagios shows hostnames as host.name.domain but collectd holds WSP files as host_name_domain. My question is how can I make a variable convert the hostname that it gets from nagios (host.name.domain) to collectd format (host_name_domain).

This is the part where the convertion has to occur. So the WSP_PATH would give out collectd format of hostname in variable $NHOST

WSP_PATH=/var/lib/carbon/whisper/ctd/$NHOST/uptime/uptime.wsp
share|improve this question

2 Answers 2

up vote 4 down vote accepted

Use sed:

$ NHOST="host.domain.tld"
$ NHOST=$(echo $NHOST | sed 's/\./-/g')
$ WSP_PATH=/var/lib/carbon/whisper/ctd/$NHOST/uptime/uptime.wsp
$ echo $WSP_PATH
/var/lib/carbin/whisper/ctd/host-domain/uptime/uptime.wsp

As above, /g is required so that all instances of . are replaced with -

Although on second thoughts, it would probably make more sense to perform this (sed) operation on the $NHOST variable (now edited to show this)

share|improve this answer
    
But it would be better to use sed on NHOST for sure. Because the sed at the moment would change the uptime.wsp to uptime-wsp and thats not what I would want. But this answer directed me to the right direction. Thank you! –  mYzk Aug 14 '14 at 8:26
    
Quite right! Answer edited –  GeoSword Aug 14 '14 at 8:30
5  
For such single letter translations, you could also use tr: NHOST=$(echo "$NHOST" | tr . _) –  Lekensteyn Aug 14 '14 at 10:47

Bash has the required functionality built in (unless it's an ancient version):

WSP_PATH=/var/lib/carbon/whisper/ctd/${NHOST//./-}/uptime/uptime.wsp
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.