本文へ移動

コマンドラインリファレンス

完全な pgBadger コマンド構文、オプション、リモート入力、リターン コード、および例

このページには、固定されたアップストリーム ソースから生成されたコマンド リファレンスが保存されます。オプション名とヘルプ テキストはそのまま残るため、pgbadger --help と直接比較できます。

使い方とオプション

使用法: pgbadger [options] logfile […]

PostgreSQL log analyzer with fully detailed reports and graphs.

引数:

logfile can be a single log file, a list of files, or a shell command
returning a list of files. If you want to pass log content from stdin
use - as filename. Note that input from stdin will not work with csvlog.

オプション:

-a | --average minutes : number of minutes to build the average graphs of
                         queries and connections. Default 5 minutes.
-A | --histo-average min: number of minutes to build the histogram graphs
                         of queries. Default 60 minutes.
-b | --begin datetime  : start date/time for the data to be parsed in log
                         (either a timestamp or a time)
-c | --dbclient host   : only report on entries for the given client host.
-C | --nocomment       : remove comments like /* ... */ from queries.
-d | --dbname database : only report on entries for the given database.
-D | --dns-resolv      : client ip addresses are replaced by their DNS name.
                         Be warned that this can really slow down pgBadger.
-e | --end datetime    : end date/time for the data to be parsed in log
                         (either a timestamp or a time)
-E | --explode         : explode the main report by generating one report
                         per database. Global information not related to a
                         database is added to the postgres database report.
-f | --format logtype  : possible values: syslog, syslog2, stderr, jsonlog,
                         csv, pgbouncer, logplex, rds and redshift. Use this
                         option when pgBadger is not able to detect the log
                         format.
-G | --nograph         : disable graphs on HTML output. Enabled by default.
-h | --help            : show this message and exit.
-H | --html-outdir path: path to directory where HTML report must be written
                         in incremental mode, binary files stay on directory
                         defined with -O, --outdir option.
-i | --ident name      : programname used as syslog ident. Default: postgres
-I | --incremental     : use incremental mode, reports will be generated by
                         days in a separate directory, --outdir must be set.
-j | --jobs number     : number of jobs to run at same time for a single log
                         file. Run as single by default or when working with
                         csvlog format.
-J | --Jobs number     : number of log files to parse in parallel. Process
                         one file at a time by default.
-l | --last-parsed file: allow incremental log parsing by registering the
                         last datetime and line parsed. Useful if you want
                         to watch errors since last run or if you want one
                         report per day with a log rotated each week.
-L | --logfile-list file:file containing a list of log files to parse.
-m | --maxlength size  : maximum length of a query, it will be restricted to
                         the given size. Default truncate size is 100000.
-M | --no-multiline    : do not collect multiline statements to avoid garbage
                         especially on errors that generate a huge report.
-N | --appname name    : only report on entries for given application name
-o | --outfile filename: define the filename for the output. Default depends
                         on the output format: out.html, out.txt, out.bin,
                         or out.json. This option can be used multiple times
                         to output several formats. To use json output, the
                         Perl module JSON::XS must be installed, to dump
                         output to stdout, use - as filename.
-O | --outdir path     : directory where out files must be saved.
-p | --prefix string   : the value of your custom log_line_prefix as
                         defined in your postgresql.conf. Only use it if you
                         aren't using one of the standard prefixes specified
                         in the pgBadger documentation, such as if your
                         prefix includes additional variables like client IP
                         or application name. MUST contain escape sequences
                         for time (%t, %m or %n) and processes (%p or %c).
                         See examples below.
-P | --no-prettify     : disable SQL queries prettify formatter.
-q | --quiet           : don't print anything to stdout, not even a progress
                         bar.
-Q | --query-numbering : add numbering of queries to the output when using
                         options --dump-all-queries or --normalized-only.
-r | --remote-host ip  : set the host where to execute the cat command on
                         remote log file to parse the file locally.
-R | --retention N     : number of weeks to keep in incremental mode. Defaults
                         to 0, disabled. Used to set the number of weeks to
                         keep in output directory. Older weeks and days
                         directories are automatically removed.
-s | --sample number   : number of query samples to store. Default: 3.
-S | --select-only     : only report SELECT queries.
-t | --top number      : number of queries to store/display. Default: 20.
-T | --title string    : change title of the HTML page report.
-u | --dbuser username : only report on entries for the given user.
-U | --exclude-user username : exclude entries for the specified user from
                         report. Can be used multiple time.
