Skip to content

Commit 3989661

Browse files
authored
Add Diagnostics (#203)
* Add container suplimentary groups support * Add diagnostic gathering * Fix incorrect userid group searching * one last tiny fix * one last tiny fix
1 parent 9a7d44f commit 3989661

File tree

4 files changed

+82
-12
lines changed

4 files changed

+82
-12
lines changed

cmd/runmqserver/logging.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,20 @@ import (
2121
"fmt"
2222
"io/ioutil"
2323
"os"
24+
"os/exec"
2425
"path/filepath"
2526
"sync"
2627

28+
"github.com/ibm-messaging/mq-container/internal/command"
2729
"github.com/ibm-messaging/mq-container/internal/logger"
2830
"github.com/ibm-messaging/mq-container/internal/mqini"
2931
)
3032

3133
// var debug = false
3234
var log *logger.Logger
3335

36+
var collectDiagOnFail bool = false
37+
3438
func logTerminationf(format string, args ...interface{}) {
3539
logTermination(fmt.Sprintf(format, args))
3640
}
@@ -45,6 +49,10 @@ func logTermination(args ...interface{}) {
4549
log.Debug(err)
4650
}
4751
log.Error(msg)
52+
53+
if collectDiagOnFail {
54+
logDiagnostics()
55+
}
4856
}
4957

5058
func getLogFormat() string {
@@ -111,3 +119,27 @@ func configureLogger(name string) (mirrorFunc, error) {
111119
return nil, fmt.Errorf("invalid value for LOG_FORMAT: %v", f)
112120
}
113121
}
122+
123+
func logDiagnostics() {
124+
log.Debug("--- Start Diagnostics ---")
125+
126+
// show the directory ownership/permissions
127+
out, _, _ := command.Run("ls", "-l", "/mnt/")
128+
log.Debugf("/mnt/:\n%s", out)
129+
out, _, _ = command.Run("ls", "-l", "/mnt/mqm")
130+
log.Debugf("/mnt/mqm:\n%s", out)
131+
out, _, _ = command.Run("ls", "-l", "/mnt/mqm/data")
132+
log.Debugf("/mnt/mqm/data:\n%s", out)
133+
out, _, _ = command.Run("ls", "-l", "/var/mqm")
134+
log.Debugf("/var/mqm:\n%s", out)
135+
out, _, _ = command.Run("ls", "-l", "/var/mqm/errors")
136+
log.Debugf("/var/mqm/errors:\n%s", out)
137+
138+
// Print out summary of any FDCs
139+
cmd := exec.Command("/opt/mqm/bin/ffstsummary")
140+
cmd.Dir = "/var/mqm/errors"
141+
outB, _ := cmd.CombinedOutput()
142+
log.Debugf("ffstsummary:\n%s", string(outB))
143+
144+
log.Debug("--- End Diagnostics ---")
145+
}

