Mongotail,以“tail”方式记录所有MongoDB查询。
mongotail的Python项目详细描述
Mongotail,以“tail”方式记录所有MongoDB查询。
mongotail是从mongo输出任何操作的命令行工具 标准输出中的数据库。您可以看到 从控制台的数据库探查器,或将结果重定向到文件、管道 它使用grep或其他命令行工具等。
语法与mongoclient非常相似,并且输出类似 tail命令将是最新的10行日志记录。
但是更有趣的特性(也像tail)是查看更改 在“real time”中使用-f选项,并偶尔过滤结果 使用grep查找特定操作。
语法
用法:
mongotail [db address] [options]
“数据库地址”可以是:
foo | foo database on local machine (IPv4 connection) |
:1234/foo | foo database on local machine on port 1234 |
192.169.0.5/foo | foo database on 192.168.0.5 machine |
remotehost/foo | foo database on remotehost machine |
192.169.0.5:9999/foo | foo database on 192.168.0.5 machine on port 9999 |
“[::1]:9999/foo” | foo database on ::1 machine on port 9999 (IPv6 connection) |
可选参数:
-u USERNAME, --username USERNAME | |
username for authentication | |
-p PASSWORD, --password PASSWORD | |
password for authentication. If username is given and password isn’t, it’s asked from tty | |
-b AUTH_DATABASE, --authenticationDatabase AUTH_DATABASE | |
database to use to authenticate the user. If not specified, the user will be authenticated against the database specified in the [db address] | |
-n N, --lines N | |
output the last N lines, instead of the last 10. Use ALL value to show all lines | |
-f, --follow | output appended data as the log grows |
-l LEVEL, --level LEVEL | |
specifies the profiling level, which is either 0 for no profiling, 1 for only slow operations, or 2 for all operations. Or use with ‘status’ word to show the current level configured. Uses this option once before logging the database | |
-s MS, --slowms MS | |
sets the threshold in milliseconds for the profile to consider a query or operation to be slow (use with –level 1). Or use with ‘status’ word to show the current milliseconds configured | |
-m, --metadata | extra metadata fields to show. Known fields (may vary depending of the operation and the MongoDB version): millis, nscanned, docsExamined, execStats, lockStats … |
-i, --info | get information about the MongoDB server we’re connected to |
-v, --verbose | verbose mode (not recommended). All the operations will printed in JSON without format and with all the information available from the log |
--ssl | creates the connection to the server using SSL |
--sslCertFile SSL_CERT_FILE | |
certificate file used to identify the local connection against MongoDB | |
--sslKeyFile SSL_KEY_FILE | |
private keyfile used to identify the local connection against MongoDB. If included with the certfile then only the sslCertFile is needed | |
--sslCertReqs SSL_CERT_REQS | |
specifies whether a certificate is required from the other side of the connection, and whether it will be validated if provided. It must be any of three values: 0 (certificate ignored), 1 (not required, but validated if provided), 2 (required and validated) | |
--sslCACerts SSL_CA_CERTS | |
file that contains a set of concatenated “certification authority” certificates, which are used to validate certificates passed from the other end of the connection | |
--sslPEMPassword SSL_PEM_PASSPHRASE | |
password or passphrase for decrypting the private key in sslCertFile or sslKeyFile. Only necessary if the private key is encrypted | |
--sslCrlFile SSL_CRLFILE | |
path to a PEM or DER formatted certificate revocation list | |
-h, --help | show this help message and exit |
-V, --version | show program’s version number and exit |
启用数据库分析并显示日志
必须先在当前数据库中激活 profiler, 因此mongodb将捕获mongotail读取的特殊文档中的所有活动。
您可以使用-l,--level选项来实现这一点。例如,如果要查看日志 在mydatabase中,首先必须执行以下操作:
$ mongotail MYDATABASE -l 2
然后您可以使用以下命令查看最新的日志记录行:
$ mongotail MYDATABASE 2015-02-24 19:17:01.194 QUERY [Company] : {"_id": ObjectId("548b164144ae122dc430376b")}. 1 returned. 2015-02-24 19:17:01.195 QUERY [User] : {"_id": ObjectId("549048806b5d3db78cf6f654")}. 1 returned. 2015-02-24 19:17:01.196 UPDATE [Activation] : {"_id": "AB524"}, {"_id": "AB524", "code": "f2cbad0c"}. 1 updated. 2015-02-24 19:17:10.729 COUNT [User] : {"active": {"$exists": true}, "firstName": {"$regex": "mac"}} ...
要连接ssl或远程mongo实例,请使用mongotail --help命令检查选项。
注意:选择的级别可能会影响性能。它还可以允许 将查询内容写入日志的服务器,该日志可能有 对部署的信息安全影响。记住设置 在调试数据之后,将数据库分析级别再次设置为0:
$ mongotail MYDATABASE -l 0
如何使用Mongotail和最新功能的逐步指南 是here。
安装
请参阅INSTALL.rst从源安装指南。安装 从PyPI repositories, 根据操作系统的不同,请遵循以下说明:
Linux安装
您可以在 环境:
$ pip install mongotail
以管理员/根用户权限执行此命令(在 debian/ubuntu linux发行版在命令前添加sudo。
必须先安装pip工具。在debian/ubuntu linux中 您可以使用(也可以使用根权限)安装它的发行版:
$ apt-get install python-pip
Mac OSX安装
首先需要在中安装python包管理器pip 你的环境,然后像linux一样安装mongotail 可以从命令行执行sudo pip install mongotail, 但也可以使用easy_install安装 旧的python包管理器出现在大多数osx版本中。试试这个:
$ sudo easy_install mongotail
Docker
使用Docker运行(不需要下载源代码):
$ docker run -it --rm fgribreau/mongotail --help
如果要连接在本地运行的数据库 容器,必须链接两个实例(参见Docker中的howto 如果数据库是一个本地实例,并且运行时没有 Docker,记得用你电脑的本地IP,因为 localhost地址(ip 127.0.0.1)指向容器,而不是指向 你的主人。例如:
$ docker run -it --rm fgribreau/mongotail 192.168.0.21/test
关于
项目:https://github.com/mrsarm/mongotail
作者:(2015-2017)Mariano Ruiz<;mrsarm@gmail.cm>;
变更日志:CHANGELOG.rst
更多指南:http://mrsarm.blogspot.com.ar/search/label/Mongotail
许可证:GPL-3