-v | --verbose         : enable verbose or debug mode. Disabled by default.
-V | --version         : show pgBadger version and exit.
-w | --watch-mode      : only report errors just like logwatch could do.
-W | --wide-char       : encode html output of queries into UTF8 to avoid
                         Perl message "Wide character in print".
-x | --extension       : output format. Values: text, html, bin or json.
                         Default: html
-X | --extra-files     : in incremental mode allow pgBadger to write CSS and
                         JS files in the output directory as separate files.
-z | --zcat exec_path  : set the full path to the zcat program. Use it if
                         zcat, bzcat or unzip is not in your path.
-Z | --timezone +/-XX  : Set the number of hours from GMT of the timezone.
                         Use this to adjust date/time in JavaScript graphs.
                         The value can be an integer, ex.: 2, or a float,
                         ex.: 2.5.
--anonymize            : obscure all literals in queries, useful to hide
--charset              : used to set the HTML charset to be used.
                         Default: utf-8.
--command CMD          : command to execute to retrieve log entries on
                         stdin. pgBadger will open a pipe to the command
                         and parse log entries generated by the command.
--csv-separator        : used to set the CSV field separator, default: ,
--day-report YYYY-MM-DD: create an HTML report over the specified day.
                         Requires incremental output directories and the
                         presence of all necessary binary data files
--disable-autovacuum   : do not generate autovacuum report.
                         confidential data.
--disable-checkpoint   : do not generate checkpoint/restartpoint report.
--disable-connection   : do not generate connection report.
--disable-error        : do not generate error report.
--disable-hourly       : do not generate hourly report.
--disable-lock         : do not generate lock report.
--disable-query        : do not generate query reports (slowest, most
                         frequent, queries by users, by database, ...).
--disable-session      : do not generate session report.
--disable-temporary    : do not generate temporary report.
--disable-type         : do not generate report of queries by type, database
                         or user.
--dump-all-queries     : dump all queries found in the log file replacing
                         bind parameters included in the queries at their
                         respective placeholders positions.
--dump-raw-csv         : parse the log and dump the information into CSV
                         format. No further processing is done, no report.
--enable-checksum      : used to add an md5 sum under each query report.
--exclude-appname name : exclude entries for the specified application name
                         from report.  Example: "pg_dump".  Can be used
                         multiple times.
--exclude-client name  : exclude log entries for the specified client ip.
                         Can be used multiple times.
--exclude-db name      : exclude entries for the specified database from
                         report. Example: "postgres". Can be used multiple
                         times.
--exclude-file filename: path of the file that contains each regex to use
                         to exclude queries from the report. One regex per
                         line.
--exclude-line regex   : exclude any log entry that will match the given
                         regex. Can be used multiple times.
--exclude-query regex  : any query matching the given regex will be excluded
                         from the report. For example: "^(VACUUM|COMMIT)"
                         You can use this option multiple times.
--exclude-time  regex  : any timestamp matching the given regex will be
                         excluded from the report. Example: "2013-04-12 .*"
                         You can use this option multiple times.
--explain-url URL      : use it to override the url of the graphical explain
                         tool. Default: https://explain.depesz.com/
--histogram-query VAL  : use custom inbound for query times histogram.
                        Default inbound in milliseconds:
                     0,1,5,10,25,50,100,500,1000,10000
--histogram-session VAL: use custom inbound for session times histogram.
                        Default inbound in milliseconds:
                     0,500,1000,30000,60000,600000,1800000,3600000,28800000
--include-file filename: path of the file that contains each regex to the
                         queries to include from the report. One regex per
                         line.
--include-query regex  : any query that does not match the given regex will
                         be excluded from the report. You can use this
                         option multiple times. For example: "(tbl1|tbl2)".
--include-pid PID      : only report events related to the session pid (%p).
                         Can be used multiple time.
--include-session ID   : only report events related to the session id (%c).
                         Can be used multiple time.
--include-time  regex  : only timestamps matching the given regex will be
                         included in the report. Example: "2013-04-12 .*"
                         You can use this option multiple times.
--iso-week-number      : in incremental mode, calendar weeks start on
                         Monday and respect the ISO 8601 week number, range
                         01 to 53, where week 1 is the first week that has
                         at least 4 days in the new year.
