Skip to content
This repository was archived by the owner on Jul 6, 2021. It is now read-only.

Commit d51c8ed

Browse files
committed
Merge branch '156-dmius-f003-table' into 'master'
#156 F003 check and template reworked Closes #156 See merge request postgres.ai/postgres-health-check!161
2 parents 2927af5 + 29bc2cb commit d51c8ed

File tree

18 files changed

+101
-59
lines changed

18 files changed

+101
-59
lines changed

pghrep/src/main.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
var DEBUG bool = false
3131

3232
// Output debug message
33-
func dbg(v ...interface{}) {
33+
func Dbg(v ...interface{}) {
3434
if DEBUG {
3535
message := ""
3636
for _, value := range v {
@@ -40,6 +40,16 @@ func dbg(v ...interface{}) {
4040
}
4141
}
4242

43+
// Output debug message
44+
func Err(v ...interface{}) {
45+
message := ""
46+
for _, value := range v {
47+
message = message + " " + pyraconv.ToString(value)
48+
}
49+
log.Println(">>> ERROR:", message)
50+
}
51+
52+
4353
// Prepropess file paths
4454
// Allow absulute and relative (of pwd) paths with or wothout file:// prefix
4555
// Return absoulute path of file
@@ -56,7 +66,7 @@ func GetFilePath(name string) string {
5666
// for relative path will combine with current path
5767
curDir, err := os.Getwd()
5868
if err != nil {
59-
dbg("Can't determine current path")
69+
Dbg("Can't determine current path")
6070
}
6171
if strings.HasSuffix(strings.ToLower(curDir), "/") {
6272
filePath = curDir + filePath
@@ -85,7 +95,7 @@ func FileExists(name string) bool {
8595
func ParseJson(jsonData string) map[string]interface{} {
8696
orderedData := orderedmap.New()
8797
if err := json.Unmarshal([]byte(jsonData), &orderedData); err != nil {
88-
dbg("Can't parse json data:", err)
98+
Err("Can't parse json data:", err)
8999
return nil
90100
} else {
91101
dt := orderedData.ToInterfaceArray()
@@ -99,7 +109,7 @@ func LoadJsonFile(filePath string) map[string]interface{} {
99109
if FileExists(filePath) {
100110
fileContent, err := ioutil.ReadFile(GetFilePath(filePath)) // just pass the file name
101111
if err != nil {
102-
log.Println("Can't read file: ", filePath, err)
112+
Err("Can't read file: ", filePath, err)
103113
return nil
104114
}
105115
return ParseJson(string(fileContent))
@@ -121,7 +131,7 @@ func loadDependencies(data map[string]interface{}) {
121131
func loadTemplates() *template.Template {
122132
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
123133
if err != nil {
124-
dbg("Can't determine current path")
134+
Dbg("Can't determine current path")
125135
}
126136

127137
var templates *template.Template
@@ -144,7 +154,7 @@ func loadTemplates() *template.Template {
144154
tplFuncMap["Nobr"] = Nobr
145155
templates, err = template.New("").Funcs(tplFuncMap).ParseFiles(allFiles...)
146156
if err != nil {
147-
dbg("Can't load templates", err)
157+
log.Fatal("Can't load templates", err)
148158
return nil
149159
}
150160

@@ -156,7 +166,7 @@ func getRawData(data map[string]interface{}) {
156166
// for every host get data
157167
var rawData []interface{}
158168
hosts := pyraconv.ToInterfaceMap(data["hosts"])
159-
dbg("Data hosts: ", hosts)
169+
Dbg("Data hosts: ", hosts)
160170
results := pyraconv.ToInterfaceMap(data["results"])
161171
masterName := pyraconv.ToString(hosts["master"])
162172
masterResults := pyraconv.ToInterfaceMap(results[masterName])
@@ -199,7 +209,7 @@ func generateMdReport(checkId string, reportFilename string, reportData map[stri
199209
_, err := filepath.Abs(filepath.Dir(os.Args[0]))
200210
f, err := os.OpenFile(outputFileName, os.O_CREATE | os.O_RDWR, 0777)
201211
if err != nil {
202-
dbg("Can't create report file", err)
212+
Err("Can't create report file", err)
203213
return false
204214
}
205215
defer f.Close()
@@ -213,14 +223,14 @@ func generateMdReport(checkId string, reportFilename string, reportData map[stri
213223
reporTpl := templates.Lookup(reportFileName)
214224
data := reportData
215225
if reporTpl == nil {
216-
dbg("Template " + checkId + ".tpl not found.")
226+
Err("Template " + checkId + ".tpl not found.")
217227
getRawData(data)
218228
reportFileName = "raw.tpl"
219229
reporTpl = templates.Lookup(reportFileName)
220230
}
221231
err = reporTpl.ExecuteTemplate(f, reportFileName, data)
222232
if err != nil {
223-
dbg("Template execute error is", err)
233+
Err("Template execute error is", err)
224234
defer os.Remove(outputFileName)
225235
return false
226236
} else {
@@ -310,7 +320,7 @@ func main() {
310320
var reportData map[string]interface{}
311321
objectPath, err := l.get(checkId);
312322
if err != nil {
313-
dbg("Cannot find and load plugin.", err)
323+
Dbg("Cannot find and load plugin.", err)
314324
reportData = resultData
315325
} else {
316326
result, err := l.call(objectPath, resultData)

pghrep/src/pluginsutil.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ func (l *loader) get(name string) (string, error) {
7171
if err != nil && os.IsNotExist(err) {
7272
_, err := os.Stat(filepath.Join(l.pluginsDir, name + ".go"))
7373
if err != nil {
74-
dbg("WARNING: Plugin not found.", pluginPath)
74+
Dbg("WARNING: Plugin not found.", pluginPath)
7575
return pluginPath, err
7676
}
77-
dbg("WARNING: Binary plugin " + pluginPath + " not found. Try compile.")
77+
Dbg("WARNING: Binary plugin " + pluginPath + " not found. Try compile.")
7878
pluginPath, err = l.compile(name)
7979
} else {
80-
dbg("Binary plugin " + pluginPath + " found.\n")
80+
Dbg("Binary plugin " + pluginPath + " found.\n")
8181
err = nil
8282
}
8383
return pluginPath, err

pghrep/templates/A001.tpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# {{ .checkId }} System information #
22

33
## Observations ##
4-
4+
{{ if .hosts.master }}
55
### Master (`{{.hosts.master}}`) ###
66
{{ if (index (index .results .hosts.master) "data").system.raw}}
77
**System**
@@ -28,7 +28,7 @@
2828
```
2929
{{ (index (index .results .hosts.master) "data").virtualization.raw }}
3030
```{{ end }}
31-
31+
{{ end }}
3232
{{ if gt (len .hosts.replicas) 0 }}
3333
### Replica servers: ###
3434
{{ range $key, $value := .hosts.replicas }}

pghrep/templates/A002.tpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# {{ .checkId }} Version information #
22

33
## Observations ##
4-
4+
{{ if .hosts.master }}
55
### Master (`{{.hosts.master}}`) ###
66
```
77
{{ (index (index .results .hosts.master) "data").version }}
88
```
9-
9+
{{ end }}
1010
{{ if gt (len .hosts.replicas) 0 }}
1111
### Replica servers: ###
1212
{{ range $key, $value := .hosts.replicas }}

pghrep/templates/A003.tpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# {{ .checkId }} Postgres settings #
22

33
## Observations ##
4-
4+
{{ if .hosts.master }}
55
### Master (`{{.hosts.master}}`) ###
66
Setting | Value | Unit
77
--------|-------|------
88
{{ range $i, $key := (index (index (index .results .hosts.master) "data") "_keys") }}
99
{{- $value := (index (index (index $.results $.hosts.master) "data") $key) -}}
1010
[{{ $key }}](https://postgresqlco.nf/en/doc/param/{{ $key }}) | {{ $value.setting}} | {{ if $value.unit }}{{ $value.unit }} {{ end }}
1111
{{ end }}
12-
12+
{{ end }}
1313
{{ if gt (len .hosts.replicas) 0 }}
1414
### Replica servers: ###
1515
{{ range $skey, $host := .hosts.replicas }}

pghrep/templates/A004.tpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# {{ .checkId }} Cluster information #
22

33
## Observations ##
4-
4+
{{ if .hosts.master }}
55
### Master (`{{.hosts.master}}`) ###
66
Indicator | Value
77
-----------|-------
88
{{ range $i, $key := (index (index (index .results .hosts.master) "data") "_keys") }}
99
{{- $value := (index (index (index $.results $.hosts.master) "data") $key) -}}
1010
{{ $key }} | {{ Nobr (index $value "value") }}
1111
{{ end }}
12-
12+
{{ end }}
1313
{{ if gt (len .hosts.replicas) 0 }}
1414
### Replica servers: ###
1515
{{ range $skey, $host := .hosts.replicas }}

pghrep/templates/A005.tpl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
## Observations ##
44

5+
{{ if .hosts.master }}
56
### Master (`{{.hosts.master}}`) ###
6-
77
Database | Extension name | Installed version | Default version | Is old
88
---------|----------------|-------------------|-----------------|--------
99
{{ range $d, $db := (index (index (index .results .hosts.master) "data") "_keys") -}}
@@ -13,6 +13,9 @@ Database | Extension name | Installed version | Default version | Is old
1313
{{ $db }} | {{ $dbext }} | {{ $extData.installed_version }} | {{ $extData.default_version }} | {{ $extData.is_old }}
1414
{{ end -}}
1515
{{ end -}}
16+
{{ else }}
17+
Extensions information not found
18+
{{ end }}
1619

1720
{{/* force empty line */}}
1821

pghrep/templates/A007.tpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# {{ .checkId }} Altered settings #
22

33
## Observations ##
4-
4+
{{ if .hosts.master }}
55
### Master (`{{.hosts.master}}`) ###
66
Source | Settings count | Changed settings
77
-------|----------------|-----------------
88
{{ range $key, $value := (index (index (index .results .hosts.master) "data") "changes") }}{{ if $value.sourcefile }}{{ $value.sourcefile }}{{ else}}default{{ end }} | {{ $value.count }} | {{ if $value.examples}} {{ if (gt (len $value.examples) 0) }}{{ range $skey, $sname := (index $value "examples") }}{{ $sname }} {{ end }} {{ end }}
99
{{ end }}{{ end }}
10-
10+
{{ end }}
1111
{{ if gt (len .hosts.replicas) 0 }}
1212
### Replica servers: ###
1313
{{ range $skey, $host := .hosts.replicas }}

pghrep/templates/D004.tpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# {{ .checkId }} pg_stat_statements and kcache settings #
22

33
## Observations ##
4-
4+
{{ if .hosts.master }}
55
### Master (`{{.hosts.master}}`) ###
66
{{ if (index (index (index .results .hosts.master) "data") "pg_stat_statements") }}
77
#### `pg_stat_statements` extension settings ####
@@ -22,7 +22,7 @@ Setting | Value | Unit | Type | Min value | Max value
2222
[{{ $setting_name }}](https://postgresqlco.nf/en/doc/param/{{ $setting_name }})|{{ $setting_data.setting }}|{{ if $setting_data.unit }}{{ $setting_data.unit }} {{ end }}|{{ $setting_data.vartype }}|{{ if $setting_data.min_val }}{{ $setting_data.min_val }} {{ end }}|{{ if $setting_data.max_val }}{{ $setting_data.max_val }} {{ end }}
2323
{{ end }}
2424
{{- end -}}
25-
25+
{{ end }}
2626
{{ if gt (len .hosts.replicas) 0 }}
2727
### Replica servers: ###
2828
{{ range $skey, $host := .hosts.replicas }}

pghrep/templates/F001.tpl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# {{ .checkId }} Heap bloat #
2+
:warning: This report is based on estimations. The errors in bloat estimates may be significant (in some cases, up to 15% and even more). Use it only as an indicator of potential issues.
23

34
## Observations ##
4-
5+
{{ if .hosts.master }}
56
### Master (`{{.hosts.master}}`) ###
67
{{ if (index (index .results .hosts.master) "data") }}
78
Table | Size | Extra | Bloat | Live | Last vacuum
@@ -13,7 +14,7 @@
1314
{{- else -}}
1415
`No data`
1516
{{- end -}}
16-
17+
{{ end }}
1718
{{ if gt (len .hosts.replicas) 0 }}
1819
### Replica servers: ###
1920
{{ range $skey, $host := .hosts.replicas }}

0 commit comments

Comments
 (0)