cmd/runmqserver/main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,16 @@ func doMain() error {
5858

5959
// Start signal handler
6060
signalControl := signalHandler(name)
61+
// Enable diagnostic collecting on failure
62+
collectDiagOnFail = true
6163

62-
err = logConfig()
64+
err = verifyCurrentUser()
6365
if err != nil {
6466
logTermination(err)
6567
return err
6668
}
6769

68-
err = verifyCurrentUser()
70+
err = logConfig()
6971
if err != nil {
7072
logTermination(err)
7173
return err

cmd/runmqserver/mirror.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ func waitForFile(ctx context.Context, path string) (os.FileInfo, error) {
4444
return nil, fmt.Errorf("mirror: unable to get info on file %v", path)
4545
}
4646
}
47-
log.Debugf("File exists: %v, %v", path, fi.Size())
4847
return fi, nil
4948
}
5049
}
@@ -121,6 +120,7 @@ func mirrorLog(ctx context.Context, wg *sync.WaitGroup, path string, fromStart b
121120
if fi == nil {
122121
return
123122
}
123+
log.Debugf("File exists: %v, %v", path, fi.Size())
124124
f, err = os.OpenFile(path, os.O_RDONLY, 0)
125125
if err != nil {
126126
log.Error(err)

cmd/runmqserver/user.go

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"github.com/ibm-messaging/mq-container/internal/command"
2424
)
2525

26-
const groupName string = "suplgroup"
26+
const groupName string = "supplgrp"
2727

2828
func verifyCurrentUser() error {
2929
log.Debug("Verifying current user information")
@@ -36,9 +36,9 @@ func verifyCurrentUser() error {
3636
// Not supported yet
3737
return fmt.Errorf("Container is running as mqm user which is not supported. Please run this container as root")
3838
} else if curUser.Username == "root" {
39-
// We're running as root so need to check for suplimentary groups.
40-
// We can't use the golang User.GroupIDs as it doesn't seem to detect container supplimentary groups..
41-
groups, err := getCurrentGroups()
39+
// We're running as root so need to check for supplementary groups.
40+
// We can't use the golang User.GroupIDs as it doesn't seem to detect container supplementary groups..
41+
groups, err := getCurrentUserGroups()
4242
for _, e := range groups {
4343
_, _, testGroup := command.Run("getent", "group", e)
4444
if testGroup != nil {
@@ -64,9 +64,9 @@ func verifyCurrentUser() error {
6464
}
6565

6666
func logUser() {
67-
u, err := user.Current()
68-
if err == nil {
69-
g, err := getCurrentGroups()
67+
u, usererr := user.Current()
68+
if usererr == nil {
69+
g, err := getCurrentUserGroups()
7070
if err != nil && len(g) == 0 {
7171
log.Printf("Running as user ID %v (%v) with primary group %v", u.Uid, u.Name, u.Gid)
7272
} else {
@@ -77,12 +77,30 @@ func logUser() {
7777
g = append(g[:i], g[i+1:]...)
7878
}
7979
}
80-
log.Printf("Running as user ID %v (%v) with primary group %v, and supplemental groups %v", u.Uid, u.Name, u.Gid, strings.Join(g, ","))
80+
log.Printf("Running as user ID %v (%v) with primary group %v, and supplementary groups %v", u.Uid, u.Name, u.Gid, strings.Join(g, ","))
81+
}
82+
}
83+
84+
if usererr == nil && u.Username != "mqm" {
85+
mqm, err := user.Lookup("mqm")
86+
// Need to print out mqm user details as well.
87+
g, err := getUserGroups(mqm)
88+
if err != nil && len(g) == 0 {
89+
log.Printf("MQM user ID %v (%v) has primary group %v", mqm.Uid, "mqm", mqm.Gid)
90+
} else {
91+
// Look for the primary group in the list of group IDs
92+
for i, v := range g {
93+
if v == mqm.Gid {
94+
// Remove the element from the slice
95+
g = append(g[:i], g[i+1:]...)
96+
}
97+
}
98+
log.Printf("MQM user ID %v (%v) has primary group %v, and supplementary groups %v", mqm.Uid, "mqm", mqm.Gid, strings.Join(g, ","))
8199
}
82100
}
83101
}
84102

85-
func getCurrentGroups() ([]string, error) {
103+
func getCurrentUserGroups() ([]string, error) {
86104
var nilArray []string
87105
out, _, err := command.Run("id", "--groups")
88106
if err != nil {
@@ -99,3 +117,21 @@ func getCurrentGroups() ([]string, error) {
99117
groups := strings.Split(out, " ")
100118
return groups, nil
101119
}
120+
121+
func getUserGroups(usr *user.User) ([]string, error) {
122+
var nilArray []string
123+
out, _, err := command.Run("id", "--groups", usr.Uid)
124+
if err != nil {
125+
log.Debugf("Unable to get user %s groups", usr.Uid)
126+
return nilArray, err
127+
}
128+
129+
out = strings.TrimSpace(out)
130+
if out == "" {
131+
// we don't have any groups?
132+
return nilArray, fmt.Errorf("Unable to determine groups for user %s", usr.Uid)
133+
}
134+
135+
groups := strings.Split(out, " ")
136+
return groups, nil
137+
}

0 commit comments

Comments
 (0)