--keep-comments        : do not remove comments from normalized queries. It
                         can be useful if you want to distinguish between
                         same normalized queries.
--journalctl command   : command to use to replace PostgreSQL logfile by
                         a call to journalctl. Basically it might be:
                            journalctl -u postgresql-9.5
--log-duration         : force pgBadger to associate log entries generated
                         by both log_duration = on and log_statement = 'all'
--log-timezone +/-XX   : Set the number of hours from GMT of the timezone
                         that must be used to adjust date/time read from
                         log file before beeing parsed. Using this option
                         makes log search with a date/time more difficult.
                         The value can be an integer, ex.: 2, or a float,
                         ex.: 2.5.
--month-report YYYY-MM : create a cumulative HTML report over the specified
                         month. Requires incremental output directories and
                         the presence of all necessary binary data files
--noexplain            : do not process lines generated by auto_explain.
--no-fork              : do not fork any process, for debugging purpose.
--no-process-info      : disable changing process title to help identify
                         pgbadger process, some system do not support it.
--no-progressbar       : disable progressbar.
--noreport             : no reports will be created in incremental mode.
--no-week              : inform pgbadger to not build weekly reports in
                         incremental mode. Useful if it takes too much time.
--normalized-only      : only dump all normalized queries to out.txt
--pgbouncer-only       : only show PgBouncer-related menus in the header.
--pid-dir path         : set the path where the pid file must be stored.
                         Default /tmp
--pid-file file        : set the name of the pid file to manage concurrent
                         execution of pgBadger. Default: pgbadger.pid
--pie-limit num        : pie data lower than num% will show a sum instead.
--prettify-json        : use it if you want json output to be prettified.
--rebuild              : used to rebuild all html reports in incremental
                         output directories where there's binary data files.
--start-monday         : in incremental mode, calendar weeks start on
                         Sunday. Use this option to start on a Monday.
--tempdir DIR          : set directory where temporary files will be written
                         Default: File::Spec->tmpdir() || '/tmp'

pgBadger は、パスワードなしの ssh 接続を使用してリモート ログ ファイルを解析できます。 -r または –remote-host を使用して、ホストの IP アドレスまたはホスト名を設定します。 SSH 接続を完全に制御するための追加オプションもいくつかあります。

--ssh-identity file      path to the identity file to use.
--ssh-option  options    list of -o options to use for the ssh connection.
                         Options always used:
                             -o ConnectTimeout=$ssh_timeout
                             -o PreferredAuthentications=hostbased,publickey
--ssh-port port          ssh port to use for the connection. Default: 22.
--ssh-program ssh        path to the ssh program to use. Default: ssh.
--ssh-timeout second     timeout to ssh connection failure. Default: 10 sec.
--ssh-user username      connection login name. Defaults to running user.

解析するログ ファイルは、URI を使用して指定することもできます。サポートされているプロトコルは http[s] および [s]ftp です。ファイルのダウンロードにはcurlコマンドが使用され、ダウンロード中にファイルが解析されます。 ssh プロトコルもサポートされており、リモート ホストの使用と同様に ssh コマンドを使用します。以下の例を参照してください。

リターンコード:

0: on success
1: die on error
2: if it has been interrupted using ctr+c for example
3: the pid file already exists or can not be created
4: no log file was given at command line

例:

pgbadger /var/log/postgresql.log
pgbadger /var/log/postgres.log.2.gz /var/log/postgres.log.1.gz /var/log/postgres.log
pgbadger /var/log/postgresql/postgresql-2012-05-*
pgbadger --exclude-query="^(COPY|COMMIT)" /var/log/postgresql.log
pgbadger -b "2012-06-25 10:56:11" -e "2012-06-25 10:59:11" /var/log/postgresql.log
cat /var/log/postgres.log | pgbadger -
# Log line prefix with stderr log output
pgbadger --prefix '%t [%p]: user=%u,db=%d,client=%h' /pglog/postgresql-2012-08-21*
pgbadger --prefix '%m %u@%d %p %r %a : ' /pglog/postgresql.log
# Log line prefix with syslog log output
pgbadger --prefix 'user=%u,db=%d,client=%h,appname=%a' /pglog/postgresql-2012-08-21*
# Use my 8 CPUs to parse my 10GB file faster, much faster
pgbadger -j 8 /pglog/postgresql-10.1-main.log

