Problem with program() destination
Hello, I'm trying to use the destination program() to forward all dhcp-related messages to a php program. The php program runs fine when I start it on the command line. My configuration in syslog-ng.conf seems to be right because when I use the "/bin/cat/" destination all dhcp messages are written to /tmp/cat.log. filter f_dhcp { facility(daemon) and match("dhcpd");}; #destination dhcpd { program("/bin/cat > /tmp/cat.log");}; destination dhcpd { program("/tmp/dhcp2.php");}; log { source(src); filter(f_dhcp); destination(dhcpd);}; /tmp/dhcp2.php: #! /usr/bin/php <?php $std=fopen("php://stdin","r"); $line = fgets($std,1024); $file=fopen("/tmp/dhcp.log","w+"); fwrite($file,$line); fclose($file); fclose($std); Can anyone give me a hint what I'm doing wrong? Thanks in advance, Stefan -- ***************************************** in-put GbR - Das Linux-Systemhaus Stefan-Michael Günther Moltkestraße 49 D-76133 Karlsruhe Tel./Fax : +49 (0)721 / 83044 - 98/93 http://www.in-put.de ***************************************** -- GMX im TV ... Die Gedanken sind frei ... Schon gesehen? Jetzt Spot online ansehen: http://www.gmx.net/de/go/tv-spot
$std=fopen("php://stdin","r"); $line = fgets($std,1024);
I guess you need to enclose the whole reading-from-a-file method into an endless loop. The fgets($source,$bufsize) function reads $bufsize amount of chars from $source, so you're reading a 1024 chars long line only. Tamas
$std=fopen("php://stdin","r"); $line = fgets($std,1024);
I guess you need to enclose the whole reading-from-a-file method into an endless loop. The fgets($source,$bufsize) function reads $bufsize amount of chars from $source, so you're reading a 1024 chars long line only.
you mean something like this: #! /usr/bin/php <?php $std=fopen("php://stdin","r"); while($line = fgets($std,1024)){ $file=fopen("/tmp/dhcp.log","w+"); fwrite($file,$line); fclose($file); } fclose($std); ?> Sorry, no entries in /tmp/dhcp.log. Stefan -- ***************************************** in-put GbR - Das Linux-Systemhaus Stefan-Michael Günther Moltkestraße 49 D-76133 Karlsruhe Tel./Fax : +49 (0)721 / 83044 - 98/93 http://www.in-put.de ***************************************** -- GMX im TV ... Die Gedanken sind frei ... Schon gesehen? Jetzt Spot online ansehen: http://www.gmx.net/de/go/tv-spot
$file=fopen("/tmp/dhcp.log","w+");
Try changing "w+" to "a". Mode "w+" means that the filepointer is set to the BEGINNING of the file so every separate flow coming from stdin is overwriting the file again and again. Mode "a" is append mode (filepointer at the end, appending to the file). Tamas
$file=fopen("/tmp/dhcp.log","w+");
Try changing "w+" to "a".
Mode "w+" means that the filepointer is set to the BEGINNING of the file so every separate flow coming from stdin is overwriting the file again and again. Mode "a" is append mode (filepointer at the end, appending to the file).
Yop, Thanks, that's it!! Although w+ should have worked, too. Even if the messages were written to the beginning of the file. Who cares, it works! Stefan -- GMX im TV ... Die Gedanken sind frei ... Schon gesehen? Jetzt Spot online ansehen: http://www.gmx.net/de/go/tv-spot
Although w+ should have worked, too. Even if the messages were written to
No, you cannot write a file backwards. "w+" (file pointer at the start) means that everytime you write, the file is overwritten). It is okay if you want the most recent entry from the log. Unfortunately somehow it alway was a left "\n", though, so you got an empty file. Tamas
you want the most recent entry from the log. Unfortunately somehow it alway was a left "\n", though, so you got an empty file.
I've just checked that in PHP "w+" means that every _LINE_ separated by "\n"'s written is rewriting the file. So if you echoing "a\nb" into a file opened with "w+", you'll get only "b" in the file. As logs are like ([:alpha:]+\n)*, there's always a trailing newline character which caused the output logfile to be cleared and nothing was written into it. Tamas
As logs are like ([:alpha:]+\n)*, there's always a trailing newline character which caused the output logfile to be cleared and nothing was written into it.
It hasn't been cleared. The file contained a few lines from my tests, when I started the php script directly on the command line. Actually, nothing happens to that file, when I use "w+". I use php for sysadmin tasks, so I'm "normally" familiar with opening and writing to files. But this behaviour really suprises me. Stefan -- ***************************************** in-put GbR - Das Linux-Systemhaus Stefan-Michael Günther Moltkestraße 49 D-76133 Karlsruhe Tel./Fax : +49 (0)721 / 83044 - 98/93 http://www.in-put.de ***************************************** -- 10 GB Mailbox, 100 FreeSMS http://www.gmx.net/de/go/topmail +++ GMX - die erste Adresse für Mail, Message, More +++
participants (2)
-
"Stefan Günther"
-
Tamas MEZEI