1/*
2** Zabbix
3** Copyright (C) 2001-2021 Zabbix SIA
4**
5** This program is free software; you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation; either version 2 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18**/
19
20package postgres
21
22import (
23	"context"
24	"errors"
25	"github.com/jackc/pgx/v4"
26	"zabbix.com/pkg/zbxerr"
27)
28
29// archiveHandler gets info about count and size of archive files and returns JSON if all is OK or nil otherwise.
30func archiveHandler(ctx context.Context, conn PostgresClient,
31	_ string, _ map[string]string, _ ...string) (interface{}, error) {
32	var archiveCountJSON, archiveSizeJSON string
33
34	queryArchiveCount := `SELECT row_to_json(T)
35							FROM (
36									SELECT archived_count, failed_count
37								   	  FROM pg_stat_archiver
38								) T;`
39
40	queryArchiveSize := `SELECT row_to_json(T)
41							FROM (
42								WITH values AS (
43									SELECT
44										4096/(ceil(pg_settings.setting::numeric/1024/1024))::int AS segment_parts_count,
45										setting::bigint AS segment_size,
46										('x' || substring(pg_stat_archiver.last_archived_wal from 9 for 8))::bit(32)::int AS last_wal_div,
47										('x' || substring(pg_stat_archiver.last_archived_wal from 17 for 8))::bit(32)::int AS last_wal_mod,
48										CASE WHEN pg_is_in_recovery() THEN NULL
49											ELSE ('x' || substring(pg_walfile_name(pg_current_wal_lsn()) from 9 for 8))::bit(32)::int END AS current_wal_div,
50										CASE WHEN pg_is_in_recovery() THEN NULL
51											ELSE ('x' || substring(pg_walfile_name(pg_current_wal_lsn()) from 17 for 8))::bit(32)::int END AS current_wal_mod
52									FROM pg_settings, pg_stat_archiver
53									WHERE pg_settings.name = 'wal_segment_size')
54								SELECT
55									greatest(coalesce((segment_parts_count - last_wal_mod) + ((current_wal_div - last_wal_div - 1) * segment_parts_count) + current_wal_mod - 1, 0), 0) AS count_files,
56									greatest(coalesce(((segment_parts_count - last_wal_mod) + ((current_wal_div - last_wal_div - 1) * segment_parts_count) + current_wal_mod - 1) * segment_size, 0), 0) AS size_files
57								FROM values
58							) T;`
59
60	row, err := conn.QueryRow(ctx, queryArchiveCount)
61	if err != nil {
62		return nil, zbxerr.ErrorCannotFetchData.Wrap(err)
63	}
64
65	err = row.Scan(&archiveCountJSON)
66	if err != nil {
67		if errors.Is(err, pgx.ErrNoRows) {
68			return nil, zbxerr.ErrorEmptyResult.Wrap(err)
69		}
70
71		return nil, zbxerr.ErrorCannotFetchData.Wrap(err)
72	}
73
74	row, err = conn.QueryRow(ctx, queryArchiveSize)
75	if err != nil {
76		return nil, zbxerr.ErrorCannotFetchData.Wrap(err)
77	}
78
79	err = row.Scan(&archiveSizeJSON)
80	if err != nil {
81		if errors.Is(err, pgx.ErrNoRows) {
82			return nil, zbxerr.ErrorEmptyResult.Wrap(err)
83		}
84
85		return nil, zbxerr.ErrorCannotFetchData.Wrap(err)
86	}
87
88	result := archiveCountJSON[:len(archiveCountJSON)-1] + "," + archiveSizeJSON[1:]
89
90	return result, nil
91}
92