リモート ログ ファイルには URI 表記を使用します。

pgbadger http://172.12.110.1//var/log/postgresql/postgresql-10.1-main.log
pgbadger ftp://username@172.12.110.14/postgresql-10.1-main.log
pgbadger ssh://username@172.12.110.14:2222//var/log/postgresql/postgresql-10.1-main.log*

ローカル PostgreSQL ログ ファイルとリモート pgbouncer ログ ファイルを一緒に使用して解析できます。

pgbadger /var/log/postgresql/postgresql-10.1-main.log ssh://username@172.12.110.14/pgbouncer.log

cron ジョブごとに毎週エラーを報告します。

30 23 * * 1 /usr/bin/pgbadger -q -w /var/log/postgresql.log -o /var/reports/pg_errors.html

増分動作を使用して毎週レポートを生成します。

0 4 * * 1 /usr/bin/pgbadger -q `find /var/log/ -mtime -7 -name "postgresql.log*"` -o /var/reports/pg_errors-`date +\%F`.html -l /var/reports/pgbadger_incremental_file.dat

これは、ログ ファイルと HTML レポートも毎週ローテーションされることを前提としています。

あるいは、自動生成された増分レポートを使用することをお勧めします。

0 4 * * * /usr/bin/pgbadger -I -q /var/log/postgresql/postgresql.log.1 -O /var/www/pg_reports/

日ごとおよび週ごとにレポートが生成されます。

増分モードでは、レポートに保存する週数を指定することもできます。

/usr/bin/pgbadger --retention 2 -I -q /var/log/postgresql/postgresql.log.1 -O /var/www/pg_reports/

毎日 30 分間、23:00 および 13:00 に pg_dump がある場合、次のように pgBadger を使用して、これらの期間をレポートから除外できます。

pgbadger --exclude-time "2013-09-.* (23|13):.*" postgresql.log

これにより、pg_dump によって生成された COPY ステートメントが最も遅いクエリのリストの上位に表示されるのを避けることができます。 –exclude-appname “pg_dump” を使用して、この問題をより簡単な方法で解決することもできます。

また、journalctl 出力をログ ファイルであるかのように解析することもできます。

pgbadger --journalctl 'journalctl -u postgresql-9.5'

最悪の場合は、リモート ホストから呼び出します。

pgbadger -r 192.168.1.159 --journalctl 'journalctl -u postgresql-9.5'

コマンド ラインでログ ファイルを指定する必要はありませんが、解析する他の PostgreSQL ログ ファイルがある場合は、通常どおり追加できます。

すべての増分 HTML レポートを後で再構築するには、次の手順を実行します。

rm /path/to/reports/*.js
rm /path/to/reports/*.css
pgbadger -X -I -O /path/to/reports/ --rebuild

また、すべてのリソース ファイル (JS および CSS) も更新されます。このオプションを使用してレポートが作成された場合は、-E または –explode を使用します。

pgBadger は、logplex 形式を使用した Heroku PostgreSQL ログもサポートしています。

heroku logs -p postgres | pgbadger -f logplex -o heroku.html -

これにより、標準入力を介して Heroku PostgreSQL ログが pgbadger にストリーミングされます。

pgBadger は、rds 形式を使用して RDS および Cloudwatch PostgreSQL ログを自動検出できます。

pgbadger -f rds -o rds_out.html rds.log

各 CloudSQL PostgreSQL ログはごく普通の PostgreSQL ログですが、JSON 形式でカプセル化されています。これは pgBadger によって自動検出されますが、ログ形式を強制する必要がある場合には `jsonlog` を使用します。

pgbadger -f jsonlog -o cloudsql_out.html cloudsql.log

これは jsonlog 拡張機能の場合と同じで、json 形式は異なりますが、pgBadger は両方の形式を解析できます。

pgBadger は、Kubernetes の CloudNativePG Postgres オペレーターによって生成されたログもサポートします。

pgbadger -f jsonlog -o cnpg_out.html cnpg.log

1 か月にわたる累積レポートを作成するには、次のコマンドを使用します。

pgbadger --month-report 2919-05 /path/to/incremental/reports/

これにより、増分レポートのカレンダー ビューに月名へのリンクが追加され、2019 5 月のレポートが表示されます。このオプションを使用してレポートが作成された場合は、-E または –explode を使用します。