I have data on one server that comes into a continuously growing log file. I need to tail that log and grep some information and then send that to another server for php to insert into a database. The computer that has the log can't be allowed access to the database. I tried this so far but my syntax on the bash command is wrong and I can't figure out if this is the way to do it or if there would be a better way? Netcat was another thought...
#!/bin/sh
tail -f /usr/local/log/thelog.log | grep -B1 "ABC=" > /usr/local/log/output.log;
while inotifywait -e modify /usr/local/log/output.log; do
sleep 10;
php /usr/bin/send.php;
done
<?php
//extract data from the post
//extract($_POST);
//set POST variables
$data = 'tail -n 3 /usr/local/log/output.log';
$url = 'http://www.blahblah.com/logtodb.php';
$data = str_replace("A", "", $data);
$data = str_replace("B=", "", $data);
$data = str_replace("C=", "", $data);
$data = str_replace("D=", "", $data);
$fields = array(
'data'=>urlencode($data)d,
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
A>
logtodb.php reads the post
To execute an external command in PHP you need to use back ticks not single quotes. They are usually found below the ESC
key on the keyboard.
$data = 'tail -n 3 /usr/local/log/output.log';
should be
$data = `tail -n 3 /usr/local/log/output.log`;
Also you should not be using tail -f
as it does not return until you provide an EOF
(control+D).
I've never done it, but it ought to (probably) work:
Why not just use a domain socket (FIFO)?
$ mkfifo /path/to/my/fifo
$ tail -f /path/to/log > /path/to/my/fifo &
$ php send.php < /path/to/my/fifo
send.php should be able to just loop on $line=fread(...) from now until enternity....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With