繁体   English   中英

在html / php输出中显示/ var / log / messages?

[英]display /var/log/messages in html/php output?

我试图在通过同一主机上的Web服务器访问的网页中显示/ var / log / messages或类似内容(例如../secure)的输出。

我应该使用bash将-f >>消息文件拖到新的输出文件中,然后在html页面中显示该文本文件,还是有更好的方法呢?

谢谢! 现场音乐

如果您正在寻找一种无需重新加载页面即可在线显示实际文件内容的方法,则应设置一个WebSocket服务器。

您可以使用,如框架构建的WebSocket服务器phpDaemonReactPHP棘轮冰柱 ,或者PHP扩展包异步库的帮助下实现自己的服务器: 事件EV ,或类似的。

我从上面的列表中选择了一个随机框架:Ratchet。 棘轮基于ReactPHP。 ReactPHP选择后端从以下列表中的事件循环: - libevent扩展, - libev扩展- event延伸, -或者基于内置的一个内部类stream_select()函数。

作为event扩展的维护者,我选择了event

我编写了一个“快速”示例,以使您了解如何实现它。 您很可能必须使用不同的工具来制定自己的版本。 但是代码应该给您冲动。

src / MyApp / Server.php

<?php
namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

class Server implements MessageComponentInterface {
  protected $clients;

  public function __construct() {
    $this->clients = new \SplObjectStorage;
  }

  public function onOpen(ConnectionInterface $conn) {
    $this->clients->attach($conn);
    echo "New connection! ({$conn->resourceId})\n";
  }

  public function onMessage(ConnectionInterface $from, $msg) {
    $numRecv = count($this->clients) - 1;
    printf("Connection %d sending '%s' to %d other connection%s\n",
      $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');

    foreach ($this->clients as $client) {
      if ($from !== $client) {
        $client->send($msg);
      }
    }
  }

  public function onClose(ConnectionInterface $conn) {
    $this->clients->detach($conn);
    echo "Connection {$conn->resourceId} has disconnected\n";
  }

  public function onError(ConnectionInterface $conn, \Exception $e) {
    echo "An error has occurred: {$e->getMessage()}\n";
    $conn->close();
  }

  public function broadcast($msg) {
    foreach ($this->clients as $client) {
      $client->send($msg);
    }
  }
}

server.php

<?php
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Server;

require __DIR__ . '/vendor/autoload.php';

$server = IoServer::factory(
  new HttpServer(
    new WsServer(
      $my_app_server = new Server()
    )
  ),
  9989
);

$loop = $server->loop;
$filename = '/var/log/messages';

$loop->addPeriodicTimer(5, function ()
  use ($filename, $my_app_server)
  {
    static $stat_info;

    if ($stat_info == null) {
      clearstatcache(true, $filename);
      $stat_info = stat($filename);
    }

    clearstatcache(true, $filename);
    $st = stat($filename);

    $size_diff = $st['size'] - $stat_info['size'];
    echo "Diff = $size_diff bytes\n";

    if ($size_diff > 0) {
      $offset = $stat_info['size'];
      $bytes = $size_diff;
    } elseif ($size_diff < 0) {
      // The file is likely truncated by `logrotate` or similar utility
      $offset = 0;
      $bytes = $st['size'];
    } else {
      $bytes = 0;
    }

    $stat_info = $st;

    if ($bytes) {
      if (! $fp = fopen($filename, 'r')) {
        fprintf(STDERR, "Failed to open file $filename\n");
        return;
      }
      if ($offset > 0) {
        fseek($fp, $offset);
      }
      if ($msg = fread($fp, $bytes)) {
        $my_app_server->broadcast($msg);
      }
      fclose($fp);
    }
  }
);

$server->run();

test.html

<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <title>Test</title>
</head>
<body>
  <script>
  var conn = new WebSocket('ws://localhost:9989');
  conn.onopen = function(e) {
    console.log("Connection established!");
  };
  conn.onmessage = function(e) {
    console.log("Msg from server", e.data);
  };
  </script>
</body>
</html>

我将跳过使用Composer设置基本测试环境所需的步骤。 假设您已经成功配置了上述文件的测试环境,则可以使用以下命令运行服务器:

php server.php

检查用户是否具有读取/var/log/messages权限。 在我的系统上,只有root可以读取文件。 因此,您可能需要使用sudo (root权限)运行上述命令。

现在,您可以在浏览器中打开test.html并查看控制台输出。 然后触发一些通常记录到消息文件中的事件。 例如,您可以使用错误的密码调用sudo 服务器应在5秒钟的间隔内检测到更改,然后将其发送到WebSocket客户端。

如果您使用tail -f,则意味着您将在文件随着命令运行而增长的同时不断地从文件中获取数据。 您可以使用cat或tail -n。 同样,当然,您可以通过创建符号或硬链接来直接访问文件(在ln -s链接文件中,在ln -s s-file链接文件中)-但是请确保您的网络服务器有足够的空间访问它们的权利。

确保您的html服务器具有访问页面和阅读页面的权限(带有cattailgrep类的东西)。 <html>将输出置于<pre></pre>

方法1

在您的一个基本目录中,创建一个符号链接

ln -s /var/log/messages messages

如果该目录属于test.web ,则使用不带网格的http://test.web/messages访问日志

方法2

如果要查找php脚本,请首先按照方法1所述创建链接。然后,在readlog.php的基本目录中创建一个新文件,例如test.web ,其内容如下:

<?php
readfile(“$DOCUMENT_ROOT/messages”);
?>

像这样访问readlog.php

http://test.web/readlog.php

需求:

应该为/var/log/messages所有用户启用Read访问权限。

注意:

为整个世界的/var/log/messages设置read选项不是一个好主意。

<!DOCTYPE html>
<html>
 <head>
  <title>toyLogs</title>
 </head>
 <body>
  <div><p><?php include('/var/www/html/accesslog.txt'); ?></p></div>
 </body>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM