1826498 Members
1896 Online
109692 Solutions
New Discussion

Macro definitions in m4

 
Ralph Grothe
Honored Contributor

Macro definitions in m4

Hello,

I haven't done any m4 scripting before.
Nevertheless, I have been presumptuous enough
to believe that it only takes a little read of the m4 manpage to get me going.

Not quite...


Though this is excercised on a Linux Heartbeat cluster which serves an Apache I think it isn't really OS specific and equally applies to m4 on HP-UX.

I simply want to define an IP based virtual host container that should bind its listening socket to the current node's IP address.
Thus I thought this could easily be placed in an m4 template which is to be parsed by m4 through the Heartbeat Apache resource start/stop script right before it fires up the webserver, in order to produce that virtual host container with the fitting IP address.

You might ask what this is good for.
The productive webserver has its fixed IP address that travels together with the package
(the same as ServiceGuard's relocative IP).
This additional webserver is only required for maintenance of the cluster nodes.
E.g. I mirrored a MiniCPAN (abt. 400MB) which I want to have serviced by the virtual host
in case should immediate installation of additional CPAN modules be required on the cluster nodes.

Ok, here's the m4 mess I have started with,
but somehow I think I haven't apprehended the notion of m4 yet.
I thought it should be possible to simply assign a "macro", very much like a variable in any scripting language, only to be referrenced later.
But this doesn't work in my definition of get_ip.
You see, I don't want to repeat the long parsing command to retreive the IP address everywhere in situ where it is required.

When I have run this through m4



# cat vhosts.conf.mc
define(`get_ip' , /sbin/ifconfig eth0|awk -F: '$1~/inet addr/{print$2}'|tr -cd '[0-9.]')

ServerName syscmd(/sbin/ifconfig eth0|awk -F: '$1~/inet addr/{print$2}'|tr -cd '[0-9.]'):ifdef(`PO
RT', `PORT', 80)
ServerAdmin root@syscmd(/sbin/ifconfig eth0|awk -F: '$1~/inet addr/{print$2}'|tr -cd '[0-9.]')



then this is what I get


# m4 -DPORT=443 vhosts.conf.mc

:443>
ServerName 192.168.2.2:443
ServerAdmin root@192.168.2.2



So how do m4 definitions are applied correctly?

Regards
Ralph
Madness, thy name is system administration